Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register

Changeset 5020

Show
Ignore:
Timestamp:
07/23/08 02:50:41 (4 months ago)
Author:
kevjay
Message:

logicws: Made patient id always returned as first column regardless of query. Added initial support for EAV stacked style data rows. The following parameter needs to be passed to request URL: datastyle=stacked

Files:

Legend:

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

    r4990 r5020  
    33import java.io.IOException; 
    44import java.io.PrintWriter; 
    5 import java.sql.Date; 
    6 import java.text.Format; 
    7 import java.text.SimpleDateFormat; 
    85import java.util.Map; 
    96import java.util.List; 
     
    1714import org.apache.commons.logging.LogFactory; 
    1815import org.openmrs.Cohort; 
    19 import org.openmrs.Obs; 
    20 import org.openmrs.Patient; 
    21 import org.openmrs.logic.LogicContext; 
    2216import org.openmrs.logic.LogicCriteria; 
    2317import org.openmrs.logic.LogicService; 
     
    2519import org.openmrs.logic.LogicException; 
    2620import org.openmrs.api.context.Context; 
    27 import org.openmrs.api.impl.CohortServiceImpl; 
    2821import org.openmrs.cohort.CohortDefinition; 
    2922 
     
    6457                                 
    6558                        out.print("<dataset>"); 
    66  
     59                        log.info("<dataset>"); 
     60                         
     61                         
    6762                        String[] tokenNamesFromQuery = dataQuery.getTokenNames(); 
    6863                         
    6964                        // List columns 
    7065                        out.print("<columns>"); 
    71                         for (String t : tokenNamesFromQuery) 
     66                        log.info("<columns>"); 
     67                         
     68                        //we always want to add the patient id 
     69                        out.print("<column token=\"PATIENTID\" />"); 
     70                        log.info("<column token=\"PATIENTID\" />"); 
     71                         
     72                        if(dataQuery.getDataStyle().equals("stacked")) 
    7273                        { 
    73                                 out.print("<column token=\"" + t + "\" />");                                                             
    74                                  
    75                                 /* 
    76                                 out.print("<column token=\"" + t + "_OBSERVATIONDATE\" />"); 
    77                                 out.print("<column token=\"" + t + "_OBSERVATIONLOCATION\" />"); 
    78                                 out.print("<column token=\"" + t + "_ENCOUNTERTYPE\" />"); 
    79                                 out.print("<column token=\"" + t + "_ENCOUNTERDATE\" />"); 
    80                                 */                               
    81                         } 
    82                         out.print("</columns>"); 
    83  
    84                         // List rows 
    85                         out.print("<rows>"); 
    86                          
    87                         Cohort patients; 
    88                         LogicService ls = Context.getLogicService(); 
    89  
    90                         try { 
    91  
    92                                 CohortDefinition cohortDefinition = Context.getCohortService().getCohortDefinition(filter); 
    93                                 patients = Context.getCohortService().evaluate(cohortDefinition, null); 
    94                                  
    95                                 Map<LogicCriteria, Map<Integer, Result>> resultsForTokens = null; 
    96                                 try 
     74                                out.print("<column token=\"KEY\" />"); 
     75                                log.info("<column token=\"KEY\" />"); 
     76                                 
     77                                out.print("<column token=\"VALUE\" />"); 
     78                                log.info("<column token=\"VALUE\" />"); 
     79                                 
     80                                out.print("</columns>"); 
     81                                log.info("</columns>"); 
     82         
     83                                // List rows 
     84                                out.print("<rows>"); 
     85                                log.info("<rows>"); 
     86                                 
     87                                Cohort patients; 
     88                                LogicService ls = Context.getLogicService(); 
     89         
     90                                try { 
     91         
     92                                        CohortDefinition cohortDefinition = Context.getCohortService().getCohortDefinition(filter); 
     93                                        patients = Context.getCohortService().evaluate(cohortDefinition, null); 
     94                                         
     95                                        Map<LogicCriteria, Map<Integer, Result>> resultsForTokens = null; 
     96                                        try 
     97                                        { 
     98                                                resultsForTokens = ls.eval(patients, tokens); 
     99                                        }  
     100                                        catch (LogicException e)  
     101                                        { 
     102                                                // TODO Auto-generated catch block 
     103                                                e.printStackTrace(); 
     104                                        }        
     105                                                                                 
     106                                        for (Map.Entry<LogicCriteria, Map<Integer, Result>> resByCrit : resultsForTokens.entrySet())  
     107                                        {                                                
     108                                                 for(Map.Entry<Integer, Result> resByPat : resByCrit.getValue().entrySet()) 
     109                                                 { 
     110                                                         if(resByPat.getValue().size() > 0) 
     111                                                         { 
     112                                                                 for(int i = 0; i < resByPat.getValue().size(); i++) 
     113                                                                 { 
     114                                                                         out.print("<row>"); 
     115                                                                         log.info("<row>"); 
     116                                                                         
     117                                                                         //always print the patient id first 
     118                                                                         out.print("<value>" + resByPat.getKey() + "</value>"); 
     119                                                                         log.info("<value>" + resByPat.getKey() + "</value>"); 
     120                                                                          
     121                                                                         out.print("<value>" + resByCrit.getKey().getRootToken() + "</value>"); 
     122                                                                         log.info("<value>" + resByCrit.getKey().getRootToken() + "</value>"); 
     123                                                                          
     124                                                                         out.print("<value>" + resByPat.getValue().get(i).toString() + "</value>"); 
     125                                                                         log.info("<value>" + resByPat.getValue().get(i).toString() + "</value>"); 
     126                                                                          
     127                                                                         out.print("</row>"); 
     128                                                                         log.info("</row>"); 
     129                                                                 } 
     130                                                         } 
     131                                                         else 
     132                                                         { 
     133                                                                 out.print("<row>"); 
     134                                                                 log.info("<row>"); 
     135                                                                 
     136                                                                 //always print the patient id first 
     137                                                                 out.print("<value>" + resByPat.getKey() + "</value>"); 
     138                                                                 log.info("<value>" + resByPat.getKey() + "</value>"); 
     139                                                                  
     140                                                                 out.print("<value>" + resByCrit.getKey().getRootToken() + "</value>"); 
     141                                                                 log.info("<value>" + resByCrit.getKey().getRootToken() + "</value>"); 
     142                                                                  
     143                                                                 out.print("<value>" + resByPat.getValue().toString() + "</value>"); 
     144                                                                 log.info("<value>" + resByPat.getValue().toString() + "</value>"); 
     145                                                                  
     146                                                                 out.print("</row>"); 
     147                                                                 log.info("</row>"); 
     148                                                                  
     149                                                         } 
     150                                                          
     151                                                 } 
     152                                                 
     153                                        } 
     154                                         
     155                                        out.print("</rows>"); 
     156                                        log.info("</rows>"); 
     157                                 
     158                                        out.print("</dataset>"); 
     159                                        log.info("<rows>");              
     160                                 
     161                                        break; 
     162                                }  
     163                                 
     164                                catch (org.openmrs.api.APIAuthenticationException e)  
    97165                                { 
    98                                         resultsForTokens = ls.eval(patients, tokens); 
    99                                 }  
    100                                 catch (LogicException e)  
     166                                        out.print("<error>" + e.toString() 
     167                                                        + "</error></rows></dataset>"); 
     168                                        break; 
     169                                } 
     170                                 
     171                        } 
     172                        else 
     173                        { 
     174                                for (String t : tokenNamesFromQuery) 
    101175                                { 
    102                                         // TODO Auto-generated catch block 
    103                                         e.printStackTrace(); 
    104                                 }        
    105                                  
    106                                 for (Integer patient: patients.getMemberIds())  
    107                                 { 
    108                                         out.print("<row>"); 
    109                                         for (LogicCriteria token: tokens)  
    110                                         { 
    111                                                 Result res = resultsForTokens.get(token).get(patient); 
    112                                                                                                  
    113                                                 out.print("<value>" + res + "</value>"); 
    114                                                  
    115                                                 /* 
    116                                                 Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    117                                                                                                  
    118                                                  
    119                                                 //get the Obs from the result 
    120                                                 Obs obs = (Obs)res.toObject(); 
    121                                          
    122                                                 if(obs == null) 
     176                                        out.print("<column token=\"" + t + "\" />");             
     177                                        log.info("<column token=\"" + t + "\" />"); 
     178                                         
     179                                        /* 
     180                                        out.print("<column token=\"" + t + "_OBSERVATIONDATE\" />"); 
     181                                        out.print("<column token=\"" + t + "_OBSERVATIONLOCATION\" />"); 
     182                                        out.print("<column token=\"" + t + "_ENCOUNTERTYPE\" />"); 
     183                                        out.print("<column token=\"" + t + "_ENCOUNTERDATE\" />"); 
     184                                        */                               
     185                                } 
     186                                out.print("</columns>"); 
     187                                log.info("</columns>"); 
     188         
     189                                // List rows 
     190                                out.print("<rows>"); 
     191                                log.info("<rows>"); 
     192                                 
     193                                Cohort patients; 
     194                                LogicService ls = Context.getLogicService(); 
     195         
     196                                try { 
     197         
     198                                        CohortDefinition cohortDefinition = Context.getCohortService().getCohortDefinition(filter); 
     199                                        patients = Context.getCohortService().evaluate(cohortDefinition, null); 
     200                                         
     201                                        Map<LogicCriteria, Map<Integer, Result>> resultsForTokens = null; 
     202                                        try 
     203                                        { 
     204                                                resultsForTokens = ls.eval(patients, tokens); 
     205                                        }  
     206                                        catch (LogicException e)  
     207                                        { 
     208                                                // TODO Auto-generated catch block 
     209                                                e.printStackTrace(); 
     210                                        }        
     211                                         
     212                                        for (Integer patient: patients.getMemberIds())  
     213                                        { 
     214                                                out.print("<row>"); 
     215                                                log.info("<row>"); 
     216                                                 
     217                                                //always print the patient id first 
     218                                                out.print("<value>" + patient + "</value>"); 
     219                                                log.info("<value>" + patient + "</value>"); 
     220                                                 
     221                                                for (LogicCriteria token: tokens)  
    123222                                                { 
    124                                                         if(res.getResultDate() != null) 
     223                                                        Result res = resultsForTokens.get(token).get(patient); 
     224                                                                                                         
     225                                                        out.print("<value>" + res + "</value>"); 
     226                                                        log.info("<value>" + res + "</value>"); 
     227                                                 
     228                                                        //res.getResultDate(); 
     229                                                         
     230                                                        /* 
     231                                                        Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     232                                                                                                         
     233                                                         
     234                                                        //get the Obs from the result 
     235                                                        Obs obs = (Obs)res.toObject(); 
     236                                                 
     237                                                        if(obs == null) 
    125238                                                        { 
    126                                                                 out.print("<value>" + formatter.format(res.getResultDate()) + "</value>"); 
     239                                                                if(res.getResultDate() != null) 
     240                                                                { 
     241                                                                        out.print("<value>" + formatter.format(res.getResultDate()) + "</value>"); 
     242                                                                } 
     243                                                                 
     244                                                                else 
     245                                                                { 
     246                                                                        out.print("<value>" + null + "</value>"); 
     247                                                                } 
     248                                                                 
     249                                                                //location 
     250                                                                out.print("<value>" + null + "</value>"); 
     251                                                                 
     252                                                                //encounter type 
     253                                                                out.print("<value>" + null + "</value>"); 
     254                                                                 
     255                                                                //encounter date 
     256                                                                out.print("<value>" + null + "</value>");                
    127257                                                        } 
    128258                                                         
    129259                                                        else 
    130                                                         { 
    131                                                                 out.print("<value>" + null + "</value>"); 
     260                                                        {                                                        
     261                                                                out.print("<value>" + formatter.format(obs.getDateCreated()) + "</value>"); 
     262                                                                                                                 
     263                                                                //location 
     264                                                                out.print("<value>" + obs.getLocation().getName() + "</value>"); 
     265                                                                 
     266                                                                //encounter type 
     267                                                                out.print("<value>" + formatter.format(obs.getEncounter().getEncounterType().getName()) + "</value>"); 
     268                                                                 
     269                                                                //encounter date 
     270                                                                out.print("<value>" + obs.getEncounter().getDateCreated() + "</value>");                 
     271                                                         
    132272                                                        } 
    133                                                          
    134                                                         //location 
    135                                                         out.print("<value>" + null + "</value>"); 
    136                                                          
    137                                                         //encounter type 
    138                                                         out.print("<value>" + null + "</value>"); 
    139                                                          
    140                                                         //encounter date 
    141                                                         out.print("<value>" + null + "</value>");                
     273                                                        */ 
    142274                                                } 
    143                                                  
    144                                                 else 
    145                                                 {                                                        
    146                                                         out.print("<value>" + formatter.format(obs.getDateCreated()) + "</value>"); 
    147                                                                                                          
    148                                                         //location 
    149                                                         out.print("<value>" + obs.getLocation().getName() + "</value>"); 
    150                                                          
    151                                                         //encounter type 
    152                                                         out.print("<value>" + formatter.format(obs.getEncounter().getEncounterType().getName()) + "</value>"); 
    153                                                          
    154                                                         //encounter date 
    155                                                         out.print("<value>" + obs.getEncounter().getDateCreated() + "</value>");                 
    156                                                  
    157                                                 } 
    158                                                 */ 
     275                                                out.print("</row>"); 
     276                                                log.info("</row>"); 
    159277                                        } 
    160                                         out.print("</row>"); 
    161                                 } 
    162  
     278                                 
    163279                                out.print("</rows>"); 
     280                                log.info("</rows>"); 
     281                                 
    164282                                out.print("</dataset>"); 
     283                                log.info("<rows>"); 
    165284                 
    166285                                 
     
    170289                                                + "</error></rows></dataset>"); 
    171290                                break; 
     291                        } 
    172292                        } 
    173293                case GET: 
  • openmrs-modules/logicws/web/src/org/openmrs/module/logicws/web/URLdataQuery.java

    r4990 r5020  
    1010public class URLdataQuery  
    1111{ 
    12         String filter; 
     12        private String filter; 
    1313         
    14         String[] tokensWithModifiers; 
     14        private String[] tokensWithModifiers; 
    1515         
    16         String query; 
     16        private String query; 
     17         
     18        private String dataStyle; 
    1719         
    1820        public URLdataQuery(HttpServletRequest request) throws IOException, ServletException 
    1921        { 
     22                if(request.getParameter("datastyle") == null) 
     23                        dataStyle = ""; 
     24                else 
     25                        dataStyle = request.getParameter("datastyle"); 
     26                 
    2027                query = request.getParameter("query"); 
     28                 
    2129                 
    2230                if(query != null) 
     
    6270                return tokensWithModifiers; 
    6371        } 
     72 
     73        public String getDataStyle() { 
     74                return dataStyle; 
     75        } 
    6476         
    6577