Commit bf2db246 authored by Kjetil Thuen's avatar Kjetil Thuen

Removed Java code

parent f301f371
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nesstar</groupId>
<artifactId>NesstarMobile</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>NesstarMobile Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>Nesstar</id>
<name>Nesstar Maven Repository</name>
<url>http://nesstar-dev.nsd.uib.no/nexus/content/groups/public/</url>
</repository>
<repository>
<id>sourceforge</id>
<url>http://oss.sonatype.org/content/groups/sourceforge/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.nesstar</groupId>
<artifactId>nesstar-api</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- <<version>0.5</version> -->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.6.2</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<finalName>NesstarMobile</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<server>LocalTomcat</server>
<path>/NesstarMobile</path>
</configuration>
</plugin>
<!-- Run the application using "mvn jetty" -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<scanIntervalSeconds>7</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9998</stopPort>
<scanTargetPatterns>
<!-- Redeploy whenever any of the following files change -->
<scanTargetPattern>
<directory>src/main/webapp</directory>
<includes>
<include>**/*.html</include>
<include>**/*.js</include>
<include>**/*.css</include>
<include>**/*.less</include>
</includes>
</scanTargetPattern>
<scanTargetPattern>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</scanTargetPattern>
</scanTargetPatterns>
</configuration>
<executions>
<!-- Launch maven when running integration tests -->
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
This diff is collapsed.
package com.nesstar;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.log4j.Logger;
public class ServerHttpSessionListener implements HttpSessionListener {
private static final Logger logger = Logger.getLogger(ServerHttpSessionListener.class);
@Override
public void sessionCreated(HttpSessionEvent se) {
logger.info("Session created: " + se.getSession().getId());
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
HttpSession session = event.getSession();
// remove the cache of this session
if (null != session.getAttribute(StaticVariables.DAO)) {
((TabulateAnalysisDAO) session.getAttribute(StaticVariables.DAO)).endCache();
}
logger.info("Clear the cache of session: " + session.getId());
logger.info("Session destroyed: " + session.getId());
}
}
This diff is collapsed.
This diff is collapsed.
package com.nesstar;
class URLParser {
private String path;
private String objectType;
private String objectId, subId;
private String operation, subOperation;
/*
* "/login
* "/study/list"
* "/study/{studyId}"
* "/study/{studyId}/metadata"
* "/study/{studyId}/metadata/full"
* "/study/{studyId}/tabulation"
* "/study/{studyId}/variable/{variableId}"
* "/study/{studyId}/variablegroups"
* "/study/{studyId}/variablegroup/{groupId}/variables"
* "/study/{studyId}/variablegroup/{groupId}/variablegroups"
* "/study/{studyId}/variables"
* "/study/{studyId}/correlation"
* "/study/{studyId}/regression"
*/
URLParser(String path) {
this.path = path;
parse();
}
private void parse() {
if (path.equalsIgnoreCase("/")) {
return;
}
if (path.startsWith("/")) {
path = path.substring(1);
}
String[] parts = path.split("/");
if (parts.length == 1) {
operation = parts[0];
return;
}
objectType = parts[0];
if ("list".equalsIgnoreCase(parts[1])) {
operation = parts[1];
} else {
if (2 < parts.length) {
operation = parts[2];
}
objectId = parts[1];
}
if (parts.length > 3) {
if ("variable".equalsIgnoreCase(parts[2]) || "variablegroup".equalsIgnoreCase(parts[2])) {
subId = parts[3];
if (parts.length > 4) {
subOperation = parts[4];
}
}
}
}
public boolean isStudyListRequest() {
return isStudy() && objectId == null;
}
public boolean isStudyRequest() {
return isStudyObject() && operation == null;
}
public boolean isStudyMetadataRequest() {
return isStudyObject() && "metadata".equalsIgnoreCase(operation);
}
public boolean isTabulationRequest() {
return isStudyObject() && "tabulation".equalsIgnoreCase(operation);
}
public boolean isVariableRequest() {
return isStudyObject() && "variable".equalsIgnoreCase(operation) && subId != null;
}
public boolean isVariableGroupListRequest() {
return isStudyObject() && "variablegroups".equalsIgnoreCase(operation);
}
public boolean isVariableGroupVariablesRequest() {
return isStudyObject() && "variablegroup".equalsIgnoreCase(operation) && subId != null && "variables".equalsIgnoreCase(subOperation);
}
public boolean isVariableGroupGroupsRequest() {
return isStudyObject() && "variablegroup".equalsIgnoreCase(operation) && subId != null && "variablegroups".equalsIgnoreCase(subOperation);
}
public boolean isVariableListRequest() {
return isStudyObject() && "variables".equalsIgnoreCase(operation);
}
public boolean isCorrelationRequest() {
return isStudyObject() && "correlation".equalsIgnoreCase(operation);
}
public boolean isRegressionRequest() {
return isStudyObject() && "regression".equalsIgnoreCase(operation);
}
public String getObjectType() {
return objectType;
}
public String getObjectId() {
return objectId;
}
public String getSubId() {
return subId;
}
public String getOperation() {
return operation;
}
public String getSubOperation() {
return subOperation;
}
private boolean isStudy() {
return "study".equalsIgnoreCase(objectType);
}
private boolean isStudyObject() {
return isStudy() && objectId != null;
}
public boolean isLogin() {
return "login".equalsIgnoreCase(operation);
}
}
This diff is collapsed.
package com.nesstar.analysis;
import java.io.IOException;
import java.util.ArrayList;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.log4j.Logger;
import com.nesstar.StaticVariables;
import com.nesstar.api.DdiList;
import com.nesstar.api.NotAuthorizedException;
import com.nesstar.api.Study;
import com.nesstar.api.Variable;
import com.nesstar.api.analysis.LinearRegressionBuilder;
import com.nesstar.api.analysis.LinearRegressionResult;
/**
* This class is used to do the regression calculation.
*
*/
public class RegressionDAO {
private static final Logger logger = Logger.getLogger(RegressionDAO.class);
/**
* Used to do the correlation calculation.
*/
private Study study;
/**
* The correlation variables.
*/
private DdiList<Variable> variables;
/**
* The result of regression.
*/
private LinearRegressionResult result;
/**
* Set the study and variables manually.
*
* @param study
* The study gonna be used
* @param variables
* The variables
*/
public void setStudyAndVariables(Study study, DdiList<Variable> variables) {
this.study = study;
this.variables = variables;
}
/**
* Calculate the regression with dependent and independent variables sent in the http request.
*
* @param request
* The Http request
* @param response
* The response
* @throws IOException
* @throws NotAuthorizedException
*/
public String calculateRegression(String data) throws NotAuthorizedException, IOException {
JSONObject jsonData = (JSONObject) JSONSerializer.toJSON(data);
Variable[] breakVariables = getIndependentVariables(jsonData);
Variable dependentVariable = getDependentVariable(jsonData);
regression(dependentVariable, breakVariables);
return new JSONObject().element("regression", packageRegressionToJSON()).toString();
}
/**
* Get the dependent variable.
*
* @param jsonData
* The variables of json type containing dependent variable
* @return The dependent variable in Variable type
* @throws NotAuthorizedException
* @throws IOException
*/
private Variable getDependentVariable(JSONObject jsonData) throws NotAuthorizedException, IOException {
Variable dependentVariable = null;
if (jsonData.has("Dependent")) {
JSONArray jsonDependentVariable = jsonData.getJSONArray("Dependent");
for (Variable var : variables) {
if (jsonDependentVariable.get(0).equals(var.getName())) {
dependentVariable = var;
} else if (jsonDependentVariable.get(0).equals(var.getLabel())) {
dependentVariable = var;
}
}
logger.info("Regression: get dependent variable");
}
return dependentVariable;
}
/**
* Get the independent variables.
*
* @param jsonData
* The variables of json type containing independent variables
* @return The independent variables in Variable type
* @throws NotAuthorizedException
* @throws IOException
*/
private Variable[] getIndependentVariables(JSONObject jsonData) throws NotAuthorizedException, IOException {
Variable[] independentVariables = null;
if (jsonData.has("Independent")) {
JSONArray jsonIndependentVariables = jsonData.getJSONArray("Independent");
independentVariables = new Variable[jsonIndependentVariables.size()];
ArrayList<Variable> listVariable = new ArrayList<Variable>();
for (int i = 0; i < jsonIndependentVariables.size(); i++) {
for (Variable var : variables) {
if (jsonIndependentVariables.get(i).equals(var.getLabel()) || jsonIndependentVariables.get(i).equals(var.getName())) {
listVariable.add(var);
}
}
}
// convert the list to array
independentVariables = listVariable.toArray(independentVariables);
logger.info("Regression: get independent variables");
}
return independentVariables;
}
/**
* Do the regression calculation with independent and dependent variables.
*
* @param dependentVariable
* The dependent variable
* @param independentVariables
* The independent variables
* @throws IOException
* @throws NotAuthorizedException
*/
private void regression(Variable dependentVariable, Variable... independentVariables) throws NotAuthorizedException, IOException {
LinearRegressionBuilder regressionBuilder = new LinearRegressionBuilder();
if (null != dependentVariable) {
regressionBuilder.setDependentVariable(dependentVariable);
}
if (null != independentVariables) {
for (Variable var : independentVariables) {
regressionBuilder.addIndependentVariable(var);
}
}
this.result = (LinearRegressionResult) study.regress(regressionBuilder);
logger.info("Regression: regression");
}
/**
* Parse the regression result to JSON type.
*
* @param regressionResult
* The regression result
* @return The result of json type
* @throws IOException
* @throws NotAuthorizedException
*/
private JSONObject packageRegressionToJSON() throws NotAuthorizedException, IOException {
JSONObject jsonResult = new JSONObject();
JSONArray jsonData = new JSONArray();
String tempVariableName = "";
for (Variable variable : this.result.getIndependentVariables()) {
// add one part of the result
jsonData.add(parseRegressionPartOne(variable));
}
jsonResult.element(StaticVariables.NESSTAR_TEXT_DATA, jsonData);
tempVariableName = this.result.getDependentVariable().getLabel();
if (null == tempVariableName || "".equals(tempVariableName)) {
jsonResult.element("DependentVariable", this.result.getDependentVariable().getName());
} else {
jsonResult.element("DependentVariable", tempVariableName);
}
jsonResult.element("Intercept", Double.toString(this.result.getIntercept()));
jsonResult.element("ValidN", Double.toString(this.result.getCaseCount()));
jsonResult.element("MultipleR", Double.toString(this.result.getR()));
jsonResult.element("MultipleRSquared", Double.toString(this.result.getrSquared()));
jsonResult.element("AdjustedRSquared", Double.toString(this.result.getAdjustedRSquared()));
jsonResult.element("F", Double.toString(this.result.getF()));
jsonResult.element("FSignificance", Double.toString(this.result.getfSignificance()));
logger.info("Regression: package regression result to JSON type");
return jsonResult;
}
private JSONObject parseRegressionPartOne(Variable variable) throws NotAuthorizedException, IOException {
JSONObject jsonObject = new JSONObject();
String tempVariableName = variable.getLabel();
if (null == tempVariableName || "".equals(tempVariableName)) {
jsonObject.element("name", variable.getName());
} else {
jsonObject.element("name", tempVariableName);
}
jsonObject.element("B", Double.toString(this.result.getUnstandarizedCoefficient(variable)));
jsonObject.element("SEB", Double.toString(this.result.getUnstandarizedCoefficientStandardError(variable)));
jsonObject.element("Beta", Double.toString(this.result.getStandarizedCoefficient(variable)));
jsonObject.element("T", Double.toString(this.result.getT(variable)));
jsonObject.element("TSignificance", Double.toString(this.result.getTSignificance(variable)));
jsonObject.element("Tolerance", Double.toString(this.result.getTolerance(variable)));
return jsonObject;
}
}
package com.nesstar.server;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.nesstar.api.NesstarDB;
import com.nesstar.api.NesstarDBFactory;
import com.nesstar.api.NesstarList;
import com.nesstar.api.NotAuthorizedException;
import com.nesstar.api.Server;
import com.nesstar.api.Study;
public class SingletonServer {
private static final Logger logger = Logger.getLogger(SingletonServer.class);
/**
* The object of nesstar database.
*/
private static NesstarDB nesstarDB;
private static Server server;
private static NesstarList<Study> studys;
private SingletonServer() {
nesstarDB = NesstarDBFactory.getInstance();
}
public static Server getServer() {
if (null == server) {
try {
server = nesstarDB.getServer(getServerURI());
} catch (IOException e) {
logger.error(e.getMessage());
}
}
return server;
}
public static NesstarList<Study> getStudys() throws NotAuthorizedException {
if (null == studys) {
server = getServer();
try {
studys = server.getBank(Study.class).getAll();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
return studys;
}
private static URI getServerURI() throws IOException {
Properties properties = new Properties();
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("nesstar.properties");
URI serverURI = null;
try {
properties.load(is);
serverURI = new URI(properties.getProperty("serverURI"));
} catch (IOException e1) {
logger.error(e1.getMessage());
throw e1;
} catch (URISyntaxException e) {
logger.error(e.getMessage());
}
return serverURI;
}
public static void setSession(String id) {
if (null == nesstarDB) {
nesstarDB = NesstarDBFactory.getInstance();
}
nesstarDB.setSession(id);
}
public static void endSession() {
nesstarDB.endSession();
}
}
package com.nesstar.study;
import java.io.IOException;
import com.nesstar.TabulateAnalysisDAO;
import com.nesstar.api.NotAuthorizedException;
import com.nesstar.api.Study;
/**
* The interface Command, used to apply Command Pattern.
*
* @author feng xue
*
*/
public interface Command {
Object execute(TabulateAnalysisDAO data, Study study, Boolean force) throws NotAuthorizedException, IOException;
}
package com.nesstar.study;
import java.io.IOException;
import org.apache.log4j.Logger;
import com.nesstar.TabulateAnalysisDAO;
import com.nesstar.api.NotAuthorizedException;
import com.nesstar.api.Study;
/**
* The thread class used to extract the details of study background.
*
* @author feng xue
*
*/
public class ExtractInformationBackgroundThread implements Runnable {
private static final Logger logger = Logger.getLogger(ExtractInformationBackgroundThread.class);
/**