From 31c77556f737dde3265c7f12fdbe7b671a7371cd Mon Sep 17 00:00:00 2001 From: Jon Van Oast Date: Fri, 3 Jan 2025 10:31:53 -0700 Subject: [PATCH 1/4] close shepherd --- src/main/java/org/ecocean/EncounterQueryProcessor.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/ecocean/EncounterQueryProcessor.java b/src/main/java/org/ecocean/EncounterQueryProcessor.java index 5b020bd3b2..dd0a038833 100644 --- a/src/main/java/org/ecocean/EncounterQueryProcessor.java +++ b/src/main/java/org/ecocean/EncounterQueryProcessor.java @@ -112,11 +112,14 @@ public static String queryStringBuilder(HttpServletRequest request, StringBuffer } catch (Exception ex) { ex.printStackTrace(); return failed; + } finally { + myShepherd.rollbackAndClose(); } filter += " (catalogNumber == \"" + String.join("\") || (catalogNumber == \"", encIds) + "\")"; System.out.println("queryStringBuilder: searchQueryId=" + searchQueryId + " yielded " + encIds.size() + " matching encounters"); + myShepherd.rollbackAndClose(); return filter; } // filter for location------------------------------------------ From 035b33e2c8dbfc7c8cd68ab5ea889664f959e93e Mon Sep 17 00:00:00 2001 From: Jon Van Oast Date: Fri, 3 Jan 2025 10:42:28 -0700 Subject: [PATCH 2/4] think we dont need this --- src/main/java/org/ecocean/EncounterQueryProcessor.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/ecocean/EncounterQueryProcessor.java b/src/main/java/org/ecocean/EncounterQueryProcessor.java index dd0a038833..011265a174 100644 --- a/src/main/java/org/ecocean/EncounterQueryProcessor.java +++ b/src/main/java/org/ecocean/EncounterQueryProcessor.java @@ -119,7 +119,6 @@ public static String queryStringBuilder(HttpServletRequest request, StringBuffer encIds) + "\")"; System.out.println("queryStringBuilder: searchQueryId=" + searchQueryId + " yielded " + encIds.size() + " matching encounters"); - myShepherd.rollbackAndClose(); return filter; } // filter for location------------------------------------------ From 73cd9dd15234837046d7d180d2e4dc6f52aab453 Mon Sep 17 00:00:00 2001 From: Jon Van Oast Date: Fri, 3 Jan 2025 11:01:18 -0700 Subject: [PATCH 3/4] begin transaction --- src/main/java/org/ecocean/EncounterQueryProcessor.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/ecocean/EncounterQueryProcessor.java b/src/main/java/org/ecocean/EncounterQueryProcessor.java index 011265a174..0e0653b338 100644 --- a/src/main/java/org/ecocean/EncounterQueryProcessor.java +++ b/src/main/java/org/ecocean/EncounterQueryProcessor.java @@ -48,6 +48,7 @@ public static String queryStringBuilder(HttpServletRequest request, StringBuffer context = ServletUtilities.getContext(request); Shepherd myShepherd = new Shepherd(context); myShepherd.setAction("EncounterQueryProcessor.class"); + myShepherd.beginDBTransaction(); String searchQueryId = request.getParameter("searchQueryId"); // hack to build a ridiculous query string from an OpenSearch query From 4fb3a149777c4832ffca8112638544413acef60a Mon Sep 17 00:00:00 2001 From: Jon Van Oast Date: Fri, 3 Jan 2025 11:57:45 -0700 Subject: [PATCH 4/4] one big try/catch wrap --- .../org/ecocean/EncounterQueryProcessor.java | 2643 +++++++++-------- 1 file changed, 1336 insertions(+), 1307 deletions(-) diff --git a/src/main/java/org/ecocean/EncounterQueryProcessor.java b/src/main/java/org/ecocean/EncounterQueryProcessor.java index 0e0653b338..a1ed1a38ba 100644 --- a/src/main/java/org/ecocean/EncounterQueryProcessor.java +++ b/src/main/java/org/ecocean/EncounterQueryProcessor.java @@ -49,220 +49,152 @@ public static String queryStringBuilder(HttpServletRequest request, StringBuffer Shepherd myShepherd = new Shepherd(context); myShepherd.setAction("EncounterQueryProcessor.class"); myShepherd.beginDBTransaction(); - - String searchQueryId = request.getParameter("searchQueryId"); - // hack to build a ridiculous query string from an OpenSearch query - if (searchQueryId != null) { - String failed = filter + " 1 == 0"; - List encIds = new ArrayList(); - User user = myShepherd.getUser(request); - if (user == null) return failed; - JSONObject searchQuery = OpenSearch.queryLoad(searchQueryId); - if (searchQuery == null) return failed; - String indexName = searchQuery.optString("indexName", null); - if (indexName == null) return failed; - searchQuery = OpenSearch.queryScrubStored(searchQuery); - JSONObject sanitized = null; - try { - sanitized = OpenSearch.querySanitize(searchQuery, user, myShepherd); - } catch (IOException ex) { - ex.printStackTrace(); - // this should be unlikely, so fail hard - throw new RuntimeException("query failed"); - } - OpenSearch os = new OpenSearch(); - String sort = request.getParameter("sort"); - String sortOrder = request.getParameter("sortOrder"); - int numFrom = 0; - // for historical code reasons, this needs to be "all" encounters, - // so we hope this does the job if background indexing set correctly - int pageSize = 10000; - try { - pageSize = os.getSettings(indexName).optInt("max_result_window", 10000); - } catch (Exception ex) {} - try { - os.deletePit(indexName); - JSONObject queryRes = os.queryPit(indexName, sanitized, numFrom, pageSize, sort, - sortOrder); - JSONObject outerHits = queryRes.optJSONObject("hits"); - if (outerHits == null) { - System.out.println("could not find (outer) hits"); - return failed; - } - JSONArray hits = outerHits.optJSONArray("hits"); - if (hits == null) { - System.out.println("could not find hits"); - return failed; + // wrap all myShepherd use in try/catch block + try { + String searchQueryId = request.getParameter("searchQueryId"); + // hack to build a ridiculous query string from an OpenSearch query + if (searchQueryId != null) { + String failed = filter + " 1 == 0"; + List encIds = new ArrayList(); + User user = myShepherd.getUser(request); + if (user == null) return failed; + JSONObject searchQuery = OpenSearch.queryLoad(searchQueryId); + if (searchQuery == null) return failed; + String indexName = searchQuery.optString("indexName", null); + if (indexName == null) return failed; + searchQuery = OpenSearch.queryScrubStored(searchQuery); + JSONObject sanitized = null; + try { + sanitized = OpenSearch.querySanitize(searchQuery, user, myShepherd); + } catch (IOException ex) { + ex.printStackTrace(); + // this should be unlikely, so fail hard + throw new RuntimeException("query failed"); } - for (int i = 0; i < hits.length(); i++) { - JSONObject h = hits.optJSONObject(i); - if (h == null) { - System.out.println("failed to parse hits[" + i + "]"); + OpenSearch os = new OpenSearch(); + String sort = request.getParameter("sort"); + String sortOrder = request.getParameter("sortOrder"); + int numFrom = 0; + // for historical code reasons, this needs to be "all" encounters, + // so we hope this does the job if background indexing set correctly + int pageSize = 10000; + try { + pageSize = os.getSettings(indexName).optInt("max_result_window", 10000); + } catch (Exception ex) {} + try { + os.deletePit(indexName); + JSONObject queryRes = os.queryPit(indexName, sanitized, numFrom, pageSize, sort, + sortOrder); + JSONObject outerHits = queryRes.optJSONObject("hits"); + if (outerHits == null) { + System.out.println("could not find (outer) hits"); return failed; } - String hId = h.optString("_id", null); - if (hId == null) { - System.out.println("failed to parse _id from hits[" + i + "]"); + JSONArray hits = outerHits.optJSONArray("hits"); + if (hits == null) { + System.out.println("could not find hits"); return failed; } - // Encounter enc = myShepherd.getEncounter(hId); - boolean hasAccess = Encounter.opensearchAccess(h.optJSONObject("_source"), user, - myShepherd); - if (hasAccess) encIds.add(hId); - } - } catch (Exception ex) { - ex.printStackTrace(); - return failed; - } finally { - myShepherd.rollbackAndClose(); - } - filter += " (catalogNumber == \"" + String.join("\") || (catalogNumber == \"", - encIds) + "\")"; - System.out.println("queryStringBuilder: searchQueryId=" + searchQueryId + " yielded " + - encIds.size() + " matching encounters"); - return filter; - } - // filter for location------------------------------------------ - if ((request.getParameter("locationField") != null) && - (!request.getParameter("locationField").equals(""))) { - String locString = request.getParameter("locationField").toLowerCase().replaceAll("%20", - " ").trim(); - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += "(verbatimLocality.toLowerCase().indexOf('" + locString + "') != -1)"; - } else { - filter += " && (verbatimLocality.toLowerCase().indexOf('" + locString + "') != -1)"; - } - prettyPrint.append("locationField contains \"" + locString + "\".
"); - } - // end location filter-------------------------------------------------------------------------------------- - // filter for organization------------------- - if ((request.getParameter("organizationId") != null) && - (!request.getParameter("organizationId").equals("")) && - Util.isUUID(request.getParameter("organizationId"))) { - String orgId = request.getParameter("organizationId"); - filter = - "SELECT FROM org.ecocean.Encounter WHERE user.username == this.submitterID && org.members.contains(user) && org.id == '" - + orgId + "'"; - String variables_statement = - " VARIABLES org.ecocean.User user; org.ecocean.Organization org"; - jdoqlVariableDeclaration = addOrgVars(variables_statement, filter); - } else {} - // end filter for organization------------------ - // filter for projectName------------------- - if (Util.isUUID(request.getParameter("projectId"))) { - filter = SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE + - " proj.encounters.contains(this) && "; - String[] projectIds = request.getParameterValues("projectId"); - if ((projectIds != null) && (!projectIds[0].equals("None"))) { - prettyPrint.append("Assigned to one of the following projects: "); - int numProjIds = projectIds.length; - String projIdFilter = "("; - for (int i = 0; i < numProjIds; i++) { - String currentProjId = projectIds[i].toLowerCase().replaceAll("%20", - " ").trim(); - if (!currentProjId.equals("")) { - if (projIdFilter.equals("(")) { - projIdFilter += " proj.id == \"" + currentProjId + "\""; - } else { - projIdFilter += " || proj.id == \"" + currentProjId + "\""; + for (int i = 0; i < hits.length(); i++) { + JSONObject h = hits.optJSONObject(i); + if (h == null) { + System.out.println("failed to parse hits[" + i + "]"); + return failed; + } + String hId = h.optString("_id", null); + if (hId == null) { + System.out.println("failed to parse _id from hits[" + i + "]"); + return failed; } + // Encounter enc = myShepherd.getEncounter(hId); + boolean hasAccess = Encounter.opensearchAccess(h.optJSONObject("_source"), + user, myShepherd); + if (hasAccess) encIds.add(hId); } + } catch (Exception ex) { + ex.printStackTrace(); + return failed; } - projIdFilter += " )"; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE + - " proj.encounters.contains(this) && ")) { - filter += projIdFilter; + filter += " (catalogNumber == \"" + String.join("\") || (catalogNumber == \"", + encIds) + "\")"; + System.out.println("queryStringBuilder: searchQueryId=" + searchQueryId + + " yielded " + encIds.size() + " matching encounters"); + return filter; + } + // filter for location------------------------------------------ + if ((request.getParameter("locationField") != null) && + (!request.getParameter("locationField").equals(""))) { + String locString = request.getParameter("locationField").toLowerCase().replaceAll( + "%20", " ").trim(); + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += "(verbatimLocality.toLowerCase().indexOf('" + locString + "') != -1)"; } else { - filter += (" && " + projIdFilter); + filter += " && (verbatimLocality.toLowerCase().indexOf('" + locString + + "') != -1)"; } - prettyPrint.append(filter); - prettyPrint.append("
"); - } - String variables_statement = " VARIABLES org.ecocean.Project proj"; - jdoqlVariableDeclaration = addOrgVars(variables_statement, filter); - } else {} - // end filter for projectName------------------ - // username filters------------------------------------------------- - String[] usernames = request.getParameterValues("username"); - if ((usernames != null) && (!usernames[0].equals("None"))) { - prettyPrint.append("Assigned to one of the following usernames: "); - int kwLength = usernames.length; - String locIDFilter = "("; - for (int kwIter = 0; kwIter < kwLength; kwIter++) { - String kwParam = usernames[kwIter].replaceAll("%20", " ").trim(); - if (!kwParam.equals("")) { - if (locIDFilter.equals("(")) { - locIDFilter += " submitterID == \"" + kwParam + "\""; - } else { - locIDFilter += " || submitterID == \"" + kwParam + "\""; + prettyPrint.append("locationField contains \"" + locString + "\".
"); + } + // end location filter-------------------------------------------------------------------------------------- + // filter for organization------------------- + if ((request.getParameter("organizationId") != null) && + (!request.getParameter("organizationId").equals("")) && + Util.isUUID(request.getParameter("organizationId"))) { + String orgId = request.getParameter("organizationId"); + filter = + "SELECT FROM org.ecocean.Encounter WHERE user.username == this.submitterID && org.members.contains(user) && org.id == '" + + orgId + "'"; + String variables_statement = + " VARIABLES org.ecocean.User user; org.ecocean.Organization org"; + jdoqlVariableDeclaration = addOrgVars(variables_statement, filter); + } else {} + // end filter for organization------------------ + // filter for projectName------------------- + if (Util.isUUID(request.getParameter("projectId"))) { + filter = SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE + + " proj.encounters.contains(this) && "; + String[] projectIds = request.getParameterValues("projectId"); + if ((projectIds != null) && (!projectIds[0].equals("None"))) { + prettyPrint.append("Assigned to one of the following projects: "); + int numProjIds = projectIds.length; + String projIdFilter = "("; + for (int i = 0; i < numProjIds; i++) { + String currentProjId = projectIds[i].toLowerCase().replaceAll("%20", + " ").trim(); + if (!currentProjId.equals("")) { + if (projIdFilter.equals("(")) { + projIdFilter += " proj.id == \"" + currentProjId + "\""; + } else { + projIdFilter += " || proj.id == \"" + currentProjId + "\""; + } + } } - prettyPrint.append(kwParam + " "); - } - } - locIDFilter += " )"; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += locIDFilter; - } else { filter += (" && " + locIDFilter); } - prettyPrint.append("
"); - } - // end username filters----------------------------------------------- - // filter for resighted encounter------------------------------------------ - if (request.getParameter("resightOnly") != null) { - // String locString=request.getParameter("locationField").toLowerCase().replaceAll("%20", " ").trim(); - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += "(individual != null)"; - } else { filter += " && (individual != null)"; } - prettyPrint.append("Identified and resighted.
"); - } - // end resighted filter-------------------------------------------------------------------------------------- - // filter for unassigned encounters------------------------------------------ - if (request.getParameter("unassigned") != null) { - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += "(individual == null)"; - } else { filter += " && (individual == null)"; } - prettyPrint.append("Unidentified.
"); - } - // end unassigned filter-------------------------------------------------------------------------------------- - // locationID filters------------------------------------------------- - String[] locCodes = request.getParameterValues("locationCodeField"); - if ((locCodes != null) && (!locCodes[0].equals(""))) { - prettyPrint.append("locationCodeField is one of the following: "); - int kwLength = locCodes.length; - String locIDFilter = "("; - for (int kwIter = 0; kwIter < kwLength; kwIter++) { - String kwParam = locCodes[kwIter].replaceAll("%20", " ").trim(); - if (!kwParam.equals("")) { - if (locIDFilter.equals("(")) { - locIDFilter += " locationID == \"" + kwParam + "\""; + projIdFilter += " )"; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE + + " proj.encounters.contains(this) && ")) { + filter += projIdFilter; } else { - locIDFilter += " || locationID == \"" + kwParam + "\""; + filter += (" && " + projIdFilter); } - prettyPrint.append(kwParam + " "); + prettyPrint.append(filter); + prettyPrint.append("
"); } - } - locIDFilter += " )"; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += locIDFilter; - } else { filter += (" && " + locIDFilter); } - prettyPrint.append("
"); - } - // end locationID filters----------------------------------------------- - // annotation viewpoint and class filters------------------------------------------------- - String[] hasViewpoint = request.getParameterValues("hasViewpoint"); - String[] hasIAClass = request.getParameterValues("hasIAClass"); - if (((hasViewpoint != null) && (!hasViewpoint[0].equals(""))) || - ((hasIAClass != null) && (!hasIAClass[0].equals("")))) { - if ((hasViewpoint != null) && (!hasViewpoint[0].equals(""))) { - prettyPrint.append("Viewpoint is one of the following: "); - int kwLength = hasViewpoint.length; + String variables_statement = " VARIABLES org.ecocean.Project proj"; + jdoqlVariableDeclaration = addOrgVars(variables_statement, filter); + } else {} + // end filter for projectName------------------ + // username filters------------------------------------------------- + String[] usernames = request.getParameterValues("username"); + if ((usernames != null) && (!usernames[0].equals("None"))) { + prettyPrint.append("Assigned to one of the following usernames: "); + int kwLength = usernames.length; String locIDFilter = "("; for (int kwIter = 0; kwIter < kwLength; kwIter++) { - String kwParam = hasViewpoint[kwIter].trim(); + String kwParam = usernames[kwIter].replaceAll("%20", " ").trim(); if (!kwParam.equals("")) { if (locIDFilter.equals("(")) { - locIDFilter += " annot46.viewpoint == \"" + kwParam + "\""; + locIDFilter += " submitterID == \"" + kwParam + "\""; } else { - locIDFilter += " || annot46.viewpoint == \"" + kwParam + "\""; + locIDFilter += " || submitterID == \"" + kwParam + "\""; } prettyPrint.append(kwParam + " "); } @@ -271,19 +203,39 @@ public static String queryStringBuilder(HttpServletRequest request, StringBuffer if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { filter += locIDFilter; } else { filter += (" && " + locIDFilter); } - prettyPrint.append("
"); + prettyPrint.append("
"); + } + // end username filters----------------------------------------------- + // filter for resighted encounter------------------------------------------ + if (request.getParameter("resightOnly") != null) { + // String locString=request.getParameter("locationField").toLowerCase().replaceAll("%20", " ").trim(); + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += "(individual != null)"; + } else { filter += " && (individual != null)"; } + prettyPrint.append("Identified and resighted.
"); } - if ((hasIAClass != null) && (!hasIAClass[0].equals(""))) { - prettyPrint.append("IA class is one of the following: "); - int kwLength = hasIAClass.length; + // end resighted filter-------------------------------------------------------------------------------------- + // filter for unassigned encounters------------------------------------------ + if (request.getParameter("unassigned") != null) { + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += "(individual == null)"; + } else { filter += " && (individual == null)"; } + prettyPrint.append("Unidentified.
"); + } + // end unassigned filter-------------------------------------------------------------------------------------- + // locationID filters------------------------------------------------- + String[] locCodes = request.getParameterValues("locationCodeField"); + if ((locCodes != null) && (!locCodes[0].equals(""))) { + prettyPrint.append("locationCodeField is one of the following: "); + int kwLength = locCodes.length; String locIDFilter = "("; for (int kwIter = 0; kwIter < kwLength; kwIter++) { - String kwParam = hasIAClass[kwIter].trim(); + String kwParam = locCodes[kwIter].replaceAll("%20", " ").trim(); if (!kwParam.equals("")) { if (locIDFilter.equals("(")) { - locIDFilter += " annot46.iaClass == \"" + kwParam + "\""; + locIDFilter += " locationID == \"" + kwParam + "\""; } else { - locIDFilter += " || annot46.iaClass == \"" + kwParam + "\""; + locIDFilter += " || locationID == \"" + kwParam + "\""; } prettyPrint.append(kwParam + " "); } @@ -294,1240 +246,1317 @@ public static String queryStringBuilder(HttpServletRequest request, StringBuffer } else { filter += (" && " + locIDFilter); } prettyPrint.append("
"); } - // set the jdoql variable used by both - filter += (" && annotations.contains(annot46)"); - if (jdoqlVariableDeclaration.length() > 0) { - jdoqlVariableDeclaration += ";org.ecocean.Annotation annot46;"; - } else { - jdoqlVariableDeclaration = " VARIABLES org.ecocean.Annotation annot46"; - } - } - // end viewpoint and class filters----------------------------------------------- - // state filters------------------------------------------------- - String[] states = request.getParameterValues("state"); - if ((states != null) && (!states[0].equals("None"))) { - prettyPrint.append("State is one of the following: "); - int kwLength = states.length; - String locIDFilter = "("; - for (int kwIter = 0; kwIter < kwLength; kwIter++) { - String kwParam = states[kwIter].replaceAll("%20", " ").trim(); - if (!kwParam.equals("")) { - if (locIDFilter.equals("(")) { - locIDFilter += " state == \"" + kwParam + "\""; - } else { - locIDFilter += " || state == \"" + kwParam + "\""; + // end locationID filters----------------------------------------------- + // annotation viewpoint and class filters------------------------------------------------- + String[] hasViewpoint = request.getParameterValues("hasViewpoint"); + String[] hasIAClass = request.getParameterValues("hasIAClass"); + if (((hasViewpoint != null) && (!hasViewpoint[0].equals(""))) || + ((hasIAClass != null) && (!hasIAClass[0].equals("")))) { + if ((hasViewpoint != null) && (!hasViewpoint[0].equals(""))) { + prettyPrint.append("Viewpoint is one of the following: "); + int kwLength = hasViewpoint.length; + String locIDFilter = "("; + for (int kwIter = 0; kwIter < kwLength; kwIter++) { + String kwParam = hasViewpoint[kwIter].trim(); + if (!kwParam.equals("")) { + if (locIDFilter.equals("(")) { + locIDFilter += " annot46.viewpoint == \"" + kwParam + "\""; + } else { + locIDFilter += " || annot46.viewpoint == \"" + kwParam + "\""; + } + prettyPrint.append(kwParam + " "); + } } - prettyPrint.append(kwParam + " "); + locIDFilter += " )"; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += locIDFilter; + } else { filter += (" && " + locIDFilter); } + prettyPrint.append("
"); } - } - locIDFilter += " )"; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += locIDFilter; - } else { filter += (" && " + locIDFilter); } - prettyPrint.append("
"); - } - // end state filters----------------------------------------------- - // individualID filters------------------------------------------------- - // supports multiple individualID parameters as well as comma-separated lists of individualIDs within them - String individualID = request.getParameter("individualID"); - if ((individualID != null) && (!individualID.equals("None")) && - (!individualID.trim().equals(""))) { - prettyPrint.append("Individual ID contains the following: "); - - String locIDFilter = " (individual.individualID == \"" + individualID + - "\" || individual.names.valuesAsString.toLowerCase().indexOf(\"" + - individualID.toLowerCase() + "\") != -1) "; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += locIDFilter; - } else { filter += (" && " + locIDFilter); } - prettyPrint.append("
"); - } - // end individualID filters----------------------------------------------- - // occurrenceID filters------------------------------------------------- - String occurrenceID = request.getParameter("occurrenceID"); - if ((occurrenceID != null) && (!occurrenceID.equals("None")) && - (!occurrenceID.trim().equals(""))) { - prettyPrint.append("occurrence ID contains the following: "); - - String locIDFilter = " occurrenceID == \"" + occurrenceID + "\""; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += locIDFilter; - } else { filter += (" && " + locIDFilter); } - prettyPrint.append("
"); - } - // end occurrenceID filters----------------------------------------------- - // individualIDExact filters------------------------------------------------- - // supports one individualID parameter as well as comma-separated lists of individualIDs within them - String individualIDExact = request.getParameter("individualIDExact"); - if ((individualIDExact != null) && (!individualIDExact.trim().equals(""))) { - prettyPrint.append("Individual ID is exactly the following: "); - - String locIDFilter = " individual.individualID == \"" + individualIDExact.trim() + - "\" "; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += locIDFilter; - } else { filter += (" && " + locIDFilter); } - prettyPrint.append("
"); - } - // end individualID filters----------------------------------------------- - // socialunit filters------------------------------------------------------------------ - // community search - /* - * to filter Encounnter based on community firstly we need to get the social units associated with the communities and get the individualIDs - * from the social units and then use these individualIDs to filter the Encounters - */ - if (request.getParameterValues("community") != null) { - String[] communities = request.getParameterValues("community"); - int numCommunities = communities.length; - prettyPrint.append("Social unit is one of the following: "); - - Set individualsIdsSet = new HashSet(); - for (int i = 0; i < numCommunities; i++) { - prettyPrint.append(communities[i] + " "); - - SocialUnit su = myShepherd.getSocialUnit(communities[i]); - if (su == null) continue; - for (Membership member : su.getAllMembers()) { - if (member.getMarkedIndividual() != null) { - individualsIdsSet.add(member.getMarkedIndividual().getId()); + if ((hasIAClass != null) && (!hasIAClass[0].equals(""))) { + prettyPrint.append("IA class is one of the following: "); + int kwLength = hasIAClass.length; + String locIDFilter = "("; + for (int kwIter = 0; kwIter < kwLength; kwIter++) { + String kwParam = hasIAClass[kwIter].trim(); + if (!kwParam.equals("")) { + if (locIDFilter.equals("(")) { + locIDFilter += " annot46.iaClass == \"" + kwParam + "\""; + } else { + locIDFilter += " || annot46.iaClass == \"" + kwParam + "\""; + } + prettyPrint.append(kwParam + " "); + } } + locIDFilter += " )"; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += locIDFilter; + } else { filter += (" && " + locIDFilter); } + prettyPrint.append("
"); + } + // set the jdoql variable used by both + filter += (" && annotations.contains(annot46)"); + if (jdoqlVariableDeclaration.length() > 0) { + jdoqlVariableDeclaration += ";org.ecocean.Annotation annot46;"; + } else { + jdoqlVariableDeclaration = " VARIABLES org.ecocean.Annotation annot46"; } } - List individualsIds = new ArrayList(individualsIdsSet); - if (individualsIds.size() > 0) { - String locIDFilter = " ( individual.individualID == \"" + individualsIds.get(0) + - "\" "; - for (int j = 1; j < individualsIds.size(); j++) { - locIDFilter += " || individual.individualID == \"" + individualsIds.get(j) + - "\" "; + // end viewpoint and class filters----------------------------------------------- + // state filters------------------------------------------------- + String[] states = request.getParameterValues("state"); + if ((states != null) && (!states[0].equals("None"))) { + prettyPrint.append("State is one of the following: "); + int kwLength = states.length; + String locIDFilter = "("; + for (int kwIter = 0; kwIter < kwLength; kwIter++) { + String kwParam = states[kwIter].replaceAll("%20", " ").trim(); + if (!kwParam.equals("")) { + if (locIDFilter.equals("(")) { + locIDFilter += " state == \"" + kwParam + "\""; + } else { + locIDFilter += " || state == \"" + kwParam + "\""; + } + prettyPrint.append(kwParam + " "); + } } - locIDFilter = locIDFilter + " ) "; + locIDFilter += " )"; if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { filter += locIDFilter; } else { filter += (" && " + locIDFilter); } + prettyPrint.append("
"); } - prettyPrint.append("
"); - } - // role search - /* - * to filter Encounnter based on role fetch all the MarkedIndividuals for each MarkedIndividuals gets all roles then check if requested role - * is not present remove that MarkedIndividuals finally use these individualIDs to filter the Encounters - */ - if (request.getParameterValues("role") != null) { - boolean orRoles = true; - Iterator allSharks = myShepherd.getAllMarkedIndividuals(); - Vector rIndividuals = new Vector(); - if (allSharks != null) { - while (allSharks.hasNext()) { - MarkedIndividual temp_shark = allSharks.next(); - rIndividuals.add(temp_shark); - } - } - if (request.getParameter("andRoles") != null) { orRoles = false; } - String[] roles = request.getParameterValues("role"); - int numRoles = roles.length; - if (!orRoles) { - prettyPrint.append("Social roles include all of the following: "); - } else { - prettyPrint.append("Social roles is one of the following: "); - } - for (int h = 0; h < numRoles; h++) { - prettyPrint.append(roles[h] + " "); + // end state filters----------------------------------------------- + // individualID filters------------------------------------------------- + // supports multiple individualID parameters as well as comma-separated lists of individualIDs within them + String individualID = request.getParameter("individualID"); + if ((individualID != null) && (!individualID.equals("None")) && + (!individualID.trim().equals(""))) { + prettyPrint.append("Individual ID contains the following: "); + + String locIDFilter = " (individual.individualID == \"" + individualID + + "\" || individual.names.valuesAsString.toLowerCase().indexOf(\"" + + individualID.toLowerCase() + "\") != -1) "; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += locIDFilter; + } else { filter += (" && " + locIDFilter); } + prettyPrint.append("
"); } - // logical OR the roles - for (int q = 0; q < rIndividuals.size(); q++) { - MarkedIndividual tShark = (MarkedIndividual)rIndividuals.get(q); - List myRoles = myShepherd.getAllRoleNamesForMarkedIndividual( - tShark.getIndividualID()); - if (orRoles) { - // logical OR the role - boolean hasRole = false; - int f = 0; - while (!hasRole && (f < numRoles)) { - if (myRoles.contains(roles[f])) { hasRole = true; } - f++; - } - if (!hasRole) { - rIndividuals.remove(q); - q--; - } - } else { - // logical AND the roles - boolean hasRole = true; - int f = 0; - while (hasRole && (f < numRoles)) { - if (!myRoles.contains(roles[f])) { hasRole = false; } - f++; - } - if (!hasRole) { - rIndividuals.remove(q); - q--; - } - } + // end individualID filters----------------------------------------------- + // occurrenceID filters------------------------------------------------- + String occurrenceID = request.getParameter("occurrenceID"); + if ((occurrenceID != null) && (!occurrenceID.equals("None")) && + (!occurrenceID.trim().equals(""))) { + prettyPrint.append("occurrence ID contains the following: "); + + String locIDFilter = " occurrenceID == \"" + occurrenceID + "\""; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += locIDFilter; + } else { filter += (" && " + locIDFilter); } + prettyPrint.append("
"); } - if (rIndividuals.size() > 0) { - String locIDFilter = " ( individual.individualID == \"" + - rIndividuals.get(0).getId() + "\" "; - for (int j = 1; j < rIndividuals.size(); j++) { - locIDFilter += " || individual.individualID == \"" + - rIndividuals.get(j).getId() + "\" "; - } - locIDFilter = locIDFilter + " ) "; + // end occurrenceID filters----------------------------------------------- + // individualIDExact filters------------------------------------------------- + // supports one individualID parameter as well as comma-separated lists of individualIDs within them + String individualIDExact = request.getParameter("individualIDExact"); + if ((individualIDExact != null) && (!individualIDExact.trim().equals(""))) { + prettyPrint.append("Individual ID is exactly the following: "); + + String locIDFilter = " individual.individualID == \"" + individualIDExact.trim() + + "\" "; if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { filter += locIDFilter; } else { filter += (" && " + locIDFilter); } + prettyPrint.append("
"); } - prettyPrint.append("
"); - } - // patterningCode filters------------------------------------------------- - String[] patterningCodes = request.getParameterValues("patterningCodeField"); - if ((patterningCodes != null) && (!patterningCodes[0].equals("None"))) { - prettyPrint.append("Patterning code is one of the following: "); - int kwLength = patterningCodes.length; - String patterningCodeFilter = "("; - for (int kwIter = 0; kwIter < kwLength; kwIter++) { - String kwParam = patterningCodes[kwIter].replaceAll("%20", " ").trim(); - if (!kwParam.equals("")) { - if (patterningCodeFilter.equals("(")) { - patterningCodeFilter += " patterningCode == \"" + kwParam + "\""; - } else { - patterningCodeFilter += " || patterningCode == \"" + kwParam + "\""; + // end individualID filters----------------------------------------------- + // socialunit filters------------------------------------------------------------------ + // community search + /* + * to filter Encounnter based on community firstly we need to get the social units associated with the communities and get the individualIDs + * from the social units and then use these individualIDs to filter the Encounters + */ + if (request.getParameterValues("community") != null) { + String[] communities = request.getParameterValues("community"); + int numCommunities = communities.length; + prettyPrint.append("Social unit is one of the following: "); + + Set individualsIdsSet = new HashSet(); + for (int i = 0; i < numCommunities; i++) { + prettyPrint.append(communities[i] + " "); + + SocialUnit su = myShepherd.getSocialUnit(communities[i]); + if (su == null) continue; + for (Membership member : su.getAllMembers()) { + if (member.getMarkedIndividual() != null) { + individualsIdsSet.add(member.getMarkedIndividual().getId()); + } } - prettyPrint.append(kwParam + " "); } - } - patterningCodeFilter += " )"; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += patterningCodeFilter; - } else { filter += (" && " + patterningCodeFilter); } - prettyPrint.append("
"); - } - // end patterningCode filters----------------------------------------------- - // behavior filters------------------------------------------------- - String[] behaviors = request.getParameterValues("behaviorField"); - if ((behaviors != null) && (!behaviors[0].equals("None"))) { - prettyPrint.append("behaviorField is one of the following: "); - int kwLength = behaviors.length; - String locIDFilter = "("; - for (int kwIter = 0; kwIter < kwLength; kwIter++) { - String kwParam = behaviors[kwIter].replaceAll("%20", " ").trim(); - if (!kwParam.equals("")) { - if (locIDFilter.equals("(")) { - locIDFilter += " behavior == \"" + kwParam + "\""; - } else { - locIDFilter += " || behavior == \"" + kwParam + "\""; + List individualsIds = new ArrayList(individualsIdsSet); + if (individualsIds.size() > 0) { + String locIDFilter = " ( individual.individualID == \"" + + individualsIds.get(0) + "\" "; + for (int j = 1; j < individualsIds.size(); j++) { + locIDFilter += " || individual.individualID == \"" + individualsIds.get(j) + + "\" "; } - prettyPrint.append(kwParam + " "); + locIDFilter = locIDFilter + " ) "; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += locIDFilter; + } else { filter += (" && " + locIDFilter); } } + prettyPrint.append("
"); } - locIDFilter += " )"; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += locIDFilter; - } else { filter += (" && " + locIDFilter); } - prettyPrint.append("
"); - } - // end behavior filters----------------------------------------------- - // begin observation filters ----------------------------------------- - boolean hasValue = false; - if (request.getParameter("numSearchedObs") != null) { - if (request.getParameter("observationKey1") != null && - !request.getParameter("observationKey1").equals("")) { - hasValue = true; - } - } - Enumeration allParams = request.getParameterNames(); - if (allParams != null && hasValue) { - String keyID = "observationKey"; - String valID = "observationValue"; - HashMap obKeys = new HashMap<>(); - HashMap obVals = new HashMap<>(); - StringBuilder obQuery = new StringBuilder(); - int numObsSearched = 0; - while (allParams.hasMoreElements()) { - String thisParam = allParams.nextElement(); - if (thisParam != null && thisParam.startsWith(keyID)) { - numObsSearched++; - System.out.println("Num Obs Searched? " + numObsSearched); - String keyParam = request.getParameter(thisParam); - String keyNum = thisParam.replace(keyID, ""); - if (keyParam != null && !keyParam.equals("")) { - obKeys.put(keyNum, keyParam); + // role search + /* + * to filter Encounnter based on role fetch all the MarkedIndividuals for each MarkedIndividuals gets all roles then check if requested role + * is not present remove that MarkedIndividuals finally use these individualIDs to filter the Encounters + */ + if (request.getParameterValues("role") != null) { + boolean orRoles = true; + Iterator allSharks = myShepherd.getAllMarkedIndividuals(); + Vector rIndividuals = new Vector(); + if (allSharks != null) { + while (allSharks.hasNext()) { + MarkedIndividual temp_shark = allSharks.next(); + rIndividuals.add(temp_shark); } } - if (thisParam != null && thisParam.startsWith(valID)) { - String valParam = request.getParameter(thisParam); - String valNum = thisParam.replace(valID, ""); - if (valParam != null && !valParam.equals("")) { - obVals.put(valNum, valParam); - } + if (request.getParameter("andRoles") != null) { orRoles = false; } + String[] roles = request.getParameterValues("role"); + int numRoles = roles.length; + if (!orRoles) { + prettyPrint.append("Social roles include all of the following: "); + } else { + prettyPrint.append("Social roles is one of the following: "); } - } - for (int i = 1; i <= numObsSearched; i++) { - String num = String.valueOf(i); - if (Util.basicSanitize(obKeys.get(num)) != null) { - String thisKey = Util.basicSanitize(obKeys.get(num)); - prettyPrint.append("observation "); - prettyPrint.append(thisKey); - prettyPrint.append("
"); - String qAsString = obQuery.toString().trim(); - System.out.println("Query As String" + qAsString); - if (qAsString.length() >= 2 && - !qAsString.substring(qAsString.length() - 2).equals("&&")) { - obQuery.append("&&"); - } - obQuery.append("(observations.contains(observation" + num + ") && "); - obQuery.append("observation" + num + ".name == " + Util.quote(thisKey.trim())); - if (obVals.get(num) != null && !obVals.get(num).trim().equals("")) { - String thisVal = Util.basicSanitize(obVals.get(num)); - prettyPrint.append(" is "); - prettyPrint.append(thisVal); - obQuery.append(" && observation" + num + ".value == " + - Util.quote(thisVal.trim())); - } - obQuery.append(")"); + for (int h = 0; h < numRoles; h++) { + prettyPrint.append(roles[h] + " "); } - if (obQuery.length() > 0) { - if (!filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += " && "; + // logical OR the roles + for (int q = 0; q < rIndividuals.size(); q++) { + MarkedIndividual tShark = (MarkedIndividual)rIndividuals.get(q); + List myRoles = myShepherd.getAllRoleNamesForMarkedIndividual( + tShark.getIndividualID()); + if (orRoles) { + // logical OR the role + boolean hasRole = false; + int f = 0; + while (!hasRole && (f < numRoles)) { + if (myRoles.contains(roles[f])) { hasRole = true; } + f++; + } + if (!hasRole) { + rIndividuals.remove(q); + q--; + } + } else { + // logical AND the roles + boolean hasRole = true; + int f = 0; + while (hasRole && (f < numRoles)) { + if (!myRoles.contains(roles[f])) { hasRole = false; } + f++; + } + if (!hasRole) { + rIndividuals.remove(q); + q--; + } } - filter += obQuery.toString(); - for (int j = 0; j < numObsSearched; j++) { - QueryProcessor.updateJdoqlVariableDeclaration(jdoqlVariableDeclaration, - "org.ecocean.Observation observation" + j); + } + if (rIndividuals.size() > 0) { + String locIDFilter = " ( individual.individualID == \"" + + rIndividuals.get(0).getId() + "\" "; + for (int j = 1; j < rIndividuals.size(); j++) { + locIDFilter += " || individual.individualID == \"" + + rIndividuals.get(j).getId() + "\" "; } - System.out.println("ObQuery: " + obQuery); - System.out.println("Filter? " + filter); + locIDFilter = locIDFilter + " ) "; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += locIDFilter; + } else { filter += (" && " + locIDFilter); } } + prettyPrint.append("
"); } - } - // Tag Filters-------------------------------------------------------- - - StringBuilder metalTagFilter = new StringBuilder(); - Enumeration parameterNames = request.getParameterNames(); - int metalTagsInQuery = 0; - while (parameterNames.hasMoreElements()) { - String parameterName = parameterNames.nextElement(); - final String metalTagPrefix = "metalTag("; - if (parameterName.startsWith(metalTagPrefix)) { - String metalTagLocation = parameterName.substring(metalTagPrefix.length(), - parameterName.lastIndexOf(')')); - String value = request.getParameter(parameterName); - if (value != null && value.trim().length() > 0) { - prettyPrint.append("metal tag "); - prettyPrint.append(metalTagLocation); - prettyPrint.append(" is "); - prettyPrint.append(value); - prettyPrint.append("
"); - String metalTagVar = "metalTag" + metalTagsInQuery++; - metalTagFilter.append("(metalTags.contains(" + metalTagVar + ") && "); - metalTagFilter.append(metalTagVar + ".location == " + - Util.quote(metalTagLocation)); - String jdoParam = "tagNumber" + metalTagsInQuery; - metalTagFilter.append(" && " + metalTagVar + ".tagNumber == " + jdoParam + ")"); - paramMap.put(jdoParam, value); - parameterDeclaration = updateParametersDeclaration(parameterDeclaration, - "String " + jdoParam); + // patterningCode filters------------------------------------------------- + String[] patterningCodes = request.getParameterValues("patterningCodeField"); + if ((patterningCodes != null) && (!patterningCodes[0].equals("None"))) { + prettyPrint.append("Patterning code is one of the following: "); + int kwLength = patterningCodes.length; + String patterningCodeFilter = "("; + for (int kwIter = 0; kwIter < kwLength; kwIter++) { + String kwParam = patterningCodes[kwIter].replaceAll("%20", " ").trim(); + if (!kwParam.equals("")) { + if (patterningCodeFilter.equals("(")) { + patterningCodeFilter += " patterningCode == \"" + kwParam + "\""; + } else { + patterningCodeFilter += " || patterningCode == \"" + kwParam + "\""; + } + prettyPrint.append(kwParam + " "); + } } + patterningCodeFilter += " )"; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += patterningCodeFilter; + } else { filter += (" && " + patterningCodeFilter); } + prettyPrint.append("
"); } - } - if (metalTagFilter.length() > 0) { - if (!filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += " && "; - } - filter += metalTagFilter.toString(); - for (int i = 0; i < metalTagsInQuery; i++) { - QueryProcessor.updateJdoqlVariableDeclaration(jdoqlVariableDeclaration, - "org.ecocean.tag.MetalTag metalTag" + i); - } - } - // We don't do metal tags (above) in processTagFilters because of the dependency on jdoqlVariableDeclaration - String tagFilters = processTagFilters(request, prettyPrint); - if (tagFilters.length() > 0) { - if (!filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += " && "; - } - filter += tagFilters.toString(); - } - // end tag filters---------------------------------------------------- - // lifeStage filters------------------------------------------------- - String[] stages = request.getParameterValues("lifeStageField"); - if ((stages != null) && (!stages[0].equals("None")) && (!stages[0].equals(""))) { - prettyPrint.append("lifeStage is one of the following: "); - int kwLength = stages.length; - String stageFilter = "("; - for (int kwIter = 0; kwIter < kwLength; kwIter++) { - String kwParam = stages[kwIter].replaceAll("%20", " ").trim(); - if (!kwParam.equals("")) { - if (stageFilter.equals("(")) { - stageFilter += " lifeStage == \"" + kwParam + "\""; - } else { - stageFilter += " || lifeStage == \"" + kwParam + "\""; + // end patterningCode filters----------------------------------------------- + // behavior filters------------------------------------------------- + String[] behaviors = request.getParameterValues("behaviorField"); + if ((behaviors != null) && (!behaviors[0].equals("None"))) { + prettyPrint.append("behaviorField is one of the following: "); + int kwLength = behaviors.length; + String locIDFilter = "("; + for (int kwIter = 0; kwIter < kwLength; kwIter++) { + String kwParam = behaviors[kwIter].replaceAll("%20", " ").trim(); + if (!kwParam.equals("")) { + if (locIDFilter.equals("(")) { + locIDFilter += " behavior == \"" + kwParam + "\""; + } else { + locIDFilter += " || behavior == \"" + kwParam + "\""; + } + prettyPrint.append(kwParam + " "); } - prettyPrint.append(kwParam + " "); } + locIDFilter += " )"; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += locIDFilter; + } else { filter += (" && " + locIDFilter); } + prettyPrint.append("
"); } - stageFilter += " ) "; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += stageFilter; - } else { filter += (" && " + stageFilter); } - prettyPrint.append("
"); - } - // end lifeStage filters - // country filters------------------------------------------------- - String[] countries = request.getParameterValues("country"); - if ((countries != null) && (!countries[0].equals("None")) && (!countries[0].equals(""))) { - prettyPrint.append("Country is one of the following: "); - int kwLength = countries.length; - String stageFilter = "("; - for (int kwIter = 0; kwIter < kwLength; kwIter++) { - String kwParam = countries[kwIter].replaceAll("%20", " ").trim(); - if (!kwParam.equals("")) { - if (stageFilter.equals("(")) { - stageFilter += " country == \"" + kwParam + "\""; - } else { - stageFilter += " || country == \"" + kwParam + "\""; - } - prettyPrint.append(kwParam + " "); + // end behavior filters----------------------------------------------- + // begin observation filters ----------------------------------------- + boolean hasValue = false; + if (request.getParameter("numSearchedObs") != null) { + if (request.getParameter("observationKey1") != null && + !request.getParameter("observationKey1").equals("")) { + hasValue = true; } } - stageFilter += " ) "; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += stageFilter; - } else { filter += (" && " + stageFilter); } - prettyPrint.append("
"); - } - // end country filters - // Measurement filters----------------------------------------------- - List measurementDescs = Util.findMeasurementDescs("en", context); - String measurementPrefix = "measurement"; - StringBuilder measurementFilter = new StringBuilder(); // "( collectedData.contains(measurement) && ("); - boolean atLeastOneMeasurement = false; - int measurementsInQuery = 0; - for (MeasurementDesc measurementDesc : measurementDescs) { - String valueParamName = measurementPrefix + measurementDesc.getType() + "(value)"; - String value = request.getParameter(valueParamName); - if (value != null) { - value = value.trim(); - if (value.length() > 0) { - String operatorParamName = measurementPrefix + measurementDesc.getType() + - "(operator)"; - String operatorParamValue = request.getParameter(operatorParamName); - if (operatorParamValue == null) { - operatorParamValue = ""; + Enumeration allParams = request.getParameterNames(); + if (allParams != null && hasValue) { + String keyID = "observationKey"; + String valID = "observationValue"; + HashMap obKeys = new HashMap<>(); + HashMap obVals = new HashMap<>(); + StringBuilder obQuery = new StringBuilder(); + int numObsSearched = 0; + while (allParams.hasMoreElements()) { + String thisParam = allParams.nextElement(); + if (thisParam != null && thisParam.startsWith(keyID)) { + numObsSearched++; + System.out.println("Num Obs Searched? " + numObsSearched); + String keyParam = request.getParameter(thisParam); + String keyNum = thisParam.replace(keyID, ""); + if (keyParam != null && !keyParam.equals("")) { + obKeys.put(keyNum, keyParam); + } } - String operator = null; - if ("gt".equals(operatorParamValue)) { - operator = ">"; - } else if ("lt".equals(operatorParamValue)) { - operator = "<"; - } else if ("eq".equals(operatorParamValue)) { - operator = "=="; - } else if ("gteq".equals(operatorParamValue)) { - operator = ">="; - } else if ("lteq".equals(operatorParamValue)) { - operator = "<="; + if (thisParam != null && thisParam.startsWith(valID)) { + String valParam = request.getParameter(thisParam); + String valNum = thisParam.replace(valID, ""); + if (valParam != null && !valParam.equals("")) { + obVals.put(valNum, valParam); + } } - if (operator != null) { - prettyPrint.append(measurementDesc.getUnitsLabel()); - prettyPrint.append(" is "); - prettyPrint.append(operator); - prettyPrint.append(value); + } + for (int i = 1; i <= numObsSearched; i++) { + String num = String.valueOf(i); + if (Util.basicSanitize(obKeys.get(num)) != null) { + String thisKey = Util.basicSanitize(obKeys.get(num)); + prettyPrint.append("observation "); + prettyPrint.append(thisKey); prettyPrint.append("
"); - if (atLeastOneMeasurement) { - measurementFilter.append("&&"); + String qAsString = obQuery.toString().trim(); + System.out.println("Query As String" + qAsString); + if (qAsString.length() >= 2 && + !qAsString.substring(qAsString.length() - 2).equals("&&")) { + obQuery.append("&&"); + } + obQuery.append("(observations.contains(observation" + num + ") && "); + obQuery.append("observation" + num + ".name == " + + Util.quote(thisKey.trim())); + if (obVals.get(num) != null && !obVals.get(num).trim().equals("")) { + String thisVal = Util.basicSanitize(obVals.get(num)); + prettyPrint.append(" is "); + prettyPrint.append(thisVal); + obQuery.append(" && observation" + num + ".value == " + + Util.quote(thisVal.trim())); } - String measurementVar = "measurement" + measurementsInQuery++; - measurementFilter.append("measurements.contains(" + measurementVar + - ") && "); - measurementFilter.append("(" + measurementVar + ".value " + operator + " " + - value + ")"); - measurementFilter.append(" && (" + measurementVar + ".type == "); - measurementFilter.append("\"" + measurementDesc.getType() + "\")"); - atLeastOneMeasurement = true; + obQuery.append(")"); + } + if (obQuery.length() > 0) { + if (!filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += " && "; + } + filter += obQuery.toString(); + for (int j = 0; j < numObsSearched; j++) { + QueryProcessor.updateJdoqlVariableDeclaration(jdoqlVariableDeclaration, + "org.ecocean.Observation observation" + j); + } + System.out.println("ObQuery: " + obQuery); + System.out.println("Filter? " + filter); } } } - } - if (atLeastOneMeasurement) { - if (jdoqlVariableDeclaration.length() > 0) { - jdoqlVariableDeclaration += ";"; - } else { - jdoqlVariableDeclaration = " VARIABLES "; - } - for (int i = 0; i < measurementsInQuery; i++) { - if (i > 0) { - jdoqlVariableDeclaration += "; "; - } - jdoqlVariableDeclaration += " org.ecocean.Measurement measurement" + i; - } - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += measurementFilter.toString(); - } else { - filter += (" && " + measurementFilter.toString()); - } - } - // end measurement filters - // BiologicalMeasurement filters----------------------------------------------- - List bioMeasurementDescs = Util.findBiologicalMeasurementDescs("en", - context); - String bioMeasurementPrefix = "biomeasurement"; - StringBuilder bioMeasurementFilter = new StringBuilder(); - bioMeasurementFilter.append("tissueSamples.contains(dce322) "); - boolean bioAtLeastOneMeasurement = false; - int bioMeasurementsInQuery = 0; - for (MeasurementDesc measurementDesc : bioMeasurementDescs) { - String valueParamName = bioMeasurementPrefix + measurementDesc.getType() + "(value)"; - String value = request.getParameter(valueParamName); - if (value != null) { - value = value.trim(); - if (value.length() > 0) { - String operatorParamName = bioMeasurementPrefix + measurementDesc.getType() + - "(operator)"; - String operatorParamValue = request.getParameter(operatorParamName); - if (operatorParamValue == null) { - operatorParamValue = ""; - } - String operator = null; - if ("gt".equals(operatorParamValue)) { - operator = ">"; - } else if ("lt".equals(operatorParamValue)) { - operator = "<"; - } else if ("eq".equals(operatorParamValue)) { - operator = "=="; - } else if ("gteq".equals(operatorParamValue)) { - operator = ">="; - } else if ("lteq".equals(operatorParamValue)) { - operator = "<="; - } - if (operator != null) { - prettyPrint.append("Biological/chemical measurement " + - measurementDesc.getType()); + // Tag Filters-------------------------------------------------------- + + StringBuilder metalTagFilter = new StringBuilder(); + Enumeration parameterNames = request.getParameterNames(); + int metalTagsInQuery = 0; + while (parameterNames.hasMoreElements()) { + String parameterName = parameterNames.nextElement(); + final String metalTagPrefix = "metalTag("; + if (parameterName.startsWith(metalTagPrefix)) { + String metalTagLocation = parameterName.substring(metalTagPrefix.length(), + parameterName.lastIndexOf(')')); + String value = request.getParameter(parameterName); + if (value != null && value.trim().length() > 0) { + prettyPrint.append("metal tag "); + prettyPrint.append(metalTagLocation); prettyPrint.append(" is "); - prettyPrint.append(operator); prettyPrint.append(value); prettyPrint.append("
"); - if (bioAtLeastOneMeasurement) { - bioMeasurementFilter.append("&&"); - } - String measurementVar = "biomeasurement" + bioMeasurementsInQuery++; - bioMeasurementFilter.append(" & dce322.analyses.contains(" + - measurementVar + ") "); - bioMeasurementFilter.append(" && ( " + measurementVar + ".value " + - operator + " " + value + " )"); - bioMeasurementFilter.append(" && ( " + measurementVar + - ".measurementType == "); - bioMeasurementFilter.append("\"" + measurementDesc.getType() + "\" )"); - bioAtLeastOneMeasurement = true; + String metalTagVar = "metalTag" + metalTagsInQuery++; + metalTagFilter.append("(metalTags.contains(" + metalTagVar + ") && "); + metalTagFilter.append(metalTagVar + ".location == " + + Util.quote(metalTagLocation)); + String jdoParam = "tagNumber" + metalTagsInQuery; + metalTagFilter.append(" && " + metalTagVar + ".tagNumber == " + jdoParam + + ")"); + paramMap.put(jdoParam, value); + parameterDeclaration = updateParametersDeclaration(parameterDeclaration, + "String " + jdoParam); } } } - } - if (bioAtLeastOneMeasurement) { - if (jdoqlVariableDeclaration.length() > 0) { - jdoqlVariableDeclaration += ";org.ecocean.genetics.TissueSample dce322;"; - } else { - jdoqlVariableDeclaration = " VARIABLES org.ecocean.genetics.TissueSample dce322;"; - } - for (int i = 0; i < bioMeasurementsInQuery; i++) { - if (i > 0) { - jdoqlVariableDeclaration += "; "; + if (metalTagFilter.length() > 0) { + if (!filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += " && "; + } + filter += metalTagFilter.toString(); + for (int i = 0; i < metalTagsInQuery; i++) { + QueryProcessor.updateJdoqlVariableDeclaration(jdoqlVariableDeclaration, + "org.ecocean.tag.MetalTag metalTag" + i); } - jdoqlVariableDeclaration += - "org.ecocean.genetics.BiologicalMeasurement biomeasurement" + i; - } - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += bioMeasurementFilter.toString(); - } else { - filter += (" && " + bioMeasurementFilter.toString()); } - } - // end BiologicalMeasurement filters - // verbatimEventDate filters------------------------------------------------- - String[] verbatimEventDates = request.getParameterValues("verbatimEventDateField"); - if ((verbatimEventDates != null) && (!verbatimEventDates[0].equals("None"))) { - prettyPrint.append("verbatimEventDateField is one of the following: "); - int kwLength = verbatimEventDates.length; - String locIDFilter = "("; - for (int kwIter = 0; kwIter < kwLength; kwIter++) { - String kwParam = verbatimEventDates[kwIter].replaceAll("%20", " ").trim(); - if (!kwParam.equals("")) { - if (locIDFilter.equals("(")) { - locIDFilter += " verbatimEventDate == \"" + kwParam + "\""; - } else { - locIDFilter += " || verbatimEventDate == \"" + kwParam + "\""; + // We don't do metal tags (above) in processTagFilters because of the dependency on jdoqlVariableDeclaration + String tagFilters = processTagFilters(request, prettyPrint); + if (tagFilters.length() > 0) { + if (!filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += " && "; + } + filter += tagFilters.toString(); + } + // end tag filters---------------------------------------------------- + // lifeStage filters------------------------------------------------- + String[] stages = request.getParameterValues("lifeStageField"); + if ((stages != null) && (!stages[0].equals("None")) && (!stages[0].equals(""))) { + prettyPrint.append("lifeStage is one of the following: "); + int kwLength = stages.length; + String stageFilter = "("; + for (int kwIter = 0; kwIter < kwLength; kwIter++) { + String kwParam = stages[kwIter].replaceAll("%20", " ").trim(); + if (!kwParam.equals("")) { + if (stageFilter.equals("(")) { + stageFilter += " lifeStage == \"" + kwParam + "\""; + } else { + stageFilter += " || lifeStage == \"" + kwParam + "\""; + } + prettyPrint.append(kwParam + " "); } - prettyPrint.append(kwParam + " "); } + stageFilter += " ) "; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += stageFilter; + } else { filter += (" && " + stageFilter); } + prettyPrint.append("
"); } - locIDFilter += " )"; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += locIDFilter; - } else { filter += (" && " + locIDFilter); } - prettyPrint.append("
"); - } - // end verbatimEventDate filters----------------------------------------------- - // hasTissueSample filters------------------------------------------------- - if (request.getParameter("hasTissueSample") != null) { - prettyPrint.append("Has tissue sample."); - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += "tissueSamples.contains(dce)"; - } else if (filter.indexOf("tissueSamples.contains(dce)") == -1) { - filter += (" && tissueSamples.contains(dce) "); + // end lifeStage filters + // country filters------------------------------------------------- + String[] countries = request.getParameterValues("country"); + if ((countries != null) && (!countries[0].equals("None")) && + (!countries[0].equals(""))) { + prettyPrint.append("Country is one of the following: "); + int kwLength = countries.length; + String stageFilter = "("; + for (int kwIter = 0; kwIter < kwLength; kwIter++) { + String kwParam = countries[kwIter].replaceAll("%20", " ").trim(); + if (!kwParam.equals("")) { + if (stageFilter.equals("(")) { + stageFilter += " country == \"" + kwParam + "\""; + } else { + stageFilter += " || country == \"" + kwParam + "\""; + } + prettyPrint.append(kwParam + " "); + } + } + stageFilter += " ) "; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += stageFilter; + } else { filter += (" && " + stageFilter); } + prettyPrint.append("
"); } - prettyPrint.append("
"); - if (jdoqlVariableDeclaration.equals("")) { - jdoqlVariableDeclaration = " VARIABLES org.ecocean.genetics.TissueSample dce"; - } else if (!jdoqlVariableDeclaration.contains( - "org.ecocean.genetics.TissueSample dce")) { - jdoqlVariableDeclaration += ";org.ecocean.genetics.TissueSample dce"; + // end country filters + // Measurement filters----------------------------------------------- + List measurementDescs = Util.findMeasurementDescs("en", context); + String measurementPrefix = "measurement"; + StringBuilder measurementFilter = new StringBuilder(); // "( collectedData.contains(measurement) && ("); + boolean atLeastOneMeasurement = false; + int measurementsInQuery = 0; + for (MeasurementDesc measurementDesc : measurementDescs) { + String valueParamName = measurementPrefix + measurementDesc.getType() + "(value)"; + String value = request.getParameter(valueParamName); + if (value != null) { + value = value.trim(); + if (value.length() > 0) { + String operatorParamName = measurementPrefix + measurementDesc.getType() + + "(operator)"; + String operatorParamValue = request.getParameter(operatorParamName); + if (operatorParamValue == null) { + operatorParamValue = ""; + } + String operator = null; + if ("gt".equals(operatorParamValue)) { + operator = ">"; + } else if ("lt".equals(operatorParamValue)) { + operator = "<"; + } else if ("eq".equals(operatorParamValue)) { + operator = "=="; + } else if ("gteq".equals(operatorParamValue)) { + operator = ">="; + } else if ("lteq".equals(operatorParamValue)) { + operator = "<="; + } + if (operator != null) { + prettyPrint.append(measurementDesc.getUnitsLabel()); + prettyPrint.append(" is "); + prettyPrint.append(operator); + prettyPrint.append(value); + prettyPrint.append("
"); + if (atLeastOneMeasurement) { + measurementFilter.append("&&"); + } + String measurementVar = "measurement" + measurementsInQuery++; + measurementFilter.append("measurements.contains(" + measurementVar + + ") && "); + measurementFilter.append("(" + measurementVar + ".value " + operator + + " " + value + ")"); + measurementFilter.append(" && (" + measurementVar + ".type == "); + measurementFilter.append("\"" + measurementDesc.getType() + "\")"); + atLeastOneMeasurement = true; + } + } + } } - } - // end hasTissueSample filters----------------------------------------------- - // TissueSample sampleID filters------------------------------------------------- - if ((request.getParameter("tissueSampleID") != null) && - (!request.getParameter("tissueSampleID").trim().equals(""))) { - prettyPrint.append("Has biological sample with ID: " + - request.getParameter("tissueSampleID")); - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += - "tissueSamples.contains(dce123) && (dce123.sampleID.toLowerCase().indexOf('" + - request.getParameter("tissueSampleID").trim().toLowerCase() + "') != -1)"; - } else if (filter.indexOf("tissueSamples.contains(dce)") == -1) { - filter += - ( - " && tissueSamples.contains(dce123) && (dce123.sampleID.toLowerCase().indexOf('" - + request.getParameter("tissueSampleID").trim().toLowerCase() + "') != -1) "); - } - prettyPrint.append("
"); - if (jdoqlVariableDeclaration.equals("")) { - jdoqlVariableDeclaration = " VARIABLES org.ecocean.genetics.TissueSample dce123"; - } else if (!jdoqlVariableDeclaration.contains( - "org.ecocean.genetics.TissueSample dce123")) { - jdoqlVariableDeclaration += ";org.ecocean.genetics.TissueSample dce123"; - } - } - // end hasTissueSample filters----------------------------------------------- - // hasPhoto filters------------------------------------------------- - if (request.getParameter("hasPhoto") != null) { - prettyPrint.append("Has at least one MediaAsset."); - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += - "annotations.contains(dce15) && dce15.features.contains(feat15) && feat15.asset != null"; - } else if (filter.indexOf("annotations.contains(dce15)") == -1) { - filter += - ( - " && annotations.contains(dce15) && dce15.features.contains(feat15) && feat15.asset != null"); - } - prettyPrint.append("
"); - if (jdoqlVariableDeclaration.equals("")) { - jdoqlVariableDeclaration = " VARIABLES org.ecocean.Annotation dce15"; - } else if (!jdoqlVariableDeclaration.contains("org.ecocean.Annotation dce15")) { - jdoqlVariableDeclaration += ";org.ecocean.Annotation dce15"; - } - if (jdoqlVariableDeclaration.equals("")) { - jdoqlVariableDeclaration = " VARIABLES org.ecocean.media.Feature feat15"; - } else if (!jdoqlVariableDeclaration.contains("org.ecocean.media.Feature feat15")) { - jdoqlVariableDeclaration += ";org.ecocean.media.Feature feat15"; + if (atLeastOneMeasurement) { + if (jdoqlVariableDeclaration.length() > 0) { + jdoqlVariableDeclaration += ";"; + } else { + jdoqlVariableDeclaration = " VARIABLES "; + } + for (int i = 0; i < measurementsInQuery; i++) { + if (i > 0) { + jdoqlVariableDeclaration += "; "; + } + jdoqlVariableDeclaration += " org.ecocean.Measurement measurement" + i; + } + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += measurementFilter.toString(); + } else { + filter += (" && " + measurementFilter.toString()); + } } - } - // end hasPhoto filters----------------------------------------------- - // hasSpots filters------------------------------------------------- - if (request.getParameter("hasSpots") != null) { - prettyPrint.append("Has patterning points."); - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += "spots != null "; - } else if (filter.indexOf("spots != null") == -1) { filter += (" && spots !=null "); } - prettyPrint.append("
"); - } - // end hasSpots filters----------------------------------------------- - // has no Spots filters------------------------------------------------- - if (request.getParameter("hasNoSpots") != null) { - prettyPrint.append("Has no patterning points."); - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += "spots == null "; - } else if (filter.indexOf("spots == null") == -1) { filter += (" && spots == null "); } - prettyPrint.append("
"); - } - // end has no Spots filters----------------------------------------------- - // filter for encounters of MarkedIndividuals that have been resighted------------------------------------------ - if ((request.getParameter("resightOnly") != null) && - (request.getParameter("numResights") != null)) { - int numResights = 1; - - try { - numResights = (new Integer(request.getParameter("numResights"))).intValue(); - prettyPrint.append("numResights for related Marked Individual is >= " + - numResights + ".
"); - } catch (NumberFormatException nfe) { nfe.printStackTrace(); } - String localFilter = - "markedindy.encounters.contains(this) && markedindy.encounters.size() >= " + - numResights + " "; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += localFilter; - } else { filter += (" && " + localFilter); } - if (jdoqlVariableDeclaration.equals("")) { - jdoqlVariableDeclaration = " VARIABLES org.ecocean.MarkedIndividual markedindy"; - } else { jdoqlVariableDeclaration += ";org.ecocean.MarkedIndividual markedindy"; } - } - // end if resightOnly-------------------------------------------------------------------------------------- - // keyword filters------------------------------------------------- - - // shared var between keywords and labeledKeywords - int nUnlabeledKeywords = 0; - - myShepherd.beginDBTransaction(); - String[] keywords = request.getParameterValues("keyword"); - String photoKeywordOperator = "&&"; - if ((request.getParameter("photoKeywordOperator") != null) && - (request.getParameter("photoKeywordOperator").equals("_OR_"))) { - photoKeywordOperator = "||"; - } - if ((keywords != null) && (!keywords[0].equals("None"))) { - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { filter += "("; } else { - filter += " && ("; + // end measurement filters + // BiologicalMeasurement filters----------------------------------------------- + List bioMeasurementDescs = Util.findBiologicalMeasurementDescs("en", + context); + String bioMeasurementPrefix = "biomeasurement"; + StringBuilder bioMeasurementFilter = new StringBuilder(); + bioMeasurementFilter.append("tissueSamples.contains(dce322) "); + boolean bioAtLeastOneMeasurement = false; + int bioMeasurementsInQuery = 0; + for (MeasurementDesc measurementDesc : bioMeasurementDescs) { + String valueParamName = bioMeasurementPrefix + measurementDesc.getType() + + "(value)"; + String value = request.getParameter(valueParamName); + if (value != null) { + value = value.trim(); + if (value.length() > 0) { + String operatorParamName = bioMeasurementPrefix + + measurementDesc.getType() + "(operator)"; + String operatorParamValue = request.getParameter(operatorParamName); + if (operatorParamValue == null) { + operatorParamValue = ""; + } + String operator = null; + if ("gt".equals(operatorParamValue)) { + operator = ">"; + } else if ("lt".equals(operatorParamValue)) { + operator = "<"; + } else if ("eq".equals(operatorParamValue)) { + operator = "=="; + } else if ("gteq".equals(operatorParamValue)) { + operator = ">="; + } else if ("lteq".equals(operatorParamValue)) { + operator = "<="; + } + if (operator != null) { + prettyPrint.append("Biological/chemical measurement " + + measurementDesc.getType()); + prettyPrint.append(" is "); + prettyPrint.append(operator); + prettyPrint.append(value); + prettyPrint.append("
"); + if (bioAtLeastOneMeasurement) { + bioMeasurementFilter.append("&&"); + } + String measurementVar = "biomeasurement" + bioMeasurementsInQuery++; + bioMeasurementFilter.append(" & dce322.analyses.contains(" + + measurementVar + ") "); + bioMeasurementFilter.append(" && ( " + measurementVar + ".value " + + operator + " " + value + " )"); + bioMeasurementFilter.append(" && ( " + measurementVar + + ".measurementType == "); + bioMeasurementFilter.append("\"" + measurementDesc.getType() + "\" )"); + bioAtLeastOneMeasurement = true; + } + } + } } - if ((request.getParameter("photoKeywordOperator") != null) && - (request.getParameter("photoKeywordOperator").equals("_OR_"))) { - prettyPrint.append("MediaAsset keyword is any one of the following: "); - } else { - prettyPrint.append("All of these MediaAsset keywords are applied: "); + if (bioAtLeastOneMeasurement) { + if (jdoqlVariableDeclaration.length() > 0) { + jdoqlVariableDeclaration += ";org.ecocean.genetics.TissueSample dce322;"; + } else { + jdoqlVariableDeclaration = + " VARIABLES org.ecocean.genetics.TissueSample dce322;"; + } + for (int i = 0; i < bioMeasurementsInQuery; i++) { + if (i > 0) { + jdoqlVariableDeclaration += "; "; + } + jdoqlVariableDeclaration += + "org.ecocean.genetics.BiologicalMeasurement biomeasurement" + i; + } + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += bioMeasurementFilter.toString(); + } else { + filter += (" && " + bioMeasurementFilter.toString()); + } } - int kwLength = keywords.length; - nUnlabeledKeywords += kwLength; - for (int kwIter = 0; kwIter < kwLength; kwIter++) { + // end BiologicalMeasurement filters + // verbatimEventDate filters------------------------------------------------- + String[] verbatimEventDates = request.getParameterValues("verbatimEventDateField"); + if ((verbatimEventDates != null) && (!verbatimEventDates[0].equals("None"))) { + prettyPrint.append("verbatimEventDateField is one of the following: "); + int kwLength = verbatimEventDates.length; String locIDFilter = "("; - String kwParam = keywords[kwIter].replaceAll("%20", " ").trim(); - if (!kwParam.equals("")) { - if (locIDFilter.equals("(")) { - locIDFilter += " word" + kwIter + ".indexname == \"" + kwParam + "\" "; - } else { - locIDFilter += " " + photoKeywordOperator + " word" + kwIter + - ".indexname == \"" + kwParam + "\" "; + for (int kwIter = 0; kwIter < kwLength; kwIter++) { + String kwParam = verbatimEventDates[kwIter].replaceAll("%20", " ").trim(); + if (!kwParam.equals("")) { + if (locIDFilter.equals("(")) { + locIDFilter += " verbatimEventDate == \"" + kwParam + "\""; + } else { + locIDFilter += " || verbatimEventDate == \"" + kwParam + "\""; + } + prettyPrint.append(kwParam + " "); } - Keyword kw = myShepherd.getKeyword(kwParam.trim()); - prettyPrint.append("\"" + kw.getReadableName() + "\" "); } locIDFilter += " )"; - if (filter.indexOf("annotations.contains(photo" + kwIter + ")") == -1) { - if (kwIter > 0) { filter += " " + photoKeywordOperator + " "; } - filter += " ( annotations.contains(photo" + kwIter + ")"; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += locIDFilter; + } else { filter += (" && " + locIDFilter); } + prettyPrint.append("
"); + } + // end verbatimEventDate filters----------------------------------------------- + // hasTissueSample filters------------------------------------------------- + if (request.getParameter("hasTissueSample") != null) { + prettyPrint.append("Has tissue sample."); + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += "tissueSamples.contains(dce)"; + } else if (filter.indexOf("tissueSamples.contains(dce)") == -1) { + filter += (" && tissueSamples.contains(dce) "); } - if (filter.indexOf("photo" + kwIter + ".features.contains(feat" + kwIter + ")") == - -1) { - filter += " && photo" + kwIter + ".features.contains(feat" + kwIter + ")"; + prettyPrint.append("
"); + if (jdoqlVariableDeclaration.equals("")) { + jdoqlVariableDeclaration = " VARIABLES org.ecocean.genetics.TissueSample dce"; + } else if (!jdoqlVariableDeclaration.contains( + "org.ecocean.genetics.TissueSample dce")) { + jdoqlVariableDeclaration += ";org.ecocean.genetics.TissueSample dce"; } - if (filter.indexOf("feat" + kwIter + ".asset.keywords.contains(word" + kwIter + - ")") == -1) { - filter += " && feat" + kwIter + ".asset.keywords.contains(word" + kwIter + ")"; + } + // end hasTissueSample filters----------------------------------------------- + // TissueSample sampleID filters------------------------------------------------- + if ((request.getParameter("tissueSampleID") != null) && + (!request.getParameter("tissueSampleID").trim().equals(""))) { + prettyPrint.append("Has biological sample with ID: " + + request.getParameter("tissueSampleID")); + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += + "tissueSamples.contains(dce123) && (dce123.sampleID.toLowerCase().indexOf('" + + request.getParameter("tissueSampleID").trim().toLowerCase() + "') != -1)"; + } else if (filter.indexOf("tissueSamples.contains(dce)") == -1) { + filter += + ( + " && tissueSamples.contains(dce123) && (dce123.sampleID.toLowerCase().indexOf('" + + request.getParameter("tissueSampleID").trim().toLowerCase() + + "') != -1) "); } - filter += (" && " + locIDFilter + ")"); - if ((kwIter == 0) && (jdoqlVariableDeclaration.equals(""))) { - jdoqlVariableDeclaration = " VARIABLES "; + prettyPrint.append("
"); + if (jdoqlVariableDeclaration.equals("")) { + jdoqlVariableDeclaration = + " VARIABLES org.ecocean.genetics.TissueSample dce123"; + } else if (!jdoqlVariableDeclaration.contains( + "org.ecocean.genetics.TissueSample dce123")) { + jdoqlVariableDeclaration += ";org.ecocean.genetics.TissueSample dce123"; } - if (kwIter > 0) { jdoqlVariableDeclaration += ";"; } - if (!jdoqlVariableDeclaration.contains("org.ecocean.Annotation photo" + kwIter)) { - jdoqlVariableDeclaration += "org.ecocean.Annotation photo" + kwIter; + } + // end hasTissueSample filters----------------------------------------------- + // hasPhoto filters------------------------------------------------- + if (request.getParameter("hasPhoto") != null) { + prettyPrint.append("Has at least one MediaAsset."); + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += + "annotations.contains(dce15) && dce15.features.contains(feat15) && feat15.asset != null"; + } else if (filter.indexOf("annotations.contains(dce15)") == -1) { + filter += + ( + " && annotations.contains(dce15) && dce15.features.contains(feat15) && feat15.asset != null"); } - if (!jdoqlVariableDeclaration.contains("org.ecocean.Keyword word" + kwIter)) { - jdoqlVariableDeclaration += ";org.ecocean.Keyword word" + kwIter; + prettyPrint.append("
"); + if (jdoqlVariableDeclaration.equals("")) { + jdoqlVariableDeclaration = " VARIABLES org.ecocean.Annotation dce15"; + } else if (!jdoqlVariableDeclaration.contains("org.ecocean.Annotation dce15")) { + jdoqlVariableDeclaration += ";org.ecocean.Annotation dce15"; } - if (!jdoqlVariableDeclaration.contains("org.ecocean.media.Feature feat" + kwIter)) { - jdoqlVariableDeclaration += ";org.ecocean.media.Feature feat" + kwIter; + if (jdoqlVariableDeclaration.equals("")) { + jdoqlVariableDeclaration = " VARIABLES org.ecocean.media.Feature feat15"; + } else if (!jdoqlVariableDeclaration.contains("org.ecocean.media.Feature feat15")) { + jdoqlVariableDeclaration += ";org.ecocean.media.Feature feat15"; } } - filter += " ) "; + // end hasPhoto filters----------------------------------------------- + // hasSpots filters------------------------------------------------- + if (request.getParameter("hasSpots") != null) { + prettyPrint.append("Has patterning points."); + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += "spots != null "; + } else if (filter.indexOf("spots != null") == -1) { + filter += (" && spots !=null "); + } + prettyPrint.append("
"); + } + // end hasSpots filters----------------------------------------------- + // has no Spots filters------------------------------------------------- + if (request.getParameter("hasNoSpots") != null) { + prettyPrint.append("Has no patterning points."); + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += "spots == null "; + } else if (filter.indexOf("spots == null") == -1) { + filter += (" && spots == null "); + } + prettyPrint.append("
"); + } + // end has no Spots filters----------------------------------------------- + // filter for encounters of MarkedIndividuals that have been resighted------------------------------------------ + if ((request.getParameter("resightOnly") != null) && + (request.getParameter("numResights") != null)) { + int numResights = 1; - prettyPrint.append("
"); - } - myShepherd.rollbackDBTransaction(); - myShepherd.closeDBTransaction(); + try { + numResights = (new Integer(request.getParameter("numResights"))).intValue(); + prettyPrint.append("numResights for related Marked Individual is >= " + + numResights + ".
"); + } catch (NumberFormatException nfe) { nfe.printStackTrace(); } + String localFilter = + "markedindy.encounters.contains(this) && markedindy.encounters.size() >= " + + numResights + " "; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += localFilter; + } else { filter += (" && " + localFilter); } + if (jdoqlVariableDeclaration.equals("")) { + jdoqlVariableDeclaration = " VARIABLES org.ecocean.MarkedIndividual markedindy"; + } else { jdoqlVariableDeclaration += ";org.ecocean.MarkedIndividual markedindy"; } + } + // end if resightOnly-------------------------------------------------------------------------------------- + // keyword filters------------------------------------------------- - // end photo keyword filters----------------------------------------------- - // labeled keyword filters------------------------------------------------- - List labels = ServletUtilities.getIndexedParameters("label", request); - System.out.println("LKW filter got labels " + labels); - int index = 0; - boolean multipleLabels = labels.size() > 1; - String lkwFilter = "("; - for (int labelN = 0; labelN < labels.size(); labelN++) { - int kwNum = labelN + nUnlabeledKeywords; - int annotNum = nUnlabeledKeywords; // this way all labeledKeyword queries apply to the same annotation - String label = labels.get(labelN); - if (labelN == 0) { - prettyPrint.append("Encounter has a photo with the LabeledKeyword label \"" + - label + "\""); - } else { - prettyPrint.append(",
\t AND that photo has LabeledKeyword label \"" + label + - "\""); - lkwFilter += " && "; + // shared var between keywords and labeledKeywords + int nUnlabeledKeywords = 0; + String[] keywords = request.getParameterValues("keyword"); + String photoKeywordOperator = "&&"; + if ((request.getParameter("photoKeywordOperator") != null) && + (request.getParameter("photoKeywordOperator").equals("_OR_"))) { + photoKeywordOperator = "||"; } - // ------ start variables and declarations for this LKW - String annotVar = "photo" + annotNum; - // we only add the annotation the first time, so all subsequent keywords still apply to that first annotation - // bc if we search for "fluke photo, of quality 3-5" we are talking about one photo with two keywords, not two photos - if (labelN == 0) { - lkwFilter += "annotations.contains(" + annotVar + ")"; - jdoqlVariableDeclaration = QueryProcessor.updateJdoqlVariableDeclaration( - jdoqlVariableDeclaration, "org.ecocean.Annotation " + annotVar); + if ((keywords != null) && (!keywords[0].equals("None"))) { + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += "("; + } else { + filter += " && ("; + } + if ((request.getParameter("photoKeywordOperator") != null) && + (request.getParameter("photoKeywordOperator").equals("_OR_"))) { + prettyPrint.append("MediaAsset keyword is any one of the following: "); + } else { + prettyPrint.append("All of these MediaAsset keywords are applied: "); + } + int kwLength = keywords.length; + nUnlabeledKeywords += kwLength; + for (int kwIter = 0; kwIter < kwLength; kwIter++) { + String locIDFilter = "("; + String kwParam = keywords[kwIter].replaceAll("%20", " ").trim(); + if (!kwParam.equals("")) { + if (locIDFilter.equals("(")) { + locIDFilter += " word" + kwIter + ".indexname == \"" + kwParam + "\" "; + } else { + locIDFilter += " " + photoKeywordOperator + " word" + kwIter + + ".indexname == \"" + kwParam + "\" "; + } + Keyword kw = myShepherd.getKeyword(kwParam.trim()); + prettyPrint.append("\"" + kw.getReadableName() + "\" "); + } + locIDFilter += " )"; + if (filter.indexOf("annotations.contains(photo" + kwIter + ")") == -1) { + if (kwIter > 0) { filter += " " + photoKeywordOperator + " "; } + filter += " ( annotations.contains(photo" + kwIter + ")"; + } + if (filter.indexOf("photo" + kwIter + ".features.contains(feat" + kwIter + + ")") == -1) { + filter += " && photo" + kwIter + ".features.contains(feat" + kwIter + ")"; + } + if (filter.indexOf("feat" + kwIter + ".asset.keywords.contains(word" + kwIter + + ")") == -1) { + filter += " && feat" + kwIter + ".asset.keywords.contains(word" + kwIter + + ")"; + } + filter += (" && " + locIDFilter + ")"); + if ((kwIter == 0) && (jdoqlVariableDeclaration.equals(""))) { + jdoqlVariableDeclaration = " VARIABLES "; + } + if (kwIter > 0) { jdoqlVariableDeclaration += ";"; } + if (!jdoqlVariableDeclaration.contains("org.ecocean.Annotation photo" + + kwIter)) { + jdoqlVariableDeclaration += "org.ecocean.Annotation photo" + kwIter; + } + if (!jdoqlVariableDeclaration.contains("org.ecocean.Keyword word" + kwIter)) { + jdoqlVariableDeclaration += ";org.ecocean.Keyword word" + kwIter; + } + if (!jdoqlVariableDeclaration.contains("org.ecocean.media.Feature feat" + + kwIter)) { + jdoqlVariableDeclaration += ";org.ecocean.media.Feature feat" + kwIter; + } + } + filter += " ) "; + + prettyPrint.append("
"); } - // only one feature per annotation, so only one feature for all keywords - String featVar = "feat" + annotNum; - if (labelN == 0) { - lkwFilter += " && "; - lkwFilter += annotVar + ".features.contains(" + featVar + ")"; + // myShepherd.rollbackDBTransaction(); + // myShepherd.closeDBTransaction(); + + // end photo keyword filters----------------------------------------------- + // labeled keyword filters------------------------------------------------- + List labels = ServletUtilities.getIndexedParameters("label", request); + System.out.println("LKW filter got labels " + labels); + int index = 0; + boolean multipleLabels = labels.size() > 1; + String lkwFilter = "("; + for (int labelN = 0; labelN < labels.size(); labelN++) { + int kwNum = labelN + nUnlabeledKeywords; + int annotNum = nUnlabeledKeywords; // this way all labeledKeyword queries apply to the same annotation + String label = labels.get(labelN); + if (labelN == 0) { + prettyPrint.append("Encounter has a photo with the LabeledKeyword label \"" + + label + "\""); + } else { + prettyPrint.append(",
\t AND that photo has LabeledKeyword label \"" + + label + "\""); + lkwFilter += " && "; + } + // ------ start variables and declarations for this LKW + String annotVar = "photo" + annotNum; + // we only add the annotation the first time, so all subsequent keywords still apply to that first annotation + // bc if we search for "fluke photo, of quality 3-5" we are talking about one photo with two keywords, not two photos + if (labelN == 0) { + lkwFilter += "annotations.contains(" + annotVar + ")"; + jdoqlVariableDeclaration = QueryProcessor.updateJdoqlVariableDeclaration( + jdoqlVariableDeclaration, "org.ecocean.Annotation " + annotVar); + } + // only one feature per annotation, so only one feature for all keywords + String featVar = "feat" + annotNum; + if (labelN == 0) { + lkwFilter += " && "; + lkwFilter += annotVar + ".features.contains(" + featVar + ")"; + jdoqlVariableDeclaration = QueryProcessor.updateJdoqlVariableDeclaration( + jdoqlVariableDeclaration, "org.ecocean.media.Feature " + featVar); + } + String wordVar = "word" + kwNum; + if (labelN == 0) lkwFilter += " && "; + lkwFilter += featVar + ".asset.keywords.contains(" + wordVar + ")"; jdoqlVariableDeclaration = QueryProcessor.updateJdoqlVariableDeclaration( - jdoqlVariableDeclaration, "org.ecocean.media.Feature " + featVar); - } - String wordVar = "word" + kwNum; - if (labelN == 0) lkwFilter += " && "; - lkwFilter += featVar + ".asset.keywords.contains(" + wordVar + ")"; - jdoqlVariableDeclaration = QueryProcessor.updateJdoqlVariableDeclaration( - jdoqlVariableDeclaration, "org.ecocean.LabeledKeyword " + wordVar); - // ------ done with variables and declarations for this LKW + jdoqlVariableDeclaration, "org.ecocean.LabeledKeyword " + wordVar); + // ------ done with variables and declarations for this LKW - lkwFilter += " && " + wordVar + ".label == " + Util.quote(label); - // the filter is now done if we don't have any values defined --- so we're querying for an enc with this label on a keyword. + lkwFilter += " && " + wordVar + ".label == " + Util.quote(label); + // the filter is now done if we don't have any values defined --- so we're querying for an enc with this label on a keyword. - String valueKey = "label" + labelN + ".values"; - String[] values = request.getParameterValues(valueKey); - if (values != null) { - System.out.println("EQP got valueKey " + valueKey + " and values " + - String.join(", ", values)); - String valueFilter = "("; - for (int valueN = 0; valueN < values.length; valueN++) { - if (valueN > 0) valueFilter += " || "; - valueFilter += wordVar + ".readableName == " + Util.quote(values[valueN]); - } - valueFilter += ")"; - if (!valueFilter.equals("()")) lkwFilter += " && " + valueFilter; - if (values.length == 1) { - prettyPrint.append(" with value \"" + values[0] + "\""); - } else if (values.length > 1) { - String allVals = String.join("\", OR \"", values); - allVals = "\"" + allVals + "\""; - prettyPrint.append(" with value (" + allVals + ")"); - } - } else { - System.out.println("EQP got null values for valueKey " + valueKey); + String valueKey = "label" + labelN + ".values"; + String[] values = request.getParameterValues(valueKey); + if (values != null) { + System.out.println("EQP got valueKey " + valueKey + " and values " + + String.join(", ", values)); + String valueFilter = "("; + for (int valueN = 0; valueN < values.length; valueN++) { + if (valueN > 0) valueFilter += " || "; + valueFilter += wordVar + ".readableName == " + Util.quote(values[valueN]); + } + valueFilter += ")"; + if (!valueFilter.equals("()")) lkwFilter += " && " + valueFilter; + if (values.length == 1) { + prettyPrint.append(" with value \"" + values[0] + "\""); + } else if (values.length > 1) { + String allVals = String.join("\", OR \"", values); + allVals = "\"" + allVals + "\""; + prettyPrint.append(" with value (" + allVals + ")"); + } + } else { + System.out.println("EQP got null values for valueKey " + valueKey); + } } - } - lkwFilter += ")"; + lkwFilter += ")"; - System.out.println("EQP got lkwFilter " + lkwFilter); - if (!lkwFilter.equals("()")) { - filter = filterWithCondition(filter, lkwFilter); - prettyPrint.append("
"); - } - // end labeled keyword filters - // ms markers filters------------------------------------------------- - myShepherd.beginDBTransaction(); - List markers = myShepherd.getAllLoci(); - int numMarkers = markers.size(); - String theseMarkers = ""; - boolean hasMarkers = false; - for (int h = 0; h < numMarkers; h++) { - String marker = markers.get(h); - if (request.getParameter(marker) != null) { - hasMarkers = true; - String locIDFilter = "("; - locIDFilter += " " + marker.replaceAll("-", "") + ".name == \"" + marker + "\" "; - locIDFilter += " )"; + System.out.println("EQP got lkwFilter " + lkwFilter); + if (!lkwFilter.equals("()")) { + filter = filterWithCondition(filter, lkwFilter); + prettyPrint.append("
"); + } + // end labeled keyword filters + // ms markers filters------------------------------------------------- + // myShepherd.beginDBTransaction(); + List markers = myShepherd.getAllLoci(); + int numMarkers = markers.size(); + String theseMarkers = ""; + boolean hasMarkers = false; + for (int h = 0; h < numMarkers; h++) { + String marker = markers.get(h); + if (request.getParameter(marker) != null) { + hasMarkers = true; + String locIDFilter = "("; + locIDFilter += " " + marker.replaceAll("-", + "") + ".name == \"" + marker + "\" "; + locIDFilter += " )"; - int alleleNum = 0; - boolean hasMoreAlleles = true; - while (hasMoreAlleles) { - if (request.getParameter((marker + "_alleleValue" + alleleNum)) != null) { - try { - Integer thisInt = new Integer(request.getParameter((marker + - "_alleleValue" + alleleNum))); - Integer relaxValue = new Integer(request.getParameter( - "alleleRelaxValue")); - Integer upperValue = thisInt + relaxValue; - Integer lowerValue = thisInt - relaxValue; - locIDFilter += (" && (" + marker.replaceAll("-", - "") + ".allele" + alleleNum + " >= " + lowerValue + ")" + " && (" + - marker.replaceAll("-", - "") + ".allele" + alleleNum + " <= " + upperValue + ")"); - } catch (Exception e) { + int alleleNum = 0; + boolean hasMoreAlleles = true; + while (hasMoreAlleles) { + if (request.getParameter((marker + "_alleleValue" + alleleNum)) != null) { + try { + Integer thisInt = new Integer(request.getParameter((marker + + "_alleleValue" + alleleNum))); + Integer relaxValue = new Integer(request.getParameter( + "alleleRelaxValue")); + Integer upperValue = thisInt + relaxValue; + Integer lowerValue = thisInt - relaxValue; + locIDFilter += (" && (" + marker.replaceAll("-", + "") + ".allele" + alleleNum + " >= " + lowerValue + ")" + + " && (" + marker.replaceAll("-", + "") + ".allele" + alleleNum + " <= " + upperValue + ")"); + } catch (Exception e) { + hasMoreAlleles = false; + } + } else { hasMoreAlleles = false; } + alleleNum++; + } + theseMarkers += (marker + " "); + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += + "tissueSamples.contains(dce) && dce.analyses.contains(msanalysis) && msanalysis.loci.contains(" + + marker.replaceAll("-", "") + ") && " + locIDFilter; + } else { + if (filter.indexOf("tissueSamples.contains(dce)") == -1) { + filter += " && tissueSamples.contains(dce)"; + } + if (filter.indexOf("dce.analyses.contains(msanalysis)") == -1) { + filter += " && dce.analyses.contains(msanalysis)"; + } + if (filter.indexOf("msanalysis.loci.contains(" + marker.replaceAll("-", + "") + ")") == -1) { + filter += " && msanalysis.loci.contains(" + marker.replaceAll("-", + "") + ")"; + } + filter += (" && " + locIDFilter); + } + if (jdoqlVariableDeclaration.equals("")) { + jdoqlVariableDeclaration = + " VARIABLES org.ecocean.genetics.TissueSample dce;org.ecocean.genetics.MicrosatelliteMarkersAnalysis msanalysis;org.ecocean.genetics.Locus " + + marker.replaceAll("-", ""); } else { - hasMoreAlleles = false; + if (!jdoqlVariableDeclaration.contains( + "org.ecocean.genetics.TissueSample dce")) { + jdoqlVariableDeclaration += ";org.ecocean.genetics.TissueSample dce"; + } + if (!jdoqlVariableDeclaration.contains( + "org.ecocean.genetics.MicrosatelliteMarkersAnalysis msanalysis")) { + jdoqlVariableDeclaration += + ";org.ecocean.genetics.MicrosatelliteMarkersAnalysis msanalysis"; + } + if (!jdoqlVariableDeclaration.contains("org.ecocean.genetics.Locus " + + marker.replaceAll("-", ""))) { + jdoqlVariableDeclaration += ";org.ecocean.genetics.Locus " + + marker.replaceAll("-", ""); + } } - alleleNum++; } - theseMarkers += (marker + " "); + } + if (hasMarkers) { + prettyPrint.append("Microsatellite marker is one of the following: "); + theseMarkers += "
"; + prettyPrint.append(theseMarkers); + } + // myShepherd.rollbackDBTransaction(); + // myShepherd.closeDBTransaction(); + // end ms markers filters----------------------------------------------- + // filter for alternate ID------------------------------------------ + if ((request.getParameter("alternateIDField") != null) && + (!request.getParameter("alternateIDField").equals(""))) { + String altID = request.getParameter("alternateIDField").replaceAll("%20", + " ").trim().toLowerCase(); if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += - "tissueSamples.contains(dce) && dce.analyses.contains(msanalysis) && msanalysis.loci.contains(" - + marker.replaceAll("-", "") + ") && " + locIDFilter; + filter += "otherCatalogNumbers.toLowerCase().indexOf('" + altID + "') != -1"; + } else { + filter += " && otherCatalogNumbers.toLowerCase().indexOf('" + altID + + "') != -1"; + } + prettyPrint.append("alternateID field contains \"" + altID + "\".
"); + } + // haplotype filters------------------------------------------------- + String[] haplotypes = request.getParameterValues("haplotypeField"); + if ((haplotypes != null) && (!haplotypes[0].equals("None"))) { + prettyPrint.append("Haplotype is one of the following: "); + int kwLength = haplotypes.length; + String locIDFilter = "("; + for (int kwIter = 0; kwIter < kwLength; kwIter++) { + String kwParam = haplotypes[kwIter].replaceAll("%20", " ").trim(); + if (!kwParam.equals("")) { + if (locIDFilter.equals("(")) { + locIDFilter += " analysis.haplotype == \"" + kwParam + "\" "; + } else { + locIDFilter += " || analysis.haplotype == \"" + kwParam + "\" "; + } + prettyPrint.append(kwParam + " "); + } + } + locIDFilter += " )"; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += "tissueSamples.contains(dce) && dce.analyses.contains(analysis) && " + + locIDFilter; } else { if (filter.indexOf("tissueSamples.contains(dce)") == -1) { filter += " && tissueSamples.contains(dce)"; } - if (filter.indexOf("dce.analyses.contains(msanalysis)") == -1) { - filter += " && dce.analyses.contains(msanalysis)"; - } - if (filter.indexOf("msanalysis.loci.contains(" + marker.replaceAll("-", - "") + ")") == -1) { - filter += " && msanalysis.loci.contains(" + marker.replaceAll("-", - "") + ")"; + if (filter.indexOf("dce.analyses.contains(analysis)") == -1) { + filter += " && dce.analyses.contains(analysis)"; } filter += (" && " + locIDFilter); } + prettyPrint.append("
"); if (jdoqlVariableDeclaration.equals("")) { jdoqlVariableDeclaration = - " VARIABLES org.ecocean.genetics.TissueSample dce;org.ecocean.genetics.MicrosatelliteMarkersAnalysis msanalysis;org.ecocean.genetics.Locus " - + marker.replaceAll("-", ""); + " VARIABLES org.ecocean.genetics.TissueSample dce;org.ecocean.genetics.MitochondrialDNAAnalysis analysis"; } else { if (!jdoqlVariableDeclaration.contains( "org.ecocean.genetics.TissueSample dce")) { jdoqlVariableDeclaration += ";org.ecocean.genetics.TissueSample dce"; } if (!jdoqlVariableDeclaration.contains( - "org.ecocean.genetics.MicrosatelliteMarkersAnalysis msanalysis")) { + "org.ecocean.genetics.MitochondrialDNAAnalysis analysis")) { jdoqlVariableDeclaration += - ";org.ecocean.genetics.MicrosatelliteMarkersAnalysis msanalysis"; - } - if (!jdoqlVariableDeclaration.contains("org.ecocean.genetics.Locus " + - marker.replaceAll("-", ""))) { - jdoqlVariableDeclaration += ";org.ecocean.genetics.Locus " + - marker.replaceAll("-", ""); + ";org.ecocean.genetics.MitochondrialDNAAnalysis analysis"; } } } - } - if (hasMarkers) { - prettyPrint.append("Microsatellite marker is one of the following: "); - theseMarkers += "
"; - prettyPrint.append(theseMarkers); - } - myShepherd.rollbackDBTransaction(); - myShepherd.closeDBTransaction(); - // end ms markers filters----------------------------------------------- - // filter for alternate ID------------------------------------------ - if ((request.getParameter("alternateIDField") != null) && - (!request.getParameter("alternateIDField").equals(""))) { - String altID = request.getParameter("alternateIDField").replaceAll("%20", - " ").trim().toLowerCase(); - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += "otherCatalogNumbers.toLowerCase().indexOf('" + altID + "') != -1"; - } else { - filter += " && otherCatalogNumbers.toLowerCase().indexOf('" + altID + "') != -1"; - } - prettyPrint.append("alternateID field contains \"" + altID + "\".
"); - } - // haplotype filters------------------------------------------------- - String[] haplotypes = request.getParameterValues("haplotypeField"); - if ((haplotypes != null) && (!haplotypes[0].equals("None"))) { - prettyPrint.append("Haplotype is one of the following: "); - int kwLength = haplotypes.length; - String locIDFilter = "("; - for (int kwIter = 0; kwIter < kwLength; kwIter++) { - String kwParam = haplotypes[kwIter].replaceAll("%20", " ").trim(); - if (!kwParam.equals("")) { - if (locIDFilter.equals("(")) { - locIDFilter += " analysis.haplotype == \"" + kwParam + "\" "; - } else { - locIDFilter += " || analysis.haplotype == \"" + kwParam + "\" "; + // end haplotype filters----------------------------------------------- + // genetic sex filters------------------------------------------------- + String[] genSexes = request.getParameterValues("geneticSexField"); + if ((genSexes != null) && (!genSexes[0].equals("None"))) { + prettyPrint.append("Genetic determination of sex is one of the following: "); + int kwLength = genSexes.length; + String locIDFilter = "("; + for (int kwIter = 0; kwIter < kwLength; kwIter++) { + String kwParam = genSexes[kwIter].replaceAll("%20", " ").trim(); + if (!kwParam.equals("")) { + if (locIDFilter.equals("(")) { + locIDFilter += " sexanalysis.sex == \"" + kwParam + "\" "; + } else { + locIDFilter += " || sexanalysis.sex == \"" + kwParam + "\" "; + } + prettyPrint.append(kwParam + " "); } - prettyPrint.append(kwParam + " "); - } - } - locIDFilter += " )"; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += "tissueSamples.contains(dce) && dce.analyses.contains(analysis) && " + - locIDFilter; - } else { - if (filter.indexOf("tissueSamples.contains(dce)") == -1) { - filter += " && tissueSamples.contains(dce)"; - } - if (filter.indexOf("dce.analyses.contains(analysis)") == -1) { - filter += " && dce.analyses.contains(analysis)"; - } - filter += (" && " + locIDFilter); - } - prettyPrint.append("
"); - if (jdoqlVariableDeclaration.equals("")) { - jdoqlVariableDeclaration = - " VARIABLES org.ecocean.genetics.TissueSample dce;org.ecocean.genetics.MitochondrialDNAAnalysis analysis"; - } else { - if (!jdoqlVariableDeclaration.contains("org.ecocean.genetics.TissueSample dce")) { - jdoqlVariableDeclaration += ";org.ecocean.genetics.TissueSample dce"; } - if (!jdoqlVariableDeclaration.contains( - "org.ecocean.genetics.MitochondrialDNAAnalysis analysis")) { - jdoqlVariableDeclaration += - ";org.ecocean.genetics.MitochondrialDNAAnalysis analysis"; - } - } - } - // end haplotype filters----------------------------------------------- - // genetic sex filters------------------------------------------------- - String[] genSexes = request.getParameterValues("geneticSexField"); - if ((genSexes != null) && (!genSexes[0].equals("None"))) { - prettyPrint.append("Genetic determination of sex is one of the following: "); - int kwLength = genSexes.length; - String locIDFilter = "("; - for (int kwIter = 0; kwIter < kwLength; kwIter++) { - String kwParam = genSexes[kwIter].replaceAll("%20", " ").trim(); - if (!kwParam.equals("")) { - if (locIDFilter.equals("(")) { - locIDFilter += " sexanalysis.sex == \"" + kwParam + "\" "; - } else { - locIDFilter += " || sexanalysis.sex == \"" + kwParam + "\" "; + locIDFilter += " )"; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += + "tissueSamples.contains(dce9) && dce9.analyses.contains(sexanalysis) && " + + locIDFilter; + } else { + if (filter.indexOf("tissueSamples.contains(dce9)") == -1) { + filter += " && tissueSamples.contains(dce9)"; } - prettyPrint.append(kwParam + " "); - } - } - locIDFilter += " )"; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += - "tissueSamples.contains(dce9) && dce9.analyses.contains(sexanalysis) && " + - locIDFilter; - } else { - if (filter.indexOf("tissueSamples.contains(dce9)") == -1) { - filter += " && tissueSamples.contains(dce9)"; - } - if (filter.indexOf("dce9.analyses.contains(sexanalysis)") == -1) { - filter += " && dce9.analyses.contains(sexanalysis)"; + if (filter.indexOf("dce9.analyses.contains(sexanalysis)") == -1) { + filter += " && dce9.analyses.contains(sexanalysis)"; + } + filter += (" && " + locIDFilter); } - filter += (" && " + locIDFilter); - } - prettyPrint.append("
"); - if (jdoqlVariableDeclaration.equals("")) { - jdoqlVariableDeclaration = - " VARIABLES org.ecocean.genetics.TissueSample dce9;org.ecocean.genetics.SexAnalysis sexanalysis"; - } else { - if (!jdoqlVariableDeclaration.contains("org.ecocean.genetics.TissueSample dce9")) { - jdoqlVariableDeclaration += ";org.ecocean.genetics.TissueSample dce9"; - } - if (!jdoqlVariableDeclaration.contains( - "org.ecocean.genetics.SexAnalysis sexanalysis")) { - jdoqlVariableDeclaration += ";org.ecocean.genetics.SexAnalysis sexanalysis"; + prettyPrint.append("
"); + if (jdoqlVariableDeclaration.equals("")) { + jdoqlVariableDeclaration = + " VARIABLES org.ecocean.genetics.TissueSample dce9;org.ecocean.genetics.SexAnalysis sexanalysis"; + } else { + if (!jdoqlVariableDeclaration.contains( + "org.ecocean.genetics.TissueSample dce9")) { + jdoqlVariableDeclaration += ";org.ecocean.genetics.TissueSample dce9"; + } + if (!jdoqlVariableDeclaration.contains( + "org.ecocean.genetics.SexAnalysis sexanalysis")) { + jdoqlVariableDeclaration += ";org.ecocean.genetics.SexAnalysis sexanalysis"; + } } } - } - // end genetic sex filters----------------------------------------------- + // end genetic sex filters----------------------------------------------- // end photo filename filtering - // filter for genus------------------------------------------ - if ((request.getParameter("genusField") != null) && - (!request.getParameter("genusField").equals(""))) { - String genusSpecies = request.getParameter("genusField").replaceAll("%20", " ").trim(); - String genus = ""; - String specificEpithet = ""; + // filter for genus------------------------------------------ + if ((request.getParameter("genusField") != null) && + (!request.getParameter("genusField").equals(""))) { + String genusSpecies = request.getParameter("genusField").replaceAll("%20", + " ").trim(); + String genus = ""; + String specificEpithet = ""; - // now we have to break apart genus species - StringTokenizer tokenizer = new StringTokenizer(genusSpecies, " "); - if (tokenizer.countTokens() == 2) { - genus = tokenizer.nextToken(); - specificEpithet = tokenizer.nextToken(); - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += "genus == '" + genus + "' "; - } else { filter += " && genus == '" + genus + "' "; } - filter += " && specificEpithet == '" + specificEpithet + "' "; + // now we have to break apart genus species + StringTokenizer tokenizer = new StringTokenizer(genusSpecies, " "); + if (tokenizer.countTokens() == 2) { + genus = tokenizer.nextToken(); + specificEpithet = tokenizer.nextToken(); + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += "genus == '" + genus + "' "; + } else { filter += " && genus == '" + genus + "' "; } + filter += " && specificEpithet == '" + specificEpithet + "' "; - prettyPrint.append("genus and species are \"" + genusSpecies + "\".
"); - } - } - // filter for identificationRemarks------------------------------------------ - if ((request.getParameter("identificationRemarksField") != null) && - (!request.getParameter("identificationRemarksField").equals(""))) { - String idRemarks = request.getParameter("identificationRemarksField").trim(); - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += "identificationRemarks.startsWith('" + idRemarks + "')"; - } else { filter += " && identificationRemarks.startsWith('" + idRemarks + "')"; } - prettyPrint.append("identificationRemarks starts with \"" + idRemarks + "\".
"); - } - // end identification remarks filter - // filter gpsOnly - return only Encounters with a defined location. This is mostly used for mapping JSP pages - if (request.getAttribute("gpsOnly") != null) { - filter = filterWithGpsBox("decimalLatitude", "decimalLongitude", filter, request); - } - // end filter gpsOnly - // filter by alive/dead status------------------------------------------ - if ((request.getParameter("alive") != null) || (request.getParameter("dead") != null)) { - if (request.getParameter("alive") == null) { - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += "!livingStatus.startsWith('alive')"; - } else { filter += " && !livingStatus.startsWith('alive')"; } - prettyPrint.append("Not alive.
"); + prettyPrint.append("genus and species are \"" + genusSpecies + "\".
"); + } } - if (request.getParameter("dead") == null) { + // filter for identificationRemarks------------------------------------------ + if ((request.getParameter("identificationRemarksField") != null) && + (!request.getParameter("identificationRemarksField").equals(""))) { + String idRemarks = request.getParameter("identificationRemarksField").trim(); if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += "!livingStatus.startsWith('dead')"; - } else { filter += " && !livingStatus.startsWith('dead')"; } - prettyPrint.append("Not dead.
"); - } - } - // filter by alive/dead status-------------------------------------------------------------------------------------- - // submitter or photographer name filter------------------------------------------ - if ((request.getParameter("nameField") != null) && - (!request.getParameter("nameField").equals(""))) { - String nameString = request.getParameter("nameField").replaceAll("%20", - " ").toLowerCase().trim(); - String filterString = "" + "(" + - "(submitters.contains(submitter) && ((submitter.fullName.toLowerCase().indexOf('" + - nameString + "') != -1)||(submitter.emailAddress.toLowerCase().indexOf('" + - nameString + "') != -1))) || " + - "(photographers.contains(photographer) && ((photographer.fullName.toLowerCase().indexOf('" - + nameString + "') != -1)||(photographer.emailAddress.toLowerCase().indexOf('" + - nameString + "') != -1))) " + - "||(informOthers.contains(other) && ((other.fullName.toLowerCase().indexOf('" + - nameString + "') != -1)||(other.emailAddress.toLowerCase().indexOf('" + nameString + - "') != -1)))" + ")"; - if (jdoqlVariableDeclaration.equals("")) { - jdoqlVariableDeclaration = - " VARIABLES org.ecocean.User submitter;org.ecocean.User photographer;org.ecocean.User other"; - } else { - if (!jdoqlVariableDeclaration.contains("org.ecocean.User submitter")) { - jdoqlVariableDeclaration += ";org.ecocean.User submitter"; - } - if (!jdoqlVariableDeclaration.contains("org.ecocean.User photographer")) { - jdoqlVariableDeclaration += ";org.ecocean.User photographer"; - } - if (!jdoqlVariableDeclaration.contains("org.ecocean.User other")) { - jdoqlVariableDeclaration += ";org.ecocean.User other"; + filter += "identificationRemarks.startsWith('" + idRemarks + "')"; + } else { filter += " && identificationRemarks.startsWith('" + idRemarks + "')"; } + prettyPrint.append("identificationRemarks starts with \"" + idRemarks + + "\".
"); + } + // end identification remarks filter + // filter gpsOnly - return only Encounters with a defined location. This is mostly used for mapping JSP pages + if (request.getAttribute("gpsOnly") != null) { + filter = filterWithGpsBox("decimalLatitude", "decimalLongitude", filter, request); + } + // end filter gpsOnly + // filter by alive/dead status------------------------------------------ + if ((request.getParameter("alive") != null) || (request.getParameter("dead") != null)) { + if (request.getParameter("alive") == null) { + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += "!livingStatus.startsWith('alive')"; + } else { filter += " && !livingStatus.startsWith('alive')"; } + prettyPrint.append("Not alive.
"); + } + if (request.getParameter("dead") == null) { + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += "!livingStatus.startsWith('dead')"; + } else { filter += " && !livingStatus.startsWith('dead')"; } + prettyPrint.append("Not dead.
"); } } - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += filterString; - } else { filter += (" && " + filterString); } - prettyPrint.append("Related fullName or emailAddress contains: \"" + nameString + - "\"
"); - } - // end name and email filter-------------------------------------------------------------------------------------- - // additional comments filter------------------------------------------ - if ((request.getParameter("additionalCommentsField") != null) && - (!request.getParameter("additionalCommentsField").equals(""))) { - String nameString = request.getParameter("additionalCommentsField").replaceAll("%20", - " ").toLowerCase().trim(); - String filterString = "(occurrenceRemarks.toLowerCase().indexOf('" + nameString + - "') != -1)"; - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += filterString; - } else { filter += (" && " + filterString); } - prettyPrint.append("Remarks contains: \"" + nameString + "\"
"); - } - // end additional comments filter-------------------------------------------------------------------------------------- - // start date filter---------------------------- - if ((request.getParameter("datepicker1") != null) && - (!request.getParameter("datepicker1").trim().equals("")) && - (request.getParameter("datepicker2") != null) && - (!request.getParameter("datepicker2").trim().equals(""))) { - try { - DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser(); - DateTime date1 = parser.parseDateTime(request.getParameter("datepicker1")); - DateTime date2 = parser.parseDateTime(request.getParameter("datepicker2")); - long date1Millis = date1.getMillis(); - long date2Millis = date2.getMillis(); - // if same dateTime is set by both pickers, then add a full day of milliseconds to picker2 to cover the entire day - date2Millis += (24 * 60 * 60 * 1000 - 1); - - prettyPrint.append("Dates between: " + date1.toString(ISODateTimeFormat.date()) + - " and " + date2.toString(ISODateTimeFormat.date()) + "
"); - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += "((dateInMilliseconds >= " + date1Millis + - ") && (dateInMilliseconds <= " + date2Millis + "))"; + // filter by alive/dead status-------------------------------------------------------------------------------------- + // submitter or photographer name filter------------------------------------------ + if ((request.getParameter("nameField") != null) && + (!request.getParameter("nameField").equals(""))) { + String nameString = request.getParameter("nameField").replaceAll("%20", + " ").toLowerCase().trim(); + String filterString = "" + "(" + + "(submitters.contains(submitter) && ((submitter.fullName.toLowerCase().indexOf('" + + nameString + "') != -1)||(submitter.emailAddress.toLowerCase().indexOf('" + + nameString + "') != -1))) || " + + "(photographers.contains(photographer) && ((photographer.fullName.toLowerCase().indexOf('" + + nameString + "') != -1)||(photographer.emailAddress.toLowerCase().indexOf('" + + nameString + "') != -1))) " + + "||(informOthers.contains(other) && ((other.fullName.toLowerCase().indexOf('" + + nameString + "') != -1)||(other.emailAddress.toLowerCase().indexOf('" + + nameString + "') != -1)))" + ")"; + if (jdoqlVariableDeclaration.equals("")) { + jdoqlVariableDeclaration = + " VARIABLES org.ecocean.User submitter;org.ecocean.User photographer;org.ecocean.User other"; } else { - filter += " && ((dateInMilliseconds >= " + date1Millis + - ") && (dateInMilliseconds <= " + date2Millis + "))"; + if (!jdoqlVariableDeclaration.contains("org.ecocean.User submitter")) { + jdoqlVariableDeclaration += ";org.ecocean.User submitter"; + } + if (!jdoqlVariableDeclaration.contains("org.ecocean.User photographer")) { + jdoqlVariableDeclaration += ";org.ecocean.User photographer"; + } + if (!jdoqlVariableDeclaration.contains("org.ecocean.User other")) { + jdoqlVariableDeclaration += ";org.ecocean.User other"; + } } - } catch (Exception e) { e.printStackTrace(); } - } - // end date filter------------------------------------------ - // start date added filter---------------------------- - if ((request.getParameter("dateaddedpicker1") != null) && - (!request.getParameter("dateaddedpicker1").trim().equals("")) && - (request.getParameter("dateaddedpicker2") != null) && - (!request.getParameter("dateaddedpicker2").trim().equals(""))) { - try { - DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser(); - DateTime date1 = parser.parseDateTime(request.getParameter("dateaddedpicker1")); - DateTime date2 = parser.parseDateTime(request.getParameter("dateaddedpicker2")); - - prettyPrint.append("Encounter creation dates between: " + - date1.toString(ISODateTimeFormat.date()) + " and " + - date2.toString(ISODateTimeFormat.date()) + "
"); if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += "((dwcDateAddedLong >= " + date1.getMillis() + - ") && (dwcDateAddedLong <= " + date2.getMillis() + "))"; - } else { - filter += " && ((dwcDateAddedLong >= " + date1.getMillis() + - ") && (dwcDateAddedLong <= " + date2.getMillis() + "))"; + filter += filterString; + } else { filter += (" && " + filterString); } + prettyPrint.append("Related fullName or emailAddress contains: \"" + nameString + + "\"
"); + } + // end name and email filter-------------------------------------------------------------------------------------- + // additional comments filter------------------------------------------ + if ((request.getParameter("additionalCommentsField") != null) && + (!request.getParameter("additionalCommentsField").equals(""))) { + String nameString = request.getParameter("additionalCommentsField").replaceAll( + "%20", " ").toLowerCase().trim(); + String filterString = "(occurrenceRemarks.toLowerCase().indexOf('" + nameString + + "') != -1)"; + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += filterString; + } else { filter += (" && " + filterString); } + prettyPrint.append("Remarks contains: \"" + nameString + "\"
"); + } + // end additional comments filter-------------------------------------------------------------------------------------- + // start date filter---------------------------- + if ((request.getParameter("datepicker1") != null) && + (!request.getParameter("datepicker1").trim().equals("")) && + (request.getParameter("datepicker2") != null) && + (!request.getParameter("datepicker2").trim().equals(""))) { + try { + DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser(); + DateTime date1 = parser.parseDateTime(request.getParameter("datepicker1")); + DateTime date2 = parser.parseDateTime(request.getParameter("datepicker2")); + long date1Millis = date1.getMillis(); + long date2Millis = date2.getMillis(); + // if same dateTime is set by both pickers, then add a full day of milliseconds to picker2 to cover the entire day + date2Millis += (24 * 60 * 60 * 1000 - 1); + + prettyPrint.append("Dates between: " + + date1.toString(ISODateTimeFormat.date()) + " and " + + date2.toString(ISODateTimeFormat.date()) + "
"); + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += "((dateInMilliseconds >= " + date1Millis + + ") && (dateInMilliseconds <= " + date2Millis + "))"; + } else { + filter += " && ((dateInMilliseconds >= " + date1Millis + + ") && (dateInMilliseconds <= " + date2Millis + "))"; + } + } catch (Exception e) { e.printStackTrace(); } + } + // end date filter------------------------------------------ + // start date added filter---------------------------- + if ((request.getParameter("dateaddedpicker1") != null) && + (!request.getParameter("dateaddedpicker1").trim().equals("")) && + (request.getParameter("dateaddedpicker2") != null) && + (!request.getParameter("dateaddedpicker2").trim().equals(""))) { + try { + DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser(); + DateTime date1 = parser.parseDateTime(request.getParameter("dateaddedpicker1")); + DateTime date2 = parser.parseDateTime(request.getParameter("dateaddedpicker2")); + + prettyPrint.append("Encounter creation dates between: " + + date1.toString(ISODateTimeFormat.date()) + " and " + + date2.toString(ISODateTimeFormat.date()) + "
"); + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += "((dwcDateAddedLong >= " + date1.getMillis() + + ") && (dwcDateAddedLong <= " + date2.getMillis() + "))"; + } else { + filter += " && ((dwcDateAddedLong >= " + date1.getMillis() + + ") && (dwcDateAddedLong <= " + date2.getMillis() + "))"; + } + } catch (NumberFormatException nfe) { + // do nothing, just skip on + nfe.printStackTrace(); } - } catch (NumberFormatException nfe) { - // do nothing, just skip on - nfe.printStackTrace(); - } - } - // end date added filter------------------------------------------ - // filter for sex------------------------------------------ - if ((request.getParameter("male") != null) || (request.getParameter("female") != null) || - (request.getParameter("unknown") != null)) { - System.out.println("Filter at beginning of sex filtering: " + filter); - if (request.getParameter("male") == null) { - filter = filterWithCondition(filter, "!sex.startsWith('male')"); - prettyPrint.append("Sex is not male.
"); } - if (request.getParameter("female") == null) { - filter = filterWithCondition(filter, "!sex.startsWith('female')"); - prettyPrint.append("Sex is not female.
"); - } - if (request.getParameter("unknown") == null) { - filter = filterWithCondition(filter, "!sex.startsWith('unknown') && sex != null"); - prettyPrint.append("Sex is not unknown.
"); - } - System.out.println("Filter at end of sex filtering: " + filter); - } - // filter by sex-------------------------------------------------------------------------------------- - String releaseDateFromStr = request.getParameter("releaseDateFrom"); - String releaseDateToStr = request.getParameter("releaseDateTo"); - String pattern = CommonConfiguration.getProperty("releaseDateFormat", context); - SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); - if (releaseDateFromStr != null && releaseDateFromStr.trim().length() > 0) { - try { - Date releaseDateFrom = simpleDateFormat.parse(releaseDateFromStr); - if (!filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += " && "; + // end date added filter------------------------------------------ + // filter for sex------------------------------------------ + if ((request.getParameter("male") != null) || + (request.getParameter("female") != null) || + (request.getParameter("unknown") != null)) { + System.out.println("Filter at beginning of sex filtering: " + filter); + if (request.getParameter("male") == null) { + filter = filterWithCondition(filter, "!sex.startsWith('male')"); + prettyPrint.append("Sex is not male.
"); + } + if (request.getParameter("female") == null) { + filter = filterWithCondition(filter, "!sex.startsWith('female')"); + prettyPrint.append("Sex is not female.
"); + } + if (request.getParameter("unknown") == null) { + filter = filterWithCondition(filter, + "!sex.startsWith('unknown') && sex != null"); + prettyPrint.append("Sex is not unknown.
"); + } + System.out.println("Filter at end of sex filtering: " + filter); + } + // filter by sex-------------------------------------------------------------------------------------- + String releaseDateFromStr = request.getParameter("releaseDateFrom"); + String releaseDateToStr = request.getParameter("releaseDateTo"); + String pattern = CommonConfiguration.getProperty("releaseDateFormat", context); + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); + if (releaseDateFromStr != null && releaseDateFromStr.trim().length() > 0) { + try { + Date releaseDateFrom = simpleDateFormat.parse(releaseDateFromStr); + if (!filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += " && "; + } + filter += "(releaseDate >= releaseDateFrom)"; + parameterDeclaration = updateParametersDeclaration(parameterDeclaration, + "java.util.Date releaseDateFrom"); + paramMap.put("releaseDateFrom", releaseDateFrom); + prettyPrint.append("release date >= " + + simpleDateFormat.format(releaseDateFrom)); + } catch (Exception e) { + e.printStackTrace(); } - filter += "(releaseDate >= releaseDateFrom)"; - parameterDeclaration = updateParametersDeclaration(parameterDeclaration, - "java.util.Date releaseDateFrom"); - paramMap.put("releaseDateFrom", releaseDateFrom); - prettyPrint.append("release date >= " + simpleDateFormat.format(releaseDateFrom)); - } catch (Exception e) { - e.printStackTrace(); } - } - if (releaseDateToStr != null && releaseDateToStr.trim().length() > 0) { - try { - Date releaseDateTo = simpleDateFormat.parse(releaseDateToStr); - if (!filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter += " && "; + if (releaseDateToStr != null && releaseDateToStr.trim().length() > 0) { + try { + Date releaseDateTo = simpleDateFormat.parse(releaseDateToStr); + if (!filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter += " && "; + } + filter += "(releaseDate <= releaseDateTo)"; + parameterDeclaration = updateParametersDeclaration(parameterDeclaration, + "java.util.Date releaseDateTo"); + paramMap.put("releaseDateTo", releaseDateTo); + prettyPrint.append("releaseDate <= " + simpleDateFormat.format(releaseDateTo)); + } catch (Exception e) { + e.printStackTrace(); } - filter += "(releaseDate <= releaseDateTo)"; - parameterDeclaration = updateParametersDeclaration(parameterDeclaration, - "java.util.Date releaseDateTo"); - paramMap.put("releaseDateTo", releaseDateTo); - prettyPrint.append("releaseDate <= " + simpleDateFormat.format(releaseDateTo)); - } catch (Exception e) { - e.printStackTrace(); } - } - for (String fieldName : SIMPLE_STRING_FIELDS) - filter = QueryProcessor.filterWithBasicStringField(filter, fieldName, request, - prettyPrint); - filter = filterWithGpsBox("decimalLatitude", "decimalLongitude", filter, request); - // end GPS filters----------------------------------------------- - if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { - filter = "SELECT FROM org.ecocean.Encounter WHERE catalogNumber != null"; - } - filter += jdoqlVariableDeclaration; - - filter += parameterDeclaration; - System.out.println("EncounterQueryProcessor filter: " + filter); + for (String fieldName : SIMPLE_STRING_FIELDS) + filter = QueryProcessor.filterWithBasicStringField(filter, fieldName, request, + prettyPrint); + filter = filterWithGpsBox("decimalLatitude", "decimalLongitude", filter, request); + // end GPS filters----------------------------------------------- + if (filter.equals(SELECT_FROM_ORG_ECOCEAN_ENCOUNTER_WHERE)) { + filter = "SELECT FROM org.ecocean.Encounter WHERE catalogNumber != null"; + } + filter += jdoqlVariableDeclaration; + filter += parameterDeclaration; + System.out.println("EncounterQueryProcessor filter: " + filter); + } catch (Exception ex) { + System.out.println("Shepherd usage caught: " + ex); + ex.printStackTrace(); + } finally { + myShepherd.rollbackAndClose(); + } return filter; }