package server.servlets;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class FormCData {
private FormCHtml fch = new FormCHtml();
private FormCDataParser fc;
private String formActionURL;
private int errorCode;
private String fieldNames[];
private String fieldValues[];
private Properties fileProp;
static final String FORMC_PROPERTY_FILE = Globals.FORMC_PROPERTY_FILE;
private static final int ERROR_PROPERTY_FILE = 0;
private static final int ERROR_CSV_FILE = 1;
private static final int ERROR_UNKNOWN = 2;
private static final int ERROR_SELECTION = 3;
public boolean init() {
try {
fileProp = FileUtil.loadProperties(FORMC_PROPERTY_FILE);
String fn = fileProp.getProperty("FileName");
System.out.println("The fileName is " + fn);
File file = FileUtil.getFile(fileProp.getProperty("FileName"));
formActionURL = fileProp.getProperty("FormCURL");
fc = new FormCDataParser(FileUtil.openInputFile(file));
return true;
} catch (PropFileException pfnf) {
errorCode = ERROR_PROPERTY_FILE;
return false;
} catch (FileNotFoundException fnf) {
errorCode = ERROR_CSV_FILE;
return false;
} catch (IOException io) {
errorCode = ERROR_UNKNOWN;
return false;
}
}
public String getDefaultFormC(String user) {
if (fc.isCourseKeyByLoginUser(user)) {
int[] keyIndexArray = fc.getCourseKeyIndexArray();
int cKeyIndexLength = fc.getCourseKeyIndexLength();
String[] selCourse = new String[cKeyIndexLength + 1];
for (int i = 0; i <= cKeyIndexLength; i++) {
selCourse[i] = fc.getCourseNoByKey(keyIndexArray[i]) + " " +
fc.getCourseNameByKey(keyIndexArray[i]);
}
return (fch.formCSelect(fc.getTerm(), selCourse,
fc.getSection(), formActionURL));
}
return (fch.formCError(ERROR_SELECTION));
}
public String getErrorFormC() {
return (fch.formCError(errorCode));
}
public String getSelectFormC(String selTerm, String selCourse,
String selSection) {
StringBuffer selection = new StringBuffer();
selection.append(selTerm);
selection.append(" ");
selection.append(selCourse);
selection.append(" ");
selection.append(selSection);
String selInstructor = null;
String selStudents[] = null;
if (fc.isCourseKey(selection.toString())) {
int keyIndex = fc.getCourseKeyIndex();
selInstructor = fc.getCourseInstructorByKey(keyIndex);
selStudents = fc.getCourseStudentsByKey(keyIndex);
return (fch.formC(selTerm, selCourse, selInstructor,
selStudents, formActionURL));
} else {
return (fch.formCError(ERROR_SELECTION));
}
}
public String getDisplayFormC(String[] fNames,
String[] fValues) {
int keyIndex = fc.getCourseKeyIndex();
fieldNames = new String[fNames.length];
for (int i = 0; i < fNames.length; i++) {
fieldNames[i] = fNames[i];
}
fieldValues = new String[fValues.length];
for (int i = 0; i < fValues.length; i++) {
fieldValues[i] = fValues[i];
}
return (fch.formCData(fc.getCourseTermByKey(keyIndex),
fc.getCourseNoByKey(keyIndex) + " " +
fc.getCourseNameByKey(keyIndex),
fc.getCourseInstructorByKey(keyIndex),
fc.getCourseStudentsByKey(keyIndex),
fieldNames,
fieldValues));
}
public String getSqlFormC() {
FormCSQL fcSql = new FormCSQL(fc);
fcSql.generateFormCSQL(fieldNames, fieldValues);
return (fch.formCUpdate());
}
}