/** * The contents of this file are subject to the OpenMRS Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://license.openmrs.org * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * Copyright (C) OpenMRS, LLC. All Rights Reserved. */ package org.eclipse.datatools.connectivity.oda.openmrs.impl; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import org.eclipse.datatools.connectivity.oda.OdaException; // TODO: Auto-generated Javadoc /** * Helper class for the data set wizard pages and Query#prepare to break down * and rebuild queries as changes are made. */ public class QueryBuilder { private static final String ENCOUNTERTYPE = "ENCOUNTERTYPE"; private static final String ENCOUNTERDATE = "ENCOUNTERDATE"; private static final String OBSERVATIONLOCATION = "OBSERVATIONLOCATION"; private static final String OBSERVATIONDATE = "OBSERVATIONDATE"; private static Logger log = Logger.getLogger(QueryBuilder.class.getName()); /** * Builds the query given the tokens, filter, and datastyle. * * @param tokens the tokens * @param filter the filter * @param dataStyle the data style * * @return the query */ public static String getQuery(String tokens, String filter, String dataStyle) { log.setLevel(Level.INFO); if (tokens == null || filter == null) return null; else { String query = "SELECT " + tokens + " FROM " + filter + "&datastyle=" + dataStyle; log.info("getQuery(String tokens, String filter) - " + query); return query; } } /** * Extracts the data style from the query. * * @param query to extract the data style from * * @return the data style */ public static String getDataStyle(String query) { String dataStyle = query.substring(query.indexOf("&datastyle=") + 11); log.info(dataStyle); return dataStyle; } /** * Builds a query given a list of Token objects, a filter, and a datastyle. * * @param tokens the tokens * @param filter the filter * @param dataStyle the data style * * @return the query */ public static String getQuery(List tokens, String filter, String dataStyle) { String query = ""; log.info(tokens.toString()); for (Token token : tokens) { log.info("Aggregate: " + token.getAggregate()); // there will always be an aggregate, so add it to the front of the // token query query += "|" + token.getAggregate() + " " + token.getAggregateValue() + " "; // add modifiers if there are any if (token.getModifiers() != null) { log.info("Has modifier: " + token.getModifiers()); query += "{" + token.getName() + "} " + token.getModifiers(); } else { query += "{" + token.getName() + "}"; } if (token.isSplitObservationDate()) { query += ":OBSERVATIONDATE"; } if (token.isSplitObservationLocation()) { query += ":OBSERVATIONLOCATION"; } if (token.isSplitEncounterDate()) { query += ":ENCOUNTERDATE"; } if (token.isSplitEncounterType()) { query += ":ENCOUNTERTYPE"; } } query = "SELECT " + query.substring(1).trim() + " FROM " + filter + "&datastyle=" + dataStyle; log.info("getQuery(List tokens, String filter) - " + query); return query; } /** * Extracts the filter from the query. * * @param query the query * * @return the filter */ public static String getFilter(String query) { String filter = query.substring(query.indexOf("FROM") + 5, query .indexOf("&")); log.info(filter); return filter; } /** * Returns an ArrayList of Token objects given the current query and a map * of token names and Token objects. * * @param query the query * @param tokenMap the token map * * @return the tokens */ public static ArrayList getTokens(String query, Map tokenMap) { ArrayList tokenObjects = new ArrayList(); // extract the SELECT field String select = query.substring(query.indexOf("SELECT") + 7, query .indexOf("FROM") - 1); log.info(select); String[] tokens = select.split("\\|"); // go through each of the tokens for (int i = 0; i < tokens.length; i++) { // extract the aggregate String aggregate = tokens[i].substring(0, tokens[i].indexOf("{") - 1); // String aggregate = "LAST 5"; // extract the actual token name String tokenName = tokens[i].substring(tokens[i].indexOf("{") + 1, tokens[i].indexOf("}")); log.info(tokenName); Token token = tokenMap.get(tokenName); token.setAggregate(Utils.getAggregateNameForTokenFromTokenProperty(tokenName, select)); token.setAggregateValue(Utils.getAggregateValueForTokenFromTokenProperty(tokenName, select)); // set the modifier data here: if (tokens[i].contains(":")) token.setModifiers(tokens[i].substring( tokens[i].indexOf("}") + 1, tokens[i].indexOf(":")) .trim()); else token.setModifiers(tokens[i].substring( tokens[i].indexOf("}") + 1).trim()); // check if the token is to be split if (tokens[i].contains(":")) { String[] splits = tokens[i].substring(tokens[i].indexOf(":")) .split(":"); for (int j = 0; j < splits.length; j++) { if (splits[j].equals(OBSERVATIONDATE)) token.setSplitObservationDate(true); else if (splits[j].equals(OBSERVATIONLOCATION)) token.setSplitObservationLocation(true); else if (splits[j].equals(ENCOUNTERDATE)) token.setSplitEncounterDate(true); else if (splits[j].equals(ENCOUNTERTYPE)) token.setSplitEncounterType(true); } } tokenObjects.add(token); } return tokenObjects; } /** * Gets the token names from the storage query. * * @param tokens the token names * * @return the token names from the storage query */ public static List getTokenNamesFromStorage(String tokens) { String[] tokenName = tokens.split("\\|"); log.info(tokens); ArrayList tokenNames = new ArrayList(); // go through each of the tokens for (int i = 0; i < tokenName.length; i++) { // extract the actual token name String token = tokenName[i].substring( tokenName[i].indexOf("{") + 1, tokenName[i].indexOf("}")); log.info(token); tokenNames.add(token); } return tokenNames; } }