Skip to content

Commit

Permalink
minor bug fixed, code clean and code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzren committed Oct 22, 2019
1 parent 5684cd2 commit 84ad05b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 62 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
Expand Down
34 changes: 15 additions & 19 deletions src/main/java/controller/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
*/
public class Search extends HttpServlet {

// it should change to use session instead, but having a issue lost session from alias site
private static String dbVersion = "v3";

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
if (request.getParameter("version") != null) {
dbVersion = request.getParameter("version");
}

List<VariantGeneScore> variantGeneScoreList = new ArrayList<>();

if (request.getParameter("query") != null) {
Expand All @@ -35,13 +42,15 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
// search by gene or region or variant
String query = request.getParameter("query").toUpperCase().replaceAll("( )+", "");

checkRegionValid(request, query);
boolean isRegionValid = checkRegionValid(request, query);

String dbVersion = initDBVersion(request);
Output.init(query, dbVersion, variantGeneScoreList);

request.setAttribute("query", query);
request.setAttribute("version", dbVersion);
request.setAttribute("isTruncated", Output.isTruncated(variantGeneScoreList));
request.setAttribute("variantGeneScoreList", variantGeneScoreList);
request.setAttribute("isRegionValid", isRegionValid);
}

final String content_type = request.getParameter("content-type");
Expand All @@ -56,30 +65,17 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
}
}

private String initDBVersion(HttpServletRequest request) {
String dbVersion = (String) request.getSession().getAttribute("version");

if (request.getParameter("version") != null) {
dbVersion = request.getParameter("version");
request.getSession().setAttribute("version", dbVersion);
}

return dbVersion;
}

private void checkRegionValid(HttpServletRequest request, String query) {
boolean isRegionValid = true;

private boolean checkRegionValid(HttpServletRequest request, String query) {
if (query.contains(":")) { // search region
String[] tmp = query.split(":"); // chr:start-end
tmp = tmp[1].split("-");
int start = Integer.valueOf(tmp[0]);
int end = Integer.valueOf(tmp[1]);

isRegionValid = Output.isRegionValid(start, end);
return Output.isRegionValid(start, end);
} else {
return true;
}

request.getSession().setAttribute("isRegionValid", isRegionValid);
}

@Override
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/model/Output.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
public class Output {

private static final int maxDisplayNum = 1000;
private static final int maxRowToQuery = 5000;
private static final int maxBaseNumToDisplay = 10000;

Expand Down Expand Up @@ -249,4 +250,14 @@ private static List<String> getEnsgGeneNameByHgnc(String hgnc, String dbVersion)

return ensgList;
}

public static boolean isTruncated(List<VariantGeneScore> variantGeneScoreList) {
if (variantGeneScoreList.size() <= maxDisplayNum) {
return false;
} else {
// hack to restrict only display 1000 rows
variantGeneScoreList = variantGeneScoreList.subList(0, maxDisplayNum);
return true;
}
}
}
46 changes: 12 additions & 34 deletions src/main/java/util/DBManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import java.sql.*;
import java.util.HashMap;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;

/**
*
* @author nick
*/
public class DBManager {

private static DataSource dataSource;
private static Connection connection;
private static Statement statement;

private static String dbUrl;
private static String dbUser;
Expand All @@ -21,36 +20,15 @@ public class DBManager {
private static String dbVersionName = "v1:vdsdb,v2:trap_v2_060117,v3:trap_v3";

public static void init() throws Exception {
initDBVersion();
if (connection == null || connection.isClosed()) {
initDBVersion();

initDataFromSystemConfig();

if (dataSource == null) {
PoolProperties p = new PoolProperties();
p.setDriverClassName("com.mysql.cj.jdbc.Driver");
p.setUrl(dbUrl);
p.setUsername(dbUser);
p.setPassword(dbPassword);
p.setJmxEnabled(true);
p.setTestWhileIdle(false);
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(100);
p.setInitialSize(10);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(10);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
p.setJdbcInterceptors(
"org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
+ "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
dataSource = new DataSource();
dataSource.setPoolProperties(p);
initDataFromSystemConfig();
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
statement = connection.createStatement();
} else if (statement.isClosed()) {
statement = connection.createStatement();
}
}

Expand Down Expand Up @@ -81,11 +59,11 @@ private static void initDataFromSystemConfig() {
}

public static ResultSet executeQuery(String sqlQuery) throws SQLException {
return dataSource.getConnection().createStatement().executeQuery(sqlQuery);
return statement.executeQuery(sqlQuery);
}

public static PreparedStatement prepareStatement(String sqlQuery) throws SQLException {
return dataSource.getConnection().prepareStatement(sqlQuery);
return connection.prepareStatement(sqlQuery);
}

public static String getDBName(String dbVersion) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
</div>

<%
String version = (String) request.getSession().getAttribute("version");
String version = (String) request.getAttribute("version");
if (version == null) {
version = "v3";
request.getSession().setAttribute("version", version);
request.setAttribute("version", version);
}
%>

Expand Down
14 changes: 12 additions & 2 deletions src/main/webapp/result.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

<%
String query = (String) request.getAttribute("query");
Boolean isRegionValid = (Boolean) request.getSession().getAttribute("isRegionValid");
Boolean isRegionValid = (Boolean) request.getAttribute("isRegionValid");
Boolean isTruncated = (Boolean) request.getAttribute("isTruncated");
List<VariantGeneScore> variantGeneScoreList
= (List<VariantGeneScore>) request.getAttribute("variantGeneScoreList");
Expand All @@ -13,7 +14,7 @@
<div class="row">
<div class="col-md-10">
<h4>
Data version: <mark><%=request.getSession().getAttribute("version")%></mark>
Data version: <mark><%=request.getAttribute("version")%></mark>
</h4>
</div>
</div>
Expand Down Expand Up @@ -46,6 +47,15 @@
</div>
<%
} else {
if (isTruncated) {
%>
<div class="alert alert-warning" style="width:90%">
<h4>
Please notice that the displayed variant list is truncated. To obtain the full database please refer to the terms of use.
</h4>
</div>
<%
}
%>
<table class="table table-striped">
<thead>
Expand Down

0 comments on commit 84ad05b

Please sign in to comment.