import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.ArrayList; import java.lang.Integer; import javax.swing.*; import org.xmldb.api.DatabaseManager; import org.xmldb.api.base.Collection; import org.xmldb.api.base.Database; import org.xmldb.api.base.Resource; import org.xmldb.api.base.ResourceIterator; import org.xmldb.api.base.ResourceSet; import org.xmldb.api.modules.XMLResource; import org.xmldb.api.modules.XPathQueryService; import org.xmldb.api.modules.XUpdateQueryService; public class ExistDBJavaInterface extends JFrame { private static String username = "admin"; private static String password = "password"; private static String dbURL = "xmldb:exist://localhost:8080/exist/xmlrpc/db/"; protected static String driver = "org.exist.xmldb.DatabaseImpl"; public Container c = getContentPane(); /** Default constructor */ public ExistDBJavaInterface() { // Sets parameters for GUI super("Irish Language CALL Interface"); GridLayout grid = new GridLayout(15,2,0,0); c.setLayout(grid); setSize(724,534); menus(); // Start Page startPage(); show(); return; } public int countFiles(String collection) { // Counts the number of files in a collection int fileCount = 0; try { Class cl = Class.forName(driver); Database database = (Database)cl.newInstance(); DatabaseManager.registerDatabase(database); Collection col = DatabaseManager.getCollection(dbURL+collection, username, password); fileCount = col.getResourceCount(); } catch (Exception e) { System.out.println("Error checking collection " + collection + ": " + e); } return fileCount; } public void cleanUp() { // Removes existing components from screen int j = c.getComponentCount() - 1; for(int i = j; i >= 0; i--) { c.getComponent(i).setVisible(false); c.remove(c.getComponent(i)); } return; } public void menus() { // Sets up menus JMenuBar bar = new JMenuBar(); setJMenuBar(bar); // create File Menu and Exit menu item JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic('F'); JMenuItem startItem = new JMenuItem("Start"); startItem.setMnemonic('S'); startItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { cleanUp(); startPage(); } } ); JMenuItem exitItem = new JMenuItem("Exit"); exitItem.setMnemonic('x'); exitItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } } ); fileMenu.add(startItem); fileMenu.add(exitItem); bar.add(fileMenu); return; } public void startPage() { // Start Page String options[] = {"Yes", "No"}; JLabel heading = new JLabel(" Welcome to our Irish Language Teaching Software"); JLabel question = new JLabel(" Do you have an existing account?"); final JComboBox optionBox; optionBox = new JComboBox(options); optionBox.setMaximumRowCount(2); JButton button = new JButton("Enter Details"); c.add(heading); c.add(question); c.add(optionBox); c.add(button); button.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { if(optionBox.getSelectedIndex() == 1) { cleanUp(); newUserPage(); } else { cleanUp(); loginPage(); } } } ); return; } public boolean testStudent(String username, String password) { // Password matcher boolean result = false; try { XMLResource listing01 = getXMLDoc(username,"Students"); if (listing01 != null) { String pass3 = XPathListDocument("//mm:Password","Students", username); String pass4 = "[" + password + "]"; if (pass3.compareTo(pass4) == 0) { System.out.println("Password Matches"); result = true; } else { System.out.println(pass3); System.out.println("Password does not match"); } } } catch (Exception e) { System.out.println("Error retrieving username: " + e); } return result; } public boolean testTeacher(String username, String password) { boolean result = false; String pass1, pass2; try { XMLResource listing01 = getXMLDoc(username,"Teachers"); pass1 = XPathListDocument("//mm:Password","Teachers", username); System.out.println(pass1); pass2 = "[" + password + "]"; if (pass1.compareTo(pass2) == 0) { // Teacher Login Page System.out.println("Password matches"); result = true; } else { System.out.println("Password Does Not Match"); } } catch(Exception e) { System.out.println("Error retrieving username: " + e); } return result; } public void loginPage() { System.out.println("Login Page"); JLabel heading = new JLabel(" Enter your username and password in the boxes provided"); JLabel userlabel = new JLabel(" Username"); JLabel passlabel = new JLabel(" Password"); final JTextField userField = new JTextField(); final JPasswordField passField = new JPasswordField(); JButton button = new JButton("Enter Details"); c.add(heading); c.add(userlabel); c.add(userField); c.add(passlabel); c.add(passField); c.add(button); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String user1, user2, pass1; int index; user1 = userField.getText(); user2 = user1 + ".xml"; pass1 = new String(passField.getPassword()); System.out.println("The username is " + user2); System.out.println("The password is " + pass1); if (testStudent(user2,pass1) == true) { cleanUp(); studentMainPage(user2); } else if (testTeacher(user2,pass1) == true) { cleanUp(); teacherMainPage(user2); } else { System.out.println("Invalid username"); } } } ); return; } public void studentMainPage(final String user) { JLabel heading = new JLabel(" Lesson Home Page"); JButton newButton = new JButton("Begin New Lesson"); JButton oldButton = new JButton("Review Old Lesson"); JButton markButton = new JButton("Check Your Marks"); c.add(heading); c.add(newButton); c.add(oldButton); c.add(markButton); newButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { //System.out.println("Doing a new lesson"); cleanUp(); selectLesson(user,true); } }); oldButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { //System.out.println("Doing an old lesson"); cleanUp(); selectLesson(user,false); } }); markButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { //System.out.println("Checking results"); cleanUp(); checkMarks("", user,true,""); } }); return; } public void selectLesson(final String user, boolean oldNew) { if (oldNew == true) { JLabel heading = new JLabel(" Doing a new lesson"); c.add(heading); } else { JLabel heading = new JLabel(" Doing an old lesson"); c.add(heading); } JLabel selectLabel = new JLabel(" Select lesson"); String options[] = {"Lesson1", "Lesson2", "Lesson3", "Lesson4", "Lesson5", "Lesson6", "Lesson7" , "Lesson8", "Lesson9", "Lesson10", "Lesson11", "Lesson12"}; final String options2[] = new String[12]; int j; int k = 0; ArrayList queryResult = new ArrayList(); String queryResultHolder = new String(); String checkValue = new String(); for (int i = 0; i < 12; i++) { j = i + 1; queryResultHolder = XPathListDocument("//mm:LessonInfo[mm:Number=" + j + "]/mm:LessonTaken","Students",user); checkValue = "[No]"; if(((queryResultHolder.compareTo(checkValue) == 0) && (oldNew == true)) || ((queryResultHolder.compareTo(checkValue) != 0) && (oldNew == false))) { options2[k] = new String(options[i]); k++; } } final JComboBox optionsBox; optionsBox = new JComboBox(options2); optionsBox.setMaximumRowCount(k); JButton button1 = new JButton("Begin Lesson"); JButton button2 = new JButton("Return to Your Home Page"); c.add(selectLabel); c.add(optionsBox); c.add(button1); c.add(button2); button1.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String lessonIndex = new String(); String lessonName = new String(); char index; lessonIndex = options2[optionsBox.getSelectedIndex()]; index = lessonIndex.charAt(6); lessonName = "lesson" + index + ".xml"; cleanUp(); displayLesson(lessonName, user); } } ); button2.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { cleanUp(); studentMainPage(user); } }); return; } public void checkMarks(final String userName, final String userID, final boolean student, final String teacherID) { String lesson[] = new String[12]; for (int i = 0; i < 12; i++) { if(student == true) lesson[i] = new String("Your mark in Lesson " + (i + 1) + " is "); else lesson[i] = new String(userName + "'s mark in Lesson " + (i + 1) + " is"); } String result1 = new String("1 out of 3 questions correct"); String result2 = new String("2 out of 3 questions correct"); String result3 = new String("3 out of 3 questions correct"); String result4 = new String("0 out of 3 questions correct"); String result5 = new String("Uncompleted"); JLabel lessonLabel[] = new JLabel[12]; JLabel result[] = new JLabel[12]; ArrayList queryResult = new ArrayList(); String queryResultHolder = new String(); String checkValue = new String(); int j; for (int i = 0; i < 12; i++) { lessonLabel[i] = new JLabel(lesson[i]); c.add(lessonLabel[i]); j = i + 1; queryResultHolder = XPathListDocument("//mm:LessonInfo[mm:Number=" + j + "]/mm:LessonTaken","Students",userID); checkValue = "[No]"; if(queryResultHolder.compareTo(checkValue) == 0) { result[i] = new JLabel(result5); c.add(result[i]); } else { checkValue = "[1]"; queryResultHolder = XPathListDocument("//mm:LessonInfo[mm:Number=" + j + "]/mm:LessonResult","Students",userID); if(queryResultHolder.compareTo(checkValue) == 0) { result[i] = new JLabel(result1); c.add(result[i]); } else { checkValue = "[2]"; if(queryResultHolder.compareTo(checkValue) == 0) { result[i] = new JLabel(result2); c.add(result[i]); } else { checkValue = "[3]"; if(queryResultHolder.compareTo(checkValue) == 0) { result[i] = new JLabel(result3); c.add(result[i]); } else { checkValue = "[0]"; if(queryResultHolder.compareTo(checkValue) == 0) { result[i] = new JLabel(result4); c.add(result[i]); } } } } } } JButton button2 = new JButton("Return to Your Home Page"); c.add(button2); button2.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { cleanUp(); if(student == true) { studentMainPage(userID); } else { teacherMainPage(teacherID); } } }); return; } public void teacherMainPage(final String teacherName) { JLabel heading = new JLabel(" Class Management Main Page"); JButton newStudentButton = new JButton("Enter Student Data"); JButton manageButton = new JButton("Manage Student File"); c.add(heading); c.add(newStudentButton); c.add(manageButton); // Entering Student Details newStudentButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { cleanUp(); studentEntryPage(true, teacherName); } }); // Student added to class manageButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { cleanUp(); studentList(teacherName); } }); return; } public void studentList(final String teacherName) { int studentCount = countFiles("Students"); final String studentNames[] = new String[studentCount]; int i, j, stringLength = 0; JButton reviewResults = new JButton("Review Results"); JButton changeClass = new JButton("Change Student Class"); JButton mainPage = new JButton("Back to Your Main Page"); String classes[] = new String[4]; for (i = 0; i < 4; i++) { classes[i] = XPathListDocument("//mm:Classname[" + (i + 1) + "]","Teachers",teacherName); stringLength = classes[i].length(); classes[i] = classes[i].substring(49,stringLength-16); } final JComboBox classBox; classBox = new JComboBox(classes); classBox.setMaximumRowCount(4); String query; String emptyFilter; final String studentID[] = new String[1]; for (j = 0; j < 4; j++) { query = "//mm:Profile[mm:Class='" + classes[j] + "']/mm:Name[" + (j + 1) + "]"; emptyFilter = XPathList(query,"Students"); stringLength = emptyFilter.length(); if (stringLength > 2) { classes[j] = emptyFilter; } else { classes[j] = ""; } } int startPoint; int endPoint; String residue = (""); i = 0; for (j = 0; ((j < 4) && (i < studentCount)); j++) { System.out.println("classes[" + j + "] is " + classes[j]); endPoint = classes[j].indexOf(','); System.out.println("i is " + i + " and j is " + j + " and endPoint is " + endPoint); if (endPoint == -1) { stringLength = classes[j].length(); if (stringLength > 2) { studentNames[i] = classes[j]; studentNames[i] = studentNames[i].substring(44,stringLength-11); System.out.println("studentNames[" + i + "] is " + studentNames[i]); } } while ((endPoint != -1) && (i < studentCount)) { startPoint = 0; emptyFilter = classes[j].substring(startPoint,endPoint); stringLength = emptyFilter.length(); if (stringLength > 2) emptyFilter = emptyFilter.substring(48,stringLength-14); studentNames[i] = emptyFilter; System.out.println("studentNames[" + i + "] is " + studentNames[i]); i++; startPoint = endPoint + 1; endPoint = classes[j].length(); residue = classes[j].substring(startPoint,endPoint); classes[j] = residue; endPoint = classes[j].indexOf(','); System.out.println("i is " + i + " and j is " + j + " and endPoint is " + endPoint); } } final JComboBox listBox; listBox = new JComboBox(studentNames); listBox.setMaximumRowCount(studentCount); c.add(listBox); c.add(reviewResults); c.add(changeClass); reviewResults.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { int l, m; l = listBox.getSelectedIndex(); System.out.println(studentNames[l]); studentID[0] = XPathList("//mm:Profile[mm:Name='" + studentNames[l] + "']/mm:Username", "Students"); m = studentID[0].length(); studentID[0] = studentID[0].substring(48, m-15); studentID[0] = studentID[0] + ".xml"; cleanUp(); checkMarks(studentNames[l], studentID[0],false,teacherName); } } ); changeClass.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { int l, m; l = listBox.getSelectedIndex(); System.out.println(studentNames[l]); studentID[0] = XPathList("//mm:Profile[mm:Name='" + studentNames[l] + "']/mm:Username", "Students"); m = studentID[0].length(); studentID[0] = studentID[0].substring(48, m-15); studentID[0] = studentID[0] + ".xml"; cleanUp(); changeStudentClass(studentNames[l], studentID[0], teacherName); } } ); mainPage.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { cleanUp(); teacherMainPage(teacherName); } }); return; } public void changeStudentClass(final String studentName, final String studentID, final String teacherName) { final JTextField classField = new JTextField(); JButton button = new JButton("Submit"); JButton button2 = new JButton("Return to Your Main Page"); JLabel heading = new JLabel(" Enter Class that " + studentName + " will be assigned to."); c.add(heading); c.add(classField); c.add(button); button.addActionListener( new ActionListener () { public void actionPerformed(ActionEvent e) { String newClass = new String(); newClass = classField.getText(); newClass = utfise(newClass, true); String classDetails = new String(); classDetails = XPathListDocument("//mm:Class", "Students", studentName); System.out.println(classDetails); int stringLength = classDetails.length(); classDetails = classDetails.substring(45,stringLength-12); updateFile(studentName, "Students", "//mm:Class", newClass); } } ); button2.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { cleanUp(); teacherMainPage(teacherName); } }); return; } public void newUserPage() { String options[] = {"Teacher", "Student"}; JLabel heading = new JLabel(" Do you wish to login as a teacher or as a student?"); final JComboBox optionsBox; optionsBox = new JComboBox(options); optionsBox.setMaximumRowCount(2); JButton button = new JButton("Enter Details"); c.add(heading); c.add(optionsBox); c.add(button); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { cleanUp(); if (optionsBox.getSelectedIndex() == 1) { studentEntryPage(false, ""); } else { teacherEntryPage(); } } }); return; } public String utfise(String gaelicString, boolean toggle) { if (toggle == false) { gaelicString = gaelicString.replaceAll("á", "á"); gaelicString = gaelicString.replaceAll("é", "é"); gaelicString = gaelicString.replaceAll("í", "í"); gaelicString = gaelicString.replaceAll("ó", "ó"); gaelicString = gaelicString.replaceAll("ú", "ú"); gaelicString = gaelicString.replaceAll("Á", "Á"); gaelicString = gaelicString.replaceAll("É", "É"); gaelicString = gaelicString.replaceAll("Í", "Í"); gaelicString = gaelicString.replaceAll("Ó", "Ó"); gaelicString = gaelicString.replaceAll("Ú", "Ú"); } else { gaelicString = gaelicString.replaceAll("á", "á"); gaelicString = gaelicString.replaceAll("é", "é"); gaelicString = gaelicString.replaceAll("í", "í"); gaelicString = gaelicString.replaceAll("ó", "ó"); gaelicString = gaelicString.replaceAll("ú", "ú"); gaelicString = gaelicString.replaceAll("Á", "Á"); gaelicString = gaelicString.replaceAll("É", "É"); gaelicString = gaelicString.replaceAll("Í", "Í"); gaelicString = gaelicString.replaceAll("Ó", "Ó"); gaelicString = gaelicString.replaceAll("Ú", "Ú"); } return gaelicString; } public void displayLesson(final String lessonName, final String userName) { System.out.println("displaylesson"); String collection = "Lessons"; int lessonCount = 0; int i = 0, j = 0; lessonCount = countFiles(collection); final JRadioButton radio[] = new JRadioButton[12]; String answer[] = new String[lessonCount]; char nameIndex = lessonName.charAt(6); final int index = nameIndex - 48; // int(nameIndex); int stringLength; String headingName = new String(); headingName = XPathListDocument("//mm:Name", "Lessons", lessonName); stringLength = headingName.length(); headingName = headingName.substring(44, stringLength-11); String headingContent = new String(); headingContent = XPathListDocument("//mm:Content", "Lessons", lessonName); stringLength = headingContent.length(); headingContent = headingContent.substring(47, stringLength-14); String questionContent[] = new String[3]; final String correctAnswer[] = new String[3]; for (i = 0; i < 3; i++) { correctAnswer[i] = XPathListDocument("//mm:Question[mm:Number=" + (i + 1) + "]/mm:CorrectAnswer","Lessons",lessonName); stringLength = correctAnswer[i].length(); correctAnswer[i] = correctAnswer[i].substring(53,stringLength-20); } final boolean results[] = {false, false, false}; JLabel heading = new JLabel(headingContent); JLabel title = new JLabel(headingName); JLabel dummyLabel[] = new JLabel[11]; for (i = 0; i < 11; i++) { dummyLabel[i] = new JLabel(""); } for (i = 0; i < 15; i++) { if (i%5 == 0) { questionContent[i/5] =XPathListDocument("//mm:Question[mm:Number=" + (i/5 +1) + "]/mm:Query","Lessons",lessonName); stringLength = questionContent[i/5].length(); questionContent[i/5] = questionContent[i/5].substring(45,stringLength-12); } else { if (i%5 == 1) { answer[j] = XPathListDocument("//mm:Question[mm:Number=" + (i/5 +1) + "]/mm:OptionA","Lessons",lessonName); } else if (i%5 == 2) { answer[j] = XPathListDocument("//mm:Question[mm:Number=" + (i/5 +1) + "]/mm:OptionB","Lessons",lessonName); } else if (i%5 == 3) { answer[j] = XPathListDocument("//mm:Question[mm:Number=" + (i/5 +1) + "]/mm:OptionC","Lessons",lessonName); } else if (i%5 == 4) { answer[j] = XPathListDocument("//mm:Question[mm:Number=" + (i/5 +1) + "]/mm:OptionD","Lessons",lessonName); } stringLength = answer[j].length(); //System.out.println(answer[j]); //System.out.println(stringLength); answer[j] = answer[j].substring(47,stringLength-14); radio[j] = new JRadioButton(answer[j], false); j++; } } JLabel question1 = new JLabel(" " + questionContent[0]); JLabel question2 = new JLabel(" " + questionContent[1]); JLabel question3 = new JLabel(" " + questionContent[2]); ButtonGroup group1 = new ButtonGroup(); group1.add(radio[0]); group1.add(radio[1]); group1.add(radio[2]); group1.add(radio[3]); ButtonGroup group2 = new ButtonGroup(); group2.add(radio[4]); group2.add(radio[5]); group2.add(radio[6]); group2.add(radio[7]); ButtonGroup group3 = new ButtonGroup(); group3.add(radio[8]); group3.add(radio[9]); group3.add(radio[10]); group3.add(radio[11]); JButton button = new JButton ("Click here to enter results"); JButton button2 = new JButton ("Return to Your Main Page"); // Add heading c.add(title); c.add(dummyLabel[0]); c.add(heading); c.add(dummyLabel[1]); // Add Question 1 c.add(question1); // Add RadioButtongroup1 c.add(radio[0]); c.add(dummyLabel[2]); c.add(radio[1]); c.add(dummyLabel[3]); c.add(radio[2]); c.add(dummyLabel[4]); c.add(radio[3]); // Add Question 2 c.add(question2); // Add RadioButtongroup2 c.add(radio[4]); c.add(dummyLabel[5]); c.add(radio[5]); c.add(dummyLabel[6]); c.add(radio[6]); c.add(dummyLabel[7]); c.add(radio[7]); // Add Question 3 c.add(question3); // Add RadioButtongroup3 c.add(radio[8]); c.add(dummyLabel[8]); c.add(radio[9]); c.add(dummyLabel[9]); c.add(radio[10]); c.add(dummyLabel[10]); c.add(radio[11]); // Add button c.add(button); c.add(button2); // Add buttonlistener button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { for (int i = 0; i < radio.length; i++) { if (radio[i].isSelected()) { if (i < 4) { if (correctAnswer[0].compareTo(radio[i].getText()) == 0) { results[0] = true; } else { results[0] = false; } } else if (i < 8) { if (correctAnswer[1].compareTo(radio[i].getText()) == 0) { results[1] = true; } else { results[1] = false; } } else { if (correctAnswer[2].compareTo(radio[i].getText()) == 0) { results[2] = true; } else { results[2] = false; } } } // Update student record // Display results } cleanUp(); displayResults(results, userName, index, lessonName); } } ); button2.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { cleanUp(); studentMainPage(userName); } }); return; } public void displayResults(boolean results[], final String userName, int index, final String lessonName) { JLabel header = new JLabel("The results are as follows: "); JLabel result1 = new JLabel(); JLabel result2 = new JLabel(); JLabel result3 = new JLabel(); int totalScore = 0; if (results[0] == true) { result1.setText("Question 1 was answered correctly"); totalScore++; } else { result1.setText("Question 1 was answered incorrectly"); } if (results[1] == true) { result2.setText("Question 2 was answered correctly"); totalScore++; } else { result2.setText("Question 2 was answered incorrectly"); } if (results[2] == true) { result3.setText("Question 3 was answered correctly"); totalScore++; } else { result3.setText("Question 3 was answered incorrectly"); } JLabel total = new JLabel("You scored " + totalScore + " out of 3 questions correctly."); JButton button1 = new JButton("Return to your main page"); JButton button2 = new JButton("Try lesson again"); Integer totalScoreContainer = new Integer(totalScore); String lessonQuery = new String(); String scoreUpdate = new String(); lessonQuery = "//mm:LessonInfo[mm:Number=" + index + "]/mm:LessonTaken"; scoreUpdate = "Yes"; updateFile(userName, "Students", lessonQuery, scoreUpdate); lessonQuery = "//mm:LessonInfo[mm:Number=" + index + "]/mm:LessonResult"; scoreUpdate = totalScoreContainer.toString(); updateFile(userName, "Students", lessonQuery, scoreUpdate); c.add(header); c.add(result1); c.add(result2); c.add(result3); c.add(total); c.add(button1); c.add(button2); button1.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { cleanUp(); studentMainPage(userName); } }); button2.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { cleanUp(); displayLesson(lessonName,userName); } }); return; } public void updateFile(String user, String collection, String field, String fieldContent) { String query = new String(); query = "" + "' + fieldContent + "" + ""; System.out.println(query); try { Class cl = Class.forName(driver); Database database = (Database)cl.newInstance(); DatabaseManager.registerDatabase(database); Collection col = DatabaseManager.getCollection(dbURL+collection, username, password); XUpdateQueryService service = (XUpdateQueryService) col.getService("XUpdateQueryService", "1.0"); service.updateResource(user,query); } catch (Exception e) { System.out.println("Exception " + e); } return; } public void teacherEntryPage() { final JLabel userlabel = new JLabel(" Username"); final JLabel passlabel = new JLabel(" Password"); final JLabel tlabel1 = new JLabel(" Name"); final JLabel tlabel2 = new JLabel(" Class 1"); final JLabel tlabel3 = new JLabel(" Class 2"); final JLabel tlabel4 = new JLabel(" Class 3"); final JLabel tlabel5 = new JLabel(" Class 4"); final JButton button = new JButton("Enter Details"); final JTextField user = new JTextField(); final JTextField inputbox1 = new JTextField(); final JTextField inputbox2 = new JTextField(); final JTextField inputbox3 = new JTextField(); final JTextField inputbox4 = new JTextField(); final JTextField inputbox5 = new JTextField(); final JPasswordField pass = new JPasswordField(); final String userDetails[] = new String[7]; System.out.println("Create new teacher"); c.add(userlabel); c.add(user); c.add(tlabel1); c.add(inputbox1); c.add(tlabel2); c.add(inputbox2); c.add(tlabel3); c.add(inputbox3); c.add(tlabel4); c.add(inputbox4); c.add(tlabel5); c.add(inputbox5); c.add(passlabel); c.add(pass); c.add(button); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { userDetails[0] = user.getText(); userDetails[1] = inputbox1.getText(); userDetails[2] = inputbox2.getText(); userDetails[3] = inputbox3.getText(); userDetails[4] = inputbox4.getText(); userDetails[5] = inputbox5.getText(); userDetails[6] = new String(pass.getPassword()); for (int i = 0; i < 7; i++) { userDetails[i] = utfise(userDetails[i], true); } enterTeacher(userDetails); } } ); return; } public void studentEntryPage(final boolean teacher, final String teacherName) { final JLabel userlabel = new JLabel(" Username"); final JLabel passlabel = new JLabel(" Password"); final JLabel slabel1 = new JLabel(" Name"); final JLabel slabel2 = new JLabel(" Class"); final JLabel slabel3 = new JLabel(" Age"); final JLabel slabel4 = new JLabel(" Reading Age"); final JTextField user = new JTextField(); final JTextField inputbox1 = new JTextField(); final JTextField inputbox2 = new JTextField(); final JTextField inputbox3 = new JTextField(); final JTextField inputbox4 = new JTextField(); final JPasswordField pass = new JPasswordField(); final JButton button = new JButton("Enter Details"); final JButton button2 = new JButton("Return to Your User Page"); final String userDetails[] = new String[6]; System.out.println("Create new student"); c.add(userlabel); c.add(user); c.add(slabel1); c.add(inputbox1); c.add(slabel2); c.add(inputbox2); c.add(slabel3); c.add(inputbox3); c.add(slabel4); c.add(inputbox4); c.add(passlabel); c.add(pass); c.add(button); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { userDetails[0] = user.getText(); userDetails[1] = inputbox1.getText(); userDetails[2] = inputbox2.getText(); userDetails[3] = inputbox3.getText(); userDetails[4] = inputbox4.getText(); userDetails[5] = new String(pass.getPassword()); for (int i = 0; i < 6; i++) { userDetails[i] = utfise(userDetails[i], true); } System.out.println(userDetails[0]); enterStudent(userDetails,teacher); } } ); if (teacher == true) { c.add(button2); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { cleanUp(); teacherMainPage(teacherName); } }); } return; } public void enterStudent(String userDetails[], boolean notStudent) { String node1 = "" + "" + "" + "" + userDetails[1] + "" + "" + userDetails[0] + "" + "" + userDetails[5] + "" + "" + userDetails[2] + "" + "" + userDetails[3] + "" + "" + userDetails[4] + "" + ""; for (int i = 1; i < 13; i++) { node1 = node1.concat("" + "" + i + "" + "No" + "N/A"); } node1 = node1.concat(""); System.out.println(node1); FileOutputStream out; try { userDetails[0] = utfise(userDetails[0],false); out = new FileOutputStream(userDetails[0] + ".xml"); PrintStream p = new PrintStream(out); p.println(node1); p.close(); } catch (Exception e) { System.out.println("FileOutputStream error: " + e); } File f = new File(userDetails[0] + ".xml"); createDocument("Students", f, userDetails[0] + ".xml"); if (notStudent == false) { cleanUp(); studentMainPage(userDetails[0] + ".xml"); } return; } public void enterTeacher(String userDetails[]) { String node1 = "" + "" + "" + "" + userDetails[1] + "" + "" + userDetails[0] + "" + "" + userDetails[6] + "" + "" + "" + "" + userDetails[2] + "" + "" + "" + "" + userDetails[3] + "" + "" + "" + "" + userDetails[4] + "" + "" + "" + "" + userDetails[5] + "" + "" + ""; FileOutputStream out; try { userDetails[0] = utfise(userDetails[0],false); out = new FileOutputStream(userDetails[0] + ".xml"); PrintStream p = new PrintStream(out); p.println(node1); p.close(); } catch (Exception e) { System.out.println("FileOutputStream error: " + e); } File f = new File(userDetails[0] + ".xml"); createDocument("Teachers", f, userDetails[0] + ".xml"); cleanUp(); teacherMainPage(userDetails[0] + ".xml"); return; } /** This method access the exist database and retrieves a file from the database * @param String xmldoc - the name of the xml document * @param String docpath - this is the path of the document in the database */ public static XMLResource getXMLDoc(String xmldoc, String docpath) { XMLResource retxmldoc = null; try { String collection = dbURL + docpath + "/"; // initialize database drivers Class cl = Class.forName(driver); Database database = (Database) cl.newInstance(); DatabaseManager.registerDatabase(database); // get the collection Collection col = DatabaseManager.getCollection(collection, username, password); col.setProperty("pretty", "true"); retxmldoc = (XMLResource)col.getResource(xmldoc); } catch(Exception e) { System.err.println("getXMLDoc() Error : ExistDBJavaInterface.java - " + e); System.err.println(e.getCause()); } return retxmldoc; } public static boolean createDocument(String docpath, File indoc, String docname) { System.out.println("Class createDocument started"); try { Class cl = Class.forName(driver); Database database = (Database)cl.newInstance(); DatabaseManager.registerDatabase(database); // try to get collection Collection col = DatabaseManager.getCollection(dbURL + docpath, username, password); XMLResource document = (XMLResource)col.createResource(docname, "XMLResource"); try { document.setContent(indoc) ; } catch (Exception e) { System.out.println(" Exception document.setContent - " + e); } try { col.storeResource(document); System.out.println("document created"); } catch (Exception e) { System.out.println("Exception col.storeResource - " + e); } return true; } catch(Exception e) { System.err.println("createDocument(String docpath, File indoc, String docname) Exception ExistDBJavaInterface.java - " + e); } return false; } /** XPath for exist - takes query and path to collection as params * Please note: * Exist now supports XQuery so this class needs to be altered slightly to use XQuery */ public static String XPathListDocument(String query, String collection, String document) { // System.out.println("Class XPathListDocument started"); String outString = new String(); ArrayList out = new ArrayList(); ResourceSet results = null; try { Class cl = Class.forName(driver); Database database = (Database)cl.newInstance(); DatabaseManager.registerDatabase(database); // try to get collection Collection col = DatabaseManager.getCollection(dbURL+collection, username, password); XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0"); service.setProperty("pretty", "true"); //System.out.println("QUERY1"); query = ("document(" + '"' + "/db/" + collection + '/' + document + '"' + ')' + query); // System.out.println(query); results = service.query(query); ResourceIterator i = results.getIterator(); while(i.hasMoreResources()) { Resource r = i.nextResource(); String value = ((String)r.getContent()).trim(); out.add(value); // System.out.println(value); } outString = out.toString(); return outString; } catch(Exception e) { System.err.println("XPathList Exception ExistDBJavaInterface.java - " + e); } return null; } public static String XPathList(String query, String collection) { //System.out.println("Class XPathListDocument started"); String outString = new String(); ArrayList out = new ArrayList(); ResourceSet results = null; try { Class cl = Class.forName(driver); Database database = (Database)cl.newInstance(); DatabaseManager.registerDatabase(database); // try to get collection Collection col = DatabaseManager.getCollection(dbURL+collection, username, password); XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0"); service.setProperty("pretty", "true"); // System.out.println("QUERY1"); // System.out.println(query); results = service.query(query); ResourceIterator i = results.getIterator(); while(i.hasMoreResources()) { Resource r = i.nextResource(); String value = ((String)r.getContent()).trim(); out.add(value); // System.out.println(value); } outString = out.toString(); return outString; } catch(Exception e) { System.err.println("XPathList Exception ExistDBJavaInterface.java - " + e); } return null; } public static void main(String [] args) { System.out.println("Running eXist Interface"); ExistDBJavaInterface existdb = new ExistDBJavaInterface(); existdb.addWindowListener ( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); return; } }