adding cs240

This commit is contained in:
Derek McQuay 2016-04-06 20:50:11 -07:00
parent 8af25fbb22
commit 9d4aab12b8
131 changed files with 137051 additions and 0 deletions

2
cs240/record-indexer/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
build/
out/

View File

@ -0,0 +1 @@
6-record-indexer

View File

@ -0,0 +1,17 @@
<component name="ArtifactManager">
<artifact type="jar" name="search_gui:jar">
<output-path>$PROJECT_DIR$/out/artifacts/search_gui_jar</output-path>
<root id="archive" name="6-record-indexer.jar">
<element id="module-output" name="6-record-indexer" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/commons-io-2.4/commons-io-2.4.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/commons-io-2.4/commons-io-2.4-tests.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/commons-io-2.4/commons-io-2.4-javadoc.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/commons-io-2.4/commons-io-2.4-sources.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/commons-io-2.4/commons-io-2.4-test-sources.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/sqlite-jdbc-3.8.0-20130827.035027-1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/checkstyle-5.3-all.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/junit-4.8.2.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/lib/xstream-1.4.2.jar" path-in-jar="/" />
</root>
</artifact>
</component>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectCodeStyleSettingsManager">
<option name="PER_PROJECT_SETTINGS">
<value>
<XML>
<option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" />
</XML>
</value>
</option>
</component>
</project>

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>

View File

@ -0,0 +1,5 @@
<component name="CopyrightManager">
<settings default="">
<module2copyright />
</settings>
</component>

View File

@ -0,0 +1,11 @@
<component name="ProjectDictionaryState">
<dictionary name="film42">
<words>
<w>accessors</w>
<w>congratz</w>
<w>hackery</w>
<w>thornburg</w>
<w>tuples</w>
</words>
</dictionary>
</component>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
</project>

View File

@ -0,0 +1,11 @@
<component name="libraryTable">
<library name="checkstyle-5.3-all">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/checkstyle-5.3-all.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/junit-4.8.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/xstream-1.4.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -0,0 +1,15 @@
<component name="libraryTable">
<library name="commons-io-2">
<CLASSES>
<root url="file://$PROJECT_DIR$/lib/commons-io-2.4" />
</CLASSES>
<JAVADOC>
<root url="file://$PROJECT_DIR$/lib/commons-io-2.4/docs" />
</JAVADOC>
<SOURCES>
<root url="file://$PROJECT_DIR$/lib/commons-io-2.4" />
</SOURCES>
<jarDirectory url="file://$PROJECT_DIR$/lib/commons-io-2.4" recursive="false" />
<jarDirectory url="file://$PROJECT_DIR$/lib/commons-io-2.4" recursive="false" type="SOURCES" />
</library>
</component>

View File

@ -0,0 +1,11 @@
<component name="libraryTable">
<library name="sqlite-jdbc-3.8.0-20130827.035027-1">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/sqlite-jdbc-3.8.0-20130827.035027-1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$PROJECT_DIR$/lib/sqlite-jdbc-3.8.0-20130827.035027-1.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="JavadocGenerationManager">
<option name="OUTPUT_DIRECTORY" value="$PROJECT_DIR$/../5-design-document/docs" />
<option name="OPTION_SCOPE" value="package" />
<option name="OPTION_HIERARCHY" value="true" />
<option name="OPTION_NAVIGATOR" value="true" />
<option name="OPTION_INDEX" value="true" />
<option name="OPTION_SEPARATE_INDEX" value="true" />
<option name="OPTION_DOCUMENT_TAG_USE" value="false" />
<option name="OPTION_DOCUMENT_TAG_AUTHOR" value="false" />
<option name="OPTION_DOCUMENT_TAG_VERSION" value="false" />
<option name="OPTION_DOCUMENT_TAG_DEPRECATED" value="true" />
<option name="OPTION_DEPRECATED_LIST" value="true" />
<option name="OTHER_OPTIONS" />
<option name="HEAP_SIZE" />
<option name="LOCALE" />
<option name="OPEN_IN_BROWSER" value="true" />
<option name="OPTION_INCLUDE_LIBS" value="false" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/6-record-indexer.iml" filepath="$PROJECT_DIR$/6-record-indexer.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,5 @@
<component name="DependencyValidationManager">
<state>
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</state>
</component>

View File

@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
</component>
</project>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,200 @@
<project name="Record Indexer" default="compile" basedir=".">
<!-- PROPERTIES -->
<!-- ********** -->
<!-- Directory containing application source code -->
<property name="src" location="src"/>
<!-- Directory containing testing source code -->
<property name="test" location="test"/>
<!-- Directory containing Java libraries -->
<property name="lib" location="lib"/>
<!-- Directory where the Java compiler puts .class files -->
<property name="build" location="build"/>
<!-- Directory containing database files -->
<property name="database" location="db"/>
<!-- Directory containing demo files -->
<property name="demo" location="demo"/>
<!-- Directory containing passoff files -->
<property name="passoff" location="passoff"/>
<!-- Database file used by the server -->
<property name="db-file" location="${database}/database.sqlite3"/>
<!-- Database file containing empty tables. -->
<property name="empty-db-file" location="${database}/template.sqlite3"/>
<!-- File containing the report output by Checkstyle -->
<property name="checkstyle-report-file" location="checkstyle-report.txt"/>
<!-- Default values for command-line properties. Values specified on the command-line will override these. -->
<property name="file" value="demo/indexer_data/Records/Records.xml"/>
<property name="host" value="localhost"/>
<property name="port" value="39640"/>
<!-- CLASS PATHS -->
<!-- *********** -->
<path id="lib-classpath">
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</path>
<path id="all-classpath">
<path refid="lib-classpath"/>
<pathelement location="${build}"/>
</path>
<path id="passoff-classpath">
<path refid="all-classpath"/>
<pathelement location="${passoff}/server-passoff.jar"/>
</path>
<!-- TASK DEFINITIONS -->
<!-- **************** -->
<taskdef classpathref="lib-classpath" resource="checkstyletask.properties"/>
<!-- TARGETS -->
<!-- ******* -->
<target name="init" description="create build directories">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${passoff}"/>
</target>
<target name="clean" description="delete build files and directories">
<delete dir="${build}"/>
<delete dir="${passoff}"/>
</target>
<target name="compile" depends="init" description="compile the source code">
<javac srcdir="${src}" classpathref="lib-classpath" destdir="${build}" debug="true" includeantruntime="true" />
<javac srcdir="${test}" classpathref="lib-classpath" destdir="${build}" debug="true" includeantruntime="true" />
</target>
<!-- ant importer -Dfile=<INDEXER_DATA_XML_FILE> -->
<!-- EXAMPLE: ant importer -Dfile=dir1/dir2/indexer_data.xml -->
<target name="import" depends="compile" description="import indexer data">
<copy file="${empty-db-file}" tofile="${db-file}" overwrite="true"/>
<java classname="server.db.importer.Importer" fork="yes">
<classpath>
<path refid="all-classpath"/>
</classpath>
<arg value="${file}"/>
<assertions>
<enable/>
</assertions>
</java>
</target>
<!-- ant server {-Dport=<SERVER_PORT_NUMBER>} -->
<!-- EXAMPLE: ant server -Dport=39640 -->
<!-- If port number is omitted, run on default port. -->
<!-- EXAMPLE: ant server -->
<target name="server" depends="compile" description="run server on specified port">
<java classname="server.Server" fork="yes">
<classpath>
<path refid="all-classpath"/>
</classpath>
<arg value="${port}"/>
<assertions>
<enable/>
</assertions>
</java>
</target>
<target name="server-gui" depends="compile" description="run the server test gui">
<java classname="servertester.GuiTester" fork="yes">
<classpath>
<path refid="all-classpath"/>
</classpath>
<assertions>
<enable/>
</assertions>
</java>
</target>
<target name="server-tests" depends="compile" description="run automated server tests">
<java classname="server.ServerUnitTests" fork="yes">
<classpath>
<path refid="all-classpath"/>
</classpath>
<assertions>
<enable/>
</assertions>
</java>
</target>
<!-- ant server-passoff {-Dhost=<SERVER_HOST>} {-Dport=<SERVER_PORT_NUMBER>} -->
<!-- EXAMPLE: ant server-passoff -Dhost=testserver.cs.byu.edu -Dport=39640 -->
<!-- If host and/or port number are omitted, default values are used. -->
<!-- EXAMPLE: ant server-passoff -->
<target name="server-passoff" depends="compile" description="run server passoff using server on specified host and port">
<java classname="passoff.ServerPassoff" fork="yes">
<classpath>
<path refid="passoff-classpath"/>
</classpath>
<arg value="${host}"/>
<arg value="${port}"/>
<assertions>
<enable/>
</assertions>
</java>
</target>
<!-- ant client {-Dhost=<SERVER_HOST>} {-Dport=<SERVER_PORT_NUMBER>} -->
<!-- EXAMPLE: ant client -Dhost=testserver.cs.byu.edu -Dport=39640 -->
<!-- If host and/or port number are omitted, default values are used. -->
<!-- EXAMPLE: ant client -->
<target name="client" depends="compile" description="run client using server on specified host and port">
<java classname="client.Client" fork="yes">
<classpath>
<path refid="all-classpath"/>
</classpath>
<arg value="${host}"/>
<arg value="${port}"/>
<assertions>
<enable/>
</assertions>
</java>
</target>
<target name="client-tests" depends="compile" description="run automated client tests">
<java classname="client.ClientUnitTests" fork="yes">
<classpath>
<path refid="all-classpath"/>
</classpath>
<assertions>
<enable/>
</assertions>
</java>
</target>
<target name="checkstyle" depends="compile" description="run checkstyle on all source code">
<checkstyle config="checkstyle.xml">
<fileset dir="${src}" includes="**/*.java">
</fileset>
<formatter type="plain" toFile="${checkstyle-report-file}"/>
<classpath>
<pathelement location="${build}"/>
</classpath>
</checkstyle>
</target>
<target name="demo" description="run the demo">
<java jar="${demo}/record-indexer-demo.jar" fork="yes" dir="${demo}">
</java>
</target>
</project>

View File

@ -0,0 +1,58 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="FileLength">
<property name="max" value="2000"/>
</module>
<module name="TreeWalker">
<property name="tabWidth" value="4"/>
<module name="NestedForDepth">
<property name="max" value="1"/>
</module>
<module name="NestedIfDepth">
<property name="max" value="2"/>
</module>
<module name="NestedTryDepth">
<property name="max" value="1"/>
</module>
<module name="BooleanExpressionComplexity">
<property name="max" value="5"/>
</module>
<module name="CyclomaticComplexity">
<property name="max" value="10"/>
</module>
<module name="NPathComplexity">
<property name="max" value="200"/>
</module>
<module name="LineLength">
<property name="max" value="100"/>
</module>
<module name="MethodLength">
<property name="max" value="120"/>
<property name="countEmpty" value="false"/>
</module>
<module name="ParameterNumber">
<property name="max" value="7"/>
<property name="tokens" value="METHOD_DEF"/>
</module>
</module>
</module>

View File

@ -0,0 +1,107 @@
package client;
import client.communication.Communicator;
import client.components.MainWindow;
import client.components.loginWindow.ErrorLoginDialog;
import client.components.loginWindow.LoginWindow;
import client.components.loginWindow.SuccessLoginDialog;
import shared.communication.params.ValidateUser_Param;
import shared.communication.responses.ValidateUser_Res;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Client {
private LoginWindow loginWindow;
private Communicator communicator;
public Client(Communicator communicator) {
this.communicator = communicator;
}
public void run() {
loginWindow = new LoginWindow(communicator);
loginWindow.addLoginListener(loginListener);
// Run
EventQueue.invokeLater(new Runnable() {
public void run() {
loginWindow.setVisible(true);
}
});
}
public static void main(String[] args) {
// Create Window
String host = args[0];
String port = args[1];
String server = "http://"+host+":"+port+"/";
Communicator communicator = new Communicator(server);
Client client = new Client(communicator);
client.run();
}
private ActionListener loginListener = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
ValidateUser_Param param = new ValidateUser_Param();
param.setUsername(loginWindow.getUsername());
param.setPassword(loginWindow.getPassword());
try {
ValidateUser_Res validateUserRes;
validateUserRes = communicator.validateUser(param);
loginWindow.setVisible(false);
SuccessLoginDialog successLoginDialog = new SuccessLoginDialog(validateUserRes);
successLoginDialog.addWindowListener(openMainWindowListener);
successLoginDialog.setVisible(true);
} catch (Exception execption) {
ErrorLoginDialog errorLoginDialog = new ErrorLoginDialog();
errorLoginDialog.setVisible(true);
}
}
};
private WindowListener openMainWindowListener = new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
super.windowClosed(e);
// Run
EventQueue.invokeLater(new Runnable() {
public void run() {
MainWindow frame = new MainWindow(communicator, loginWindow.getUsername(),
loginWindow.getPassword());
frame.addWindowListener(logoutListener);
frame.setVisible(true);
}
});
}
};
private WindowListener logoutListener = new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
super.windowClosed(e);
loginWindow = new LoginWindow(communicator);
loginWindow.addLoginListener(loginListener);
// Run
EventQueue.invokeLater(new Runnable() {
public void run() {
loginWindow.setVisible(true);
}
});
}
};
}

View File

@ -0,0 +1,119 @@
package client.communication;
import client.communication.errors.RemoteServerErrorException;
import client.communication.errors.UnauthorizedAccessException;
import client.communication.modules.HttpClient;
import shared.communication.params.*;
import shared.communication.responses.*;
import java.io.ByteArrayOutputStream;
public class Communicator {
public Communicator(String serverPath) {
this.serverPath = serverPath;
}
private String serverPath;
public String getServerPath() {
return serverPath;
}
public ValidateUser_Res validateUser(ValidateUser_Param user)
throws UnauthorizedAccessException, RemoteServerErrorException {
String resource = "validateUser/";
String response = HttpClient.post(serverPath + resource, user.toXML());
if(response == null)
return null;
return ValidateUser_Res.serialize(response);
}
public Projects_Res getProjects(Projects_Param projects)
throws UnauthorizedAccessException, RemoteServerErrorException {
String resource = "getProjects/";
String response = HttpClient.post(serverPath + resource, projects.toXML());
if(response == null)
return null;
return Projects_Res.serialize(response);
}
public SampleImage_Res getSampleImage(SampleImage_Param sampleImage)
throws UnauthorizedAccessException, RemoteServerErrorException {
String resource = "getSampleImage/";
String response = HttpClient.post(serverPath + resource, sampleImage.toXML());
if(response == null)
return null;
return SampleImage_Res.serialize(response);
}
public DownloadBatch_Res downloadBatch(DownloadBatch_Param downloadBatch)
throws UnauthorizedAccessException, RemoteServerErrorException {
String resource = "downloadBatch/";
String response = HttpClient.post(serverPath + resource, downloadBatch.toXML());
if(response == null)
return null;
return DownloadBatch_Res.serialize(response);
}
public SubmitBatch_Res submitBatch(SubmitBatch_Param submitBatch)
throws UnauthorizedAccessException, RemoteServerErrorException {
String resource = "submitBatch/";
String response = HttpClient.post(serverPath + resource, submitBatch.toXML());
if(response == null)
return null;
return SubmitBatch_Res.serialize(response);
}
public Fields_Res getFields(Fields_Param fields)
throws UnauthorizedAccessException, RemoteServerErrorException {
String resource = "getFields/";
String response = HttpClient.post(serverPath + resource, fields.toXML());
if(response == null)
return null;
return Fields_Res.serialize(response);
}
public Search_Res search(Search_Param search)
throws UnauthorizedAccessException, RemoteServerErrorException {
String resource = "search/";
String response = HttpClient.post(serverPath + resource, search.toXML());
if(response == null)
return null;
return Search_Res.serialize(response);
}
public ByteArrayOutputStream downloadStatic(String resource) {
try {
return HttpClient.getStatic(serverPath+resource);
} catch (UnauthorizedAccessException e) {
return null;
} catch (RemoteServerErrorException e) {
return null;
}
}
}

View File

@ -0,0 +1,20 @@
package client.communication.errors;
public class RemoteServerErrorException extends Exception {
public RemoteServerErrorException() {
}
public RemoteServerErrorException(String message) {
super(message);
}
public RemoteServerErrorException(Throwable throwable) {
super(throwable);
}
public RemoteServerErrorException(String message, Throwable throwable) {
super(message, throwable);
}
}

View File

@ -0,0 +1,21 @@
package client.communication.errors;
@SuppressWarnings("serial")
public class UnauthorizedAccessException extends Exception {
public UnauthorizedAccessException() {
}
public UnauthorizedAccessException(String message) {
super(message);
}
public UnauthorizedAccessException(Throwable throwable) {
super(throwable);
}
public UnauthorizedAccessException(String message, Throwable throwable) {
super(message, throwable);
}
}

View File

@ -0,0 +1,113 @@
package client.communication.modules;
import client.communication.errors.RemoteServerErrorException;
import client.communication.errors.UnauthorizedAccessException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpClient {
private static InputStream request(String url, String method, String request)
throws RemoteServerErrorException, UnauthorizedAccessException{
try {
URL requestURL = new URL(url);
HttpURLConnection connection = (HttpURLConnection) requestURL.openConnection();
// We can generalize, whatever
connection.setDoOutput(true);
connection.setRequestMethod(method);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
connection.getOutputStream());
outputStreamWriter.write(request);
outputStreamWriter.close();
switch (connection.getResponseCode()) {
case HttpURLConnection.HTTP_OK:
return connection.getInputStream();
case HttpURLConnection.HTTP_UNAUTHORIZED:
throw new UnauthorizedAccessException();
default:
throw new RemoteServerErrorException();
}
} catch (MalformedURLException e) {
throw new RemoteServerErrorException();
} catch (IOException e) {
throw new RemoteServerErrorException();
}
}
public static String get(String url)
throws UnauthorizedAccessException, RemoteServerErrorException {
InputStream response = request(url, "GET", "");
return inputStreamToString(response);
}
public static String post(String url, String req)
throws UnauthorizedAccessException, RemoteServerErrorException {
InputStream response = request(url, "POST", req);
return inputStreamToString(response);
}
public static ByteArrayOutputStream getStatic(String url)
throws UnauthorizedAccessException, RemoteServerErrorException {
InputStream response = request(url, "GET", "");
if(response != null) {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] byteArray = new byte[512];
int bytesRead = 0;
while((bytesRead = response.read(byteArray)) != -1) {
byteArrayOutputStream.write(byteArray, 0, bytesRead);
}
return byteArrayOutputStream;
} catch (IOException e) {
return null;
}
}
return null;
}
private static String inputStreamToString(InputStream inputStream) {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
try {
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try { bufferedReader.close(); } catch (IOException e) {
e.printStackTrace(); }
}
try {
inputStream.close();
} catch (IOException e) {
}
return stringBuilder.toString();
}
}

View File

@ -0,0 +1,102 @@
package client.components;
import client.communication.Communicator;
import client.components.downloadModal.DownloadModal;
import client.components.loginWindow.ErrorLoginDialog;
import client.persistence.ImageState;
import client.persistence.NewProjectListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
public class FileMenu extends JMenuBar {
private MainWindow mainWindow;
private Communicator communicator;
private ImageState imageState;
private JMenuItem eMenuItem1;
public FileMenu(MainWindow mainWindow, Communicator communicator, ImageState imageState) {
this.mainWindow = mainWindow;
this.communicator = communicator;
this.imageState = imageState;
this.imageState.addNewProjectListener(newProjectListener);
setupView();
}
private void setupView() {
// Prevents menu items from filling the whole length
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
JMenu file1 = new JMenu("File");
eMenuItem1 = new JMenuItem("Download Batch");
eMenuItem1.addActionListener(downloadBatchAction);
eMenuItem1.setEnabled(!imageState.isHasImage());
eMenuItem1.setToolTipText("Exit application");
JMenuItem eMenuItem2 = new JMenuItem("Logout");
eMenuItem2.addActionListener(logoutAction);
eMenuItem2.setToolTipText("Exit application");
JMenuItem eMenuItem3 = new JMenuItem("Exit");
eMenuItem3.addActionListener(exitAction);
eMenuItem2.setToolTipText("Exit application");
file1.add(eMenuItem1);
file1.add(eMenuItem2);
file1.add(eMenuItem3);
// Add to self
this.add(file1);
this.setBackground(Color.WHITE);
}
private void updateSettings() {
imageState.getSettings().setWindowHeight(mainWindow.getHeight());
imageState.getSettings().setWindowWidth(mainWindow.getWidth());
Point point = mainWindow.getLocationOnScreen();
imageState.getSettings().setWindowPositionX((int) point.getX());
imageState.getSettings().setWindowPositionY((int) point.getY());
imageState.save();
}
private ActionListener downloadBatchAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
DownloadModal downloadModal = new DownloadModal(imageState, communicator);
downloadModal.setVisible(true);
}
};
private ActionListener logoutAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
updateSettings();
mainWindow.dispose();
}
};
private ActionListener exitAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
updateSettings();
System.exit(1);
}
};
private NewProjectListener newProjectListener = new NewProjectListener() {
@Override
public void hasNewProject() {
boolean status = imageState.isHasImage();
eMenuItem1.setEnabled(!status);
}
};
}

View File

@ -0,0 +1,147 @@
package client.components;
import client.communication.Communicator;
import client.components.downloadModal.DownloadModal;
import client.components.imagePanel.ImagePanel;
import client.persistence.Cell;
import client.persistence.ImageState;
import client.persistence.NewProjectListener;
import client.persistence.Settings;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.*;
public class MainWindow extends JFrame implements Serializable {
public ImageState imageState;
private Communicator communicator;
JSplitPane body = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JPanel(), new JPanel());
public MainWindow(Communicator communicator, String username, String password) {
Settings settings = loadSettings(username);
this.imageState = loadImageState(username);
if(this.imageState == null) {
this.imageState = new ImageState(settings, communicator, username, password);
} else {
this.imageState.setCommunicator(communicator);
}
this.imageState.setSettings(settings);
this.imageState.addNewProjectListener(newProjectListener);
this.communicator = communicator;
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(settings.getWindowWidth(), settings.getWindowHeight());
this.setLocation(settings.getWindowPositionX(), settings.getWindowPositionY());
setupView();
this.imageState.initEvents();
this.addWindowListener(windowListener);
}
private void setupView() {
setupFileMenu();
setupImagePanel();
setupSplitView();
this.add(body, BorderLayout.CENTER);
body.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent pce) {
int dividerLocation = body.getDividerLocation();
imageState.getSettings().setBaseSplitY(dividerLocation);
}
});
}
private void setupFileMenu() {
// Setup File Menu
this.add(new FileMenu(this, communicator, imageState), BorderLayout.NORTH);
}
private void setupImagePanel() {
body.setTopComponent(new ImagePanel(imageState));
}
private void setupSplitView() {
SplitBase splitBase = new SplitBase(imageState, communicator);
body.setBottomComponent(splitBase);
body.setBorder(null);
body.setDividerLocation(imageState.getSettings().getBaseSplitY());
}
public ImageState loadImageState(String username) {
File dest = new File("profiles/"+username);
if(dest.exists()) {
FileInputStream fis = null;
ObjectInputStream in = null;
try {
fis = new FileInputStream("profiles/"+username+"/state.ser");
in = new ObjectInputStream(fis);
return (ImageState)in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return null;
}
public Settings loadSettings(String username) {
File dest = new File("profiles/"+username);
if(dest.exists()) {
FileInputStream fis = null;
ObjectInputStream in = null;
try {
fis = new FileInputStream("profiles/"+username+"/settings.ser");
in = new ObjectInputStream(fis);
return (Settings)in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return Settings.defaultSettings();
}
private WindowListener windowListener = new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
}
@Override
public void windowClosed(WindowEvent e) {
super.windowClosed(e);
}
};
private NewProjectListener newProjectListener = new NewProjectListener() {
@Override
public void hasNewProject() {
}
};
}

View File

@ -0,0 +1,74 @@
package client.components;
import client.communication.Communicator;
import client.components.fieldHelp.FieldHelp;
import client.components.formEntry.FormEntry;
import client.persistence.SyncContext;
import client.components.tableEntry.TableEntry;
import client.persistence.Cell;
import client.persistence.ImageState;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class SplitBase extends JSplitPane {
private ImageState imageState;
private JTabbedPane tabbedPane;
private TableEntry tableEntry;
private FormEntry formEntry;
private Communicator communicator;
public SplitBase(ImageState imageState, Communicator communicator) {
this.imageState = imageState;
this.communicator = communicator;
setupView();
}
private void setupView() {
tabbedPane = new JTabbedPane();
tabbedPane.addChangeListener(changeListener);
tableEntry = new TableEntry(imageState);
tabbedPane.addTab("Table Entry", tableEntry);
formEntry = new FormEntry(imageState);
tabbedPane.addTab("Form Entry", formEntry);
this.setLeftComponent(tabbedPane);
JTabbedPane tabbedPane2 = new JTabbedPane();
tabbedPane2.addTab("Field Help", new FieldHelp(imageState, communicator));
tabbedPane2.addTab("Image Navigator", new JPanel());
this.setRightComponent(tabbedPane2);
this.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent pce) {
int dividerLocation = getDividerLocation();
imageState.getSettings().setBaseSplitX(dividerLocation);
}
});
this.setDividerLocation(imageState.getSettings().getBaseSplitX());
}
private ChangeListener changeListener = new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if(tabbedPane.getSelectedIndex() == 1) {
formEntry.becameVisible();
}
}
};
}

View File

@ -0,0 +1,156 @@
package client.components.downloadModal;
import client.communication.Communicator;
import client.communication.errors.RemoteServerErrorException;
import client.communication.errors.UnauthorizedAccessException;
import client.persistence.ImageState;
import shared.communication.common.Project_Res;
import shared.communication.params.Projects_Param;
import shared.communication.params.SampleImage_Param;
import shared.communication.responses.Projects_Res;
import shared.communication.responses.SampleImage_Res;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import java.util.List;
public class DownloadModal extends JDialog {
private ImageState imageState;
private Communicator communicator;
private List<Project_Res> projects;
private JComboBox batchSelect;
public DownloadModal(ImageState imageState, Communicator communicator) {
this.imageState = imageState;
this.communicator = communicator;
setupView();
}
private void setupView() {
this.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
this.setTitle("Download Image");
this.setSize(350, 100);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setLayout(new FlowLayout());
JLabel label = new JLabel("Project: ");
this.add(label);
projects = getProjects().getProjectsList();
List<String> values = getProjectNames();
batchSelect = new JComboBox(values.toArray());
this.add(batchSelect);
JButton sampleImageButton = new JButton("Sample Image?");
sampleImageButton.addActionListener(getSampleImageListener);
this.add(sampleImageButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(closeListener);
this.add(cancelButton);
JButton downloadButton = new JButton("Download");
downloadButton.addActionListener(downloadListener);
this.add(downloadButton);
}
private Projects_Res getProjects() {
Projects_Param param = new Projects_Param();
param.setUsername(imageState.getUsername());
param.setPassword(imageState.getPassword());
try {
return communicator.getProjects(param);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private SampleImage_Res getSampleImage(int projectId) {
SampleImage_Param param = new SampleImage_Param();
param.setUsername(imageState.getUsername());
param.setPassword(imageState.getPassword());
param.setProjectId(projectId);
try {
return communicator.getSampleImage(param);
} catch (Exception e) {
}
return null;
}
private ActionListener getSampleImageListener = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
String projectTitle = (String)batchSelect.getSelectedItem();
int projectId = getProjectIdForName(projectTitle);
SampleImage_Res res = getSampleImage(projectId);
String fullPath = communicator.getServerPath() + res.getUrl();
SampleImageModal sampleImageModal = new SampleImageModal(fullPath);
sampleImageModal.setVisible(true);
}
};
private ActionListener closeListener = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispatchEvent(new WindowEvent(DownloadModal.this, WindowEvent.WINDOW_CLOSING));
}
};
private ActionListener downloadListener = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
String projectTitle = (String)batchSelect.getSelectedItem();
int projectId = getProjectIdForName(projectTitle);
imageState.downloadProject(projectId);
setVisible(false);
dispatchEvent(new WindowEvent(DownloadModal.this, WindowEvent.WINDOW_CLOSING));
}
};
private int getProjectIdForName(String title) {
for(Project_Res project : projects) {
if(title.equals(project.getTitle())) {
return project.getId();
}
}
return -1;
}
private List<String> getProjectNames() {
ArrayList<String> titles = new ArrayList<>();
for(Project_Res project : projects) {
titles.add(project.getTitle());
}
return titles;
}
}

View File

@ -0,0 +1,25 @@
package client.components.downloadModal;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class SampleImage extends JPanel {
BufferedImage image;
public SampleImage(BufferedImage image) {
this.image = image;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.scale(0.7, 0.7);
g2.drawImage(image, 0, 0, null);
}
}

View File

@ -0,0 +1,52 @@
package client.components.downloadModal;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class SampleImageModal extends JDialog {
BufferedImage image;
public SampleImageModal(String path) {
try {
image = ImageIO.read(new URL(path));
} catch (Exception e1) {
return;
}
setupView();
}
private void setupView() {
this.setTitle("Sample Image from XXXXXXX");
this.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
this.setSize(500, 410);
this.setResizable(false);
this.setLocationRelativeTo(null);
SampleImage sampleImage = new SampleImage(image);
this.add(sampleImage, BorderLayout.CENTER);
JButton closeButton = new JButton("Close");
closeButton.addActionListener(closeListener);
this.add(closeButton, BorderLayout.SOUTH);
}
private ActionListener closeListener = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispatchEvent(new WindowEvent(SampleImageModal.this, WindowEvent.WINDOW_CLOSING));
}
};
}

View File

@ -0,0 +1,77 @@
package client.components.fieldHelp;
import client.communication.Communicator;
import client.persistence.Cell;
import client.persistence.ImageState;
import client.persistence.ImageStateListener;
import client.persistence.NewProjectListener;
import shared.communication.common.Fields;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
public class FieldHelp extends JPanel {
private ImageState imageState;
private String[] columns;
private int currentColumn;
private JEditorPane editorPane;
private Communicator communicator;
public FieldHelp(ImageState imageState, Communicator communicator) {
this.imageState = imageState;
this.communicator = communicator;
this.currentColumn = 0;
this.columns = imageState.getColumnNames();
setupView();
this.imageState.addListener(imageStateListener);
this.imageState.addNewProjectListener(newProjectListener);
}
private void setupView() {
editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setEditable(false);
this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
this.add(new JScrollPane(editorPane), BorderLayout.CENTER);
}
private void updateView() {
if(!imageState.isHasImage()) return;
Fields field = imageState.getFieldsMetaData().get(currentColumn);
String path = communicator.getServerPath() + field.getHelpUrl();
try {
editorPane.setPage(path);
} catch (IOException e) {
e.printStackTrace();
}
}
private ImageStateListener imageStateListener = new ImageStateListener() {
@Override
public void valueChanged(Cell cell, String newValue) {
}
@Override
public void selectedCellChanged(Cell newSelectedCell) {
currentColumn = newSelectedCell.getField();
updateView();
}
};
private NewProjectListener newProjectListener = new NewProjectListener() {
@Override
public void hasNewProject() {
editorPane.setText("");
}
};
}

View File

@ -0,0 +1,141 @@
package client.components.formEntry;
import client.modules.spellChecker.KnownData;
import client.persistence.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;
public class FormEntry extends JPanel {
private JList rowNumberList;
private FormTable formTable;
private JSplitPane splitPane;
private Cell currentCell;
private String[][] model;
private String[] columnNames;
private Integer[] rowIds;
private ImageState imageState;
public FormEntry(ImageState imageState) {
this.imageState = imageState;
this.model = this.imageState.getModel();
this.columnNames = this.imageState.getColumnNames();
this.imageState.addNewProjectListener(newProjectListener);
setupView();
}
private void setupView() {
this.setLayout(new GridLayout(1,1));
this.rowIds = new Integer[model.length];
generateListData();
splitPane = new JSplitPane();
splitPane.setDividerLocation(50);
splitPane.setBorder(null);
formTable = new FormTable(imageState);
splitPane.setRightComponent(new JScrollPane(formTable));
rowNumberList = new JList(rowIds);
rowNumberList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
rowNumberList.setLayoutOrientation(JList.VERTICAL);
rowNumberList.setVisibleRowCount(-1);
rowNumberList.addListSelectionListener(listSelectionListener);
splitPane.setLeftComponent(new JScrollPane(rowNumberList));
this.add(splitPane);
imageState.addListener(imageStateListener);
}
public void generateListData() {
for(int i = 0; i < rowIds.length; i ++) {
rowIds[i] = (i+1);
}
}
@Override
public Dimension getMinimumSize() {
Dimension dim = super.getMinimumSize();
dim.width = 350;
return dim;
}
private ImageStateListener imageStateListener = new ImageStateListener() {
@Override
public void valueChanged(Cell cell, String newValue) {
int row = cell.getRecord();
int column = cell.getField();
rowNumberList.setSelectedIndex(row);
formTable.setValue(newValue, row, column);
splitPane.repaint();
}
@Override
public void selectedCellChanged(Cell newSelectedCell) {
int row = newSelectedCell.getRecord();
int column = newSelectedCell.getField();
currentCell = newSelectedCell;
rowNumberList.setSelectedIndex(row);
formTable.setCurrentCell(row, column);
splitPane.repaint();
}
};
private NewProjectListener newProjectListener = new NewProjectListener() {
@Override
public void hasNewProject() {
model = imageState.getModel();
columnNames = imageState.getColumnNames();
formTable.setDeactivated(true);
formTable = new FormTable(imageState);
splitPane.setRightComponent(new JScrollPane(formTable));
rowIds = new Integer[model.length];
generateListData();
rowNumberList.setListData(rowIds);
}
};
private ListSelectionListener listSelectionListener = new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if(!getParent().getParent().getParent().isVisible()) return;
int newRow = rowNumberList.getSelectedIndex();
Cell cell = new Cell();
cell.setRecord(newRow);
cell.setField(currentCell.getField());
imageState.setSelectedCell(cell);
repaint();
}
};
public void becameVisible() {
formTable.setCurrentCellForce();
}
}

View File

@ -0,0 +1,195 @@
package client.components.formEntry;
import client.modules.spellChecker.KnownData;
import client.persistence.Cell;
import client.persistence.ImageState;
import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
public class FormTable extends JPanel {
private String[] fieldNames;
private String[][] values;
private boolean updatingCell;
private ImageState imageState;
private int currentRow;
private boolean deactivated = false;
public FormTable(ImageState imageState) {
this.imageState = imageState;
this.fieldNames = this.imageState.getColumnNames();
this.values = this.imageState.getModel();
this.currentRow = 0;
setupView();
}
private void setupView() {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
currentRow = 0;
initForm();
}
private void initForm() {
for(int i = 0; i < fieldNames.length; i++) {
String labelString = fieldNames[i];
String textFieldString = values[currentRow][i];
JPanel formContainer = new JPanel();
JLabel label = new JLabel(labelString);
label.setPreferredSize(new Dimension(100,30));
formContainer.add(label, BorderLayout.WEST);
JTextField textField = new JTextField(textFieldString);
textField.addFocusListener(generateFocusListener(textField, i));
textField.setPreferredSize(new Dimension(150, 30));
if(hasSuggestion(textField.getText(), i)) {
textField.setBackground(Color.RED);
}
formContainer.add(textField, BorderLayout.CENTER);
this.add(formContainer);
}
}
public void updateCurrentCell(JTextField textField, int index) {
if(updatingCell || deactivated) return;
Cell cell = new Cell();
cell.setRecord(currentRow);
cell.setField(index);
updatingCell = true;
imageState.setSelectedCell(cell);
updatingCell = false;
}
public void updateCellValue(JTextField textField, int index) {
if(updatingCell || deactivated) return;
Cell cell = new Cell();
cell.setRecord(currentRow);
cell.setField(index);
values[currentRow][index] = textField.getText();
if(hasSuggestion(textField.getText(), index)) {
textField.setBackground(Color.RED);
} else {
textField.setBackground(Color.WHITE);
}
updatingCell = true;
imageState.setValue(cell, textField.getText());
updatingCell = false;
}
public boolean hasSuggestion(String value, int column) {
if(value.equals("")) return false;
KnownData knownData = imageState.getKnownDataValues().get(column);
String[] words = knownData.getWordArray();
for(String val : words) {
if(val.toLowerCase().equals(value.toLowerCase())) return false;
}
return true;
}
private FocusListener generateFocusListener(final JTextField textField, final int index) {
return new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
updateCurrentCell(textField, index);
}
@Override
public void focusLost(FocusEvent e) {
updateCellValue(textField, index);
}
};
}
private void updateView() {
if(deactivated) return;
updatingCell = true;
for(int i = 0; i < this.getComponents().length; i++) {
JPanel formSet = (JPanel)this.getComponent(i);
JTextField form = (JTextField)formSet.getComponent(1);
form.setText(values[currentRow][i]);
if(hasSuggestion(form.getText(), i)) {
form.setBackground(Color.RED);
} else {
form.setBackground(Color.WHITE);
}
}
updatingCell = false;
}
public void setValue(String newValue, int row, int column) {
if(updatingCell || deactivated) return;
this.updateView();
this.repaint();
}
public void setCurrentCell(int row, int column) {
if(updatingCell || deactivated) return;
this.currentRow = row;
this.updateView();
this.repaint();
this.setFocus(column);
}
public void setCurrentCellForce() {
if(deactivated) return;
final Cell cell = imageState.getSelectedCell();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setFocus(cell.getField());
}
});
}
public void setFocus(int columnField) {
if(deactivated) return;
// offset is x - 1, cause start at 0.
int column = columnField;
if(values.length == 0) return;
// get the column textField and request focus
JPanel formList = (JPanel)this.getComponent(column);
final JTextField form = (JTextField)formList.getComponent(1);
form.requestFocus();
}
public void setDeactivated(boolean deactivated) {
this.deactivated = deactivated;
}
}

View File

@ -0,0 +1,45 @@
package client.components.imagePanel;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class ImageCell {
private double x;
private double y;
private double width;
private double height;
private boolean isSelected;
Rectangle2D.Double rectangle2D;
public ImageCell(Rectangle2D.Double rectangle2D) {
this.rectangle2D = rectangle2D;
this.x = rectangle2D.getBounds2D().getX();
this.y = rectangle2D.getBounds2D().getY();
this.width = this.x = rectangle2D.getBounds2D().getWidth();
this.height = this.x = rectangle2D.getBounds2D().getHeight();
this.isSelected = false;
}
public void paint(Graphics2D g2, boolean isSelected) {
if(isSelected) {
g2.setColor(new Color(0,119,204, 150));
} else {
g2.setColor(new Color(0,0,0, 0));
}
g2.fill(rectangle2D);
}
public boolean contains(double x, double y) {
return rectangle2D.contains(x, y);
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}}

View File

@ -0,0 +1,174 @@
package client.components.imagePanel;
import client.components.imagePanel.listeners.ImageControlsListener;
import client.persistence.ImageState;
import client.persistence.NewProjectListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class ImageControl extends JPanel {
private ArrayList<ImageControlsListener> imageControlsListeners;
private ImageState imageState;
private JButton zoomInButton;
private JButton zoomOutButton;
private JButton invertButton;
private JButton toggleHighlightsButton;
private JButton saveButton;
private JButton submitButton;
public ImageControl(ImageState imageState) {
this.imageState = imageState;
this.imageState.addNewProjectListener(newProjectListener);
setupView();
imageControlsListeners = new ArrayList<>();
}
private void setupView() {
boolean enabled = false;
if(imageState.isHasImage()) enabled = true;
zoomInButton = new JButton("Zoom In");
zoomInButton.addActionListener(zoomInAction);
zoomInButton.setEnabled(enabled);
this.add(zoomInButton, BorderLayout.WEST);
zoomOutButton = new JButton("Zoom Out");
zoomOutButton.addActionListener(zoomOutAction);
zoomOutButton.setEnabled(enabled);
this.add(zoomOutButton, BorderLayout.WEST);
invertButton = new JButton("Invert");
invertButton.addActionListener(invertImageAction);
invertButton.setEnabled(enabled);
this.add(invertButton, BorderLayout.WEST);
toggleHighlightsButton = new JButton("Toggle Highlights");
toggleHighlightsButton.addActionListener(toggleHighlightsAction);
toggleHighlightsButton.setEnabled(enabled);
this.add(toggleHighlightsButton, BorderLayout.WEST);
saveButton = new JButton("Save");
saveButton.setEnabled(enabled);
saveButton.addActionListener(saveAction);
this.add(saveButton, BorderLayout.WEST);
submitButton = new JButton("Submit");
submitButton.setEnabled(enabled);
submitButton.addActionListener(submitAction);
this.add(submitButton, BorderLayout.WEST);
this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
}
@Override
public Dimension getMaximumSize() {
Dimension dim = super.getMaximumSize();
dim.height = 60;
return dim;
}
public void addControlsListener(ImageControlsListener imageControlsListener) {
imageControlsListeners.add(imageControlsListener);
}
private void updateZoomInListeners() {
for(ImageControlsListener cL : imageControlsListeners) {
cL.onScrollIncrease();
}
}
private void updateZoomOutListeners() {
for(ImageControlsListener cL : imageControlsListeners) {
cL.onScrollDecrease();
}
}
private void updateInvertImageListeners() {
for(ImageControlsListener cL : imageControlsListeners) {
cL.onInvertImage();
}
}
private void updateToggleHighlightsListeners() {
for(ImageControlsListener cL : imageControlsListeners) {
cL.onToggleHighlights();
}
}
private void updateSaveListeners() {
imageState.save();
}
private void updateSubmitListeners() {
imageState.submitProject();
}
private ActionListener zoomInAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
updateZoomInListeners();
}
};
private ActionListener zoomOutAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
updateZoomOutListeners();
}
};
private ActionListener invertImageAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
updateInvertImageListeners();
}
};
private ActionListener toggleHighlightsAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
updateToggleHighlightsListeners();
}
};
private ActionListener saveAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
updateSaveListeners();
}
};
private ActionListener submitAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
updateSubmitListeners();
}
};
private NewProjectListener newProjectListener = new NewProjectListener() {
@Override
public void hasNewProject() {
boolean status = imageState.isHasImage();
zoomInButton.setEnabled(status);
zoomOutButton.setEnabled(status);
invertButton.setEnabled(status);
toggleHighlightsButton.setEnabled(status);
saveButton.setEnabled(status);
submitButton.setEnabled(status);
}
};
}

View File

@ -0,0 +1,36 @@
package client.components.imagePanel;
import client.components.imagePanel.listeners.ImageControlsListener;
import client.persistence.*;
import javax.swing.*;
import java.awt.*;
public class ImagePanel extends JPanel {
private ImageControl imageControl;
private ScalableImage scalableImage;
private ImageState imageState;
public ImagePanel(ImageState imageState) {
this.imageState = imageState;
setupView();
}
private void setupView() {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.setBackground(Color.DARK_GRAY);
scalableImage = new ScalableImage(imageState);
ImageControlsListener imageControlsListener = scalableImage.getImageControlsListener();
imageControl = new ImageControl(imageState);
imageControl.addControlsListener(imageControlsListener);
this.add(imageControl, Component.LEFT_ALIGNMENT);
this.add(scalableImage, BorderLayout.CENTER);
}
}

View File

@ -0,0 +1,147 @@
package client.components.imagePanel;
import client.components.imagePanel.ImageCell;
import client.persistence.Cell;
import client.persistence.ImageState;
import client.persistence.ImageStateListener;
import client.persistence.SyncContext;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
public class ImageTable {
private int recordsPerImage;
private int firstYCoord;
private int recordHeight;
private int columnCount;
private ArrayList<Integer> fieldXValues;
private ArrayList<Integer> fieldWidthValues;
private ImageCell[][] model;
private Rectangle2D.Double tableBoundaries;
private boolean highlightsEnabled;
private ImageCell currentSelected;
private ImageState imageState;
private boolean deactivated = false;
public ImageTable(ImageState imageState) {
this.imageState = imageState;
this.recordsPerImage = imageState.getRecordsPerImage();
this.firstYCoord = imageState.getFirstYCoord();
this.recordHeight = imageState.getRecordHeight();
this.columnCount = imageState.getColumnCount();
this.fieldXValues = imageState.getFieldXValues();
this.fieldWidthValues = imageState.getFieldWidthValues();
// Note we go [y][x]
model = new ImageCell[recordsPerImage][columnCount];
highlightsEnabled = imageState.getSettings().isImageHighlights();
if(!imageState.isHasImage()) return;
generateModel();
generateTableBoundaries();
}
private void generateModel() {
for(int y = 0; y < recordsPerImage; y++) {
for(int x = 0; x < columnCount; x++) {
Rectangle2D.Double rect = new Rectangle2D.Double();
int rectY = firstYCoord + (y * recordHeight); // y starts at 0.
int rectX = fieldXValues.get(x);
int width = fieldWidthValues.get(x);
int height = recordHeight;
rect.setRect(rectX, rectY, width, height);
model[y][x] = new ImageCell(rect);
}
}
}
public void paint(Graphics2D g2) {
for(int y = 0; y < recordsPerImage; y++) {
for(int x = 0; x < columnCount; x++) {
ImageCell imageCell = model[y][x];
if(imageCell == currentSelected && highlightsEnabled) {
model[y][x].paint(g2, true);
} else {
model[y][x].paint(g2, false);
}
}
}
}
public void contains(int worldX, int worldY) {
// Make sure in table boundaries first.
if(!tableBoundaries.contains(worldX, worldY)) {
return;
}
for(int y = 0; y < recordsPerImage; y++) {
for(int x = 0; x < columnCount; x++) {
ImageCell imageCell = model[y][x];
if(imageCell.contains(worldX, worldY)) {
this.currentSelected = imageCell;
setCurrentCell(x, y);
Cell cell = new Cell();
cell.setField(x);
cell.setRecord(y);
this.imageState.setSelectedCell(cell);
}
}
}
}
public void setCurrentCell(int x, int y) {
if(model.length == 0 || deactivated) return;
this.currentSelected = model[y][x];
}
private void generateTableBoundaries() {
int y = firstYCoord;
int x = fieldXValues.get(0);
int width = 0;
for(int fw : fieldWidthValues) {
width += fw;
}
int height = recordsPerImage * recordHeight;
tableBoundaries = new Rectangle2D.Double(x, y, width, height);
}
public void enableHighlights(boolean value) {
this.highlightsEnabled = value;
imageState.getSettings().setImageHighlights(value);
}
public boolean isHighlightsEnabled() {
return highlightsEnabled;
}
public void setDeactivated(boolean deactivated) {
this.deactivated = deactivated;
}
public boolean isDeactivated() {
return deactivated;
}
}

View File

@ -0,0 +1,291 @@
package client.components.imagePanel;
import client.components.imagePanel.listeners.ImageControlsListener;
import client.components.listeners.DrawingListener;
import client.persistence.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class ScalableImage extends JPanel {
private BufferedImage image;
private int w_originX;
private int w_originY;
private double scale;
private boolean dragging;
private int w_dragStartX;
private int w_dragStartY;
private int w_dragStartOriginX;
private int w_dragStartOriginY;
private ImageTable imageTable;
private double MAX_SCROLL = 1.0f;
private double MIN_SCROLL = 0.2f;
private ImageState imageState;
public ScalableImage(ImageState imageState) {
this.imageState = imageState;
this.setBackground(Color.DARK_GRAY);
initDrag();
this.addMouseListener(mouseAdapter);
this.addMouseMotionListener(mouseAdapter);
this.addMouseWheelListener(mouseAdapter);
setupView();
this.imageState.addListener(imageStateListener);
this.imageState.addNewProjectListener(newProjectListener);
}
private void setupView() {
image = imageState.getImage();
imageTable = new ImageTable(imageState);
this.setOrigin(imageState.getSettings().getImageOriginX(),
imageState.getSettings().getImageOriginY());
this.setScale(imageState.getSettings().getImageScaleLevel());
this.repaint();
}
public void refreshImage() {
imageTable.setDeactivated(true);
setupView();
}
boolean redrawHack = false;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.translate(this.getWidth() / 2, this.getHeight() / 2);
g2.scale(scale, scale);
g2.translate(-w_originX, -w_originY);
g2.drawImage(image, 0, 0, null);
imageTable.paint(g2);
if(!redrawHack) {
redrawHack = true;
this.repaint();
}
}
public void invertImage(BufferedImage b) {
for (int x = 0; x < b.getWidth(); x++) {
for (int y = 0; y < b.getHeight(); y++) {
int rgba = b.getRGB(x, y);
Color col = new Color(rgba, true);
col = new Color(255 - col.getRed(),
255 - col.getGreen(),
255 - col.getBlue());
b.setRGB(x, y, col.getRGB());
}
}
boolean current = imageState.getSettings().isImageInverted();
imageState.getSettings().setImageInverted(!current);
redrawHack = false;
this.repaint();
}
public void setValue(Cell cell, String value) {
imageTable.setCurrentCell(cell.getField(), cell.getRecord());
this.repaint();
}
public void setCurrentCell(Cell cell) {
imageTable.setCurrentCell(cell.getField(), cell.getRecord());
this.repaint();
}
private void initDrag() {
dragging = false;
w_dragStartX = 0;
w_dragStartY = 0;
w_dragStartOriginX = 0;
w_dragStartOriginY = 0;
}
public void setScale(double newScale) {
scale = newScale;
this.repaint();
}
public void setOrigin(int w_newOriginX, int w_newOriginY) {
w_originX = w_newOriginX;
w_originY = w_newOriginY;
this.repaint();
}
private MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(!imageState.isHasImage()) return;
int d_X = e.getX();
int d_Y = e.getY();
AffineTransform transform = new AffineTransform();
transform.translate(getWidth()/2, getHeight()/2);
transform.scale(scale, scale);
transform.translate(-w_originX, -w_originY);
Point2D d_Pt = new Point2D.Double(d_X, d_Y);
Point2D w_Pt = new Point2D.Double();
try {
transform.inverseTransform(d_Pt, w_Pt);
} catch (NoninvertibleTransformException ex) {
return;
}
int w_X = (int)w_Pt.getX();
int w_Y = (int)w_Pt.getY();
imageTable.contains(w_X, w_Y);
dragging = true;
w_dragStartX = w_X;
w_dragStartY = w_Y;
w_dragStartOriginX = w_originX;
w_dragStartOriginY = w_originY;
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
if (dragging) {
int d_X = e.getX();
int d_Y = e.getY();
AffineTransform transform = new AffineTransform();
transform.translate(getWidth()/2, getHeight()/2);
transform.scale(scale, scale);
transform.translate(-w_dragStartOriginX, -w_dragStartOriginY);
Point2D d_Pt = new Point2D.Double(d_X, d_Y);
Point2D w_Pt = new Point2D.Double();
try {
transform.inverseTransform(d_Pt, w_Pt);
} catch (NoninvertibleTransformException ex) {
return;
}
int w_X = (int)w_Pt.getX();
int w_Y = (int)w_Pt.getY();
int w_deltaX = w_X - w_dragStartX;
int w_deltaY = w_Y - w_dragStartY;
w_originX = w_dragStartOriginX - w_deltaX;
w_originY = w_dragStartOriginY - w_deltaY;
repaint();
}
}
@Override
public void mouseReleased(MouseEvent e) {
initDrag();
imageState.getSettings().setImageOriginX(w_originX);
imageState.getSettings().setImageOriginY(w_originY);
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
int rotation = e.getWheelRotation();
if((rotation > 0) && scale > MIN_SCROLL ) {
setScale(scale - 0.02f);
} else if (scale < MAX_SCROLL) {
setScale(scale + 0.02f);
}
imageState.getSettings().setImageScaleLevel(scale);
}
};
private ImageStateListener imageStateListener = new ImageStateListener() {
@Override
public void valueChanged(Cell cell, String newValue) {
return;
}
@Override
public void selectedCellChanged(Cell newSelectedCell) {
imageTable.setCurrentCell(newSelectedCell.getField(), newSelectedCell.getRecord());
repaint();
}
};
private NewProjectListener newProjectListener = new NewProjectListener() {
@Override
public void hasNewProject() {
refreshImage();
}
};
private ImageControlsListener imageControlsListener = new ImageControlsListener() {
@Override
public void onScrollIncrease() {
if (scale < MAX_SCROLL) {
setScale(scale + 0.02f);
}
}
@Override
public void onScrollDecrease() {
if(scale > MIN_SCROLL ) {
setScale(scale - 0.02f);
}
}
@Override
public void onInvertImage() {
invertImage(image);
}
@Override
public void onToggleHighlights() {
if(imageTable.isHighlightsEnabled()) {
imageTable.enableHighlights(false);
} else {
imageTable.enableHighlights(true);
}
repaint();
}
};
public ImageControlsListener getImageControlsListener() {
return imageControlsListener;
}
}

View File

@ -0,0 +1,12 @@
package client.components.imagePanel.listeners;
public interface ImageControlsListener {
public void onScrollIncrease();
public void onScrollDecrease();
public void onInvertImage();
public void onToggleHighlights();
}

View File

@ -0,0 +1,5 @@
package client.components.listeners;
public interface DrawingListener {
void originChanged(int w_newOriginX, int w_newOriginY);
}

View File

@ -0,0 +1,42 @@
package client.components.loginWindow;
import client.components.downloadModal.DownloadModal;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
public class ErrorLoginDialog extends JDialog {
public ErrorLoginDialog() {
setupView();
}
private void setupView() {
this.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
this.setTitle("Error!");
this.setSize(270, 80);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setLayout(new FlowLayout());
JLabel label = new JLabel("Error, incorrect Username or Password!");
this.add(label);
JButton closeButton = new JButton("Close");
closeButton.addActionListener(closeListener);
this.add(closeButton);
}
private ActionListener closeListener = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispatchEvent(new WindowEvent(ErrorLoginDialog.this, WindowEvent.WINDOW_CLOSING));
}
};
}

View File

@ -0,0 +1,74 @@
package client.components.loginWindow;
import client.communication.Communicator;
import client.communication.errors.RemoteServerErrorException;
import client.communication.errors.UnauthorizedAccessException;
import shared.communication.params.ValidateUser_Param;
import shared.communication.responses.ValidateUser_Res;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginWindow extends JFrame {
private Communicator communicator;
private JPasswordField passwordTextField;
private JTextField userTextField;
private JButton loginButton;
public LoginWindow(Communicator communicator) {
this.communicator = communicator;
setupView();
}
private void setupView() {
this.setTitle("Login to Indexer");
this.setSize(350, 130);
this.setResizable(false);
this.setLayout(new FlowLayout());
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel usernameLabel = new JLabel("Username: ");
this.add(usernameLabel);
userTextField = new JTextField();
userTextField.setPreferredSize(new Dimension(250, 30));
this.add(userTextField);
JLabel passwordLabel = new JLabel("Password: ");
this.add(passwordLabel);
passwordTextField = new JPasswordField();
passwordTextField.setPreferredSize(new Dimension(250, 30));
this.add(passwordTextField);
loginButton = new JButton("Login");
this.add(loginButton);
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(exitListener);
this.add(exitButton);
}
public void addLoginListener(ActionListener actionListener) {
loginButton.addActionListener(actionListener);
}
private ActionListener exitListener = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
};
public String getUsername() {
return this.userTextField.getText();
}
public String getPassword() {
return this.passwordTextField.getText();
}
}

View File

@ -0,0 +1,53 @@
package client.components.loginWindow;
import shared.communication.responses.ValidateUser_Res;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
public class SuccessLoginDialog extends JDialog {
private ValidateUser_Res validateUserRes;
public SuccessLoginDialog(ValidateUser_Res validateUserRes) {
this.validateUserRes = validateUserRes;
setupView();
}
private void setupView() {
this.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
this.setTitle("Error!");
this.setSize(230, 100);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setLayout(new FlowLayout());
String welcome = "Welcome, " + validateUserRes.getFirstName() + " " +
validateUserRes.getLastName() + ".";
String record = "You have inexed " + validateUserRes.getIndexedRecords() + " records.";
JLabel label = new JLabel(welcome);
this.add(label);
JLabel label2 = new JLabel(record);
this.add(label2);
JButton closeButton = new JButton("Close");
closeButton.addActionListener(closeListener);
this.add(closeButton);
}
private ActionListener closeListener = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispatchEvent(new WindowEvent(SuccessLoginDialog.this, WindowEvent.WINDOW_CLOSED));
}
};
}

View File

@ -0,0 +1,22 @@
package client.components.menus;
import javax.swing.*;
import java.awt.event.ActionListener;
public class SpellCheckPopup extends JPopupMenu {
private JMenuItem show;
public SpellCheckPopup() {
setupView();
}
private void setupView() {
show = new JMenuItem("See Suggestions?");
this.add(show);
}
public void addShowAction(ActionListener actionListener) {
show.addActionListener(actionListener);
}
}

View File

@ -0,0 +1,89 @@
package client.components.spellCheck;
import client.components.downloadModal.DownloadModal;
import client.modules.spellChecker.KnownData;
import client.modules.spellChecker.SpellChecker;
import client.modules.spellChecker.WordSelectedListener;
import client.persistence.Cell;
import client.persistence.ImageState;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
public class SpellingModal extends JDialog {
private ImageState imageState;
private int column;
private String word;
private JButton button;
private JList<String> jList;
private String[] words;
private WordSelectedListener wordSelectedListener;
public SpellingModal(ImageState imageState, String word, int column,
WordSelectedListener wordSelectedListener) {
this.imageState = imageState;
this.column = column;
this.word = word;
this.wordSelectedListener = wordSelectedListener;
setupView();
}
private void setupView() {
this.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
this.setTitle("Spelling Suggestions");
this.setSize(350, 500);
this.setResizable(false);
this.setLocationRelativeTo(null);
KnownData knownData = imageState.getKnownDataValues().get(column);
jList = new JList<>();
SpellChecker spellChecker = new SpellChecker();
spellChecker.getSuggestionsForString(word);
spellChecker.restrictToList(knownData.getWords());
words = spellChecker.getWordArray();
jList.setListData(words);
jList.addListSelectionListener(listSelectionListener);
this.add(new JScrollPane(jList), BorderLayout.CENTER);
button = new JButton("Use Suggestion");
button.setEnabled(false);
button.addActionListener(actionListener);
this.add(button, BorderLayout.SOUTH);
}
private ActionListener actionListener = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
int index = jList.getSelectedIndex();
String word = words[index];
wordSelectedListener.wordSelected(word);
dispatchEvent(new WindowEvent(SpellingModal.this, WindowEvent.WINDOW_CLOSING));
setVisible(false);
}
};
private ListSelectionListener listSelectionListener = new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
button.setEnabled(true);
repaint();
}
};
}

View File

@ -0,0 +1,115 @@
package client.components.tableEntry;
import client.components.menus.SpellCheckPopup;
import client.components.spellCheck.SpellingModal;
import client.modules.spellChecker.KnownData;
import client.modules.spellChecker.WordSelectedListener;
import client.persistence.Cell;
import client.persistence.ImageState;
import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.awt.event.*;
public class EntryCellEditor extends AbstractCellEditor implements TableCellEditor {
private String currentValue;
private JTextField textField;
private ImageState imageState;
private SpellCheckPopup spellCheckPopup;
private int column;
public EntryCellEditor(ImageState imageState) {
this.imageState = imageState;
this.spellCheckPopup = new SpellCheckPopup();
this.spellCheckPopup.addShowAction(showSuggestionsListener);
}
public boolean hasSuggestion(String value, int column) {
if(value.equals("")) return false;
KnownData knownData = imageState.getKnownDataValues().get(column);
for(String val : knownData.getWords()) {
if(val.toLowerCase().equals(value.toLowerCase())) return false;
}
return true;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
int row, int column) {
textField = new JTextField();
currentValue = (String)value;
textField.setText(currentValue);
textField.setBorder(BorderFactory.createLineBorder(Color.BLUE, 1));
textField.addMouseListener(generateMouseListener(row, column));
this.column = column;
if(isSelected) {
Cell cell = new Cell();
cell.setRecord(row);
cell.setField(column);
}
if(hasSuggestion((String)value, column - 1)) {
textField.setBackground(Color.RED);
textField.addMouseListener(rightClickPopupAction);
}
return textField;
}
@Override
public Object getCellEditorValue() {
return textField.getText();
}
private MouseListener generateMouseListener(final int row, final int column){
return new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mouseClicked(e);
Cell cell = new Cell();
cell.setRecord(row);
cell.setField(column - 1);
imageState.setSelectedCell(cell);
}
};
}
private MouseListener rightClickPopupAction = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(e.isPopupTrigger()) {;
spellCheckPopup.show(textField, e.getX(), e.getY());
}
}
};
private WordSelectedListener wordSelectedListener = new WordSelectedListener() {
@Override
public void wordSelected(String word) {
textField.setText(word);
}
};
private ActionListener showSuggestionsListener = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
SpellingModal spellingModal = new SpellingModal(imageState, textField.getText(),
column - 1, wordSelectedListener);
spellingModal.setVisible(true);
}
};
}

View File

@ -0,0 +1,58 @@
package client.components.tableEntry;
import client.components.menus.SpellCheckPopup;
import client.modules.spellChecker.KnownData;
import client.persistence.Cell;
import client.persistence.ImageState;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.*;
public class EntryCellRenderer extends JLabel implements TableCellRenderer {
private ImageState imageState;
public EntryCellRenderer(ImageState imageState) {
this.imageState = imageState;
}
public boolean hasSuggestion(String value, int column) {
if(value.equals("")) return false;
KnownData knownData = imageState.getKnownDataValues().get(column);
for(String val : knownData.getWords()) {
if(val.toLowerCase().equals(value.toLowerCase())) return false;
}
return true;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
this.setText((String)value);
this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
if(isSelected) {
this.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
Cell cell = new Cell();
cell.setRecord(row);
cell.setField(column - 1);
imageState.setSelectedCell(cell);
}
if(hasSuggestion((String)value, column - 1)) {
this.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
this.setBackground(Color.RED);
}
return this;
}
}

View File

@ -0,0 +1,65 @@
package client.components.tableEntry;
import client.components.menus.SpellCheckPopup;
import client.persistence.Cell;
import client.persistence.ImageState;
import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.awt.event.*;
public class RecordCellEditor extends AbstractCellEditor implements TableCellEditor {
private String currentValue;
private JTextField textField;
private ImageState imageState;
public RecordCellEditor(ImageState imageState) {
this.imageState = imageState;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
int row, int column) {
textField = new JTextField();
currentValue = (String)value;
textField.setText(currentValue);
textField.setBorder(BorderFactory.createLineBorder(Color.BLUE, 1));
textField.setEnabled(false);
textField.addMouseListener(generateMouseListener(row, column));
textField.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
super.focusGained(e);
System.out.println("focs");
}
});
return textField;
}
@Override
public Object getCellEditorValue() {
return textField.getText();
}
private MouseListener generateMouseListener(final int row, final int column){
return new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mouseClicked(e);
Cell cell = new Cell();
cell.setRecord(row);
cell.setField(0);
imageState.setSelectedCell(cell);
}
};
}
}

View File

@ -0,0 +1,36 @@
package client.components.tableEntry;
import client.modules.spellChecker.KnownData;
import client.persistence.Cell;
import client.persistence.ImageState;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
public class RecordCellRenderer extends JLabel implements TableCellRenderer {
private ImageState imageState;
public RecordCellRenderer(ImageState imageState) {
this.imageState = imageState;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
this.setText((String)value);
this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
if(isSelected) {
Cell cell = new Cell();
cell.setRecord(row);
cell.setField(0);
imageState.setSelectedCell(cell);
}
return this;
}
}

View File

@ -0,0 +1,106 @@
package client.components.tableEntry;
import client.components.menus.SpellCheckPopup;
import client.persistence.*;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class TableEntry extends JScrollPane {
private TableModel tableModel;
private JTable table;
private ImageState imageState;
public TableEntry(ImageState imageState) {
this.imageState = imageState;
this.tableModel = new TableModel(imageState);
this.imageState.addNewProjectListener(newProjectListener);
setupView();
if(imageState.isHasImage())
imageState.addListener(imageStateListener);
}
private void setupView() {
table = new JTable(tableModel);
table.setRowHeight(20);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setCellSelectionEnabled(true);
table.getTableHeader().setReorderingAllowed(false);
table.getTableHeader().setResizingAllowed(false);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tableInit();
}
private void tableInit() {
TableColumnModel columnModel = table.getColumnModel();
for (int i = 1; i < tableModel.getColumnCount(); ++i) {
TableColumn column = columnModel.getColumn(i);
EntryCellRenderer entryCellRenderer = new EntryCellRenderer(imageState);
column.setCellRenderer(entryCellRenderer);
column.setCellEditor(new EntryCellEditor(imageState));
}
this.getViewport().add(table.getTableHeader());
this.getViewport().add(table);
if(!imageState.isHasImage()) return;
TableColumn column = columnModel.getColumn(0);
column.setCellRenderer(new RecordCellRenderer(imageState));
column.setCellEditor(new RecordCellEditor(imageState));
}
private ImageStateListener imageStateListener = new ImageStateListener() {
@Override
public void valueChanged(Cell cell, String newValue) {
}
@Override
public void selectedCellChanged(Cell newSelectedCell) {
tableModel.setQuiet(true);
table.changeSelection(newSelectedCell.getRecord(),
newSelectedCell.getField() + 1, false, false);
table.editCellAt(newSelectedCell.getRecord(), newSelectedCell.getField() + 1);
tableModel.setQuiet(false);
}
};
private NewProjectListener newProjectListener = new NewProjectListener() {
@Override
public void hasNewProject() {
tableModel.setDeactivated(true);
if(imageState.isHasImage())
imageState.addListener(imageStateListener);
tableModel = new TableModel(imageState);
table.setModel(tableModel);
tableInit();
}
};
}

View File

@ -0,0 +1,138 @@
package client.components.tableEntry;
import client.persistence.Cell;
import client.persistence.ImageState;
import client.persistence.ImageStateListener;
import javax.swing.table.AbstractTableModel;
public class TableModel extends AbstractTableModel {
private String[] columnNames;
private String[][] model;
private ImageState imageState;
private boolean quiet = false;
private boolean updating = false;
private boolean deactivated = false;
public TableModel(ImageState imageState) {
this.imageState = imageState;
this.model = this.imageState.getModel();
this.columnNames = this.imageState.getColumnNames();
this.imageState.addListener(imageStateListener);
if(!this.imageState.isHasImage()) return;
overrideTableModel();
}
private void overrideTableModel() {
String[] imageStateColumns = this.imageState.getColumnNames();
int width = imageStateColumns.length;
String[][] imageStateModel = this.imageState.getModel();
this.model = new String[imageStateModel.length][width + 1];
this.columnNames = new String[width + 1];
this.columnNames[0] = "Record Number";
// Copy Column names from image state so we can have record number
for(int i = 0; i < width; i++) {
this.columnNames[i+1] = imageStateColumns[i];
}
// Copy Model values
for(int x = 0; x < imageStateModel.length; x ++) {
// Set row number first
this.model[x][0] = Integer.toString(x + 1);
// Copy the values with the new offset
for(int i = 0; i < width; i++) {
this.model[x][i+1] = imageStateModel[x][i];
}
}
}
public void setDeactivated(boolean deactivated) {
this.deactivated = deactivated;
}
@Override
public boolean isCellEditable(int row, int column) {
return true;
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
@Override
public int getRowCount() {
return model.length;
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return model[rowIndex][columnIndex];
}
@Override
public void setValueAt(Object value, int row, int column) {
if(quiet || deactivated) return;
model[row][column] = (String)value;
updating = true;
Cell cell = new Cell();
cell.setField(column - 1);
// Check for record number col which is 0
if(column == 0) {
cell.setField(0);
}
cell.setRecord(row);
imageState.setValue(cell, (String)value);
updating = false;
}
private ImageStateListener imageStateListener = new ImageStateListener() {
@Override
public void valueChanged(Cell cell, String newValue) {
if(updating || deactivated) return;
model[cell.getRecord()][cell.getField() + 1] = newValue;
}
@Override
public void selectedCellChanged(Cell newSelectedCell) {
if(updating) return;
}
};
public void setValueQuiet(String newValue, int row, int column) {
if(deactivated) return;
model[row][column] = newValue;
}
public void setQuiet(boolean quiet) {
this.quiet = quiet;
}
}

View File

@ -0,0 +1,81 @@
package client.modules.spellChecker;
import org.apache.commons.io.FileUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class KnownData implements Serializable {
private List<String> words;
public KnownData(ArrayList<String> knownDataValue) {
this.words = knownDataValue;
}
public KnownData() {
this.words = new ArrayList<>();
}
public void addWord(String word) {
words.add(word);
}
public String[] getWordArray() {
String[] arr = new String[words.size()];
for(int i = 0; i < words.size(); i++) {
arr[i] = words.get(i);
}
return arr;
}
public List<String> getWords() {
return words;
}
public void setWords(List<String> words) {
this.words = words;
}
private static String downloadValues(String path) {
StringBuilder stringBuilder = new StringBuilder();
try {
URL url = new URL(path);
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String temp;
while ((temp = in.readLine()) != null) {
stringBuilder.append(temp);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
public static KnownData getList(String knownDataFullPath) {
ArrayList<String> knownDataValue = null;
String knownValuesString = downloadValues(knownDataFullPath);
String[] values = knownValuesString.split(",");
knownDataValue = new ArrayList<>(Arrays.asList(values));
return new KnownData(knownDataValue);
}
}

View File

@ -0,0 +1,120 @@
package client.modules.spellChecker;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class SpellChecker {
private Set<String> words;
public Set<String> getSuggestionsForString(String word) {
words = new TreeSet<>();
// First Distance
words.addAll(alterationDistance(word));
words.addAll(insertionDistance(word));
words.addAll(transpositionDistance(word));
words.addAll(deletionDistance(word));
Set<String> wordsCopy = new TreeSet<>(words);
for(String w : wordsCopy) {
words.addAll(alterationDistance(w));
words.addAll(insertionDistance(w));
words.addAll(transpositionDistance(w));
words.addAll(deletionDistance(w));
}
return words;
}
public void restrictToList(List<String> list) {
Set<String> set = new TreeSet<>();
for(String similar : words) {
for(String foregin : list) {
if(foregin.toLowerCase().equals(similar.toLowerCase())) {
set.add(foregin);
}
}
}
words = set;
}
public String[] getWordArray() {
String[] arr = new String[words.size()];
ArrayList<String> temp = new ArrayList<>(words);
for(int i = 0; i < temp.size(); i++) {
arr[i] = temp.get(i);
}
return arr;
}
public List<String> alterationDistance(String word) {
List<String> wordList = new ArrayList<String>();
for(int i = 0; i <= (word.length() - 1); i++ ) {
String firstHalf = word.substring(0, i);
String lastHalf = word.substring(i+1);
for(int y = 0; y < 26; y++) {
String letter = Character.toString((char) ('a' + y));
String cutlet = firstHalf + letter + lastHalf;
if(word.equals(cutlet)) continue;
wordList.add(cutlet);
}
}
return wordList;
}
public List<String> insertionDistance(String word) {
// Declare a new String list (to avoid making a comparator).
List<String> wordList = new ArrayList<String>();
for(int i = 0; i <= (word.length()); i++ ) {
String firstHalf = word.substring(0, i);
String lastHalf = word.substring(i);
for(int y = 0; y < 26; y++) {
String letter = Character.toString((char) ('a' + y));
String cutlet = firstHalf + letter + lastHalf;
if(word.equals(cutlet)) continue;
wordList.add(cutlet);
}
}
return wordList;
}
public List<String> transpositionDistance(String word) {
List<String> wordList = new ArrayList<String>();
for(int i = 0; i <= (word.length() - 2); i++ ) {
String letterOne = word.substring(i,i+1);
String letterTwo = word.substring(i+1,i+2);
String firstHalf = word.substring(0, i);
String lastHalf = word.substring(i+2);
String cutlet = firstHalf + letterTwo + letterOne + lastHalf;
wordList.add(cutlet);
}
return wordList;
}
public List<String> deletionDistance(String word) {
List<String> wordList = new ArrayList<String>();
for(int i = 1; i <= word.length(); i++ ) {
String base = word.substring(0,i-1);
String cutlet = base + word.substring(i);
wordList.add(cutlet);
}
return wordList;
}
}

View File

@ -0,0 +1,6 @@
package client.modules.spellChecker;
public interface WordSelectedListener {
public void wordSelected(String word);
}

View File

@ -0,0 +1,25 @@
package client.persistence;
import java.io.Serializable;
public class Cell implements Serializable {
private int record;
private int field;
public int getRecord() {
return record;
}
public void setRecord(int record) {
this.record = record;
}
public int getField() {
return field;
}
public void setField(int field) {
this.field = field;
}
}

View File

@ -0,0 +1,349 @@
package client.persistence;
import client.communication.Communicator;
import client.communication.errors.RemoteServerErrorException;
import client.communication.errors.UnauthorizedAccessException;
import client.modules.spellChecker.KnownData;
import shared.communication.common.Fields;
import shared.communication.params.DownloadBatch_Param;
import shared.communication.params.SubmitBatch_Param;
import shared.communication.responses.DownloadBatch_Res;
import shared.models.Value;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class ImageState implements Serializable {
private String[][] values;
private String[] columns;
private Cell selectedCell;
private transient List<ImageStateListener> listeners;
private transient List<NewProjectListener> projectListeners;
private List<Fields> fieldsMetaData;
private transient Communicator communicator;
private String username;
private String password;
private int imageId = -1;
private int firstYCoord = 0;
private int recordHeight = 0;
private int columnCount = 0;
private int recordsPerImage = 0;
private transient BufferedImage image;
private boolean hasImage;
private ArrayList<Integer> fieldXValues;
private ArrayList<Integer> fieldWidthValues;
private ArrayList<KnownData> knownDataValues;
private Settings settings;
public ImageState(Settings settings, Communicator communicator,
String username, String password) {
this.settings = settings;
this.communicator = communicator;
this.username = username;
this.password = password;
// Init Listeners
listeners = new ArrayList<>();
projectListeners = new ArrayList<>();
loadFromNoSettings();
}
public void loadFromNoSettings() {
values = new String[0][0];
firstYCoord = 0;
recordHeight = 0;
columnCount = 0;
recordsPerImage = 0;
hasImage = false;
image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
knownDataValues = new ArrayList<>();
columns = new String[0];
values = new String[0][0];
Cell initCell = new Cell();
initCell.setField(0);
initCell.setRecord(0);
selectedCell = initCell;
}
public void initEvents() {
setSelectedCell(selectedCell);
}
public void addListener(ImageStateListener imageStateListener) {
listeners.add(imageStateListener);
}
public void addNewProjectListener(NewProjectListener npl) {
projectListeners.add(npl);
}
public void setValue(Cell cell, String value) {
values[cell.getRecord()][cell.getField()] = value;
for(ImageStateListener isl : listeners) {
isl.valueChanged(cell, value);
}
}
public String getValue(Cell cell) {
return values[cell.getRecord()][cell.getField()];
}
public void setSelectedCell(Cell cell) {
this.selectedCell = cell;
for(ImageStateListener isl : listeners) {
isl.selectedCellChanged(cell);
}
}
public void save() {
try{
// Create path
File dest = new File("profiles/"+username);
if(!dest.exists()) dest.mkdirs();
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("profiles/"+username+"/settings.ser"));
out.writeObject(settings);
out.close();
ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
out = new ObjectOutputStream(bos) ;
out.writeObject(settings);
out.close();
ObjectOutputStream out1 = new ObjectOutputStream(
new FileOutputStream("profiles/"+username+"/state.ser"));
out1.writeObject(this);
out1.close();
ByteArrayOutputStream bos1 = new ByteArrayOutputStream() ;
out1 = new ObjectOutputStream(bos1) ;
out1.writeObject(this);
out1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
ImageIO.write(image, "png", out); // png is lossless
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
image = ImageIO.read(in);
listeners = new ArrayList<>();
projectListeners = new ArrayList<>();
}
private void initWithProject(DownloadBatch_Res downloadBatch) {
hasImage = true;
firstYCoord = downloadBatch.getFirstYCoord();
recordHeight = downloadBatch.getRecordHeight();
columnCount = downloadBatch.getNumberOfFields();
recordsPerImage = downloadBatch.getRecordsPerImage();
imageId = downloadBatch.getBatchId();
values = new String[recordsPerImage][columnCount];
columns = new String[columnCount];
knownDataValues = new ArrayList<>();
fieldsMetaData = downloadBatch.getFields();
fieldXValues = new ArrayList<>();
fieldWidthValues = new ArrayList<>();
List<Fields> fields = downloadBatch.getFields();
for(int i = 0; i < fields.size(); i++) {
// Copy essential values and store the rest
columns[i] = fields.get(i).getTitle();
fieldXValues.add(fields.get(i).getxCoord());
fieldWidthValues.add(fields.get(i).getPixelWidth());
if(fields.get(i).getKnownData() != null) {
knownDataValues.add(KnownData.getList(communicator.getServerPath() +
fields.get(i).getKnownData()));
} else {
knownDataValues.add(new KnownData());
}
for(int y = 0; y < recordsPerImage; y++) {
// Ensure we have no null values.
values[y][i] = "";
}
}
String path = communicator.getServerPath() + downloadBatch.getImageUrl();
try {
image = ImageIO.read(new URL(path));
} catch (Exception e1) {
return;
}
for(NewProjectListener npl : projectListeners) {
npl.hasNewProject();
}
Cell cell = new Cell();
cell.setField(0);
cell.setRecord(0);
setSelectedCell(cell);
}
public void submitProject() {
SubmitBatch_Param param = new SubmitBatch_Param();
param.setImageId(imageId);
param.setUsername(username);
param.setPassword(password);
for(String[] vs : values) {
List<Value> valueList = new ArrayList<>();
for(String v : vs) {
Value model = new Value();
model.setValue(v);
model.setType("String");
valueList.add(model);
}
param.addRecord(valueList);
}
try {
communicator.submitBatch(param);
} catch (Exception e) {
e.printStackTrace();
}
loadFromNoSettings();
save();
for(NewProjectListener npl : projectListeners) {
npl.hasNewProject();
}
}
public void downloadProject(int projectId) {
DownloadBatch_Param param = new DownloadBatch_Param();
param.setUsername(this.username);
param.setPassword(this.password);
param.setProjectId(projectId);
DownloadBatch_Res downloadBatchRes = null;
try {
downloadBatchRes = communicator.downloadBatch(param);
initWithProject(downloadBatchRes);
} catch (UnauthorizedAccessException e) {
e.printStackTrace();
} catch (RemoteServerErrorException e) {
e.printStackTrace();
}
}
public Cell getSelectedCell() {
return this.selectedCell;
}
public Settings getSettings() {
return settings;
}
public String[][] getModel() {
return values;
}
public String[] getColumnNames() {
return columns;
}
public ArrayList<Integer> getFieldWidthValues() {
return fieldWidthValues;
}
public ArrayList<Integer> getFieldXValues() {
return fieldXValues;
}
public int getRecordsPerImage() {
return recordsPerImage;
}
public int getColumnCount() {
return columnCount;
}
public int getRecordHeight() {
return recordHeight;
}
public int getFirstYCoord() {
return firstYCoord;
}
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public String[] getColumns() {
return columns;
}
public String[][] getValues() {
return values;
}
public List<Fields> getFieldsMetaData() {
return fieldsMetaData;
}
public void setCommunicator(Communicator communicator) {
this.communicator = communicator;
}
public boolean isHasImage() {
return hasImage;
}
public void setHasImage(boolean hasImage) {
this.hasImage = hasImage;
}
public void setSettings(Settings settings) {
this.settings = settings;
}
public BufferedImage getImage() {
return image;
}
public ArrayList<KnownData> getKnownDataValues() {
return knownDataValues;
}
}

View File

@ -0,0 +1,9 @@
package client.persistence;
public interface ImageStateListener {
public void valueChanged(Cell cell, String newValue);
public void selectedCellChanged(Cell newSelectedCell);
}

View File

@ -0,0 +1,7 @@
package client.persistence;
public interface NewProjectListener {
public void hasNewProject();
}

View File

@ -0,0 +1,131 @@
package client.persistence;
import java.awt.image.BufferedImage;
import java.io.Serializable;
public class Settings implements Serializable {
private int baseSplitY;
private int baseSplitX;
private int windowHeight;
private int windowWidth;
private int windowPositionY;
private int windowPositionX;
private double imageScaleLevel;
private int imageOriginX;
private int imageOriginY;
private boolean imageInverted;
private boolean imageHighlights;
public Settings() {
loadDefaults();
}
private void loadDefaults() {
this.windowHeight = 650;
this.windowWidth = 1000;
this.windowPositionX = 36;
this.windowPositionY = 73;
this.baseSplitY = 400;
this.baseSplitX = 500;
this.imageScaleLevel = 0.8f;
this.imageOriginX = 0;
this.imageOriginY = 0;
this.imageHighlights = true;
this.imageInverted = false;
}
public int getBaseSplitY() {
return baseSplitY;
}
public void setBaseSplitY(int baseSplitY) {
this.baseSplitY = baseSplitY;
}
public int getBaseSplitX() {
return baseSplitX;
}
public void setBaseSplitX(int baseSplitX) {
this.baseSplitX = baseSplitX;
}
public int getWindowHeight() {
return windowHeight;
}
public void setWindowHeight(int windowHeight) {
this.windowHeight = windowHeight;
}
public int getWindowWidth() {
return windowWidth;
}
public void setWindowWidth(int windowWidth) {
this.windowWidth = windowWidth;
}
public int getWindowPositionY() {
return windowPositionY;
}
public void setWindowPositionY(int windowPositionY) {
this.windowPositionY = windowPositionY;
}
public int getWindowPositionX() {
return windowPositionX;
}
public void setWindowPositionX(int windowPositionX) {
this.windowPositionX = windowPositionX;
}
public double getImageScaleLevel() {
return imageScaleLevel;
}
public void setImageScaleLevel(double imageScaleLevel) {
this.imageScaleLevel = imageScaleLevel;
}
public boolean isImageInverted() {
return imageInverted;
}
public void setImageInverted(boolean imageInverted) {
this.imageInverted = imageInverted;
}
public boolean isImageHighlights() {
return imageHighlights;
}
public void setImageHighlights(boolean imageHighlights) {
this.imageHighlights = imageHighlights;
}
public int getImageOriginX() {
return imageOriginX;
}
public void setImageOriginX(int imageOriginX) {
this.imageOriginX = imageOriginX;
}
public int getImageOriginY() {
return imageOriginY;
}
public void setImageOriginY(int imageOriginY) {
this.imageOriginY = imageOriginY;
}
public static Settings defaultSettings() {
Settings settings = new Settings();
settings.loadDefaults();
return settings;
}
}

View File

@ -0,0 +1,11 @@
package client.persistence;
import client.persistence.Cell;
public interface SyncContext {
public void onChangeCurrentCell(Cell cell);
public void onChangeCellValue(Cell cell, String value);
}

View File

@ -0,0 +1,251 @@
package search;
import search.elements.FieldButton;
import search.elements.ProjectGroup;
import search.forms.InputField;
import search.helpers.Networking;
import search.helpers.dataModels.ProjectContainer;
import shared.communication.common.Fields;
import shared.communication.common.Tuple;
import shared.communication.responses.Search_Res;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.ByteArrayOutputStream;
import java.util.HashSet;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;
public class Main extends JFrame {
private JPanel menuPanel;
private JPanel sidebarPanel;
private JPanel mainBody;
private JPanel searchResults;
private JSplitPane searchArea;
private JSplitPane searchForm;
public Main() {
this.setTitle("Search Program");
this.setSize(800, 650);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setupMenu();
setupSidebar();
setupBody();
setupSearchResults();
searchArea = new JSplitPane(JSplitPane.VERTICAL_SPLIT, mainBody, new JScrollPane());
searchArea.setOneTouchExpandable(false);
searchArea.setEnabled(false);
searchArea.setDividerLocation(80);
searchArea.setDividerSize(0);
searchForm = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sidebarPanel, searchArea);
searchForm.setOneTouchExpandable(false);
searchForm.setDividerLocation(120);
searchForm.setEnabled(false);
searchForm.setDividerSize(1);
searchForm.setPreferredSize(new Dimension(120, 500));
JSplitPane jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, menuPanel, searchForm);
jSplitPane.setOneTouchExpandable(false);
jSplitPane.setDividerLocation(40);
jSplitPane.setDividerSize(1);
jSplitPane.setEnabled(false);
this.add(jSplitPane);
this.setVisible(true);
}
private final InputField usernameField = new InputField("Username", 15);
private final JTextField passwordField = new JTextField("Password", 15);
private final JTextField hostField = new JTextField("localhost", 7);
private final JTextField portField = new JTextField("39640", 4);
private void setupMenu() {
// Add buttons
FlowLayout flowLayout = new FlowLayout();
final JPanel jPanel = new JPanel();
jPanel.setLayout(flowLayout);
flowLayout.setAlignment(FlowLayout.LEFT);
JButton button = new JButton("Get Projects");
jPanel.add(hostField);
jPanel.add(portField);
jPanel.add(usernameField);
jPanel.add(passwordField);
jPanel.add(button);
jPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Networking networking = new Networking(
hostField.getText(), Integer.parseInt(portField.getText()));
String username = usernameField.getText();
String password = passwordField.getText();
List<ProjectContainer> projectContainerList =
networking.getProjectsWithFields(username, password);
JPanel search = new JPanel();
if(projectContainerList != null)
for(ProjectContainer projectContainer : projectContainerList) {
search.add(new ProjectGroup(projectContainer));
for(final Fields field : projectContainer.getFieldsList()) {
FieldButton fieldButton = new FieldButton(field);
fieldButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String fieldId = Integer.toString(field.getId());
updateFieldsField(fieldId);
}
});
search.add(fieldButton);
}
}
searchForm.setLeftComponent(search);
searchForm.setDividerLocation(120);
searchForm.validate();
searchForm.repaint();
String fieldName = "";
if(projectContainerList != null)
fieldName = projectContainerList.get(0).getFieldsList().get(0).getTitle();
System.out.println(fieldName);
}
});
menuPanel = jPanel;
}
private void setupSidebar() {
final JPanel jPanel = new JPanel();
jPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
sidebarPanel = jPanel;
}
private final JTextField fieldsField = new JTextField("",48);
private void updateFieldsField(String fieldId) {
String text = fieldsField.getText();
String[] options = text.split(",");
if(options.length > 0 && !text.isEmpty())
text += ",";
fieldsField.setText(text + fieldId);
}
private void setupBody() {
final JPanel jPanel = new JPanel();
jPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
final JButton searchButton = new JButton("Search");
final JTextField queryField = new JTextField("",41);
jPanel.add(new JLabel("Fields: "));
jPanel.add(fieldsField);
jPanel.add(new JLabel("Terms: "));
jPanel.add(queryField);
jPanel.add(searchButton);
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel results = new JPanel();
Networking networking = new Networking(
hostField.getText(), Integer.parseInt(portField.getText()));
searchResults.setVisible(false);
String username = usernameField.getText();
String password = passwordField.getText();
Search_Res searchRes = networking.search(username,password,
fieldsField.getText(), queryField.getText());
if(searchRes != null)
for(Tuple searchResult : searchRes.getSearchResults()) {
String imageUrl = searchResult.getImageUrl();
final BufferedImage image = networking.getImage(imageUrl);
if(image == null) return;
BufferedImage scaledImage = resize(image, 200, 150);
JLabel imageLabel = new JLabel(new ImageIcon(scaledImage));
imageLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JFrame frame2 = new JFrame();
JLabel bigImage = new JLabel(new ImageIcon(image));
frame2.add(bigImage);
frame2.setSize(bigImage.getPreferredSize());
frame2.setVisible(true);
}
});
imageLabel.setPreferredSize(new Dimension(200, 150));
results.add(imageLabel);
}
JScrollPane scrollPane = new JScrollPane();
results.setPreferredSize(new Dimension(600, 2000));
scrollPane.getViewport().add(results);
searchArea.setRightComponent(scrollPane);
searchArea.validate();
searchArea.setDividerLocation(80);
searchArea.repaint();
}
});
mainBody = jPanel;
}
private void setupSearchResults() {
JPanel jPanel = new JPanel();
searchResults = jPanel;
}
public BufferedImage resize(BufferedImage image, int width, int height) {
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(image, 0, 0, width, height, null);
g2d.dispose();
return bi;
}
}

View File

@ -0,0 +1,24 @@
package search;
import javax.swing.*;
import java.awt.*;
public class MainLayout extends JFrame {
public MainLayout() {
initView();
}
FlowLayout flowLayout = new FlowLayout();
public void initView() {
final JPanel component = new JPanel();
component.setLayout(flowLayout);
}
public void open() {
setVisible(true);
}
}

View File

@ -0,0 +1,19 @@
package search;
import javax.swing.*;
import java.awt.*;
public class Search {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Main main = new Main();
main.setVisible(true);
}
});
}
}

View File

@ -0,0 +1,22 @@
package search.elements;
import shared.communication.common.Fields;
import javax.swing.*;
public class FieldButton extends JButton {
private Fields field;
public FieldButton(Fields field) {
super(field.getTitle());
this.field = field;
setupView();
}
public void setupView() {
}
}

View File

@ -0,0 +1,10 @@
package search.elements;
import javax.swing.*;
public class ImageButton extends JPanel {
public ImageButton() {
}
}

View File

@ -0,0 +1,25 @@
package search.elements;
import search.helpers.dataModels.ProjectContainer;
import shared.communication.common.Fields;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.List;
public class ProjectGroup extends JPanel {
private ProjectContainer projectContainer;
public ProjectGroup(ProjectContainer projectContainer) {
this.projectContainer = projectContainer;
setupView();
}
private void setupView() {
add(new JLabel(projectContainer.getTitle()));
}
}

View File

@ -0,0 +1,10 @@
package search.forms;
import javax.swing.*;
public class InputField extends JTextField {
public InputField(String text, int width) {
super(text, width);
}
}

View File

@ -0,0 +1,132 @@
package search.helpers;
import client.communication.Communicator;
import client.communication.errors.RemoteServerErrorException;
import client.communication.errors.UnauthorizedAccessException;
import search.helpers.dataModels.ProjectContainer;
import shared.communication.common.Fields;
import shared.communication.common.Project_Res;
import shared.communication.params.Fields_Param;
import shared.communication.params.Projects_Param;
import shared.communication.params.Search_Param;
import shared.communication.responses.Fields_Res;
import shared.communication.responses.Projects_Res;
import shared.communication.responses.Search_Res;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class Networking {
public static int DEFAULT = 1;
private String serverPath;
public Networking(String host, int port) {
serverPath = "http://"+host+":"+port+"/";
this.communicator = new Communicator(serverPath);
}
public Networking(int startCode) {
if(startCode == 1) {
serverPath = "http://localhost:39640/";
this.communicator = new Communicator(serverPath);
}
}
private Communicator communicator;
public Projects_Res getProjects(String username, String password) {
Projects_Param projectsParam = new Projects_Param();
projectsParam.setUsername(username);
projectsParam.setPassword(password);
try {
return communicator.getProjects(projectsParam);
} catch (UnauthorizedAccessException e) {
return null;
} catch (RemoteServerErrorException e) {
return null;
}
}
public Fields_Res getFields(String username, String password, int projectId) {
Fields_Param fieldsParam = new Fields_Param();
fieldsParam.setUsername(username);
fieldsParam.setPassword(password);
fieldsParam.setProjectId(projectId);
try {
return communicator.getFields(fieldsParam);
} catch (UnauthorizedAccessException e) {
return null;
} catch (RemoteServerErrorException e) {
return null;
}
}
public List<ProjectContainer> getProjectsWithFields(String username, String password) {
Projects_Res projectsRes = getProjects(username, password);
Fields_Res allFieldsRes = getFields(username, password, -1);
if(projectsRes == null || allFieldsRes == null) return null;
List<ProjectContainer> projectContainerList = new ArrayList<ProjectContainer>();
for(Project_Res projectRes : projectsRes.getProjectsList()) {
ProjectContainer projectContainer = new ProjectContainer(projectRes);
for(Fields field : allFieldsRes.getFields()) {
if(field.getProjectId() == projectRes.getId()) {
projectContainer.addField(field);
}
}
projectContainerList.add(projectContainer);
}
return projectContainerList;
}
public Search_Res search(String username, String password, String fieldIds, String queryWords) {
Search_Param searchParam = new Search_Param();
searchParam.setUsername(username);
searchParam.setPassword(password);
// Add FieldIds
for(String fieldId : parseCommaString(fieldIds)) {
searchParam.addFieldId(Integer.parseInt(fieldId));
}
// Add Query Strings
for(String query : parseCommaString(queryWords)) {
searchParam.addSearchParam(query);
}
try {
return communicator.search(searchParam);
} catch (UnauthorizedAccessException e) {
return null;
} catch (RemoteServerErrorException e) {
return null;
}
}
public BufferedImage getImage(String imageUrl) {
try {
return ImageIO.read(new URL(serverPath+imageUrl));
} catch (IOException e) {
return null;
}
}
// For search
private String[] parseCommaString(String stringWithCommas) {
return stringWithCommas.split(",");
}
}

View File

@ -0,0 +1,25 @@
package search.helpers.dataModels;
import shared.communication.common.Fields;
import shared.communication.common.Project_Res;
import java.util.ArrayList;
import java.util.List;
public class ProjectContainer extends Project_Res{
private List<Fields> fieldsList = new ArrayList<Fields>();
public ProjectContainer(Project_Res projectRes) {
super(projectRes.getId(), projectRes.getTitle());
}
public void addField(Fields field) {
fieldsList.add(field);
}
public List<Fields> getFieldsList() {
return this.fieldsList;
}
}

View File

@ -0,0 +1,49 @@
package server;
import com.sun.net.httpserver.HttpServer;
import server.db.common.Database;
import server.errors.ServerException;
import server.handlers.*;
import java.io.IOException;
import java.net.InetSocketAddress;
public class Server {
private static int SERVER_LISTENING_PORT = 8090;
private static int MAX_WAITING_CONNECTIONS = 10;
private HttpServer server;
public void run(int port) throws ServerException {
SERVER_LISTENING_PORT = port;
Database.init(Database.PRODUCTION_MODE);
try {
server = HttpServer.create(new InetSocketAddress(SERVER_LISTENING_PORT),
MAX_WAITING_CONNECTIONS);
} catch (IOException e) {
e.printStackTrace();
}
server.setExecutor(null);
server.createContext("/getProjects", new GetProjectsHandler().getHandler());
server.createContext("/getSampleImage", new GetSampleImageHandler().getHandler());
server.createContext("/validateUser", new ValidateUserHandler().getHandler());
server.createContext("/downloadBatch", new DownloadBatchHandler().getHandler());
server.createContext("/getFields", new GetFieldsHandler().getHandler());
server.createContext("/submitBatch", new SubmitBatchHandler().getHandler());
server.createContext("/search", new SearchHandler().getHandler());
server.createContext("/", new StaticsHandler().getHandler());
System.out.println("Starting server on port: " + SERVER_LISTENING_PORT);
server.start();
}
public static void main(String[] args) throws ServerException {
new Server().run(Integer.parseInt(args[0]));
}
}

View File

@ -0,0 +1,15 @@
package server.controllers;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import shared.communication.params.ValidateUser_Param;
import shared.communication.responses.ValidateUser_Res;
import java.io.InputStream;
public class UsersController {
public static ValidateUser_Res validateUser(ValidateUser_Param validateUserParam) {
return null;
}
}

View File

@ -0,0 +1,142 @@
package server.db.common;
import server.errors.ServerException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.sql.*;
public class Database {
public static final boolean AUTO_PRIMARY_KEY = true;
public static final boolean SPECIFIED_PRIMARY_KEY = false;
public static final boolean PRODUCTION_MODE = true;
public static final boolean DEVELOPMENT_MODE = true;
public static final boolean TEST_MODE = false;
private static Logger logger;
private Connection connection;
private static String dbUrl = "";
public static void init(boolean production) throws ServerException {
try {
final String driver = "org.sqlite.JDBC";
Class.forName(driver);
if(production) dbUrl = "jdbc:sqlite:db/database.sqlite3";
else dbUrl = "jdbc:sqlite:db/test.sqlite3";
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new ServerException("SQLite was not found");
}
}
public static void erase() throws ServerException, SQLException {
Database database = new Database();
database.openConnection();
database.voidQuery("delete from users;");
database.voidQuery("delete from projects;");
database.voidQuery("delete from records;");
database.voidQuery("delete from images;");
database.voidQuery("delete from 'values';");
database.voidQuery("delete from fields;");
database.commit();
database.closeConnection();
}
public void openConnection() throws ServerException {
try {
// Ensure we don't hit a collision
if(connection != null) return;
connection = DriverManager.getConnection(dbUrl);
connection.setAutoCommit(false);
} catch (SQLException e) {
throw new ServerException("Error establishing connection to: " + dbUrl);
}
}
public void closeConnection() throws SQLException {
connection.close();
connection = null;
}
public void commit() throws ServerException, SQLException {
try {
for(PreparedStatement query : queryBatch) {
int response = 0;
response = query.executeUpdate();
if(response != 1) {
connection.rollback();
throw new ServerException("Bad query update");
}
}
try {
connection.commit();
} catch (Exception e) {
e.printStackTrace();
}
} catch (SQLException e) {
//e.printStackTrace();
throw new ServerException(String.format("Error committing %d queries, rolling back!",
queryBatch.size()));
} finally {
queryBatch.clear();
}
}
public ResultSet query(String sql) throws ServerException, SQLException {
PreparedStatement preparedStatement;
try {
preparedStatement = connection.prepareStatement(sql);
return preparedStatement.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
throw new ServerException("Error running query!");
}
}
public void voidQuery(String sql) throws ServerException, SQLException {
Statement statement;
try {
statement = connection.createStatement();
statement.addBatch(sql);
statement.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
throw new ServerException("Error running query!");
}
}
private List<PreparedStatement> queryBatch = new ArrayList<PreparedStatement>();
public void addQuery(String sql) throws SQLException {
PreparedStatement preparedStatement;
preparedStatement = connection.prepareStatement(sql);
queryBatch.add(preparedStatement);
}
public int getLastIdForTable(String table) throws SQLException, ServerException {
ResultSet resultSet = query("SELECT MAX(ID) FROM '" +table+ "';" );
if(resultSet.next()) return resultSet.getInt(1);
return -1;
}
public String LAST_PROJECT = "(SELECT MAX(ID) FROM projects)";
public String LAST_IMAGE = "(SELECT MAX(ID) FROM images)";
public String LAST_RECORD = "(SELECT MAX(ID) FROM records)";
public String LAST_FIELD = "(SELECT MAX(ID) FROM fields)";
public String LAST_USER = "(SELECT MAX(ID) FROM users)";
public String LAST_VALUE = "(SELECT MAX(ID) FROM \'values\')";
}

View File

@ -0,0 +1,10 @@
package server.db.common;
public class SQL {
public static String format(String string) {
if(string == null) return null;
return "\'" + string + "\'";
}
}

View File

@ -0,0 +1,80 @@
package server.db.common;
import server.errors.ServerException;
import shared.models.*;
import java.sql.SQLException;
import java.util.List;
public abstract class Transaction {
public static boolean logic(Transaction transaction, Database database) {
try {
return transaction.logic();
} catch (SQLException e) {
System.out.println("TODO: SWAP WITH LOGGER LOGIC SAVE FUNCTION ERROR");
} catch (ServerException e) {
System.out.println("TODO: SWAP WITH LOGGER LOGIC COMMIT FUNCTION ERROR");
} finally {
try {
database.closeConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public static Object object(Transaction transaction, Database database) {
try {
return transaction.object();
} catch (SQLException e) {
System.out.println("TODO: SWAP WITH LOGGER object SAVE FUNCTION ERROR");
} catch (ServerException e) {
System.out.println("TODO: SWAP WITH LOGGER object COMMIT FUNCTION ERROR");
} finally {
try {
database.closeConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
public static List<Object> array(Transaction transaction, Database database) {
try {
return transaction.array();
} catch (SQLException e) {
System.out.println("TODO: SWAP WITH LOGGER array SAVE FUNCTION ERROR");
} catch (ServerException e) {
System.out.println("TODO: SWAP WITH LOGGER array COMMIT FUNCTION ERROR");
} finally {
try {
database.closeConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
public Object object() throws SQLException, ServerException {
return null;
}
public boolean logic() throws SQLException, ServerException {
return false;
}
public List<Object> array() throws SQLException, ServerException {
return null;
}
public void transaction() throws SQLException, ServerException {
}
}

View File

@ -0,0 +1,235 @@
package server.db.importer;
import org.apache.commons.io.FileUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import server.db.*;
import server.db.common.Database;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
public class Importer {
public static void main(String[] args) throws Exception {
Database.init(Database.PRODUCTION_MODE);
Database.erase();
new Importer().run(args[0]);
}
public void run(String xmlParam) throws Exception {
// Copy Files
String pathToXML = xmlParam;
File file = new File(pathToXML);
String basePath = file.getParentFile().getAbsolutePath();
String pathToImages = basePath + "/images/";
String pathToKnownData = basePath + "/knowndata/";
String pathToFieldHelp = basePath + "/fieldhelp/";
File destinationPath = new File("db/statics/");
// Copy images to statics/images
FileUtils.copyDirectoryToDirectory(new File(pathToImages), destinationPath);
// Copy known_data to statics/knownData
FileUtils.copyDirectoryToDirectory(new File(pathToKnownData), destinationPath);
// Copy field_help tp statics/fieldHelp
FileUtils.copyDirectoryToDirectory(new File(pathToFieldHelp), destinationPath);
// Import XML
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
File xmlFile = new File(pathToXML);
Document document = builder.parse(xmlFile);
NodeList usersList = document.getElementsByTagName("user");
for(int i = 0; i < usersList.getLength(); i++) {
// Get the the element
Element userElement = (Element)usersList.item(i);
parseUserBlock(userElement);
}
NodeList projectsList = document.getElementsByTagName("project");
for(int i = 0; i < projectsList.getLength(); i++) {
// Get the the element
Element projectElement = (Element)projectsList.item(i);
parseProjectBlock(projectElement);
NodeList fieldsList = projectElement.getElementsByTagName("field");
for(int j = 0; j < fieldsList.getLength(); j++) {
Element fieldElement = (Element)fieldsList.item(j);
parseFieldBlock(fieldElement, (j+1));
}
NodeList imagesList = projectElement.getElementsByTagName("image");
for(int j = 0; j < imagesList.getLength(); j++) {
Element imageElement = (Element)imagesList.item(j);
parseImageBlock(imageElement);
iteratingThroughRecords(imageElement);
}
}
}
public void iteratingThroughRecords(Element imageElement) throws Exception {
NodeList recordList = imageElement.getElementsByTagName("record");
for(int k = 0; k < recordList.getLength(); k++) {
Element recordElement = (Element)recordList.item(k);
parseRecordBlock(recordElement, (k + 1));
NodeList valueList = recordElement.getElementsByTagName("value");
for(int l = 0; l < valueList.getLength(); l++) {
Element valueElement = (Element)valueList.item(l);
parseValueBlock(valueElement, (l+1));
}
}
}
public UserAccessor parseUserBlock(Element userElement) throws Exception {
System.out.print("Parsing User Block ------- ");
UserAccessor userAccessor = new UserAccessor();
String username = userElement.getElementsByTagName("username").item(0).getTextContent();
String password = userElement.getElementsByTagName("password").item(0).getTextContent();
String firstname = userElement.getElementsByTagName("firstname").item(0).getTextContent();
String lastname = userElement.getElementsByTagName("lastname").item(0).getTextContent();
String email = userElement.getElementsByTagName("email").item(0).getTextContent();
String indexedrecords = userElement.getElementsByTagName("indexedrecords")
.item(0).getTextContent();
userAccessor.setUsername(username);
userAccessor.setPassword(password);
userAccessor.setFirstName(firstname);
userAccessor.setLastName(lastname);
userAccessor.setEmail(email);
userAccessor.setIndexedRecords(Integer.parseInt(indexedrecords));
if(userAccessor.save()) System.out.println("Success!");
else {
System.out.println("ERROR!");
throw new Exception("Error saving the user: " + userAccessor.getUsername());
}
return userAccessor;
}
public ProjectAccessor parseProjectBlock(Element projectElement) throws Exception {
System.out.print("Parsing Project Block ------- ");
ProjectAccessor projectAccessor = new ProjectAccessor();
String title = projectElement.getElementsByTagName("title").item(0).getTextContent();
String recordsperimage = projectElement.getElementsByTagName("recordsperimage")
.item(0).getTextContent();
String firstycoord = projectElement.getElementsByTagName("firstycoord")
.item(0).getTextContent();
String recordheight = projectElement.getElementsByTagName("recordheight")
.item(0).getTextContent();
projectAccessor.setTitle(title);
projectAccessor.setRecordsPerImage(Integer.parseInt(recordsperimage));
projectAccessor.setRecordHeight(Integer.parseInt(recordheight));
projectAccessor.setFirstYCoord(Integer.parseInt(firstycoord));
if(projectAccessor.save()) System.out.println("Success!");
else {
System.out.println("ERROR!");
throw new Exception("Error saving the: " + projectAccessor.getTitle());
}
return projectAccessor;
}
public FieldAccessor parseFieldBlock(Element fieldElement, int number) throws Exception {
System.out.print(" Parsing Field Block ------- ");
FieldAccessor fieldAccessor = new FieldAccessor();
String title = fieldElement.getElementsByTagName("title").item(0).getTextContent();
String xcoord = fieldElement.getElementsByTagName("xcoord").item(0).getTextContent();
String width = fieldElement.getElementsByTagName("width").item(0).getTextContent();
String helphtml = fieldElement.getElementsByTagName("helphtml").item(0).getTextContent();
String knowndata = null;
if(fieldElement.getElementsByTagName("knowndata").item(0) != null)
knowndata = fieldElement.getElementsByTagName("knowndata").item(0).getTextContent();
fieldAccessor.setTitle(title);
fieldAccessor.setxCoord(Integer.parseInt(xcoord));
fieldAccessor.setWidth(Integer.parseInt(width));
fieldAccessor.setHelpHtml(helphtml);
fieldAccessor.setKnownData(knowndata);
fieldAccessor.setPosition(number);
if(fieldAccessor.save()) System.out.println("Success!");
else {
System.out.println("ERROR!");
throw new Exception("Error saving the: " + fieldAccessor.getTitle());
}
return fieldAccessor;
}
public ImageAccessor parseImageBlock(Element imageElement) throws Exception {
System.out.print(" Parsing Image Block ------- ");
ImageAccessor imageAccessor = new ImageAccessor();
String file = imageElement.getElementsByTagName("file").item(0).getTextContent();
imageAccessor.setFile(file);
if(imageAccessor.save()) System.out.println("Success!");
else {
System.out.println("ERROR!");
throw new Exception("Error saving the: " + imageAccessor.getFile());
}
return imageAccessor;
}
public RecordAccessor parseRecordBlock(Element recordElement, int number) throws Exception {
System.out.print(" Parsing Record Block ------- ");
RecordAccessor recordAccessor = new RecordAccessor();
recordAccessor.setPosition(number);
if(recordAccessor.save()) System.out.println("Success!");
else {
System.out.println("ERROR!");
throw new Exception("Error saving the: " + recordAccessor.getId());
}
return recordAccessor;
}
public ValueAccessor parseValueBlock(Element valueElement, int number) throws Exception {
System.out.print(" Parsing Value Block ------- ");
ValueAccessor valueAccessor = new ValueAccessor();
String value = valueElement.getTextContent();
valueAccessor.setValue(value);
valueAccessor.setType("String");
valueAccessor.setPosition(number);
if(valueAccessor.save()) System.out.println("Success!");
else {
System.out.println("ERROR!");
throw new Exception("Error saving the: " + valueAccessor.getId());
}
return valueAccessor;
}
}

View File

@ -0,0 +1,21 @@
package server.errors;
@SuppressWarnings("serial")
public class ServerException extends Exception {
public ServerException() {
}
public ServerException(String message) {
super(message);
}
public ServerException(Throwable throwable) {
super(throwable);
}
public ServerException(String message, Throwable throwable) {
super(message, throwable);
}
}

View File

@ -0,0 +1,77 @@
package server.handlers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import server.db.FieldAccessor;
import server.db.ImageAccessor;
import server.db.ProjectAccessor;
import server.db.UserAccessor;
import server.handlers.common.BaseHanlder;
import shared.communication.params.DownloadBatch_Param;
import shared.communication.responses.DownloadBatch_Res;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class DownloadBatchHandler extends BaseHanlder {
private HttpHandler handler = new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
InputStream inputStream = exchange.getRequestBody();
String request = inputStreamToString(inputStream);
DownloadBatch_Param downloadBatchParam = DownloadBatch_Param.serialize(request);
UserAccessor userAccessor = UserAccessor.find(downloadBatchParam.getUsername());
ImageAccessor imageAccessor;
List<ImageAccessor> availableList;
if(userAccessor != null) {
imageAccessor = userAccessor.getImage();
availableList = ImageAccessor.allAvailible(downloadBatchParam.getProjectId());
} else {
writeServerErrorResponse(exchange);
return;
}
if(userAccessor == null || availableList.size() == 0) {
writeServerErrorResponse(exchange);
} else if (userAccessor.login(downloadBatchParam.getPassword())) {
if(userAccessor.getImage() != null) {
writeServerErrorResponse(exchange);
return;
}
ImageAccessor assignImage = availableList.get(0);
userAccessor.setImageId(assignImage.getId());
if(userAccessor.save()) {
String response;
ProjectAccessor projectAccessor = assignImage.getProject();
List<FieldAccessor> fieldAccessorList = projectAccessor.getFields();
DownloadBatch_Res downloadBatchRes = new DownloadBatch_Res(
assignImage.getModel(), projectAccessor.getModel(), fieldAccessorList.size(),
projectAccessor.getRecordsPerImage());
// Add each field
for(FieldAccessor fieldAccessor : fieldAccessorList)
downloadBatchRes.addField(fieldAccessor.getModel());
response = downloadBatchRes.toXML();
writeSuccessResponse(exchange, response);
return;
}
}
writeServerErrorResponse(exchange);
}
};
public HttpHandler getHandler() {
return handler;
}
}

View File

@ -0,0 +1,70 @@
package server.handlers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import server.db.FieldAccessor;
import server.db.ProjectAccessor;
import server.db.UserAccessor;
import server.handlers.common.BaseHanlder;
import shared.communication.params.Fields_Param;
import shared.communication.params.Projects_Param;
import shared.communication.responses.Fields_Res;
import shared.communication.responses.Projects_Res;
import shared.models.Field;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class GetFieldsHandler extends BaseHanlder {
private HttpHandler handler = new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
InputStream inputStream = exchange.getRequestBody();
String request = inputStreamToString(inputStream);
Fields_Param fieldsParam = Fields_Param.serialize(request);
UserAccessor userAccessor = UserAccessor.find(fieldsParam.getUsername());
if(userAccessor == null) {
writeServerErrorResponse(exchange);
return;
} else if(userAccessor.login(fieldsParam.getPassword())) {
String response;
Fields_Res fieldsRes = new Fields_Res();
List<FieldAccessor> fieldAccessorList;
ProjectAccessor projectAccessor;
if(fieldsParam.getProjectId() == -1) {
fieldAccessorList = FieldAccessor.all();
} else {
// if there find a project
projectAccessor = ProjectAccessor.find(fieldsParam.getProjectId());
if(projectAccessor == null) {
writeServerErrorResponse(exchange);
return;
}
// Get the projects fields
fieldAccessorList = projectAccessor.getFields();
}
for(int i = 0; i < fieldAccessorList.size(); i++) {
Field field = fieldAccessorList.get(i).getModel();
fieldsRes.addField(field, field.getPosition());
}
response = fieldsRes.toXML();
writeSuccessResponse(exchange, response);
return;
}
writeBadAuthenticationResponse(exchange);
}
};
public HttpHandler getHandler() {
return handler;
}
}

View File

@ -0,0 +1,58 @@
package server.handlers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import server.db.ProjectAccessor;
import server.db.UserAccessor;
import server.handlers.common.BaseHanlder;
import shared.communication.params.Projects_Param;
import shared.communication.responses.Projects_Res;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class GetProjectsHandler extends BaseHanlder {
private HttpHandler handler = new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
InputStream inputStream = exchange.getRequestBody();
String request = inputStreamToString(inputStream);
Projects_Param projectsParam = Projects_Param.serialize(request);
UserAccessor userAccessor = UserAccessor.find(projectsParam.getUsername());
if(userAccessor == null) {
writeServerErrorResponse(exchange);
return;
} else if(userAccessor.login(projectsParam.getPassword())) {
String response;
Projects_Res projectsRes = new Projects_Res();
List<ProjectAccessor> projectAccessors = ProjectAccessor.all();
if(projectAccessors.size() == 0) {
writeServerErrorResponse(exchange);
return;
}
for(ProjectAccessor pA : projectAccessors) {
projectsRes.addProject(pA.getId(), pA.getTitle());
}
response = projectsRes.toXML();
writeSuccessResponse(exchange, response);
return;
}
writeBadAuthenticationResponse(exchange);
}
};
public HttpHandler getHandler() {
return handler;
}
}

View File

@ -0,0 +1,55 @@
package server.handlers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import server.db.ImageAccessor;
import server.db.ProjectAccessor;
import server.db.UserAccessor;
import server.handlers.common.BaseHanlder;
import shared.communication.params.SampleImage_Param;
import shared.communication.responses.SampleImage_Res;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class GetSampleImageHandler extends BaseHanlder {
private HttpHandler handler = new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
InputStream inputStream = exchange.getRequestBody();
String request = inputStreamToString(inputStream);
SampleImage_Param sampleImageParam = SampleImage_Param.serialize(request);
UserAccessor userAccessor = UserAccessor.find(sampleImageParam.getUsername());
ProjectAccessor projectAccessor = ProjectAccessor.find(sampleImageParam.getProjectId());
if(userAccessor == null || projectAccessor == null) {
writeServerErrorResponse(exchange);
return;
} else if (userAccessor.login(sampleImageParam.getPassword())) {
String response = "";
List<ImageAccessor> imageAccessor = projectAccessor.getImages();
if(imageAccessor.size() > 0) {
SampleImage_Res sampleImageRes = new SampleImage_Res(
imageAccessor.get(0).getModel());
response = sampleImageRes.toXML();
writeSuccessResponse(exchange, response);
return;
}
}
writeServerErrorResponse(exchange);
}
};
public HttpHandler getHandler() {
return handler;
}
}

View File

@ -0,0 +1,85 @@
package server.handlers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import server.db.*;
import server.handlers.common.BaseHanlder;
import shared.communication.common.Tuple;
import shared.communication.params.Search_Param;
import shared.communication.params.ValidateUser_Param;
import shared.communication.responses.Search_Res;
import shared.communication.responses.ValidateUser_Res;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class SearchHandler extends BaseHanlder {
private List<Tuple> search(int fieldId, Set<String> words) {
FieldAccessor fieldAccessor = FieldAccessor.find(fieldId);
if(fieldAccessor == null) return new ArrayList<Tuple>();
List<ValueAccessor> valueAccessorList = fieldAccessor.getColumnValues();
List<ValueAccessor> matchingValueAccessorsList = new ArrayList<ValueAccessor>();
for(ValueAccessor valueAccessor : valueAccessorList) {
for(String word : words) {
word = word.toLowerCase();
if(word.equals(valueAccessor.getValue().toLowerCase())) {
matchingValueAccessorsList.add(valueAccessor);
}
}
}
List<Tuple> tupleList = new ArrayList<Tuple>();
for(ValueAccessor valueAccessor : matchingValueAccessorsList) {
RecordAccessor recordAccessor = valueAccessor.getRecord();
ImageAccessor imageAccessor = recordAccessor.getImage();
Tuple tuple = new Tuple();
tuple.setBatchId(imageAccessor.getId());
tuple.setImageUrl(imageAccessor.getFile());
tuple.setRecordNumber(recordAccessor.getPosition());
tuple.setFieldId(fieldId);
tupleList.add(tuple);
}
return tupleList;
}
private HttpHandler handler = new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
InputStream inputStream = exchange.getRequestBody();
String request = inputStreamToString(inputStream);
Search_Param searchParam = Search_Param.serialize(request);
UserAccessor userAccessor = UserAccessor.find(searchParam.getUsername());
if(userAccessor == null) {
writeBadAuthenticationResponse(exchange);
return;
} else if(userAccessor.login(searchParam.getPassword())) {
List<Tuple> foundTupleList = new ArrayList<Tuple>();
for(int id : searchParam.getFieldsIds()) {
foundTupleList.addAll(search(id, searchParam.getSearchParams()));
}
Search_Res searchRes = new Search_Res(foundTupleList);
writeSuccessResponse(exchange, searchRes.toXML());
return;
}
writeBadAuthenticationResponse(exchange);
}
};
public HttpHandler getHandler() {
return handler;
}
}

View File

@ -0,0 +1,36 @@
package server.handlers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import server.db.UserAccessor;
import server.handlers.common.BaseHanlder;
import shared.communication.params.ValidateUser_Param;
import shared.communication.responses.ValidateUser_Res;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class StaticsHandler extends BaseHanlder {
private HttpHandler handler = new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
String prefix = "db/statics";
String uri = exchange.getRequestURI().toString();
File staticFile;
try {
staticFile = new File(prefix+uri);
} catch (Exception e) {
writeServerErrorResponse(exchange);
return;
}
writeFileResponse(exchange, staticFile);
}
};
public HttpHandler getHandler() {
return handler;
}
}

View File

@ -0,0 +1,75 @@
package server.handlers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import server.db.ImageAccessor;
import server.db.UserAccessor;
import server.handlers.common.BaseHanlder;
import shared.communication.common.ARecord;
import shared.communication.params.SubmitBatch_Param;
import shared.communication.responses.SubmitBatch_Res;
import java.io.IOException;
import java.io.InputStream;
public class SubmitBatchHandler extends BaseHanlder {
private HttpHandler handler = new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
InputStream inputStream = exchange.getRequestBody();
String request = inputStreamToString(inputStream);
SubmitBatch_Param submitBatchParam = SubmitBatch_Param.serialize(request);
UserAccessor userAccessor = UserAccessor.find(submitBatchParam.getUsername());
if(userAccessor == null) {
writeServerErrorResponse(exchange);
return;
} else if (userAccessor.login(submitBatchParam.getPassword())) {
if(userAccessor.getImageId() != submitBatchParam.getImageId()) {
writeServerErrorResponse(exchange);
return;
}
ImageAccessor imageAccessor = userAccessor.getImage();
if(imageAccessor == null
|| submitBatchParam.getRecordValues().size()
!= imageAccessor.getProject().getRecordsPerImage()) {
writeServerErrorResponse(exchange);
return;
}
for(int i = 0; i < submitBatchParam.getRecordValues().size(); i++) {
ARecord aRecord = submitBatchParam.getRecordValues().get(i);
if(imageAccessor.getProject().getFields().size() != aRecord.getValues().size()){
writeServerErrorResponse(exchange);
return;
}
imageAccessor.addRecord(aRecord.getValues(), (i + 1));
}
if(imageAccessor.save()) {
int current = userAccessor.getIndexedRecords();
int toAdd = imageAccessor.getProject().getRecordsPerImage();
userAccessor.setIndexedRecords(current + toAdd);
userAccessor.setImageId(0);
if(userAccessor.save()) {
SubmitBatch_Res submitBatchRes = new SubmitBatch_Res(true);
writeSuccessResponse(exchange, submitBatchRes.toXML());
return;
}
}
}
writeServerErrorResponse(exchange);
}
};
public HttpHandler getHandler() {
return handler;
}
}

View File

@ -0,0 +1,46 @@
package server.handlers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import server.db.UserAccessor;
import server.handlers.common.BaseHanlder;
import shared.communication.params.ValidateUser_Param;
import shared.communication.responses.ValidateUser_Res;
import java.io.IOException;
import java.io.InputStream;
public class ValidateUserHandler extends BaseHanlder {
private HttpHandler handler = new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
InputStream inputStream = exchange.getRequestBody();
String request = inputStreamToString(inputStream);
ValidateUser_Param validateUserParam = ValidateUser_Param.serialize(request);
UserAccessor userAccessor = UserAccessor.find(validateUserParam.getUsername());
if(userAccessor == null) {
writeBadAuthenticationResponse(exchange);
return;
} else if(userAccessor.login(validateUserParam.getPassword())) {
String response;
ValidateUser_Res validateUserRes;
validateUserRes = new ValidateUser_Res(true, userAccessor.getModel());
response = validateUserRes.toXML();
writeSuccessResponse(exchange, response);
return;
}
writeBadAuthenticationResponse(exchange);
}
};
public HttpHandler getHandler() {
return handler;
}
}

View File

@ -0,0 +1,76 @@
package server.handlers.common;
import com.sun.net.httpserver.HttpExchange;
import java.io.*;
public class BaseHanlder {
protected static final String NOT_AUTHORIZED =
"<error><authenticated>false</authenticated></error>";
protected static final String INTERNAL_ERROR = "<error>Failed</error>";
protected String inputStreamToString(InputStream inputStream) {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
try {
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try { bufferedReader.close(); } catch (IOException e) {
e.printStackTrace(); }
}
return stringBuilder.toString();
}
protected void writeReponse(int status, HttpExchange exchange, String response)
throws IOException {
exchange.sendResponseHeaders(status, response.length());
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(response.getBytes());
outputStream.close();
}
protected void writeSuccessResponse(HttpExchange exchange, String response) throws IOException {
writeReponse(200, exchange, response);
}
protected void writeServerErrorResponse(HttpExchange exchange) throws IOException {
writeReponse(500, exchange, INTERNAL_ERROR);
}
protected void writeBadAuthenticationResponse(HttpExchange exchange) throws IOException {
writeReponse(401, exchange, NOT_AUTHORIZED);
}
protected void writeFileResponse(HttpExchange exchange, File file) throws IOException {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
} catch (Exception e) {
writeServerErrorResponse(exchange);
return;
}
OutputStream outputStream = exchange.getResponseBody();
byte[] bufferArray = new byte[512];
exchange.sendResponseHeaders(200, file.length());
int c = 0;
while ((c = fileInputStream.read(bufferArray, 0, bufferArray.length)) > 0) {
outputStream.write(bufferArray, 0, c);
outputStream.flush();
}
outputStream.close();
fileInputStream.close();
}
}

View File

@ -0,0 +1,5 @@
package shared.common;
public class BaseModel implements Cloneable {
}

View File

@ -0,0 +1,22 @@
package shared.communication.common;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import shared.models.Value;
import java.util.ArrayList;
import java.util.List;
@XStreamAlias("record")
public class ARecord {
private List<Value> values = new ArrayList<Value>();
public List<Value> getValues() {
return values;
}
public void setValues(List<Value> values) {
this.values = values;
}
}

View File

@ -0,0 +1,71 @@
package shared.communication.common;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import java.io.Serializable;
@XStreamAlias("field")
public class Fields implements Serializable{
private String knownData;
private int id;
private int number;
private String title;
private String helpUrl;
private int xCoord;
private int pixelWidth;
private int projectId;
public Fields(int id, int number,
String title, String helpUrl,
int xCoord, int pixelWidth,
String knownData, int projectId) {
this.id = id;
this.number = number;
this.title = title;
this.helpUrl = helpUrl;
this.xCoord = xCoord;
this.pixelWidth = pixelWidth;
this.knownData = knownData;
this.projectId = projectId;
}
public String getKnownData() {
return knownData;
}
public int getId() {
return id;
}
public int getNumber() {
return number;
}
public String getTitle() {
return title;
}
public String getHelpUrl() {
return helpUrl;
}
public int getxCoord() {
return xCoord;
}
public int getPixelWidth() {
return pixelWidth;
}
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
}

View File

@ -0,0 +1,24 @@
package shared.communication.common;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("project")
public class Project_Res {
private int id;
private String title;
public Project_Res(int id, String title) {
this.id = id;
this.title = title;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
}

View File

@ -0,0 +1,56 @@
package shared.communication.common;
public class Tuple {
private int batchId;
private String imageUrl;
private int recordNumber;
private int fieldId;
public Tuple(int batchId, String imageUrl,
int recordNumber, int fieldId) {
this.batchId = batchId;
this.imageUrl = imageUrl;
this.recordNumber = recordNumber;
this.fieldId = fieldId;
}
public Tuple(){
}
public int getBatchId() {
return batchId;
}
public void setBatchId(int batchId) {
this.batchId = batchId;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public int getRecordNumber() {
return recordNumber;
}
public void setRecordNumber(int recordNumber) {
this.recordNumber = recordNumber;
}
public int getFieldId() {
return fieldId;
}
public void setFieldId(int fieldId) {
this.fieldId = fieldId;
}
}

View File

@ -0,0 +1,55 @@
package shared.communication.params;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.xml.StaxDriver;
@XStreamAlias("downloadBatch")
public class DownloadBatch_Param {
private String username;
private String password;
private int projectId;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public static DownloadBatch_Param serialize(String xml) {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
xstream.alias("batch", DownloadBatch_Param.class);
xstream.alias("downloadBatch", DownloadBatch_Param.class);
xstream.alias("downloadbatch", DownloadBatch_Param.class);
xstream.alias("download", DownloadBatch_Param.class);
return (DownloadBatch_Param)xstream.fromXML(xml);
}
public String toXML() {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
xstream.alias("downloadBatch", DownloadBatch_Param.class);
return xstream.toXML(this);
}
}

View File

@ -0,0 +1,50 @@
package shared.communication.params;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.xml.StaxDriver;
@XStreamAlias("fields")
public class Fields_Param {
private String username;
private String password;
private int projectId;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public static Fields_Param serialize(String xml) {
XStream xstream = new XStream(new StaxDriver());
xstream.alias("fields", Fields_Param.class);
return (Fields_Param)xstream.fromXML(xml);
}
public String toXML() {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
return xstream.toXML(this);
}
}

View File

@ -0,0 +1,41 @@
package shared.communication.params;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.xml.StaxDriver;
@XStreamAlias("project")
public class Projects_Param {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static Projects_Param serialize(String xml) {
XStream xstream = new XStream(new StaxDriver());
xstream.alias("project", Projects_Param.class);
return (Projects_Param)xstream.fromXML(xml);
}
public String toXML() {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
return xstream.toXML(this);
}
}

View File

@ -0,0 +1,51 @@
package shared.communication.params;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;
public class SampleImage_Param {
private String username;
private String password;
private int projectId;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public static SampleImage_Param serialize(String xml) {
XStream xstream = new XStream(new StaxDriver());
xstream.alias("user", SampleImage_Param.class);
xstream.alias("sampleImage", SampleImage_Param.class);
xstream.alias("sampleimage", SampleImage_Param.class);
xstream.alias("sample", SampleImage_Param.class);
return (SampleImage_Param)xstream.fromXML(xml);
}
public String toXML() {
XStream xstream = new XStream(new StaxDriver());
xstream.alias("sampleImage", SampleImage_Param.class);
return xstream.toXML(this);
}
}

View File

@ -0,0 +1,63 @@
package shared.communication.params;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import shared.models.Value;
import java.util.*;
public class Search_Param {
private String username;
private String password;
private Set<Integer> fieldsIds = new TreeSet<Integer>();
private Set<String> searchParams = new TreeSet<String>();
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void addSearchParam(String param) {
searchParams.add(param);
}
public void addFieldId(int fieldId) {
fieldsIds.add(fieldId);
}
public Set<Integer> getFieldsIds() {
return fieldsIds;
}
public Set<String> getSearchParams() {
return searchParams;
}
public String toXML() {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
xstream.alias("search", Search_Param.class);
return xstream.toXML(this);
}
public static Search_Param serialize(String xml) {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
xstream.alias("search", Search_Param.class);
return (Search_Param)xstream.fromXML(xml);
}
}

View File

@ -0,0 +1,85 @@
package shared.communication.params;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import shared.communication.common.ARecord;
import shared.communication.responses.Fields_Res;
import shared.models.Value;
import java.util.ArrayList;
import java.util.List;
public class SubmitBatch_Param {
private String username;
private String password;
private int imageId;
private List<ARecord> recordValues = new ArrayList<ARecord>();
public SubmitBatch_Param() {}
public SubmitBatch_Param(String username, String password, int imageId) {
this.username = username;
this.password = password;
this.imageId = imageId;
}
public List<ARecord> getRecordValues() {
return this.recordValues;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public int getImageId() {
return imageId;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public void addRecord(List<Value> values) {
ARecord aRecord = new ARecord();
aRecord.setValues(values);
recordValues.add(aRecord);
}
public static SubmitBatch_Param serialize(String xml) {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
xstream.alias("submit", SubmitBatch_Param.class);
xstream.alias("submitBatch", SubmitBatch_Param.class);
xstream.alias("value", Value.class);
return (SubmitBatch_Param)xstream.fromXML(xml);
}
public String toXML() {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
xstream.alias("submitBatch", SubmitBatch_Param.class);
xstream.alias("value", Value.class);
return xstream.toXML(this);
}
}

View File

@ -0,0 +1,44 @@
package shared.communication.params;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import shared.communication.responses.ValidateUser_Res;
@XStreamAlias("user")
public class ValidateUser_Param {
private String username;
private String password;
public static ValidateUser_Param serialize(String xml) {
XStream xstream = new XStream(new StaxDriver());
xstream.alias("user", ValidateUser_Param.class);
return (ValidateUser_Param)xstream.fromXML(xml);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toXML() {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
xstream.alias("user", ValidateUser_Res.class);
return xstream.toXML(this);
}
}

View File

@ -0,0 +1,131 @@
package shared.communication.responses;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import shared.communication.common.Fields;
import shared.models.Field;
import shared.models.Image;
import shared.models.Project;
import java.util.ArrayList;
import java.util.List;
@XStreamAlias("downloadBatch")
public class DownloadBatch_Res {
private int batchId;
private int projectId;
private String imageUrl;
private int firstYCoord;
private int recordHeight;
private int numberOfRecords;
private int numberOfFields;
private int recordsPerImage;
private List<Fields> fields = new ArrayList<Fields>();
public DownloadBatch_Res(Image image, Project project,
int fieldCount, int recordCount) {
this.batchId = image.getId();
this.imageUrl = image.getFile();
this.projectId = project.getId();
this.firstYCoord = project.getFirstYCoord();
this.recordHeight = project.getRecordHeight();
this.recordsPerImage = project.getRecordsPerImage();
this.numberOfRecords = recordCount;
this.numberOfFields = fieldCount;
}
public void addField(Field field) {
Fields response = null;
response = new Fields(field.getId(), field.getPosition(), field.getTitle(),
field.getHelpHtml(), field.getxCoord(),
field.getWidth(), field.getKnownData(),
field.getProjectId());
fields.add(response);
}
public int getBatchId() {
return batchId;
}
public int getProjectId() {
return projectId;
}
public String getImageUrl() {
return imageUrl;
}
public int getFirstYCoord() {
return firstYCoord;
}
public int getRecordHeight() {
return recordHeight;
}
public int getNumberOfRecords() {
return numberOfRecords;
}
public int getNumberOfFields() {
return numberOfFields;
}
public int getRecordsPerImage() {
return recordsPerImage;
}
public List<Fields> getFields() {
return fields;
}
public String toString(String serverPath) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(getBatchId() + "\n");
stringBuilder.append(getProjectId() + "\n");
stringBuilder.append(serverPath + getImageUrl() + "\n");
stringBuilder.append(getFirstYCoord() + "\n");
stringBuilder.append(getRecordHeight() + "\n");
stringBuilder.append(getNumberOfRecords() + "\n");
stringBuilder.append(getNumberOfFields() + "\n");
for(Fields field : fields) {
stringBuilder.append(field.getId() + "\n");
stringBuilder.append(field.getNumber() + "\n");
stringBuilder.append(field.getTitle() + "\n");
stringBuilder.append(serverPath + field.getHelpUrl() + "\n");
stringBuilder.append(field.getxCoord() + "\n");
stringBuilder.append(field.getPixelWidth() + "\n");
if(field.getKnownData() != null && !field.getKnownData().isEmpty()) {
stringBuilder.append(serverPath + field.getKnownData() + "\n");
}
}
return stringBuilder.toString();
}
public String toXML() {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
return xstream.toXML(this);
}
public static DownloadBatch_Res serialize(String xml) {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
xstream.alias("downloadBatch", DownloadBatch_Res.class);
return (DownloadBatch_Res)xstream.fromXML(xml);
}
}

View File

@ -0,0 +1,61 @@
package shared.communication.responses;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import shared.communication.common.Fields;
import shared.models.Field;
import java.util.ArrayList;
import java.util.List;
@XStreamAlias("fields")
public class Fields_Res {
@XStreamImplicit()
private List<Fields> fields = new ArrayList<Fields>();
public void addField(Field field, int position) {
Fields response = null;
response = new Fields(field.getId(), position, field.getTitle(),
field.getHelpHtml(), field.getxCoord(),
field.getWidth(), field.getKnownData(),
field.getProjectId());
fields.add(response);
}
public List<Fields> getFields() {
return fields;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
for(Fields field : fields) {
stringBuilder.append(field.getProjectId() + "\n");
stringBuilder.append(field.getId() + "\n");
stringBuilder.append(field.getTitle() + "\n");
}
return stringBuilder.toString();
}
public String toXML() {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
return xstream.toXML(this);
}
public static Fields_Res serialize(String xml) {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
xstream.alias("fields", Fields_Res.class);
return (Fields_Res)xstream.fromXML(xml);
}
}

View File

@ -0,0 +1,59 @@
package shared.communication.responses;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import server.db.ProjectAccessor;
import server.db.UserAccessor;
import shared.communication.common.Project_Res;
import shared.models.Project;
import java.util.ArrayList;
import java.util.List;
@XStreamAlias("projects")
public class Projects_Res {
public Projects_Res() {
}
@XStreamImplicit()
private List<Project_Res> projectsList = new ArrayList<Project_Res>();
public void addProject(int id, String title) {
projectsList.add(new Project_Res(id, title));
}
public List<Project_Res> getProjectsList() {
return projectsList;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
for(Project_Res projectRes : projectsList) {
stringBuilder.append(projectRes.getId() + "\n");
stringBuilder.append(projectRes.getTitle() + "\n");
}
return stringBuilder.toString();
}
public String toXML() {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
return xstream.toXML(this);
}
public static Projects_Res serialize(String xml) {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
xstream.alias("projects", Projects_Res.class);
return (Projects_Res)xstream.fromXML(xml);
}
}

Some files were not shown because too many files have changed in this diff Show More