| 15 | | public class URLdataQuery |
|---|
| 16 | | { |
|---|
| 17 | | private Log log = LogFactory.getLog(this.getClass()); |
|---|
| 18 | | |
|---|
| 19 | | private String filter; |
|---|
| 20 | | |
|---|
| 21 | | private String[] tokensWithModifiers; |
|---|
| 22 | | |
|---|
| 23 | | private String query; |
|---|
| 24 | | |
|---|
| 25 | | private String dataStyle; |
|---|
| 26 | | |
|---|
| 27 | | private Map<String, String[]> tokenSplitters; |
|---|
| 28 | | |
|---|
| 29 | | public URLdataQuery(HttpServletRequest request) throws IOException, ServletException |
|---|
| 30 | | { |
|---|
| 31 | | try |
|---|
| 32 | | { |
|---|
| 33 | | if(request.getParameter("datastyle") == null) |
|---|
| 34 | | dataStyle = ""; |
|---|
| 35 | | else |
|---|
| 36 | | dataStyle = request.getParameter("datastyle"); |
|---|
| 37 | | |
|---|
| 38 | | query = request.getParameter("query"); |
|---|
| 39 | | |
|---|
| 40 | | tokenSplitters = new HashMap<String, String[]>(); |
|---|
| 41 | | |
|---|
| 42 | | if(query != null) |
|---|
| 43 | | { |
|---|
| 44 | | filter = query.indexOf("FROM") == -1 ? null |
|---|
| 45 | | : query.substring(query.indexOf("FROM") + 5); |
|---|
| 46 | | |
|---|
| 47 | | //extract the SELECT piece if it exists |
|---|
| 48 | | if(query.contains("SELECT") && query.contains("FROM")) |
|---|
| 49 | | { |
|---|
| 50 | | String select = query.substring(query.indexOf("SELECT") + 7, query.indexOf("FROM") - 1); |
|---|
| 51 | | tokensWithModifiers = select.split("\\|"); |
|---|
| 52 | | |
|---|
| 53 | | //remove the split indicators |
|---|
| 54 | | for(int i = 0; i < tokensWithModifiers.length; i++) |
|---|
| 55 | | { |
|---|
| 56 | | if(tokensWithModifiers[i].contains(":")) |
|---|
| 57 | | { |
|---|
| 58 | | //populate token splitters |
|---|
| 59 | | String[] splits = tokensWithModifiers[i].substring(tokensWithModifiers[i].indexOf(":") + 1).split(":"); |
|---|
| 60 | | tokenSplitters.put(tokensWithModifiers[i].substring(tokensWithModifiers[i].indexOf("{") + 1, tokensWithModifiers[i].indexOf("}")), splits); |
|---|
| 61 | | |
|---|
| 62 | | tokensWithModifiers[i] = tokensWithModifiers[i].substring(0, tokensWithModifiers[i].indexOf(":")); |
|---|
| 63 | | |
|---|
| 64 | | |
|---|
| 65 | | } |
|---|
| 66 | | } |
|---|
| 67 | | } |
|---|
| 68 | | else |
|---|
| 69 | | tokensWithModifiers = null; |
|---|
| 70 | | } |
|---|
| 71 | | } |
|---|
| 72 | | catch(Exception e) |
|---|
| 73 | | { |
|---|
| 74 | | log.error(e.getMessage()); |
|---|
| 75 | | |
|---|
| 76 | | e.printStackTrace(); |
|---|
| 77 | | } |
|---|
| 78 | | } |
|---|
| | 26 | /** |
|---|
| | 27 | * Helper class that extracts the important information for the URL query so |
|---|
| | 28 | * that the different data styles can quickly access this information. |
|---|
| | 29 | */ |
|---|
| | 30 | public class URLdataQuery { |
|---|
| 112 | | public String getDataStyle() { |
|---|
| 113 | | return dataStyle; |
|---|
| 114 | | } |
|---|
| 115 | | |
|---|
| 116 | | public static void main(String[] args) |
|---|
| 117 | | { |
|---|
| 118 | | String query = "SELECT {AGE}| {WEIGHT (KG)}:OBSERVATIONDATE| {HEIGHT (CM)}:OBSERVATIONDATE FROM 3:org.openmrs.cohort.StaticCohortDefinition"; |
|---|
| 119 | | |
|---|
| 120 | | Map<String,String[]> tokenSplitters = new HashMap<String, String[]>(); |
|---|
| 121 | | String[] tokensWithModifiers; |
|---|
| 122 | | |
|---|
| 123 | | if(query != null) |
|---|
| 124 | | { |
|---|
| 125 | | String filter = query.indexOf("FROM") == -1 ? null |
|---|
| 126 | | : query.substring(query.indexOf("FROM") + 5); |
|---|
| 127 | | |
|---|
| 128 | | //extract the SELECT piece if it exists |
|---|
| 129 | | if(query.contains("SELECT") && query.contains("FROM")) |
|---|
| 130 | | { |
|---|
| 131 | | String select = query.substring(query.indexOf("SELECT") + 7, query.indexOf("FROM") - 1); |
|---|
| 132 | | tokensWithModifiers = select.split("\\|"); |
|---|
| 133 | | |
|---|
| 134 | | //remove the split indicators |
|---|
| 135 | | for(int i = 0; i < tokensWithModifiers.length; i++) |
|---|
| 136 | | { |
|---|
| 137 | | if(tokensWithModifiers[i].contains(":")) |
|---|
| 138 | | { |
|---|
| 139 | | System.out.println(tokensWithModifiers[i]); |
|---|
| 140 | | String[] splits = tokensWithModifiers[i].substring(tokensWithModifiers[i].indexOf(":")).split(":"); |
|---|
| 141 | | tokenSplitters.put(tokensWithModifiers[i].substring(tokensWithModifiers[i].indexOf("{") + 1, tokensWithModifiers[i].indexOf("}")), splits); |
|---|
| 142 | | |
|---|
| 143 | | tokensWithModifiers[i] = tokensWithModifiers[i].substring(0, tokensWithModifiers[i].indexOf(":")); |
|---|
| 144 | | |
|---|
| 145 | | //populate token splitters |
|---|
| 146 | | |
|---|
| 147 | | |
|---|
| 148 | | } |
|---|
| 149 | | } |
|---|
| 150 | | } |
|---|
| 151 | | else |
|---|
| 152 | | tokensWithModifiers = null; |
|---|
| 153 | | } |
|---|
| 154 | | |
|---|
| 155 | | } |
|---|
| | 34 | private String filter; |
|---|
| | 35 | |
|---|
| | 36 | private String[] tokensWithModifiers; |
|---|
| | 37 | |
|---|
| | 38 | private String query; |
|---|
| | 39 | |
|---|
| | 40 | private String dataStyle; |
|---|
| | 41 | |
|---|
| | 42 | private Map<String, String[]> tokenSplitters; |
|---|
| | 43 | |
|---|
| | 44 | /** |
|---|
| | 45 | * Instantiates a new URLdataQuery. |
|---|
| | 46 | * |
|---|
| | 47 | * @param request the data request |
|---|
| | 48 | * |
|---|
| | 49 | * @throws IOException Signals that an I/O exception has occurred. |
|---|
| | 50 | * @throws ServletException the servlet exception |
|---|
| | 51 | */ |
|---|
| | 52 | public URLdataQuery(HttpServletRequest request) throws IOException, |
|---|
| | 53 | ServletException { |
|---|
| | 54 | try { |
|---|
| | 55 | if (request.getParameter("datastyle") == null) |
|---|
| | 56 | dataStyle = ""; |
|---|
| | 57 | else |
|---|
| | 58 | dataStyle = request.getParameter("datastyle"); |
|---|
| | 59 | |
|---|
| | 60 | query = request.getParameter("query"); |
|---|
| | 61 | |
|---|
| | 62 | tokenSplitters = new HashMap<String, String[]>(); |
|---|
| | 63 | |
|---|
| | 64 | if (query != null) { |
|---|
| | 65 | filter = query.indexOf("FROM") == -1 ? null : query |
|---|
| | 66 | .substring(query.indexOf("FROM") + 5); |
|---|
| | 67 | |
|---|
| | 68 | // extract the SELECT piece if it exists |
|---|
| | 69 | if (query.contains("SELECT") && query.contains("FROM")) { |
|---|
| | 70 | String select = query.substring( |
|---|
| | 71 | query.indexOf("SELECT") + 7, |
|---|
| | 72 | query.indexOf("FROM") - 1); |
|---|
| | 73 | tokensWithModifiers = select.split("\\|"); |
|---|
| | 74 | |
|---|
| | 75 | // remove the split indicators |
|---|
| | 76 | for (int i = 0; i < tokensWithModifiers.length; i++) { |
|---|
| | 77 | if (tokensWithModifiers[i].contains(":")) { |
|---|
| | 78 | // populate token splitters |
|---|
| | 79 | String[] splits = tokensWithModifiers[i].substring( |
|---|
| | 80 | tokensWithModifiers[i].indexOf(":") + 1) |
|---|
| | 81 | .split(":"); |
|---|
| | 82 | tokenSplitters.put( |
|---|
| | 83 | tokensWithModifiers[i] |
|---|
| | 84 | .substring(tokensWithModifiers[i] |
|---|
| | 85 | .indexOf("{") + 1, |
|---|
| | 86 | tokensWithModifiers[i] |
|---|
| | 87 | .indexOf("}")), |
|---|
| | 88 | splits); |
|---|
| | 89 | |
|---|
| | 90 | tokensWithModifiers[i] = tokensWithModifiers[i] |
|---|
| | 91 | .substring(0, tokensWithModifiers[i] |
|---|
| | 92 | .indexOf(":")); |
|---|
| | 93 | |
|---|
| | 94 | } |
|---|
| | 95 | } |
|---|
| | 96 | } else |
|---|
| | 97 | tokensWithModifiers = null; |
|---|
| | 98 | } |
|---|
| | 99 | } catch (Exception e) { |
|---|
| | 100 | log.error(e.getMessage()); |
|---|
| | 101 | |
|---|
| | 102 | e.printStackTrace(); |
|---|
| | 103 | } |
|---|
| | 104 | } |
|---|
| | 105 | |
|---|
| | 106 | /** |
|---|
| | 107 | * Gets the filter from the data query. |
|---|
| | 108 | * |
|---|
| | 109 | * @return the filter |
|---|
| | 110 | */ |
|---|
| | 111 | public String getFilter() { |
|---|
| | 112 | return filter; |
|---|
| | 113 | } |
|---|
| | 114 | |
|---|
| | 115 | /** |
|---|
| | 116 | * Gets the token splits from the data query for a given token. |
|---|
| | 117 | * |
|---|
| | 118 | * @param token the token to get the splits for |
|---|
| | 119 | * |
|---|
| | 120 | * @return the token splits |
|---|
| | 121 | */ |
|---|
| | 122 | public String[] getTokenSplits(String token) { |
|---|
| | 123 | return tokenSplitters.get(token); |
|---|
| | 124 | } |
|---|
| | 125 | |
|---|
| | 126 | /** |
|---|
| | 127 | * Gets the token names from the data query. |
|---|
| | 128 | * |
|---|
| | 129 | * @return the token names |
|---|
| | 130 | */ |
|---|
| | 131 | public String[] getTokenNames() { |
|---|
| | 132 | String[] tokenName = query.split("\\|"); |
|---|
| | 133 | |
|---|
| | 134 | String[] tokenNames = new String[tokenName.length]; |
|---|
| | 135 | |
|---|
| | 136 | // go through each of the tokens |
|---|
| | 137 | for (int i = 0; i < tokenName.length; i++) { |
|---|
| | 138 | // extract the actual token name |
|---|
| | 139 | String token = tokenName[i].substring( |
|---|
| | 140 | tokenName[i].indexOf("{") + 1, tokenName[i].indexOf("}")); |
|---|
| | 141 | |
|---|
| | 142 | tokenNames[i] = token; |
|---|
| | 143 | } |
|---|
| | 144 | |
|---|
| | 145 | return tokenNames; |
|---|
| | 146 | } |
|---|
| | 147 | |
|---|
| | 148 | /** |
|---|
| | 149 | * Gets the token names and their modifiers from the data query. |
|---|
| | 150 | * |
|---|
| | 151 | * @return the tokens with modifiers |
|---|
| | 152 | */ |
|---|
| | 153 | public String[] getTokensWithModifiers() { |
|---|
| | 154 | return tokensWithModifiers; |
|---|
| | 155 | } |
|---|
| | 156 | |
|---|
| | 157 | /** |
|---|
| | 158 | * Gets the data style from the data query. |
|---|
| | 159 | * |
|---|
| | 160 | * @return the data style |
|---|
| | 161 | */ |
|---|
| | 162 | public String getDataStyle() { |
|---|
| | 163 | return dataStyle; |
|---|
| | 164 | } |
|---|