package server.servlets;
import java.io.BufferedReader;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Vector;
public class FormCDataParser implements FormCDataColumns {
private Vector vData;
private Vector term = new Vector();
private Vector course = new Vector();
private Vector section = new Vector();
private Vector instructor = new Vector();
private Vector courseKey = new Vector();
private Vector courseStudents = new Vector();
private int courseKeyIndex = 0;
private int courseKeyIndexArray[];
private int courseKeyIndexLength;
public FormCDataParser(BufferedReader br) {
CsvLineConsumer cv = new CsvLineConsumer(br, columnOrder);
vData = cv.getVData();
processData();
courseKeyIndexArray = new int[20];
}
public String[] getTerm() {
Object[] objTerm = term.toArray();
Arrays.sort(objTerm);
String[] strTerm = new String[objTerm.length];
for (int i = 0; i < strTerm.length; i++) {
strTerm[i] = (String) objTerm[i];
}
return strTerm;
}
public String[] getCourse() {
Object[] objCourse = course.toArray();
Arrays.sort(objCourse);
String[] strCourse = new String[objCourse.length];
for (int i = 0; i < strCourse.length; i++) {
strCourse[i] = (String) objCourse[i];
}
return strCourse;
}
public String[] getSection() {
Object[] objSection = section.toArray();
Arrays.sort(objSection);
String[] strSection = new String[objSection.length];
for (int i = 0; i < strSection.length; i++) {
strSection[i] = (String) objSection[i];
}
return strSection;
}
public String[] getInstructor() {
Object[] objInstructor = instructor.toArray();
Arrays.sort(objInstructor);
String[] strInstructor = new String[objInstructor.length];
for (int i = 0; i < strInstructor.length; i++) {
strInstructor[i] = (String) objInstructor[i];
System.out.println("Instr is " + strInstructor[i]);
}
return strInstructor;
}
public boolean isCourseKeyByLoginUser(String user) {
courseKeyIndexLength = -1;
int index = 0;
boolean hasCourse = false;
for (Enumeration e = (getCourseKey()).elements();
e.hasMoreElements(); index++) {
String strData[] = (String[]) e.nextElement();
if ((strData[COURSE_KEY_INSTR]).regionMatches(true, 3, user, 0,
user.length())) {
courseKeyIndexLength++;
courseKeyIndexArray[courseKeyIndexLength] = index;
hasCourse = true;
}
}
return hasCourse;
}
public boolean isCourseKey(String s) {
int index = 0;
for (Enumeration e = (getCourseKey()).elements();
e.hasMoreElements(); index++) {
String strData[] = (String[]) e.nextElement();
if (s.equals(strData[COURSE_KEY])) {
courseKeyIndex = index;
return true;
}
}
return false;
}
public int getCourseKeyIndex() {
return courseKeyIndex;
}
public int[] getCourseKeyIndexArray() {
return courseKeyIndexArray;
}
public int getCourseKeyIndexLength() {
System.out.println("The courseKeyIndexLength here is " + courseKeyIndexLength);
return courseKeyIndexLength;
}
public Vector getCourseKey() {
return courseKey;
}
public String getCourseKeyValue(int keyIndex, int column) {
String courseElements[] = (String[]) (courseKey.elementAt(keyIndex));
return courseElements[column];
}
public String getCourseKeyByKey(int keyIndex) {
return getCourseKeyValue(keyIndex, COURSE_KEY);
}
public String getCourseTermByKey(int keyIndex) {
return getCourseKeyValue(keyIndex, COURSE_KEY_TERM);
}
public String getCourseNoByKey(int keyIndex) {
return getCourseKeyValue(keyIndex, COURSE_KEY_COURSE_NO);
}
public String getCourseNameByKey(int keyIndex) {
return getCourseKeyValue(keyIndex, COURSE_KEY_COURSE_NAME);
}
public String getCourseSectionByKey(int keyIndex) {
return getCourseKeyValue(keyIndex, COURSE_KEY_SECTION);
}
public String getCourseYearByKey(int keyIndex) {
return getCourseKeyValue(keyIndex, COURSE_KEY_YEAR);
}
public String getCourseInstructorByKey(int keyIndex) {
return getCourseKeyValue(keyIndex, COURSE_KEY_INSTR);
}
public Vector getCourseStudents() {
return courseStudents;
}
public String[] getCourseStudentsByKey(int keyIndex, int column) {
Vector sv = (Vector) (getCourseStudents()).elementAt(keyIndex);
String students[] = new String[sv.size()];
int i = 0;
for (Enumeration e = sv.elements();
e.hasMoreElements(); i++) {
String strObj[] = (String[]) e.nextElement();
students[i] = strObj[column];
}
return students;
}
public String[] getCourseStudentsByKey(int keyIndex) {
return getCourseStudentsByKey(keyIndex, S_STUDENT_NAME);
}
public String[] getCourseStudentsIdByKey(int keyIndex) {
return getCourseStudentsByKey(keyIndex, S_STUDENT_ID);
}
private void processData() {
int count = vData.size();
for (int i = 0; i < count; i++) {
String data[] = (String[]) vData.elementAt(i);
splitColumns(data);
}
}
private void splitColumns(String data[]) {
StringBuffer courseBuffer = new StringBuffer();
StringBuffer courseKeyBuffer = new StringBuffer();
StringBuffer studentBuffer = new StringBuffer();
String courseKeyArray[] = new String[COURSE_KEY_ARRAY_SIZE];
String studentsArray[] = new String[STUDENTS_ARRAY_SIZE];
addElement(term, data[IND_TERM]);
courseBuffer.append(data[IND_COURSE_NO]);
courseBuffer.append(" ");
courseBuffer.append(data[IND_COURSE_NAME]);
addElement(course, courseBuffer.toString());
addElement(section, data[IND_SECTION]);
courseKeyBuffer.append(data[IND_TERM]);
courseKeyBuffer.append(" ");
courseKeyBuffer.append(courseBuffer.toString());
courseKeyBuffer.append(" ");
courseKeyBuffer.append(data[IND_SECTION]);
courseKeyArray[0] = courseKeyBuffer.toString();
courseKeyArray[1] = data[IND_TERM];
courseKeyArray[2] = data[IND_COURSE_NO];
courseKeyArray[3] = data[IND_COURSE_NAME];
courseKeyArray[4] = data[IND_SECTION];
courseKeyArray[5] = data[IND_YEAR];
courseKeyArray[6] = data[IND_INSTR] + data[IND_INSTR_NAME];
addElement(courseKey, courseStudents, courseKeyArray);
studentBuffer.append(data[IND_FIRST_NAME]);
studentBuffer.append(" ");
studentBuffer.append(data[IND_MIDDLE_NAME]);
studentBuffer.append(" ");
studentBuffer.append(data[IND_LAST_NAME]);
studentsArray[0] = data[IND_STUDENT_ID];
studentsArray[1] = studentBuffer.toString();
addElement(courseKey, courseStudents,
courseKeyArray, studentsArray);
}
private void addElement(Vector v, String data) {
boolean dataExists = false;
for (Enumeration e = v.elements(); e.hasMoreElements();) {
if (data.equals(e.nextElement())) {
dataExists = true;
break;
}
}
if (!dataExists) {
v.addElement(data);
}
}
private void addElement(Vector v, String data[]) {
String strData[];
boolean dataExists = false;
for (Enumeration e = v.elements(); e.hasMoreElements();) {
strData = (String[]) e.nextElement();
if (data[0].equals(strData[0])) {
dataExists = true;
break;
}
}
if (!dataExists) {
v.addElement(data);
}
}
private void addElement(Vector key, Vector detail, String data[]) {
String strData[];
boolean dataExists = false;
for (Enumeration ke = key.elements(); ke.hasMoreElements();) {
strData = (String[]) ke.nextElement();
if (data[0].equals(strData[0])) {
dataExists = true;
break;
}
}
if (!dataExists) {
key.addElement(data);
detail.addElement(new Vector());
}
}
private void addElement(Vector vkey, Vector detail,
String skey[], String data[]) {
String strData[];
Enumeration ke = vkey.elements();
for (int i = 0; ke.hasMoreElements(); i++) {
strData = (String[]) ke.nextElement();
if (skey[0].equals(strData[0])) {
Vector sv = (Vector) detail.elementAt(i);
addElement(sv, data);
break;
}
}
}
}