import java.util.Map;
import java.sql.SQLException;
/**
* Static methods to verify certain user inputs are valid.
*/
public class InputChecker
{
public static final int USER_ID_LENGTH = 9;
public static final int PASSWORD_LENGTH = 6;
/**
* Checks to see if a Banner user id and password are "valid". This validation
* only checks to see if the lengths of the strings are correct, nothing more. True
* validation is done by Banner itself.
* @param userId the user id to check.
* @param password the password to check.
* @return a non-null user-friendly String if an error occurs, or null if everything was ok.
*/
public static String isLoginOk(String userId, String password)
{
if(userId == null || password == null)
{
throw new RuntimeException(
"User Id or Password was null.");
}
StringBuffer errorBuffer = new StringBuffer();
boolean foundError = false;
if(userId.length() != USER_ID_LENGTH)
{
errorBuffer.append("User Id must be " + USER_ID_LENGTH + " characters long.");
foundError = true;
}
if(password.length() != PASSWORD_LENGTH)
{
if(foundError) errorBuffer.append("
");
errorBuffer.append("Password must be " + PASSWORD_LENGTH + " characters long.");
foundError = true;
}
return foundError ? errorBuffer.toString() : null;
}
/**
* Ensures the input map passed into the processor contains the correct entries and that
* these entries contain valid values.
* @param map the map containing values to check prior to invoking the processor.
* @return a non-null user-friendly String if an error occurs, or null if everything was ok.
* @exception SQLException if an error occurs while connecting to the SAS database.
*/
public static String isInputMapOk(Map map) throws SQLException
{
if(map == null)
{
throw new RuntimeException(
"The map was null.");
}
String majorStr = (String) map.get("major");
String minorStr = (String) map.get("minor");
String secondMajorStr = (String) map.get("secondmajor");
StringBuffer errorBuffer = new StringBuffer();
boolean foundError = false;
if(majorStr.equals("none"))
{
errorBuffer.append("You must specify a major in your preferences.");
foundError = true;
}
if(minorStr.equals("none"))
{
if(foundError) errorBuffer.append("
");
errorBuffer.append("You must specify a minor in your preferences.");
foundError = true;
}
if(majorStr.equals(minorStr))
{
if(foundError) errorBuffer.append("
");
errorBuffer.append("You must specify a different major and minor in your preferences.");
foundError = true;
}
if(majorStr.equals(secondMajorStr))
{
if(foundError) errorBuffer.append("
");
errorBuffer.append("You must specify a different major and second major in your preferences.");
foundError = true;
}
if(minorStr.equals(secondMajorStr))
{
if(foundError) errorBuffer.append("
");
errorBuffer.append("You must specify a different minor and second major in your preferences.");
foundError = true;
}
if(!foundError && !RetrieverUtils.isMajorOkWithMinor(majorStr, minorStr))
{
if(foundError) errorBuffer.append("
");
errorBuffer.append("Selected major and minor cannot be chosen together.");
foundError = true;
}
if(!foundError && !secondMajorStr.equals("-1") &&
!RetrieverUtils.isMajorOkWithMinor(secondMajorStr, minorStr))
{
if(foundError) errorBuffer.append("
");
errorBuffer.append("Selected second major and minor cannot be chosen together.");
foundError = true;
}
String coreNeededStr = (String) map.get("coreneeded");
if(coreNeededStr.length() > 0)
{
int coreNeededInt = 0;
try
{
coreNeededInt = Integer.parseInt(
coreNeededStr);
if(coreNeededInt < 0)
{
if(foundError) errorBuffer.append("
");
errorBuffer.append(
"You must specify a number of CORE credits needed that is not negative." +
" If you are not a transfer student, leave this field blank.");
foundError = true;
}
}
catch(NumberFormatException e)
{
if(foundError) errorBuffer.append("
");
errorBuffer.append(
"The number of CORE credits you entered was invalid.");
foundError = true;
}
}
else map.put("coreneeded", "no");
return foundError ? errorBuffer.toString() : null;
}
}