Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
08/03/08 02:50:22 (5 months ago)
Author:
kevjay
Message:

logicws: Added javadocs, formatted, and reorganized code.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs-modules/logicws/web/src/org/openmrs/module/logicws/web/URLdataQuery.java

    r5059 r5144  
     1/** 
     2 * The contents of this file are subject to the OpenMRS Public License 
     3 * Version 1.0 (the "License"); you may not use this file except in 
     4 * compliance with the License. You may obtain a copy of the License at 
     5 * http://license.openmrs.org 
     6 * 
     7 * Software distributed under the License is distributed on an "AS IS" 
     8 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the 
     9 * License for the specific language governing rights and limitations 
     10 * under the License. 
     11 * 
     12 * Copyright (C) OpenMRS, LLC.  All Rights Reserved. 
     13 */ 
    114package org.openmrs.module.logicws.web; 
    215 
    316import java.io.IOException; 
    4 import java.util.ArrayList; 
    517import java.util.HashMap; 
    6 import java.util.List; 
    718import java.util.Map; 
    819 
     
    1324import org.apache.commons.logging.LogFactory; 
    1425 
    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 */ 
     30public class URLdataQuery { 
    7931 
    80         public String getFilter()  
    81         { 
    82                 return filter; 
    83         } 
    84          
    85         public String[] getTokenSplits(String token) 
    86         { 
    87                 return tokenSplitters.get(token); 
    88         } 
    89          
    90         public String[] getTokenNames() 
    91         { 
    92                 String[] tokenName = query.split("\\|"); 
    93                  
    94                 String[] tokenNames = new String[tokenName.length]; 
    95                  
    96                 //go through each of the tokens 
    97                 for(int i = 0; i < tokenName.length; i++) 
    98                 { 
    99                                 //extract the actual token name 
    100                         String token = tokenName[i].substring(tokenName[i].indexOf("{") + 1, tokenName[i].indexOf("}")); 
    101                          
    102                         tokenNames[i] = token; 
    103                 } 
    104                  
    105                 return tokenNames; 
    106         } 
    107          
    108         public String[] getTokensWithModifiers() { 
    109                 return tokensWithModifiers; 
    110         } 
     32    private Log log = LogFactory.getLog(this.getClass()); 
    11133 
    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    } 
    156165}