Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
05/28/08 05:16:52 (6 months ago)
Author:
bmckown
Message:

complex_obs branch: Merging trunk to complex-obs [3891] [4378] TODO: sqldiff.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs/branches/complex-obs/src/api/org/openmrs/api/impl/ConceptServiceImpl.java

    • Property svn:eol-style set to CRLF
    r4239 r4417  
    3737import org.openmrs.ConceptProposal; 
    3838import org.openmrs.ConceptSet; 
     39import org.openmrs.ConceptSynonym; 
    3940import org.openmrs.ConceptWord; 
    4041import org.openmrs.Drug; 
     42import org.openmrs.Obs; 
     43import org.openmrs.User; 
     44import org.openmrs.api.APIException; 
    4145import org.openmrs.api.ConceptService; 
    4246import org.openmrs.api.ConceptsLockedException; 
     
    4448import org.openmrs.api.db.ConceptDAO; 
    4549import org.openmrs.util.OpenmrsConstants; 
     50import org.springframework.util.StringUtils; 
    4651 
    4752/** 
    48  * Concept-related services 
    49  * @version 1.0 
     53 * Default Implementation of ConceptService service layer classes 
     54 *  
     55 * @see org.openmrs.ConceptService to access these methods 
    5056 */ 
    51 public class ConceptServiceImpl implements ConceptService { 
     57public class ConceptServiceImpl extends BaseOpenmrsService implements ConceptService { 
    5258 
    5359        private final Log log = LogFactory.getLog(getClass()); 
     
    5561        private ConceptDAO dao; 
    5662         
    57         public ConceptServiceImpl() { } 
    58  
    59         private ConceptDAO getConceptDAO() { 
    60                 return dao; 
    61         } 
    62          
     63        /** 
     64         * @see org.openmrs.api.ConceptService#setConceptDAO(org.openmrs.api.db.ConceptDAO) 
     65         */ 
    6366        public void setConceptDAO(ConceptDAO dao) { 
    6467                this.dao = dao; 
     
    6669 
    6770        /** 
    68          * @param concept 
    69          *            to be created 
     71         * @deprecated use {@link #saveConcept(Concept)} 
    7072         */ 
    7173        public void createConcept(Concept concept) { 
     74                saveConcept(concept); 
     75        } 
     76                 
     77        /** 
     78         * @deprecated use {@link #saveConcept(Concept)} 
     79         */ 
     80        public void createConcept(ConceptNumeric concept) { 
     81                saveConcept(concept); 
     82        } 
     83 
     84        /** 
     85         * @deprecated use {@link #saveConcept(Concept)} 
     86         */ 
     87        public void updateConcept(Concept concept) { 
     88                saveConcept(concept); 
     89        } 
     90 
     91        /** 
     92         * @deprecated use {@link #saveConcept(Concept)} 
     93         */ 
     94        public void updateConcept(ConceptNumeric concept) { 
     95                saveConcept(concept); 
     96        } 
     97 
     98        /** 
     99         * @deprecated use #saveDrug(Drug) 
     100         */ 
     101        public void createDrug(Drug drug) { 
     102                saveDrug(drug); 
     103        } 
     104                 
     105        /** 
     106         * @deprecated Use #saveDrug(Drug) 
     107         */ 
     108        public void updateDrug(Drug drug) { 
     109                saveDrug(drug); 
     110        } 
     111 
     112        /** 
     113         * @deprecated use #purgeConcept(Concept concept) 
     114         */ 
     115        public void deleteConcept(Concept concept) { 
     116                purgeConcept(concept); 
     117        } 
     118 
     119        /** 
     120         * @deprecated use {@link #retireConcept(Concept, String)}etireConcept 
     121         */ 
     122        public void voidConcept(Concept concept, String reason) { 
     123                retireConcept(concept, reason); 
     124        } 
     125         
     126        /** 
     127         * @see org.openmrs.api.ConceptService#saveConcept(org.openmrs.Concept) 
     128         */ 
     129    public Concept saveConcept(Concept concept) throws APIException { 
    72130                checkIfLocked(); 
    73                  
    74                 String authUserId = Context.getAuthenticatedUser().getUserId() 
    75                                 .toString(); 
    76  
    77                 log.info(authUserId + "|" + concept.getConceptId().toString()); 
    78  
    79                 getConceptDAO().createConcept(concept); 
    80         } 
    81  
    82         /** 
    83          * @param numeric 
    84          *            concept to be created 
    85          */ 
    86         public void createConcept(ConceptNumeric concept) { 
     131                this.modifyCollections(concept); 
     132                this.updateConceptWord(concept); 
     133                return dao.saveConcept(concept);     
     134        } 
     135 
     136        /** 
     137     * @see org.openmrs.api.ConceptService#saveDrug(org.openmrs.Drug) 
     138         */ 
     139    public Drug saveDrug(Drug drug) throws APIException { 
    87140                checkIfLocked(); 
    88141                 
    89                 String authUserId = Context.getAuthenticatedUser().getUserId() 
    90                                 .toString(); 
    91  
    92                 log.info(authUserId + "|" + concept.getConceptId().toString() 
    93                                 + "|numeric"); 
    94  
    95                 getConceptDAO().createConcept(concept); 
     142        if (drug.getCreator() == null) 
     143                drug.setCreator(Context.getAuthenticatedUser()); 
     144        if (drug.getDateCreated() == null) 
     145                        drug.setDateCreated(new Date()); 
     146 
     147                return dao.saveDrug(drug); 
     148    } 
     149 
     150        /** 
     151     * @see org.openmrs.api.ConceptService#purgeConcept(java.lang.Object) 
     152     */ 
     153    public void purgeConcept(Concept concept) throws APIException { 
     154        checkIfLocked(); 
     155        dao.purgeConcept(concept); 
     156        } 
     157 
     158        /** 
     159         * @see org.openmrs.api.ConceptService#retireConcept(org.openmrs.Concept, java.lang.String) 
     160         */ 
     161    public Concept retireConcept(Concept concept, String reason) 
     162            throws APIException { 
     163         
     164        // only do this if the concept isn't retired already 
     165        if (concept.isRetired() == false) { 
     166                checkIfLocked(); 
     167                 
     168                concept.setRetired(true); 
     169                concept.setRetireReason(reason); 
     170                concept.setRetiredBy(Context.getAuthenticatedUser()); 
     171                concept.setDateRetired(new Date()); 
     172                    return dao.saveConcept(concept); 
     173        } 
     174         
     175        return concept; 
     176    } 
     177     
     178        /** 
     179     * @see org.openmrs.api.ConceptService#retireDrug(org.openmrs.Drug, java.lang.String) 
     180     * @throws APIException 
     181     */ 
     182    public Drug retireDrug(Drug drug, String reason) throws APIException { 
     183 
     184        if (drug.isRetired() == false) { 
     185                drug.setRetired(true); 
     186                drug.setRetiredBy(Context.getAuthenticatedUser()); 
     187                drug.setRetireReason(reason); 
     188                drug.setDateRetired(new Date()); 
     189                return dao.saveDrug(drug); 
     190        } 
     191 
     192        return drug; 
     193        } 
     194 
     195        /** 
     196         *  
     197         * @see org.openmrs.api.ConceptService#purgeDrug(org.openmrs.Drug) 
     198         * @throws APIException 
     199         */ 
     200    public void purgeDrug(Drug drug) throws APIException { 
     201        dao.purgeDrug(drug); 
     202    } 
     203     
     204        /** 
     205         * @see org.openmrs.api.ConceptService#getConcept(java.lang.Integer) 
     206         */ 
     207        public Concept getConcept(Integer conceptId) throws APIException { 
     208                return dao.getConcept(conceptId); 
     209        } 
     210                 
     211        /** 
     212         * @see org.openmrs.api.ConceptService#getConceptAnswer(java.lang.Integer) 
     213         */ 
     214        public ConceptAnswer getConceptAnswer(Integer conceptAnswerId) throws APIException { 
     215                return dao.getConceptAnswer(conceptAnswerId); 
     216        } 
     217 
     218        /** 
     219         * @see org.openmrs.api.ConceptService#getDrug(java.lang.Integer) 
     220         */ 
     221        public Drug getDrug(Integer drugId) throws APIException { 
     222                return dao.getDrug(drugId); 
     223        } 
     224 
     225        /** 
     226         * @see org.openmrs.api.ConceptService#getConceptNumeric(java.lang.Integer) 
     227         */ 
     228        public ConceptNumeric getConceptNumeric(Integer conceptId) throws APIException { 
     229                return dao.getConceptNumeric(conceptId); 
    96230        } 
    97231 
     
    99233         * @see org.openmrs.api.ConceptService#saveConcept(org.openmrs.ConceptComplex) 
    100234         */ 
    101         public void saveConcept(ConceptComplex concept) { 
    102                         getConceptDAO().saveConcept(concept);          
     235        public ConceptComplex saveConcept(ConceptComplex concept) { 
     236                        return dao.saveConcept(concept);               
    103237        } 
    104238 
     
    107241         */ 
    108242        public ConceptComplex getConceptComplex(Integer conceptId) { 
    109                         return getConceptDAO().getConceptComplex(conceptId); 
    110         } 
    111          
    112         /** 
    113          * Gets the concept with the given internal identifier 
    114          *  
    115          * @param conceptId 
    116          * @return Concept 
    117          */ 
    118         public Concept getConcept(Integer conceptId) { 
    119                 return getConceptDAO().getConcept(conceptId); 
    120         } 
    121  
    122         /** 
    123          * Gets the conceptAnswer with the given internal identifier 
    124          *  
    125          * @param conceptAnswerId 
    126          * @return ConceptAnswer 
    127          */ 
    128         public ConceptAnswer getConceptAnswer(Integer conceptAnswerId) { 
    129                 return getConceptDAO().getConceptAnswer(conceptAnswerId); 
    130         } 
    131          
    132         /** 
    133          * Return a list of concepts sorted on sortBy in dir direction (asc/desc) 
    134          *  
    135          * @param sortBy 
    136          * @param dir 
    137          * @return List of concepts 
    138          */ 
    139         public List<Concept> getConcepts(String sortBy, String dir) { 
    140                 return getConceptDAO().getConcepts(sortBy, dir); 
    141         } 
    142  
    143         /** 
    144          * Update the given concept 
    145          *  
    146          * @param concept 
    147          *            to be updated 
    148          */ 
    149         public void updateConcept(Concept concept) { 
    150                 checkIfLocked(); 
    151                  
    152                 String authUserId = Context.getAuthenticatedUser().getUserId() 
    153                                 .toString(); 
    154  
    155                 log.info(authUserId + "|" + concept.getConceptId().toString()); 
    156  
    157                 getConceptDAO().updateConcept(concept); 
    158         } 
    159  
    160         /** 
    161          * Update the given numeric concept 
    162          *  
    163          * @param numeric 
    164          *            concept to be updated 
    165          */ 
    166         public void updateConcept(ConceptNumeric concept) { 
    167                 checkIfLocked(); 
    168                  
    169                 String authUserId = Context.getAuthenticatedUser().getUserId() 
    170                                 .toString(); 
    171  
    172                 log.info(authUserId + "|" + concept.getConceptId().toString()); 
    173  
    174                 getConceptDAO().updateConcept(concept); 
    175         } 
    176  
    177         /** 
    178          * Delete the given concept 
    179          *  
    180          * For super users only. If dereferencing concepts, use 
    181          * <code>voidConcept(org.openmrs.Concept)</code> 
    182          *  
    183          * @param Concept 
    184          *            to be deleted 
    185          */ 
    186         public void deleteConcept(Concept concept) { 
    187                 checkIfLocked(); 
    188                  
    189                 String authUserId = Context.getAuthenticatedUser().getUserId() 
    190                                 .toString(); 
    191  
    192                 log.info(authUserId + "|" + concept.getConceptId().toString()); 
    193  
    194                 getConceptDAO().deleteConcept(concept); 
    195         } 
    196  
    197         /** 
    198          * Voiding a concept essentially removes it from circulation 
    199          *  
    200          * @param Concept 
    201          *            concept 
    202          * @param String 
    203          *            reason 
    204          */ 
    205         public void voidConcept(Concept concept, String reason) { 
    206                 checkIfLocked(); 
    207                  
    208                 String authUserId = Context.getAuthenticatedUser().getUserId() 
    209                                 .toString(); 
    210  
    211                 log.info(authUserId + "|" + concept.getConceptId().toString()); 
    212  
    213                 getConceptDAO().voidConcept(concept, reason); 
    214         } 
    215  
    216         /** 
    217          * @param drug 
    218          *            to be created 
    219          */ 
    220         public void createDrug(Drug drug) { 
    221                 checkIfLocked(); 
    222                  
    223                 String authUserId = Context.getAuthenticatedUser().getUserId() 
    224                                 .toString(); 
    225  
    226                 log.info("creating drug - " + authUserId + "|" 
    227                                 + drug.getConcept().toString()); 
    228  
    229                 getConceptDAO().createDrug(drug); 
    230         } 
    231  
    232         /** 
    233          * Update the given drug 
    234          *  
    235          * @param drug 
    236          *            to be updated 
    237          */ 
    238         public void updateDrug(Drug drug) { 
    239                 checkIfLocked(); 
    240                  
    241                 String authUserId = Context.getAuthenticatedUser().getUserId() 
    242                                 .toString(); 
    243  
    244                 if (drug.getConcept() != null) 
    245                         log.info("updating drug - " + authUserId + "|" 
    246                                         + drug.getConcept().toString()); 
    247  
    248                 getConceptDAO().updateDrug(drug); 
    249         } 
    250  
    251         /** 
    252          * Return a list of concepts matching "name" anywhere in the name 
    253          *  
    254          * @param name 
    255          * @return List of concepts 
    256          */ 
    257         public List<Concept> getConceptsByName(String name) { 
    258                 return getConceptDAO().getConceptsByName(name); 
    259         } 
    260  
    261         /** 
    262          * Return a Concept that matches the name exactly 
    263          *  
    264          * @param name 
    265          * @return Concept with matching name 
     243                        return dao.getConceptComplex(conceptId); 
     244        } 
     245                 
     246        /** 
     247     * @see org.openmrs.api.ConceptService#getAllConcepts() 
     248         */ 
     249    public List<Concept> getAllConcepts() throws APIException { 
     250            return getAllConcepts(null, true, true); 
     251    } 
     252                 
     253        /** 
     254     * @see org.openmrs.api.ConceptService#getAllConcepts(java.lang.String, boolean, boolean) 
     255     */ 
     256    public List<Concept> getAllConcepts(String sortBy, boolean asc, 
     257            boolean includeRetired) throws APIException { 
     258            return dao.getAllConcepts(sortBy, asc, includeRetired); 
     259    } 
     260 
     261        /** 
     262         * @deprecated use {@link #getAllConcepts(String, boolean, boolean)} 
     263         */ 
     264        public List<Concept> getConcepts(String sortBy, String dir) throws APIException{ 
     265                boolean asc = true ? dir.equals("asc") : !dir.equals("asc"); 
     266                return getAllConcepts(sortBy, asc, true); 
     267        } 
     268 
     269        /** 
     270         * @see org.openmrs.api.ConceptService#getConceptsByName(java.lang.String) 
     271         */ 
     272        public List<Concept> getConceptsByName(String name) throws APIException { 
     273                return getConcepts(name, Context.getLocale(), true, null, null); 
     274        } 
     275 
     276        /** 
     277         * @see org.openmrs.api.ConceptService#getConceptByName(java.lang.String) 
    266278         */ 
    267279        public Concept getConceptByName(String name) { 
    268                 return getConceptDAO().getConceptByName(name); 
    269         } 
    270          
    271         /** 
    272          * Return the drug object corresponding to the given id 
    273          *  
    274          * @return Drug 
    275          */ 
    276         public Drug getDrug(Integer drugId) { 
    277                 return getConceptDAO().getDrug(drugId); 
    278         } 
    279  
    280         /** 
    281          * Return the drug object corresponding to the given name 
    282          *  
    283          * @return Drug 
    284          */ 
    285         public Drug getDrug(String drugName) { 
    286                 return getConceptDAO().getDrug(drugName); 
    287         } 
    288  
    289         public Drug getDrugByNameOrId(String drug) { 
    290                 Integer drugId = null; 
    291                  
     280                List<Concept> concepts =  getConcepts(name, Context.getLocale(), false, null, null); 
     281                int size = concepts.size();  
     282                if (size > 0){ 
     283                        if (size > 1) 
     284                                log.warn("Multiple concepts found for '" + name + "'"); 
     285                        return concepts.get(0); 
     286                }        
     287                return null; 
     288        } 
     289                 
     290        /** 
     291         * @see org.openmrs.api.ConceptService#getConceptByIdOrName(java.lang.String) 
     292         * @deprecated use {@link #getConcept(String))} 
     293         */ 
     294        public Concept getConceptByIdOrName(String idOrName) { 
     295                return getConcept(idOrName); 
     296        } 
     297 
     298    /** 
     299     *  
     300     * @see org.openmrs.api.ConceptService#getConcept(java.lang.String) 
     301     */ 
     302    public Concept getConcept(String conceptIdOrName){ 
     303                Concept c = null; 
     304                Integer conceptId = null;        
    292305                try { 
    293                         drugId = new Integer(drug); 
    294                 } catch ( NumberFormatException nfe ) { 
    295                         drugId = null; 
    296                 } 
    297                  
    298                 if ( drugId != null ) { 
    299                         return getDrug(drugId); 
     306                        conceptId = new Integer(conceptIdOrName); 
     307                } catch (NumberFormatException nfe) { 
     308                        conceptId = null; 
     309                } 
     310 
     311                if ( conceptId != null ) { 
     312                        c = getConcept(conceptId); 
    300313                } else { 
    301                         return getDrug(drug); 
    302                 } 
    303         } 
    304  
    305         /** 
    306          * Return a list of drugs currently in the database 
    307          *  
    308          * @return List of Drugs 
    309          */ 
    310         public List<Drug> getDrugs() { 
    311                 return getConceptDAO().getDrugs(); 
    312         } 
    313  
    314         /** 
    315          * Find drugs in the system. The string search can match either drug.name or 
    316          * drug.concept.name 
    317          *  
    318          * @param phrase 
    319          * @param includeRetired 
    320          * @return List of Drugs 
    321          */ 
    322         public List<Drug> findDrugs(String phrase, boolean includeRetired) { 
    323                 return getConceptDAO().findDrugs(phrase, includeRetired); 
    324         } 
    325  
    326         /** 
    327          * Return a list of drugs associated with the given concept 
    328          *  
    329          * @param Concept 
    330          * @return List of Drugs 
    331          */ 
    332         public List<Drug> getDrugs(Concept concept) { 
    333                 return getConceptDAO().getDrugs(concept); 
    334         } 
    335  
    336         /** 
    337          * Return a list of concept classes currently in the database 
    338          *  
    339          * @return List of Concept class objects 
    340          */ 
    341         public List<ConceptClass> getConceptClasses() { 
    342                 return getConceptDAO().getConceptClasses(); 
    343         } 
    344  
    345         /** 
    346          * Return a Concept class matching the given identifier 
    347          *  
    348          * @param i Integer 
    349          * @return ConceptClass 
    350          */ 
    351         public ConceptClass getConceptClass(Integer i) { 
    352                 return getConceptDAO().getConceptClass(i); 
    353         } 
    354          
    355         /** 
    356          * Return a Concept class matching the given name 
    357          *  
    358          * @param name String 
    359          * @return ConceptClass 
    360          */ 
    361         public ConceptClass getConceptClassByName(String name) { 
    362                 return getConceptDAO().getConceptClassByName(name); 
    363         } 
    364          
    365  
    366         /** 
    367          * Return a list of concept datatypes currently in the database 
    368          *  
    369          * @return List of ConceptDatatypes 
    370          */ 
    371         public List<ConceptDatatype> getConceptDatatypes() { 
    372                 return getConceptDAO().getConceptDatatypes(); 
    373         } 
    374  
    375         /** 
    376          * Return a ConceptDatatype matching the given identifier 
    377          *  
    378          * @return ConceptDatatype 
    379          */ 
    380         public ConceptDatatype getConceptDatatype(Integer i) { 
    381                 return getConceptDAO().getConceptDatatype(i); 
    382         } 
    383          
    384         public ConceptDatatype getConceptDatatypeByName(String name) { 
    385                 return getConceptDAO().getConceptDatatypeByName(name); 
    386         } 
    387  
    388         /** 
    389          * Return a list of the concept sets with concept_set matching concept 
    390          * For example to find all concepts for ARVs, you would do 
    391          *    getConceptSets(getConcept("ANTIRETROVIRAL MEDICATIONS")) 
    392          * and then take the conceptIds from the resulting list. 
    393          *  
    394          * @return List 
    395          */ 
    396         public List<ConceptSet> getConceptSets(Concept c) { 
    397                 return getConceptDAO().getConceptSets(c); 
    398         } 
    399          
    400         public List<Concept> getConceptsInSet(Concept c) { 
    401                 Set<Integer> alreadySeen = new HashSet<Integer>(); 
    402                 List<Concept> ret = new ArrayList<Concept>(); 
    403                 explodeConceptSetHelper(c, ret, alreadySeen); 
    404                 return ret; 
    405         } 
    406          
    407         /** 
    408          * @see org.openmrs.api.ConceptService#getConceptsByClass(org.openmrs.ConceptClass) 
    409          */ 
    410         public List<Concept> getConceptsByClass(ConceptClass cc) { 
    411                 return getConceptDAO().getConceptsByClass(cc); 
    412         } 
    413          
    414         /** 
    415          * @see org.openmrs.api.ConceptService#getSetsContainingConcept(org.openmrs.Concept) 
    416          */ 
    417         public List<ConceptSet> getSetsContainingConcept(Concept concept) { 
    418                 return getConceptDAO().getSetsContainingConcept(concept); 
    419         } 
    420  
    421         private void explodeConceptSetHelper(Concept concept, Collection<Concept> ret, Collection<Integer> alreadySeen) { 
    422                 if (alreadySeen.contains(concept.getConceptId())) 
    423                         return; 
    424                 alreadySeen.add(concept.getConceptId()); 
    425                 List<ConceptSet> cs = getConceptSets(concept); 
    426                 for (ConceptSet set : cs) { 
    427                         Concept c = set.getConcept(); 
    428                         if (c.isSet()) { 
    429                                 explodeConceptSetHelper(c, ret, alreadySeen); 
    430                         } else { 
    431                                 ret.add(c); 
    432                         } 
    433                 } 
    434         } 
    435  
    436         /** 
    437          * Return a concept numeric object given the concept id 
    438          *  
    439          * @return ConceptNumeric 
    440          */ 
    441         public ConceptNumeric getConceptNumeric(Integer conceptId) { 
    442                 return getConceptDAO().getConceptNumeric(conceptId); 
    443         } 
    444  
    445         /** 
    446          * Searches on given phrase via the concept word table 
    447          *  
    448          * @param phrase/search/words 
    449          *            String 
    450          * @param locale 
    451          *            Locale 
    452          * @param includeRetired 
    453          *            boolean 
    454          * @return 
    455          */ 
    456         public List<ConceptWord> findConcepts(String phrase, Locale locale, 
    457                         boolean includeRetired) { 
    458                 return findConcepts(phrase, locale, includeRetired, null, null, null, null); 
    459         } 
    460  
    461         /** 
    462          * Searches on given phrase via the concept word table 
    463          *  
    464          * @param phrase/search/words 
    465          *            String 
    466          * @param locale 
    467          *            Locale 
    468          * @param includeRetired 
    469          *            boolean 
    470          * @param requireClasses 
    471          *            List<ConceptClass> 
    472          * @param excludeClasses 
    473          *            List<ConceptClass> 
    474          * @param requireDatatypes 
    475          *            List<ConceptDatatype> 
    476          * @param excludeDatatypes 
    477          *            List<ConceptDatatype> 
    478          * @return 
    479          *  
    480          * @see ConceptService.findConcepts(String,Locale,boolean) 
    481          */ 
    482         public List<ConceptWord> findConcepts(String phrase, Locale locale, boolean includeRetired,  
    483                         List<ConceptClass> requireClasses, List<ConceptClass> excludeClasses,  
    484                         List<ConceptDatatype> requireDatatypes, List<ConceptDatatype> excludeDatatypes) { 
     314                        c = getConceptByName(conceptIdOrName); 
     315                } 
     316                return c; 
     317        } 
     318 
     319        /** 
     320         * @see org.openmrs.api.ConceptService#getConceptWords(java.lang.String, java.util.Locale, boolean, java.util.List, java.util.List, java.util.List, java.util.List, Concept, int, int) 
     321         */ 
     322        public List<ConceptWord> getConceptWords(String phrase, List<Locale> locales, boolean includeRetired,  
     323                                                List<ConceptClass> requireClasses, List<ConceptClass> excludeClasses, 
     324                                                List<ConceptDatatype> requireDatatypes, List<ConceptDatatype> excludeDatatypes, 
     325                                                Concept answerToConcept, Integer start, Integer size){ 
    485326                 
    486327                if (requireClasses == null) 
     
    493334                        excludeDatatypes = new Vector<ConceptDatatype>(); 
    494335                 
    495                 List<ConceptWord> conceptWords = getConceptDAO().findConcepts(phrase, 
    496                                 locale, includeRetired, requireClasses, excludeClasses, requireDatatypes, excludeDatatypes); 
    497  
    498                 return weightWords(phrase, locale, conceptWords); 
    499         } 
    500  
    501         /** 
    502          *  
    503          * Finds concepts but only returns the given range 
    504          *  
    505          * @param phrase 
    506          * @param locale 
    507          * @param includeRetired 
    508          * @param start 
    509          * @param size 
    510          * @return ConceptWord list 
     336                List<ConceptWord> conceptWords = dao.getConceptWords(phrase, 
     337                                                                                locales, includeRetired, requireClasses, excludeClasses, requireDatatypes, excludeDatatypes, answerToConcept, start, size); 
     338                 
     339                return weightWords(phrase, locales, conceptWords); 
     340        } 
     341 
     342        /** 
     343     * @see org.openmrs.api.ConceptService#getConceptWords(java.lang.String, java.util.Locale) 
     344     */ 
     345    public List<ConceptWord> getConceptWords(String phrase, Locale locale) 
     346            throws APIException { 
     347        List<Locale> locales = new Vector<Locale>(); 
     348                locales.add(locale); 
     349 
     350            return getConceptWords(phrase, locales, false, null, null, null, null, null, null, null); 
     351        } 
     352 
     353        /** 
     354         * @deprecated use {@link #getConceptWords(String, List, boolean, List, List, List, List, Integer, Integer)} 
    511355         */ 
    512356        public List<ConceptWord> findConcepts(String phrase, Locale locale, 
    513357                        boolean includeRetired, int start, int size) { 
    514  
    515                 List<ConceptWord> conceptWords = findConcepts(phrase, locale, 
    516                                 includeRetired); 
    517  
     358                List<Locale> locales = new Vector<Locale>(); 
     359                locales.add(locale); 
     360                 
     361                // delegate to the non-deprecated method 
     362                List<ConceptWord> conceptWords = getConceptWords(phrase, locales, 
     363                                includeRetired, null, null, null, null, null, start, size); 
     364                 
    518365                List<ConceptWord> subList = conceptWords.subList(start, start + size); 
    519366 
     
    521368        } 
    522369 
    523         public List<ConceptWord> findConceptAnswers(String phrase, Locale locale, 
    524                         Concept concept, boolean includeRetired) { 
    525                 List<ConceptWord> conceptWords = getConceptDAO().findConceptAnswers( 
    526                                 phrase, locale, concept, includeRetired); 
    527  
    528                 return weightWords(phrase, locale, conceptWords); 
    529  
    530         } 
    531  
    532         /** 
    533          * Get the questions that have this concept as a possible answer 
     370        /** 
     371         * @deprecated use {@link #getConceptWords(String, List, boolean, List, List, List, List, Integer, Integer)} 
     372         */ 
     373        public List<ConceptWord> findConcepts(String phrase, Locale locale, 
     374                        boolean includeRetired) { 
     375                 
     376                List<Locale> locales = new Vector<Locale>(); 
     377                locales.add(locale); 
     378                 
     379                return getConceptWords(phrase, locales, includeRetired, null, null, null, null, null, null, null); 
     380        } 
     381         
     382        /** 
     383         * @deprecated use {@link #getConceptWords(String, List, boolean, List, List, List, List, Integer, Integer)} 
     384         */ 
     385        public List<ConceptWord> findConcepts(String phrase, Locale locale, boolean includeRetired,  
     386                        List<ConceptClass> requireClasses, List<ConceptClass> excludeClasses,  
     387                        List<ConceptDatatype> requireDatatypes, List<ConceptDatatype> excludeDatatypes) { 
     388                 
     389                List<Locale> locales = new Vector<Locale>(); 
     390                locales.add(locale); 
     391                 
     392                return getConceptWords(phrase, locales, includeRetired, requireClasses, excludeClasses, requireDatatypes, excludeDatatypes, null, null, null); 
     393        } 
     394         
     395        /** 
     396         * @deprecated use {@link #getConceptWords(String, List, boolean, List, List, List, List, Integer, Integer)} 
     397         */ 
     398        public List<ConceptWord> findConcepts(String phrase, List<Locale> locales, boolean includeRetired,  
     399                        List<ConceptClass> requireClasses, List<ConceptClass> excludeClasses,  
     400                        List<ConceptDatatype> requireDatatypes, List<ConceptDatatype> excludeDatatypes) { 
     401                 
     402                return getConceptWords(phrase, locales, includeRetired, requireClasses, excludeClasses, requireDatatypes, excludeDatatypes, null, null, null); 
     403        } 
     404         
     405        /** 
     406         * Generic getConcepts method (used internally) to get concepts matching a  
     407         * on name 
    534408         *  
    535          * @param concept 
    536          *            Concept to get 
    537          * @return list of concepts 
    538          */ 
    539         public List<Concept> getQuestionsForAnswer(Concept concept) { 
    540                 List<Concept> concepts = getConceptDAO().getQuestionsForAnswer(concept); 
    541  
    542                 return concepts; 
    543         } 
    544  
    545         /** 
    546          * This will weight and sort the concepts we are assuming the hits are 
    547          * sorted with synonym matches at the bottom 
    548          *  
    549          * @param phrase 
    550          *            that was used to get this search 
    551          * @param locale 
    552          *            that was used to get this search 
    553          * @param conceptWords 
     409         * @param name 
     410         * @param loc 
     411         * @param searchOnPhrase 
    554412         * @return 
    555413         */ 
    556         protected List<ConceptWord> weightWords(String phrase, Locale locale, 
    557                         List<ConceptWord> conceptWords) { 
    558  
    559                 // Map<ConceptId, ConceptWord> 
    560                 Map<Integer, ConceptWord> uniqueConcepts = new HashMap<Integer, ConceptWord>(); 
    561  
    562                 // phrase words 
    563                 if (phrase == null) 
    564                         phrase = ""; 
    565                 List<String> searchedWords = ConceptWord.getUniqueWords(phrase); 
    566  
    567                 Integer id = null; 
    568                 Concept concept = null; 
    569                 for (ConceptWord tmpWord : conceptWords) { 
    570                         concept = tmpWord.getConcept(); 
    571                         id = concept.getConceptId(); 
    572  
    573                         if (uniqueConcepts.containsKey(id)) { 
    574                                 ConceptWord initialWord = uniqueConcepts.get(id); 
    575  
    576                                 // this concept is already in the list 
    577                                 // because we're sort synonyms at the bottom, the initial 
    578                                 // concept must be a match on the conceptName 
    579                                 // check synonym in case we have multiple synonym hits 
    580                                 String toSplit = initialWord.getSynonym(); 
    581                                 if (toSplit == null || toSplit.equals("")) { 
    582                                         ConceptName cn = initialWord.getConcept().getName(locale); 
    583                                         toSplit = cn.getName(); 
    584                                 } 
    585                                 List<String> nameWords = ConceptWord.getUniqueWords(toSplit); 
    586  
    587                                 // if the conceptName doesn't contain all of the search words, 
    588                                 // replace the initial word with this synonym based word 
    589                                 if (!containsAll(nameWords, searchedWords)) { 
    590                                         tmpWord.setWeight(initialWord.getWeight()); 
    591                                         uniqueConcepts.put(id, tmpWord); 
    592                                 } else 
    593                                         tmpWord = null; 
    594  
    595                         } else { 
    596                                 // normalize the weighting 
    597                                 tmpWord.setWeight(0.0); 
    598                                 // its not in the list, add it 
    599                                 uniqueConcepts.put(id, tmpWord); 
    600                         } 
    601  
    602                         // don't increase weight with second/third/... synonym 
    603                         if (tmpWord != null) { 
    604                                 // default matched string 
    605                                 String matchedString = tmpWord.getSynonym(); 
    606  
    607                                 // if there isn't a synonym, it is matching on the name, 
    608                                 if (matchedString.length() == 0) { 
    609                                         // We weight name matches higher 
    610                                         tmpWord.increaseWeight(2.0); 
    611                                         matchedString = tmpWord.getConcept().getName(locale) 
    612                                                         .getName(); 
    613                                 } 
    614  
    615                                 // increase the weight by a factor of the % of words matched 
    616                                 Double percentMatched = getPercentMatched(searchedWords, 
    617                                                 matchedString); 
    618                                 tmpWord.increaseWeight(5.0 * percentMatched); 
    619                         } 
    620                 } 
    621  
    622                 conceptWords = new Vector<ConceptWord>(); 
    623                 conceptWords.addAll(uniqueConcepts.values()); 
    624                 Collections.sort(conceptWords); 
    625  
    626                 return conceptWords; 
    627         } 
    628  
    629         /** 
    630          * Finds the previous available concept via concept id 
    631          *  
    632          * @param c 
    633          * @param offset 
    634          * @r