Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register

Changeset 4990

Show
Ignore:
Timestamp:
07/20/08 23:59:55 (4 months ago)
Author:
kevjay
Message:

logicws: 1) Added latest jars from logic api refactoring branch, 2) changed data resource to accept queries of the new format: SELECT {token} optionalModifier x|{token2} optionalModifier y|... FROM cohortID, 3) changed call that populates filter to single Context.getCohortService().getAllCohortDefinitions() call, 4) uses Burke's new LogicCriteria.parse() method to evaluate results, and 5) added help class for easily getting information out data URL request

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs-modules/logicws/.classpath

    r4916 r4990  
    77        <classpathentry kind="lib" path="lib-common/commons-collections-3.1.jar"/> 
    88        <classpathentry kind="lib" path="lib-common/commons-io-1.1.jar"/> 
    9         <classpathentry kind="lib" path="lib-common/commons-logging-1.0.4.jar"/> 
    109        <classpathentry kind="lib" path="lib-common/hibernate325.jar"/> 
    1110        <classpathentry kind="lib" path="lib-common/junit-3.8.1.jar"/> 
     
    2928        <classpathentry kind="lib" path="lib-common/cglib-2.1_3.jar"/> 
    3029        <classpathentry kind="lib" path="lib-common/ehcache-1.2.4.jar"/> 
    31         <classpathentry kind="lib" path="lib-common/log4j.jar"/> 
    3230        <classpathentry kind="lib" path="lib-common/hapi-0.5.jar"/> 
    3331        <classpathentry kind="lib" path="lib-common/simple-xml-1.6.1-mod.jar"/> 
    34         <classpathentry kind="lib" path="lib-common/tests-openmrs-api-1.3.0.00.4356.jar"/> 
    35         <classpathentry kind="lib" path="lib-common/web-openmrs-api-1.3.0.00.4356.jar"/> 
    3632        <classpathentry kind="lib" path="lib-common/httpunit.jar"/> 
    3733        <classpathentry kind="lib" path="lib-common/js-1.6R5.jar"/> 
    3834        <classpathentry kind="lib" path="lib-common/nekohtml-0.9.5.jar"/> 
    39         <classpathentry kind="lib" path="lib-common/openmrs-api-1.3.0.00.4641.jar" sourcepath="/openmrs-logic-api-refactoring"/> 
     35        <classpathentry kind="lib" path="lib-common/commons-logging-1.1.1.jar"/> 
     36        <classpathentry kind="lib" path="lib-common/log4j-1.2.15.jar"/> 
     37        <classpathentry kind="lib" path="lib-common/openmrs-api-1.3.0.13.4989.jar" sourcepath="/logic-api-refactoring"/> 
     38        <classpathentry kind="lib" path="lib-common/tests-openmrs-api-1.3.0.13.4989.jar"/> 
     39        <classpathentry kind="lib" path="lib-common/web-openmrs-api-1.3.0.13.4989.jar"/> 
    4040        <classpathentry kind="output" path="build"/> 
    4141</classpath> 
  • openmrs-modules/logicws/test/org/openmrs/module/logicws/test/LogicWsHTTPtest.java

    r4918 r4990  
    129129        } 
    130130         
     131        public void testGetDataWhereCohortIsEmpty()  
     132        {                
     133            WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?filter=3:org.openmrs.reporting.PatientSearch&token=AGE&token=GENDER"); 
     134             
     135            try  
     136            { 
     137                        WebResponse resp = wc.getResponse(req);                  
     138                        assertTrue(resp.getText().indexOf("dataset") > 0);                       
     139                         
     140                        System.out.println("\n"); 
     141                        System.out.println("Data Response:\n"); 
     142                        prettyPrint(resp.getDOM(), System.out); 
     143                        System.out.println("\n"); 
     144                         
     145                } catch (IOException e) { 
     146                        // TODO Auto-generated catch block 
     147                        e.printStackTrace(); 
     148                } catch (SAXException e) { 
     149                        // TODO Auto-generated catch block 
     150                        e.printStackTrace(); 
     151                }  
     152                catch (Exception e) { 
     153                        // TODO Auto-generated catch block 
     154                        e.printStackTrace(); 
     155                }                
     156        } 
     157         
     158        public void testGetDataWhenNoArguments()  
     159        {                
     160            WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData"); 
     161             
     162            try  
     163            { 
     164                        WebResponse resp = wc.getResponse(req);                  
     165                        assertTrue(resp.getText().indexOf("dataset") > 0);                       
     166                         
     167                        System.out.println("\n"); 
     168                        System.out.println("Data Response:\n"); 
     169                        prettyPrint(resp.getDOM(), System.out); 
     170                        System.out.println("\n"); 
     171                         
     172                } catch (IOException e) { 
     173                        // TODO Auto-generated catch block 
     174                        e.printStackTrace(); 
     175                } catch (SAXException e) { 
     176                        // TODO Auto-generated catch block 
     177                        e.printStackTrace(); 
     178                }  
     179                catch (Exception e) { 
     180                        // TODO Auto-generated catch block 
     181                        e.printStackTrace(); 
     182                }                
     183        } 
     184 
    131185        public void testGetData()  
    132186        {                
    133             WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?filter=3&filterType=static&token=AGE&token=GENDER"); 
    134              
     187           // WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{GENDER}%20FROM%203:org.openmrs.cohort.StaticCohortDefinition"); 
     188               WebRequest req = new PostMethodWebRequest("http://localhost:8080/openmrs/moduleServlet/logicws/api/getData?query=SELECT%20{WEIGHT%20(KG)}%20FROM%203:org.openmrs.cohort.StaticCohortDefinition"); 
    135189            try  
    136190            { 
  • openmrs-modules/logicws/web/src/org/openmrs/module/logicws/web/DataResource.java

    r4918 r4990  
    1414import javax.servlet.http.HttpServletResponse; 
    1515 
     16import org.apache.commons.logging.Log; 
     17import org.apache.commons.logging.LogFactory; 
    1618import org.openmrs.Cohort; 
    1719import org.openmrs.Obs; 
     20import org.openmrs.Patient; 
     21import org.openmrs.logic.LogicContext; 
     22import org.openmrs.logic.LogicCriteria; 
    1823import org.openmrs.logic.LogicService; 
    1924import org.openmrs.logic.result.Result; 
    2025import org.openmrs.logic.LogicException; 
    2126import org.openmrs.api.context.Context; 
     27import org.openmrs.api.impl.CohortServiceImpl; 
     28import org.openmrs.cohort.CohortDefinition; 
     29 
     30 
    2231 
    2332public class DataResource implements RestResource { 
    2433 
     34        private Log log = LogFactory.getLog(this.getClass()); 
     35         
    2536        public void handleRequest(Operation operation, HttpServletRequest request, 
    2637                        HttpServletResponse response) throws ServletException, IOException { 
    2738                PrintWriter out = response.getWriter(); 
    2839 
     40                log.info("Data request: " + request.getParameter("query")); 
     41                 
     42                 
    2943                switch (operation) { 
    3044                case POST: 
    3145                        URLdataQuery dataQuery = new URLdataQuery(request); 
    32                         Integer filter = dataQuery.getFilter(); 
    33                         String filterType = dataQuery.getFilterType(); 
    34                         String[] tokens = dataQuery.getTokens(); 
    35  
     46                         
     47                        String filter = dataQuery.getFilter(); 
     48                         
     49                        String[] tokensFromQuery = dataQuery.getTokensWithModifiers(); 
     50                         
    3651                        // Check for lack of parameters 
    37                         if (filter == null 
    38                                         || filterType == null 
    39                                         || tokens == null 
    40                                         || (!filterType.equals("static") && !filterType 
    41                                                         .equals("dynamic"))) { 
     52                        if (filter == null || tokensFromQuery == null) 
     53                        { 
    4254                                out.print("<dataset />"); 
    4355                                break; 
    4456                        } 
    45  
     57                         
     58                        List<LogicCriteria> tokens = new ArrayList<LogicCriteria>(); 
     59                        for(int i = 0 ; i < tokensFromQuery.length; i++) 
     60                        { 
     61                                tokens.add(LogicCriteria.parse(tokensFromQuery[i]));             
     62                                //tokens.add(new LogicCriteria(tokensFromQuery[i])); 
     63                        } 
     64                                 
    4665                        out.print("<dataset>"); 
    4766 
     67                        String[] tokenNamesFromQuery = dataQuery.getTokenNames(); 
     68                         
    4869                        // List columns 
    4970                        out.print("<columns>"); 
    50                         for (String t : tokens
     71                        for (String t : tokenNamesFromQuery
    5172                        { 
    52                                 out.print("<column token=\"" + t + "\" />"); 
    53                                                                  
     73                                out.print("<column token=\"" + t + "\" />");                                                             
     74                                 
     75                                /* 
    5476                                out.print("<column token=\"" + t + "_OBSERVATIONDATE\" />"); 
    5577                                out.print("<column token=\"" + t + "_OBSERVATIONLOCATION\" />"); 
    5678                                out.print("<column token=\"" + t + "_ENCOUNTERTYPE\" />"); 
    57                                 out.print("<column token=\"" + t + "_ENCOUNTERDATE\" />");                               
     79                                out.print("<column token=\"" + t + "_ENCOUNTERDATE\" />"); 
     80                                */                               
    5881                        } 
    5982                        out.print("</columns>"); 
     
    6184                        // List rows 
    6285                        out.print("<rows>"); 
     86                         
    6387                        Cohort patients; 
    6488                        LogicService ls = Context.getLogicService(); 
     
    6690                        try { 
    6791 
    68  
    69                                 if (filterType.equals("static")) { 
    70                                        patients = Context.getCohortService().getCohort(filter); 
    71                                 } else { 
    72                                        patients = Context.getReportObjectService().getPatientFilterById(filter).filter(Context.getPatientSetService().getAllPatients(), null); 
    73                                 } 
    74  
    75                                 List<Map<Integer, Result>> resultsForTokens = new ArrayList<Map<Integer, Result>>(); 
    76                                 try { 
    77                                        for (String t : tokens)
    78                                                resultsForTokens.add(ls.eval(patients, t)); 
    79                                         } 
    80                                 } catch (LogicException e) { 
    81                                        throw new ServletException(e); 
    82                                 } 
    83                                 for (Integer patient : patients.getPatientIds())
     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 (Integer patient: patients.getMemberIds())  
     107                               
    84108                                        out.print("<row>"); 
    85                                         for (Map<Integer, Result> map : resultsForTokens) { 
    86                                                 Result res = map.get(patient); 
     109                                        for (LogicCriteria token: tokens)  
     110                                        { 
     111                                                Result res = resultsForTokens.get(token).get(patient); 
    87112                                                                                                 
    88113                                                out.print("<value>" + res + "</value>"); 
    89114                                                 
     115                                                /* 
    90116                                                Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    91117                                                                                                 
     118                                                 
    92119                                                //get the Obs from the result 
    93120                                                Obs obs = (Obs)res.toObject(); 
     
    129156                                                 
    130157                                                } 
     158                                                */ 
    131159                                        } 
    132160                                        out.print("</row>"); 
     
    135163                                out.print("</rows>"); 
    136164                                out.print("</dataset>"); 
     165                 
     166                                 
    137167                                break; 
    138168                        } catch (org.openmrs.api.APIAuthenticationException e) { 
  • openmrs-modules/logicws/web/src/org/openmrs/module/logicws/web/FilterResource.java

    r4918 r4990  
    1111import org.openmrs.reporting.PatientFilter; 
    1212import org.openmrs.api.context.Context; 
     13import org.openmrs.cohort.CohortDefinitionItemHolder; 
    1314 
    1415public class FilterResource implements RestResource { 
     
    2223            out.print("<filterList>"); 
    2324 
     25             
     26            for (CohortDefinitionItemHolder c : Context.getCohortService().getAllCohortDefinitions())  
     27                { 
     28                out.print("<filter name=\"" + c.getName() + "\" id=\"" + c.getKey() + "\" />"); 
     29            } 
     30             
     31             
     32            /* 
    2433            // List Filters <filter name="VALUE" id="VALUE" /> 
    2534            for (Cohort c : Context.getCohortService().getCohorts()) { 
     
    2837            } 
    2938             
    30             /* 
     39             
    3140            for (PatientFilter pf : Context.getReportObjectService() 
    3241                    .getAllPatientFilters()) { 
  • openmrs-modules/logicws/web/src/org/openmrs/module/logicws/web/TokenResource.java

    r4918 r4990  
    2525            HttpServletResponse response) throws ServletException, IOException { 
    2626        LogicService ls = Context.getLogicService(); 
     27         
    2728        PrintWriter out = response.getWriter(); 
    2829 
  • openmrs-modules/logicws/web/src/org/openmrs/module/logicws/web/TokenTagResource.java

    r4918 r4990  
    99 
    1010import java.util.Collection; 
     11import java.util.Set; 
    1112 
    1213import org.openmrs.api.context.Context; 
  • openmrs-modules/logicws/web/src/org/openmrs/module/logicws/web/URLdataQuery.java

    r4919 r4990  
    22 
    33import java.io.IOException; 
     4import java.util.ArrayList; 
     5import java.util.List; 
    46 
    57import javax.servlet.ServletException; 
     
    810public class URLdataQuery  
    911{ 
    10         Integer filter; 
     12        String filter; 
    1113         
    12         String filterType
     14        String[] tokensWithModifiers
    1315         
    14         String[] tokens
     16        String query
    1517         
    1618        public URLdataQuery(HttpServletRequest request) throws IOException, ServletException 
    1719        { 
    18                 filter = request.getParameter("filter") == null ? null 
    19                                 : Integer.parseInt(request.getParameter("filter")); 
     20                query = request.getParameter("query"); 
    2021                 
    21                 filterType = request.getParameter("filterType"); 
    22                  
    23                 tokens = request.getParameterValues("token"); 
     22                if(query != null) 
     23                {        
     24                        filter = query.indexOf("FROM") == -1 ? null 
     25                                        : query.substring(query.indexOf("FROM") + 5); 
     26                         
     27                        //extract the SELECT piece if it exists 
     28                        if(query.contains("SELECT") && query.contains("FROM")) 
     29                        { 
     30                                String select = query.substring(query.indexOf("SELECT") + 7, query.indexOf("FROM") - 1); 
     31                                tokensWithModifiers = select.split("\\|"); 
     32                        } 
     33                        else 
     34                                tokensWithModifiers = null; 
     35                } 
    2436        } 
    2537 
    26         public Integer getFilter()  
     38        public String getFilter()  
    2739        { 
    2840                return filter; 
    2941        } 
    30  
    31         public String getFilterType() { 
    32                 return filterType; 
     42         
     43        public String[] getTokenNames() 
     44        { 
     45                String[] tokenName = query.split("\\|"); 
     46                 
     47                String[] tokenNames = new String[tokenName.length]; 
     48                 
     49                //go through each of the tokens 
     50                for(int i = 0; i < tokenName.length; i++) 
     51                { 
     52                                //extract the actual token name 
     53                        String token = tokenName[i].substring(tokenName[i].indexOf("{") + 1, tokenName[i].indexOf("}")); 
     54                         
     55                        tokenNames[i] = token; 
     56                } 
     57                 
     58                return tokenNames; 
    3359        } 
    34  
    35         public String[] getTokens() { 
    36                 return tokens
     60         
     61        public String[] getTokensWithModifiers() { 
     62                return tokensWithModifiers
    3763        } 
    3864