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/DataResource.java

    r5078 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; 
    417import java.io.PrintWriter; 
    5 import java.util.HashSet; 
    6 import java.util.Map; 
    7 import java.util.List; 
    8 import java.util.ArrayList; 
    9 import java.util.Set; 
    1018 
    1119import javax.servlet.ServletException; 
     
    1523import org.apache.commons.logging.Log; 
    1624import org.apache.commons.logging.LogFactory; 
    17 import org.openmrs.Cohort; 
    18 import org.openmrs.Obs; 
    19 import org.openmrs.logic.LogicCriteria; 
    20 import org.openmrs.logic.LogicService; 
    21 import org.openmrs.logic.result.Result; 
    22 import org.openmrs.logic.result.Result.Datatype; 
    23 import org.openmrs.logic.LogicException; 
    24 import org.openmrs.api.context.Context; 
    25 import org.openmrs.cohort.CohortDefinition; 
    2625 
     26/** 
     27 * Handles requests for data and directs the query to appropriate data style 
     28 * printer. 
     29 */ 
     30public class DataResource implements RestResource { 
     31  private Log log = LogFactory.getLog(this.getClass()); 
    2732 
     33    /** 
     34     * Handles data request. 
     35     *  
     36     * @param operation the operation 
     37     * @param request the request 
     38     * @param response the response 
     39     *  
     40     * @throws ServletException the servlet exception 
     41     * @throws IOException Signals that an I/O exception has occurred. 
     42     *  
     43     * @see org.openmrs.module.logicws.web.RestResource#handleRequest(org.openmrs.module.logicws.web.RestResource.Operation, 
     44     *      javax.servlet.http.HttpServletRequest, 
     45     *      javax.servlet.http.HttpServletResponse) 
     46     */ 
     47    public void handleRequest(Operation operation, HttpServletRequest request, 
     48            HttpServletResponse response) throws ServletException, IOException { 
     49        PrintWriter out = response.getWriter(); 
    2850 
    29 public class DataResource implements RestResource { 
     51        log.info("Data request: " + request.getParameter("query")); 
     52        log.info("Data request: " + request.getParameter("datastyle")); 
    3053 
    31         private static final String ENCOUNTERDATE = "ENCOUNTERDATE"; 
    32         private static final String ENCOUNTERTYPE = "ENCOUNTERTYPE"; 
    33         private static final String OBSERVATIONLOCATION = "OBSERVATIONLOCATION"; 
    34         private static final String OBSERVATIONDATE = "OBSERVATIONDATE"; 
    35         private Log log = LogFactory.getLog(this.getClass()); 
    36          
    37         public void handleRequest(Operation operation, HttpServletRequest request, 
    38                         HttpServletResponse response) throws ServletException, IOException { 
    39                 PrintWriter out = response.getWriter(); 
     54        switch (operation) { 
     55        case POST: 
     56            URLdataQuery dataQuery = new URLdataQuery(request); 
    4057 
    41                 log.info("Data request: " + request.getParameter("query")); 
    42                 log.info("Data request: " + request.getParameter("datastyle")); 
    43                  
    44                 switch (operation) { 
    45                 case POST: 
    46                         URLdataQuery dataQuery = new URLdataQuery(request); 
    47                          
    48                         log.info("Built query"); 
    49                          
    50                         String filter = dataQuery.getFilter(); 
    51                          
    52                         String[] tokensFromQuery = dataQuery.getTokensWithModifiers(); 
    53                          
    54                         // Check for lack of parameters 
    55                         if (filter == null || tokensFromQuery == null) 
    56                         { 
    57                                 out.print("<dataset />"); 
    58                                 break; 
    59                         } 
    60                          
    61                         List<LogicCriteria> tokens = new ArrayList<LogicCriteria>(); 
    62                         for(int i = 0 ; i < tokensFromQuery.length; i++) 
    63                         { 
    64                                 tokens.add(LogicCriteria.parse(tokensFromQuery[i]));             
    65                         } 
    66                                  
    67                         out.print("<dataset>"); 
    68                         log.info("<dataset>"); 
    69                          
    70                          
    71                         String[] tokenNamesFromQuery = dataQuery.getTokenNames(); 
    72                          
    73                         // List columns 
    74                         out.print("<columns>"); 
    75                         log.info("<columns>"); 
    76                          
    77                         //we always want to add the patient id 
    78                         out.print("<column token=\"PATIENTID\" />"); 
    79                         log.info("<column token=\"PATIENTID\" />"); 
    80                          
    81                         if(dataQuery.getDataStyle().equals("stacked")) 
    82                         { 
    83                                 out.print("<column token=\"TOKEN\" />"); 
    84                                 log.info("<column token=\"TOKEN\" />"); 
    85                                  
    86                                 out.print("<column token=\"VALUE\" />"); 
    87                                 log.info("<column token=\"VALUE\" />"); 
    88                                  
    89                                 //add the neccessary splitter columns 
    90                                 //if any of the tokens split, we need to include this column 
    91                                 Set<String> uniqueSplits = new HashSet<String>(); 
    92                                  
    93                                 for(int i = 0; i < tokenNamesFromQuery.length; i++) 
    94                                 { 
    95                                         String[] splits = dataQuery.getTokenSplits(tokenNamesFromQuery[i]); 
    96                                          
    97                                         if(splits != null) 
    98                                         { 
    99                                                 for(int j = 0; j < splits.length; j++) 
    100                                                 { 
    101                                                         uniqueSplits.add(splits[j]); 
    102                                                 } 
    103                                         } 
    104                                          
    105                                          
    106                                 } 
    107                                  
    108                                 for(String split: uniqueSplits) 
    109                                 { 
    110                                         out.print("<column token=\"" + split + "\" />"); 
    111                                         log.info("<column token=\"" + split + "\" />"); 
    112                                 } 
    113                                  
    114                                 out.print("</columns>"); 
    115                                 log.info("</columns>"); 
    116          
    117                                 // List rows 
    118                                 out.print("<rows>"); 
    119                                 log.info("<rows>"); 
    120                                  
    121                                 Cohort patients; 
    122                                 LogicService ls = Context.getLogicService(); 
    123          
    124                                 try { 
    125          
    126                                         CohortDefinition cohortDefinition = Context.getCohortService().getCohortDefinition(filter); 
    127                                         patients = Context.getCohortService().evaluate(cohortDefinition, null); 
    128                                          
    129                                         Map<LogicCriteria, Map<Integer, Result>> resultsForTokens = null; 
    130                                         try 
    131                                         { 
    132                                                 resultsForTokens = ls.eval(patients, tokens); 
    133                                         }  
    134                                         catch (LogicException e)  
    135                                         { 
    136                                                 // TODO Auto-generated catch block 
    137                                                 e.printStackTrace(); 
    138                                         }        
    139                                                                                  
    140                                         for (Map.Entry<LogicCriteria, Map<Integer, Result>> resByCrit : resultsForTokens.entrySet())  
    141                                         {                                                
    142                                                  for(Map.Entry<Integer, Result> resByPat : resByCrit.getValue().entrySet()) 
    143                                                  { 
    144                                                          if(resByPat.getValue().size() > 0) 
    145                                                          { 
    146                                                                  for(int i = 0; i < resByPat.getValue().size(); i++) 
    147                                                                  { 
    148                                                                          out.print("<row>"); 
    149                                                                          log.info("<row>"); 
    150                                                                          
    151                                                                          //always print the patient id first 
    152                                                                          out.print("<value>" + resByPat.getKey() + "</value>"); 
    153                                                                          log.info("<value>" + resByPat.getKey() + "</value>"); 
    154                                                                           
    155                                                                          //resByCrit.getValue().get(i).getResultObject().toString(); 
    156                                                                           
    157                                                                          out.print("<value>" + resByCrit.getKey().getRootToken() + "</value>"); 
    158                                                                          log.info("<value>" + resByCrit.getKey().getRootToken() + "</value>"); 
    159                                                                           
    160                                                                          
    161                                                                          out.print("<value>" + resByPat.getValue().get(i).toString() + "</value>"); 
    162                                                                          log.info("<value>" + resByPat.getValue().get(i).toString() + "</value>"); 
    163                                                                           
    164                                                                          //add splitter values 
    165                                                                          for(String split: uniqueSplits) 
    166                                                                          { 
    167                                                                                  if(split.equals(OBSERVATIONDATE)) 
    168                                                                                  { 
    169                                                                                          if(resByPat.getValue().get(i).getResultDate() != null) 
    170                                                                                          { 
    171                                                                                                  out.print("<value>" + resByPat.getValue().get(i).getResultDate().getTime() + "</value>"); 
    172                                                                                                  log.info("<value>" + resByPat.getValue().get(i).getResultDate().getTime() + "</value>"); 
    173                                                                                          } 
    174                                                                                          else 
    175                                                                                          { 
    176                                                                                                  out.print("<value></value>"); 
    177                                                                                                  log.info("<value></value>");  
    178                                                                                          } 
    179                                                                                           
    180                                                                                           
    181                                                                                  } 
    182                                                                                  else if(split.equals(OBSERVATIONLOCATION)) 
    183                                                                                  { 
    184                                                                                          Obs obs = (Obs) resByPat.getValue().get(i).getResultObject(); 
    185                                                                                           
    186                                                                                          if(obs != null) 
    187                                                                                          { 
    188                                                                                                  out.print("<value>" + obs.getLocation() + "</value>"); 
    189                                                                                                  log.info("<value>" + obs.getLocation() + "</value>"); 
    190                                                                                          } 
    191                                                                                          else 
    192                                                                                          { 
    193                                                                                                  out.print("<value></value>"); 
    194                                                                                                  log.info("<value></value>"); 
    195                                                                                          }                                                                                        
    196                                                                                  } 
    197                                                                                  else if(split.equals(ENCOUNTERDATE)) 
    198                                                                                  { 
    199                                                                                          Obs obs = (Obs) resByPat.getValue().get(i).getResultObject(); 
    200                                                                                           
    201                                                                                          if(obs != null) 
    202                                                                                          { 
    203                                                                                                  out.print("<value>" + obs.getEncounter().getEncounterDatetime().getTime() + "</value>"); 
    204                                                                                                  log.info("<value>" + obs.getEncounter().getEncounterDatetime().getTime() + "</value>"); 
    205                                                                                          } 
    206                                                                                          else 
    207                                                                                          { 
    208                                                                                                  out.print("<value></value>"); 
    209                                                                                                  log.info("<value></value>"); 
    210                                                                                          }                                                                                        
    211                                                                                  } 
    212                                                                                  else if(split.equals(ENCOUNTERTYPE)) 
    213                                                                                  { 
    214                                                                                          Obs obs = (Obs) resByPat.getValue().get(i).getResultObject(); 
    215                                                                                           
    216                                                                                          if(obs != null) 
    217                                                                                          { 
    218                                                                                                  out.print("<value>" + obs.getEncounter().getEncounterType() + "</value>"); 
    219                                                                                                  log.info("<value>" + obs.getEncounter().getEncounterType() + "</value>"); 
    220                                                                                          } 
    221                                                                                          else 
    222                                                                                          { 
    223                                                                                                  out.print("<value></value>"); 
    224                                                                                                  log.info("<value></value>"); 
    225                                                                                          }                                                                                        
    226                                                                                  } 
    227                                                                          } 
    228                                                                           
    229                                                                          out.print("</row>"); 
    230                                                                          log.info("</row>"); 
    231                                                                  } 
    232                                                          } 
    233                                                          else 
    234                                                          { 
    235                                                                  out.print("<row>"); 
    236                                                                  log.info("<row>"); 
    237                                                                  
    238                                                                  //always print the patient id first 
    239                                                                  out.print("<value>" + resByPat.getKey() + "</value>"); 
    240                                                                  log.info("<value>" + resByPat.getKey() + "</value>"); 
    241                                                                   
    242                                                                  out.print("<value>" + resByCrit.getKey().getRootToken() + "</value>"); 
    243                                                                  log.info("<value>" + resByCrit.getKey().getRootToken() + "</value>"); 
    244                                                                   
    245                                                                  out.print("<value>" + resByPat.getValue().toString() + "</value>"); 
    246                                                                  log.info("<value>" + resByPat.getValue().toString() + "</value>"); 
    247                                                                   
    248                                                                   
    249                                                                 //add splitter values 
    250                                                                  for(String split: uniqueSplits) 
    251                                                                  { 
    252                                                                          if(split.equals(OBSERVATIONDATE)) 
    253                                                                          { 
    254                                                                                  if(resByPat.getValue().getResultDate() != null) 
    255                                                                                  { 
    256                                                                                          out.print("<value>" + resByPat.getValue().getResultDate().getTime() + "</value>"); 
    257                                                                                          log.info("<value>" + resByPat.getValue().getResultDate().getTime() + "</value>"); 
    258                                                                                  } 
    259                                                                                  else 
    260                                                                                  { 
    261                                                                                          out.print("<value></value>"); 
    262                                                                                          log.info("<value></value>"); 
    263                                                                                  } 
    264                                                                          } 
    265                                                                          else if(split.equals(OBSERVATIONLOCATION)) 
    266                                                                          { 
    267                                                                                  Obs obs = (Obs) resByPat.getValue().getResultObject(); 
    268                                                                                   
    269                                                                                  if(obs != null) 
    270                                                                                  { 
    271                                                                                          out.print("<value>" + obs.getLocation() + "</value>"); 
    272                                                                                          log.info("<value>" + obs.getLocation() + "</value>"); 
    273                                                                                  } 
    274                                                                                  else 
    275                                                                                  { 
    276                                                                                          out.print("<value></value>"); 
    277                                                                                          log.info("<value></value>"); 
    278                                                                                  }                                                                                
    279                                                                          }       
    280                                                                          else if(split.equals(ENCOUNTERDATE)) 
    281                                                                          { 
    282                                                                                  Obs obs = (Obs) resByPat.getValue().getResultObject(); 
    283                                                                                   
    284                                                                                  if(obs != null) 
    285                                                                                  { 
    286                                                                                          out.print("<value>" + obs.getEncounter().getEncounterDatetime().getTime() + "</value>"); 
    287                                                                                          log.info("<value>" + obs.getEncounter().getEncounterDatetime().getTime() + "</value>"); 
    288                                                                                  } 
    289                                                                                  else 
    290                                                                                  { 
    291                                                                                          out.print("<value></value>"); 
    292                                                                                          log.info("<value></value>"); 
    293                                                                                  }                                                                                
    294                                                                          } 
    295                                                                          else if(split.equals(ENCOUNTERTYPE)) 
    296                                                                          { 
    297                                                                                  Obs obs = (Obs) resByPat.getValue().getResultObject(); 
    298                                                                                   
    299                                                                                  if(obs != null) 
    300                                                                                  { 
    301                                                                                          out.print("<value>" + obs.getEncounter().getEncounterType() + "</value>"); 
    302                                                                                          log.info("<value>" + obs.getEncounter().getEncounterType() + "</value>"); 
    303                                                                                  } 
    304                                                                                  else 
    305                                                                                  { 
    306                                                                                          out.print("<value></value>"); 
    307                                                                                          log.info("<value></value>"); 
    308                                                                                  }                                                                                
    309                                                                          } 
    310                                                                  } 
    311                                                                  out.print("</row>"); 
    312                                                                  log.info("</row>");                                                              
    313                                                          }                                                        
    314                                                  }                                               
    315                                         } 
    316                                          
    317                                         out.print("</rows>"); 
    318                                         log.info("</rows>"); 
    319                                  
    320                                         out.print("</dataset>"); 
    321                                         log.info("<rows>");              
    322                                  
    323                                         break; 
    324                                 }  
    325                                  
    326                                 catch (org.openmrs.api.APIAuthenticationException e)  
    327                                 { 
    328                                         out.print("<error>" + e.toString() 
    329                                                         + "</error></rows></dataset>"); 
    330                                         break; 
    331                                 } 
    332                                  
    333                         } 
    334                         else if(dataQuery.getDataStyle().equals("flat")) 
    335                         { 
    336                                 int max = 5; 
    337                                  
    338                                 for (String t : tokenNamesFromQuery) 
    339                                 { 
    340                                         for(int i = 1; i < max + 1; i++) 
    341                                         { 
    342                                                 out.print("<column token=\"" + t + "_" + i + "\" />");           
    343                                                 log.info("<column token=\"" + t + "_" + i + "\" />"); 
    344                                                  
    345                                                 //add the split columns 
    346                                                 String[] splits = dataQuery.getTokenSplits(t); 
    347                                                  
    348                                                 if(splits != null) 
    349                                                 { 
    350                                                         for(String split: splits) 
    351                                                         { 
    352                                                                 out.print("<column token=\"" + t + "_" + i + "_" + split + "\" />"); 
    353                                                                 log.info("<column token=\"" + t + "_" + i + "_" + split + "\" />"); 
    354                                                         }                                                        
    355                                                 } 
    356                                                  
    357                                         } 
    358                                          
    359                                 } 
    360                                 out.print("</columns>"); 
    361                                 log.info("</columns>"); 
    362          
    363                                 // List rows 
    364                                 out.print("<rows>"); 
    365                                 log.info("<rows>"); 
    366                                  
    367                                 Cohort patients; 
    368                                 LogicService ls = Context.getLogicService(); 
    369          
    370                                 try { 
    371          
    372                                         CohortDefinition cohortDefinition = Context.getCohortService().getCohortDefinition(filter); 
    373                                         patients = Context.getCohortService().evaluate(cohortDefinition, null); 
    374                                          
    375                                         Map<LogicCriteria, Map<Integer, Result>> resultsForTokens = null; 
    376                                         try 
    377                                         { 
    378                                                 resultsForTokens = ls.eval(patients, tokens); 
    379                                         }  
    380                                         catch (LogicException e)  
    381                                         { 
    382                                                 // TODO Auto-generated catch block 
    383                                                 e.printStackTrace(); 
    384                                         }        
    385                                                                                  
    386                                         for (Integer patient: patients.getMemberIds())  
    387                                         { 
    388                                                 out.print("<row>"); 
    389                                                 log.info("<row>"); 
    390                                                  
    391                                                 //always print the patient id first 
    392                                                 out.print("<value>" + patient + "</value>"); 
    393                                                 log.info("<value>" + patient + "</value>"); 
    394                                                  
    395                                                 for (LogicCriteria token: tokens)  
    396                                                 { 
    397                                                         Result res = resultsForTokens.get(token).get(patient); 
    398                                                                                                                  
    399                                                         for(int i = 0; i < max; i++) 
    400                                                         { 
    401                                                                 out.print("<value>" + res.get(i) + "</value>"); 
    402                                                                 log.info("<value>" + res.get(i) + "</value>");           
    403                                                                  
    404                                                                         //add splitter values 
    405                                                                 String[] splits = dataQuery.getTokenSplits(token.getRootToken()); 
    406                                                          
    407                                                                 if(splits != null) 
    408                                                                 { 
    409                                     &nbs