/api/src/main/java/org/openmrs/api/impl/AdministrationServiceImpl.java

https://github.com/babitha/openmrs · Java · 1215 lines · 642 code · 159 blank · 414 comment · 64 complexity · 1a2ac7363960b56cdcec1974c8e952b3 MD5 · raw file

  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. */
  14. package org.openmrs.api.impl;
  15. import java.io.StringWriter;
  16. import java.io.UnsupportedEncodingException;
  17. import java.net.InetAddress;
  18. import java.net.UnknownHostException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.ArrayList;
  21. import java.util.Calendar;
  22. import java.util.Collection;
  23. import java.util.HashMap;
  24. import java.util.HashSet;
  25. import java.util.LinkedHashMap;
  26. import java.util.List;
  27. import java.util.Locale;
  28. import java.util.Map;
  29. import java.util.Properties;
  30. import java.util.Set;
  31. import java.util.SortedMap;
  32. import java.util.TreeMap;
  33. import org.apache.commons.logging.Log;
  34. import org.apache.commons.logging.LogFactory;
  35. import org.openmrs.Concept;
  36. import org.openmrs.ConceptClass;
  37. import org.openmrs.ConceptDatatype;
  38. import org.openmrs.ConceptProposal;
  39. import org.openmrs.ConceptSource;
  40. import org.openmrs.EncounterType;
  41. import org.openmrs.FieldType;
  42. import org.openmrs.GlobalProperty;
  43. import org.openmrs.ImplementationId;
  44. import org.openmrs.Location;
  45. import org.openmrs.MimeType;
  46. import org.openmrs.OpenmrsObject;
  47. import org.openmrs.PatientIdentifierType;
  48. import org.openmrs.Privilege;
  49. import org.openmrs.Role;
  50. import org.openmrs.Tribe;
  51. import org.openmrs.User;
  52. import org.openmrs.api.APIAuthenticationException;
  53. import org.openmrs.api.APIException;
  54. import org.openmrs.api.AdministrationService;
  55. import org.openmrs.api.EventListeners;
  56. import org.openmrs.api.GlobalPropertyListener;
  57. import org.openmrs.api.context.Context;
  58. import org.openmrs.api.db.AdministrationDAO;
  59. import org.openmrs.module.Module;
  60. import org.openmrs.module.ModuleFactory;
  61. import org.openmrs.module.ModuleUtil;
  62. import org.openmrs.reporting.AbstractReportObject;
  63. import org.openmrs.reporting.Report;
  64. import org.openmrs.util.LocaleUtility;
  65. import org.openmrs.util.OpenmrsConstants;
  66. import org.openmrs.util.OpenmrsUtil;
  67. import org.openmrs.util.PrivilegeConstants;
  68. import org.springframework.util.StringUtils;
  69. /**
  70. * Default implementation of the administration services. This class should not be used on its own.
  71. * The current OpenMRS implementation should be fetched from the Context
  72. *
  73. * @see org.openmrs.api.AdministrationService
  74. * @see org.openmrs.api.context.Context
  75. */
  76. public class AdministrationServiceImpl extends BaseOpenmrsService implements AdministrationService, GlobalPropertyListener {
  77. protected Log log = LogFactory.getLog(getClass());
  78. protected AdministrationDAO dao;
  79. private EventListeners eventListeners;
  80. /**
  81. * An always up-to-date collection of the allowed locales.
  82. */
  83. private GlobalLocaleList globalLocaleList;
  84. /**
  85. * Default empty constructor
  86. */
  87. public AdministrationServiceImpl() {
  88. }
  89. /**
  90. * @see org.openmrs.api.AdministrationService#setAdministrationDAO(org.openmrs.api.db.AdministrationDAO)
  91. */
  92. public void setAdministrationDAO(AdministrationDAO dao) {
  93. this.dao = dao;
  94. }
  95. public void setEventListeners(EventListeners eventListeners) {
  96. this.eventListeners = eventListeners;
  97. }
  98. /**
  99. * @see org.openmrs.api.AdministrationService#createEncounterType(org.openmrs.EncounterType)
  100. * @deprecated
  101. */
  102. @Deprecated
  103. public void createEncounterType(EncounterType encounterType) throws APIException {
  104. Context.getEncounterService().saveEncounterType(encounterType);
  105. }
  106. /**
  107. * @see org.openmrs.api.AdministrationService#updateEncounterType(org.openmrs.EncounterType)
  108. * @deprecated
  109. */
  110. @Deprecated
  111. public void updateEncounterType(EncounterType encounterType) throws APIException {
  112. Context.getEncounterService().saveEncounterType(encounterType);
  113. }
  114. /**
  115. * @see org.openmrs.api.AdministrationService#deleteEncounterType(org.openmrs.EncounterType)
  116. * @deprecated
  117. */
  118. @Deprecated
  119. public void deleteEncounterType(EncounterType encounterType) throws APIException {
  120. Context.getEncounterService().purgeEncounterType(encounterType);
  121. }
  122. /**
  123. * @see org.openmrs.api.PatientService#savePatientIdentifierType(PatientIdentifierType)
  124. * @deprecated replaced by
  125. * {@link org.openmrs.api.PatientService#savePatientIdentifierType(PatientIdentifierType)}
  126. */
  127. @Deprecated
  128. public void createPatientIdentifierType(PatientIdentifierType patientIdentifierType) throws APIException {
  129. Context.getPatientService().savePatientIdentifierType(patientIdentifierType);
  130. }
  131. /**
  132. * @see org.openmrs.api.PatientService#savePatientIdentifierType(PatientIdentifierType)
  133. * @deprecated replaced by
  134. * {@link org.openmrs.api.PatientService#savePatientIdentifierType(PatientIdentifierType)}
  135. */
  136. @Deprecated
  137. public void updatePatientIdentifierType(PatientIdentifierType patientIdentifierType) throws APIException {
  138. Context.getPatientService().savePatientIdentifierType(patientIdentifierType);
  139. }
  140. /**
  141. * @see org.openmrs.api.PatientService#purgePatientIdentifierType(PatientIdentifierType)
  142. * @deprecated replaced by
  143. * {@link org.openmrs.api.PatientService#purgePatientIdentifierType(PatientIdentifierType)}
  144. */
  145. @Deprecated
  146. public void deletePatientIdentifierType(PatientIdentifierType patientIdentifierType) throws APIException {
  147. Context.getPatientService().purgePatientIdentifierType(patientIdentifierType);
  148. }
  149. /**
  150. * Create a new Tribe
  151. *
  152. * @param tribe Tribe to create
  153. * @throws APIException
  154. * @deprecated
  155. */
  156. @Deprecated
  157. public void createTribe(Tribe tribe) throws APIException {
  158. throw new APIException("The Tribe object is no longer supported. Install the Tribe module");
  159. }
  160. /**
  161. * Update Tribe
  162. *
  163. * @param tribe Tribe to update
  164. * @throws APIException
  165. * @deprecated
  166. */
  167. @Deprecated
  168. public void updateTribe(Tribe tribe) throws APIException {
  169. throw new APIException("The Tribe object is no longer supported. Install the Tribe module");
  170. }
  171. /**
  172. * Delete Tribe
  173. *
  174. * @param tribe Tribe to delete
  175. * @throws APIException
  176. * @deprecated
  177. */
  178. @Deprecated
  179. public void deleteTribe(Tribe tribe) throws APIException {
  180. throw new APIException("The Tribe object is no longer supported. Install the Tribe module");
  181. }
  182. /**
  183. * Retire Tribe
  184. *
  185. * @param tribe Tribe to retire
  186. * @throws APIException
  187. * @deprecated
  188. */
  189. @Deprecated
  190. public void retireTribe(Tribe tribe) throws APIException {
  191. throw new APIException("The Tribe object is no longer supported. Install the Tribe module");
  192. }
  193. /**
  194. * Unretire Tribe
  195. *
  196. * @param tribe Tribe to unretire
  197. * @throws APIException
  198. * @deprecated
  199. */
  200. @Deprecated
  201. public void unretireTribe(Tribe tribe) throws APIException {
  202. throw new APIException("The Tribe object is no longer supported. Install the Tribe module");
  203. }
  204. /**
  205. * @deprecated
  206. */
  207. @Deprecated
  208. public void createFieldType(FieldType fieldType) throws APIException {
  209. Context.getFormService().saveFieldType(fieldType);
  210. }
  211. /**
  212. * @deprecated
  213. */
  214. @Deprecated
  215. public void updateFieldType(FieldType fieldType) throws APIException {
  216. Context.getFormService().saveFieldType(fieldType);
  217. }
  218. /**
  219. * @deprecated
  220. */
  221. @Deprecated
  222. public void deleteFieldType(FieldType fieldType) throws APIException {
  223. Context.getFormService().purgeFieldType(fieldType);
  224. }
  225. /**
  226. * @deprecated use {@link org.openmrs.api.ObsService#saveMimeType(MimeType)}
  227. */
  228. @Deprecated
  229. public void createMimeType(MimeType mimeType) throws APIException {
  230. Context.getObsService().saveMimeType(mimeType);
  231. }
  232. /**
  233. * @deprecated use {@link org.openmrs.api.ObsService#saveMimeType(MimeType)}
  234. */
  235. @Deprecated
  236. public void updateMimeType(MimeType mimeType) throws APIException {
  237. Context.getObsService().saveMimeType(mimeType);
  238. }
  239. /**
  240. * @deprecated use {@link org.openmrs.api.ObsService#purgeMimeType(MimeType)}
  241. */
  242. @Deprecated
  243. public void deleteMimeType(MimeType mimeType) throws APIException {
  244. Context.getObsService().purgeMimeType(mimeType);
  245. }
  246. /**
  247. * @see org.openmrs.api.AdministrationService#createLocation(org.openmrs.Location)
  248. * @deprecated
  249. */
  250. @Deprecated
  251. public void createLocation(Location location) throws APIException {
  252. Context.getLocationService().saveLocation(location);
  253. }
  254. /**
  255. * @see org.openmrs.api.AdministrationService#updateLocation(org.openmrs.Location)
  256. * @deprecated
  257. */
  258. @Deprecated
  259. public void updateLocation(Location location) throws APIException {
  260. Context.getLocationService().saveLocation(location);
  261. }
  262. /**
  263. * @see org.openmrs.api.AdministrationService#deleteLocation(org.openmrs.Location)
  264. * @deprecated
  265. */
  266. @Deprecated
  267. public void deleteLocation(Location location) throws APIException {
  268. Context.getLocationService().purgeLocation(location);
  269. }
  270. /**
  271. * @see org.openmrs.api.AdministrationService#createRole(org.openmrs.Role)
  272. * @deprecated
  273. */
  274. @Deprecated
  275. public void createRole(Role role) throws APIException {
  276. Context.getUserService().saveRole(role);
  277. }
  278. /**
  279. * @see org.openmrs.api.AdministrationService#updateRole(org.openmrs.Role)
  280. * @deprecated
  281. */
  282. @Deprecated
  283. public void updateRole(Role role) throws APIException {
  284. Context.getUserService().saveRole(role);
  285. }
  286. /**
  287. * @see org.openmrs.api.AdministrationService#deleteRole(org.openmrs.Role)
  288. * @deprecated
  289. */
  290. @Deprecated
  291. public void deleteRole(Role role) throws APIException {
  292. Context.getUserService().purgeRole(role);
  293. }
  294. /**
  295. * @see org.openmrs.api.AdministrationService#createPrivilege(org.openmrs.Privilege)
  296. * @deprecated
  297. */
  298. @Deprecated
  299. public void createPrivilege(Privilege privilege) throws APIException {
  300. Context.getUserService().savePrivilege(privilege);
  301. }
  302. /**
  303. * @see org.openmrs.api.AdministrationService#updatePrivilege(org.openmrs.Privilege)
  304. * @deprecated
  305. */
  306. @Deprecated
  307. public void updatePrivilege(Privilege privilege) throws APIException {
  308. Context.getUserService().savePrivilege(privilege);
  309. }
  310. /**
  311. * @see org.openmrs.api.AdministrationService#deletePrivilege(org.openmrs.Privilege)
  312. * @deprecated
  313. */
  314. @Deprecated
  315. public void deletePrivilege(Privilege privilege) throws APIException {
  316. Context.getUserService().purgePrivilege(privilege);
  317. }
  318. /**
  319. * @deprecated moved to ConceptService
  320. */
  321. @Deprecated
  322. public void createConceptClass(ConceptClass cc) throws APIException {
  323. Context.getConceptService().saveConceptClass(cc);
  324. }
  325. /**
  326. * @deprecated moved to ConceptService
  327. */
  328. @Deprecated
  329. public void updateConceptClass(ConceptClass cc) throws APIException {
  330. Context.getConceptService().saveConceptClass(cc);
  331. }
  332. /**
  333. * @deprecated moved to ConceptService
  334. */
  335. @Deprecated
  336. public void deleteConceptClass(ConceptClass cc) throws APIException {
  337. Context.getConceptService().purgeConceptClass(cc);
  338. }
  339. /**
  340. * @deprecated moved to ConceptService
  341. */
  342. @Deprecated
  343. public void createConceptDatatype(ConceptDatatype cd) throws APIException {
  344. Context.getConceptService().saveConceptDatatype(cd);
  345. }
  346. /**
  347. * @deprecated moved to ConceptService
  348. */
  349. @Deprecated
  350. public void updateConceptDatatype(ConceptDatatype cd) throws APIException {
  351. Context.getConceptService().saveConceptDatatype(cd);
  352. }
  353. /**
  354. * @deprecated moved to ConceptService
  355. */
  356. @Deprecated
  357. public void deleteConceptDatatype(ConceptDatatype cd) throws APIException {
  358. Context.getConceptService().purgeConceptDatatype(cd);
  359. }
  360. /**
  361. * Create a new Report
  362. *
  363. * @param report Report to create
  364. * @deprecated see reportingcompatibility module
  365. * @throws APIException
  366. */
  367. @Deprecated
  368. public void createReport(Report report) throws APIException {
  369. if (!Context.hasPrivilege(OpenmrsConstants.PRIV_ADD_REPORTS))
  370. throw new APIAuthenticationException("Privilege required: " + OpenmrsConstants.PRIV_ADD_REPORTS);
  371. dao.createReport(report);
  372. }
  373. /**
  374. * Update Report
  375. *
  376. * @param report Report to update
  377. * @deprecated see reportingcompatibility module
  378. * @throws APIException
  379. */
  380. @Deprecated
  381. public void updateReport(Report report) throws APIException {
  382. if (!Context.hasPrivilege(OpenmrsConstants.PRIV_EDIT_REPORTS))
  383. throw new APIAuthenticationException("Privilege required: " + OpenmrsConstants.PRIV_EDIT_REPORTS);
  384. dao.updateReport(report);
  385. }
  386. /**
  387. * Delete Report
  388. *
  389. * @param report Report to delete
  390. * @deprecated see reportingcompatibility module
  391. * @throws APIException
  392. */
  393. @Deprecated
  394. public void deleteReport(Report report) throws APIException {
  395. if (!Context.hasPrivilege(OpenmrsConstants.PRIV_DELETE_REPORTS))
  396. throw new APIAuthenticationException("Privilege required: " + OpenmrsConstants.PRIV_DELETE_REPORTS);
  397. dao.deleteReport(report);
  398. }
  399. /**
  400. * Create a new Report Object
  401. *
  402. * @param reportObject Report Object to create
  403. * @deprecated see reportingcompatibility module
  404. * @throws APIException
  405. */
  406. @Deprecated
  407. public void createReportObject(AbstractReportObject reportObject) throws APIException {
  408. if (!Context.hasPrivilege(OpenmrsConstants.PRIV_ADD_REPORT_OBJECTS))
  409. throw new APIAuthenticationException("Privilege required: " + OpenmrsConstants.PRIV_ADD_REPORT_OBJECTS);
  410. dao.createReportObject(reportObject);
  411. }
  412. /**
  413. * Update Report Object
  414. *
  415. * @param reportObject Report Object to update
  416. * @deprecated see reportingcompatibility module
  417. * @throws APIException
  418. */
  419. @Deprecated
  420. public void updateReportObject(AbstractReportObject reportObject) throws APIException {
  421. if (!Context.hasPrivilege(OpenmrsConstants.PRIV_EDIT_REPORT_OBJECTS))
  422. throw new APIAuthenticationException("Privilege required: " + OpenmrsConstants.PRIV_EDIT_REPORT_OBJECTS);
  423. dao.updateReportObject(reportObject);
  424. }
  425. /**
  426. * Delete Report Object
  427. *
  428. * @param reportObjectId Internal Integer identifier of Report Object to delete
  429. * @deprecated see reportingcompatibility module
  430. * @throws APIException
  431. */
  432. @Deprecated
  433. public void deleteReportObject(Integer reportObjectId) throws APIException {
  434. if (!Context.hasPrivilege(OpenmrsConstants.PRIV_DELETE_REPORT_OBJECTS))
  435. throw new APIAuthenticationException("Privilege required: " + OpenmrsConstants.PRIV_DELETE_REPORT_OBJECTS);
  436. dao.deleteReportObject(reportObjectId);
  437. }
  438. /**
  439. * @deprecated moved to ConceptServiceImpl
  440. */
  441. @Deprecated
  442. public void updateConceptWord(Concept concept) throws APIException {
  443. Context.getConceptService().updateConceptWord(concept);
  444. }
  445. /**
  446. * @deprecated moved to ConceptServiceImpl
  447. */
  448. @Deprecated
  449. public void updateConceptWords() throws APIException {
  450. Context.getConceptService().updateConceptWords();
  451. }
  452. /**
  453. * @deprecated moved to ConceptService
  454. */
  455. @Deprecated
  456. public void updateConceptWords(Integer conceptIdStart, Integer conceptIdEnd) throws APIException {
  457. Context.getConceptService().updateConceptWords(conceptIdStart, conceptIdEnd);
  458. }
  459. /**
  460. * @deprecated moved to ConceptService
  461. */
  462. @Deprecated
  463. public void updateConceptSetDerived(Concept concept) throws APIException {
  464. Context.getConceptService().updateConceptSetDerived(concept);
  465. }
  466. /**
  467. * @deprecated moved to ConceptService
  468. */
  469. @Deprecated
  470. public void updateConceptSetDerived() throws APIException {
  471. Context.getConceptService().updateConceptSetDerived();
  472. }
  473. /**
  474. * @deprecated moved to ConceptService
  475. */
  476. @Deprecated
  477. public void createConceptProposal(ConceptProposal cp) throws APIException {
  478. Context.getConceptService().saveConceptProposal(cp);
  479. }
  480. /**
  481. * @deprecated moved to ConceptService
  482. */
  483. @Deprecated
  484. public void updateConceptProposal(ConceptProposal cp) throws APIException {
  485. Context.getConceptService().saveConceptProposal(cp);
  486. }
  487. /**
  488. * @deprecated moved to ConceptService
  489. */
  490. @Deprecated
  491. public void mapConceptProposalToConcept(ConceptProposal cp, Concept mappedConcept) throws APIException {
  492. Context.getConceptService().mapConceptProposalToConcept(cp, mappedConcept);
  493. }
  494. /**
  495. * @deprecated moved to ConceptService
  496. * @see org.openmrs.api.AdministrationService#rejectConceptProposal(org.openmrs.ConceptProposal)
  497. */
  498. @Deprecated
  499. public void rejectConceptProposal(ConceptProposal cp) {
  500. Context.getConceptService().rejectConceptProposal(cp);
  501. }
  502. /**
  503. * @see org.openmrs.api.AdministrationService#mrnGeneratorLog(java.lang.String,
  504. * java.lang.Integer, java.lang.Integer)
  505. * @deprecated
  506. */
  507. @Deprecated
  508. public void mrnGeneratorLog(String site, Integer start, Integer count) throws APIException {
  509. if (!Context.hasPrivilege(PrivilegeConstants.EDIT_PATIENTS))
  510. throw new APIAuthenticationException("Privilege required: " + PrivilegeConstants.EDIT_PATIENTS);
  511. dao.mrnGeneratorLog(site, start, count);
  512. }
  513. /**
  514. * @see org.openmrs.api.AdministrationService#getMRNGeneratorLog()
  515. * @deprecated
  516. */
  517. @Deprecated
  518. public Collection<?> getMRNGeneratorLog() throws APIException {
  519. if (!Context.hasPrivilege(PrivilegeConstants.EDIT_PATIENTS))
  520. throw new APIAuthenticationException("Privilege required: " + PrivilegeConstants.EDIT_PATIENTS);
  521. return dao.getMRNGeneratorLog();
  522. }
  523. /**
  524. * Static-ish variable used to cache the system variables. This is not static so that every time
  525. * a module is loaded or removed the variable is destroyed (along with the administration
  526. * service) and recreated the next time it is called
  527. */
  528. protected SortedMap<String, String> systemVariables = null;
  529. /**
  530. * Set of locales which can be used to present messages in the user interface. Created lazily as
  531. * needed by {@link #getAllowedLocales()}.
  532. */
  533. private HashSet<Locale> presentationLocales;
  534. /**
  535. * @see org.openmrs.api.AdministrationService#getSystemVariables()
  536. */
  537. public SortedMap<String, String> getSystemVariables() throws APIException {
  538. if (systemVariables == null) {
  539. systemVariables = new TreeMap<String, String>();
  540. // Added the server's fully qualified domain name
  541. try {
  542. systemVariables.put("OPENMRS_HOSTNAME", InetAddress.getLocalHost().getCanonicalHostName());
  543. }
  544. catch (UnknownHostException e) {
  545. systemVariables.put("OPENMRS_HOSTNAME", "Unknown host: " + e.getMessage());
  546. }
  547. systemVariables.put("OPENMRS_VERSION", String.valueOf(OpenmrsConstants.OPENMRS_VERSION));
  548. systemVariables.put("DATABASE_NAME", OpenmrsConstants.DATABASE_NAME);
  549. systemVariables.put("DATABASE_BUSINESS_NAME", OpenmrsConstants.DATABASE_BUSINESS_NAME);
  550. systemVariables.put("OBSCURE_PATIENTS", String.valueOf(OpenmrsConstants.OBSCURE_PATIENTS));
  551. systemVariables.put("OBSCURE_PATIENTS_FAMILY_NAME", OpenmrsConstants.OBSCURE_PATIENTS_FAMILY_NAME);
  552. systemVariables.put("OBSCURE_PATIENTS_GIVEN_NAME", OpenmrsConstants.OBSCURE_PATIENTS_GIVEN_NAME);
  553. systemVariables.put("OBSCURE_PATIENTS_MIDDLE_NAME", OpenmrsConstants.OBSCURE_PATIENTS_MIDDLE_NAME);
  554. systemVariables.put("MODULE_REPOSITORY_PATH", ModuleUtil.getModuleRepository().getAbsolutePath());
  555. systemVariables.put("OPERATING_SYSTEM_KEY", String.valueOf(OpenmrsConstants.OPERATING_SYSTEM_KEY));
  556. systemVariables.put("OPERATING_SYSTEM", String.valueOf(OpenmrsConstants.OPERATING_SYSTEM));
  557. }
  558. return systemVariables;
  559. }
  560. /**
  561. * @see org.openmrs.api.AdministrationService#getGlobalProperty(java.lang.String)
  562. */
  563. public String getGlobalProperty(String propertyName) throws APIException {
  564. // This method should not have any authorization check
  565. if (propertyName == null)
  566. return null;
  567. return dao.getGlobalProperty(propertyName);
  568. }
  569. /**
  570. * @see org.openmrs.api.AdministrationService#getGlobalProperty(java.lang.String,
  571. * java.lang.String)
  572. */
  573. public String getGlobalProperty(String propertyName, String defaultValue) throws APIException {
  574. String s = getGlobalProperty(propertyName);
  575. if (s == null)
  576. return defaultValue;
  577. return s;
  578. }
  579. /**
  580. * @see org.openmrs.api.AdministrationService#getGlobalPropertyObject(java.lang.String)
  581. */
  582. public GlobalProperty getGlobalPropertyObject(String propertyName) {
  583. return dao.getGlobalPropertyObject(propertyName);
  584. }
  585. /**
  586. * @see org.openmrs.api.AdministrationService#getGlobalProperties()
  587. * @deprecated
  588. */
  589. @Deprecated
  590. public List<GlobalProperty> getGlobalProperties() throws APIException {
  591. return getAllGlobalProperties();
  592. }
  593. /**
  594. * @see org.openmrs.api.AdministrationService#setGlobalProperties(java.util.List)
  595. * @deprecated
  596. */
  597. @Deprecated
  598. public void setGlobalProperties(List<GlobalProperty> props) throws APIException {
  599. Context.getAdministrationService().saveGlobalProperties(props);
  600. }
  601. /**
  602. * @see org.openmrs.api.AdministrationService#deleteGlobalProperty(java.lang.String)
  603. * @deprecated
  604. */
  605. @Deprecated
  606. public void deleteGlobalProperty(String propertyName) throws APIException {
  607. purgeGlobalProperty(new GlobalProperty(propertyName));
  608. }
  609. /**
  610. * @see org.openmrs.api.AdministrationService#setGlobalProperty(java.lang.String,
  611. * java.lang.String)
  612. * @deprecated
  613. */
  614. @Deprecated
  615. public void setGlobalProperty(String propertyName, String propertyValue) throws APIException {
  616. Context.getAdministrationService().saveGlobalProperty(new GlobalProperty(propertyName, propertyValue));
  617. }
  618. /**
  619. * @see org.openmrs.api.AdministrationService#setGlobalProperty(org.openmrs.GlobalProperty)
  620. * @deprecated
  621. */
  622. @Deprecated
  623. public void setGlobalProperty(GlobalProperty gp) throws APIException {
  624. Context.getAdministrationService().saveGlobalProperty(gp);
  625. }
  626. /**
  627. * @see org.openmrs.api.AdministrationService#addGlobalProperty(org.openmrs.GlobalProperty)
  628. * @deprecated
  629. */
  630. @Deprecated
  631. public void addGlobalProperty(GlobalProperty gp) {
  632. setGlobalProperty(gp);
  633. }
  634. /**
  635. * @see org.openmrs.api.AdministrationService#addGlobalProperty(java.lang.String,
  636. * java.lang.String)
  637. * @deprecated
  638. */
  639. @Deprecated
  640. public void addGlobalProperty(String propertyName, String propertyValue) throws APIException {
  641. //dao.addGlobalProperty(propertyName, propertyValue);
  642. Context.getAdministrationService().saveGlobalProperty(new GlobalProperty(propertyName, propertyValue));
  643. }
  644. /**
  645. * @see org.openmrs.api.AdministrationService#getAllGlobalProperties()
  646. */
  647. public List<GlobalProperty> getAllGlobalProperties() throws APIException {
  648. return dao.getAllGlobalProperties();
  649. }
  650. /**
  651. * @see org.openmrs.api.AdministrationService#getGlobalPropertiesByPrefix(java.lang.String)
  652. */
  653. public List<GlobalProperty> getGlobalPropertiesByPrefix(String prefix) {
  654. return dao.getGlobalPropertiesByPrefix(prefix);
  655. }
  656. /**
  657. * @see org.openmrs.api.AdministrationService#getGlobalPropertiesBySuffix(java.lang.String)
  658. */
  659. public List<GlobalProperty> getGlobalPropertiesBySuffix(String suffix) {
  660. return dao.getGlobalPropertiesBySuffix(suffix);
  661. }
  662. /**
  663. * @see org.openmrs.api.AdministrationService#purgeGlobalProperty(org.openmrs.GlobalProperty)
  664. */
  665. public void purgeGlobalProperty(GlobalProperty globalProperty) throws APIException {
  666. notifyGlobalPropertyDelete(globalProperty.getProperty());
  667. dao.deleteGlobalProperty(globalProperty);
  668. }
  669. /**
  670. * @see org.openmrs.api.AdministrationService#saveGlobalProperties(java.util.List)
  671. */
  672. public List<GlobalProperty> saveGlobalProperties(List<GlobalProperty> props) throws APIException {
  673. log.debug("saving a list of global properties");
  674. // add all of the new properties
  675. for (GlobalProperty prop : props) {
  676. if (prop.getProperty() != null && prop.getProperty().length() > 0) {
  677. saveGlobalProperty(prop);
  678. }
  679. }
  680. return props;
  681. }
  682. /**
  683. * @see org.openmrs.api.AdministrationService#saveGlobalProperty(org.openmrs.GlobalProperty)
  684. */
  685. public GlobalProperty saveGlobalProperty(GlobalProperty gp) throws APIException {
  686. // only try to save it if the global property has a key
  687. if (gp.getProperty() != null && gp.getProperty().length() > 0) {
  688. dao.saveGlobalProperty(gp);
  689. notifyGlobalPropertyChange(gp);
  690. return gp;
  691. }
  692. return gp;
  693. }
  694. /**
  695. * @see org.openmrs.api.AdministrationService#executeSQL(java.lang.String, boolean)
  696. */
  697. public List<List<Object>> executeSQL(String sql, boolean selectOnly) throws APIException {
  698. if (sql == null || sql.trim().equals(""))
  699. return null;
  700. return dao.executeSQL(sql, selectOnly);
  701. }
  702. /**
  703. * @see org.openmrs.api.AdministrationService#addGlobalPropertyListener(GlobalPropertyListener)
  704. */
  705. public void addGlobalPropertyListener(GlobalPropertyListener listener) {
  706. eventListeners.getGlobalPropertyListeners().add(listener);
  707. }
  708. /**
  709. * @see org.openmrs.api.AdministrationService#removeGlobalPropertyListener(GlobalPropertyListener)
  710. */
  711. public void removeGlobalPropertyListener(GlobalPropertyListener listener) {
  712. eventListeners.getGlobalPropertyListeners().remove(listener);
  713. }
  714. /**
  715. * Calls global property listeners registered for this create/change
  716. *
  717. * @param gp
  718. */
  719. private void notifyGlobalPropertyChange(GlobalProperty gp) {
  720. for (GlobalPropertyListener listener : eventListeners.getGlobalPropertyListeners())
  721. if (listener.supportsPropertyName(gp.getProperty()))
  722. listener.globalPropertyChanged(gp);
  723. }
  724. /**
  725. * Calls global property listeners registered for this delete
  726. *
  727. * @param propertyName
  728. */
  729. private void notifyGlobalPropertyDelete(String propertyName) {
  730. for (GlobalPropertyListener listener : eventListeners.getGlobalPropertyListeners())
  731. if (listener.supportsPropertyName(propertyName))
  732. listener.globalPropertyDeleted(propertyName);
  733. }
  734. /**
  735. * @see org.openmrs.api.AdministrationService#getImplementationId()
  736. */
  737. public ImplementationId getImplementationId() throws APIException {
  738. String property = getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_IMPLEMENTATION_ID);
  739. // fail early if no gp has been defined yet
  740. if (property == null)
  741. return null;
  742. try {
  743. ImplementationId implId = OpenmrsUtil.getSerializer().read(ImplementationId.class, property);
  744. return implId;
  745. }
  746. catch (Throwable t) {
  747. log.debug("Error while getting implementation id", t);
  748. }
  749. return null;
  750. }
  751. /**
  752. * @see org.openmrs.api.AdministrationService#setImplementationId(org.openmrs.ImplementationId)
  753. */
  754. public void setImplementationId(ImplementationId implementationId) throws APIException {
  755. if (implementationId == null)
  756. return;
  757. // check the validity of this implementation id with the server
  758. String description = implementationId.getDescription();
  759. try {
  760. // check that source id is valid
  761. description = checkImplementationIdValidity(implementationId.getImplementationId(), description,
  762. implementationId.getPassphrase());
  763. // save the server's description back to this concept source object
  764. implementationId.setDescription(description);
  765. boolean foundMatchingSource = false;
  766. // loop over the concept sources to make sure one exists for this hl7Code/implementationId
  767. List<ConceptSource> sources = Context.getConceptService().getAllConceptSources();
  768. if (sources != null) {
  769. for (ConceptSource source : sources) {
  770. if (implementationId.getImplementationId().equals(source.getHl7Code())) {
  771. foundMatchingSource = true;
  772. }
  773. }
  774. }
  775. // if no ConceptSource currently exists with this implementationId, save this implId
  776. // as a new ConceptSource
  777. if (!foundMatchingSource) {
  778. ConceptSource newConceptSource = new ConceptSource();
  779. newConceptSource.setName(implementationId.getName());
  780. newConceptSource.setDescription(implementationId.getDescription());
  781. newConceptSource.setHl7Code(implementationId.getImplementationId());
  782. if (Context.getAuthenticatedUser() == null) // (hackish)
  783. newConceptSource.setCreator(new User(1)); // fake the user because no one is logged in
  784. Context.getConceptService().saveConceptSource(newConceptSource);
  785. }
  786. // serialize and save the ImplementationId to the global properties table
  787. StringWriter stringWriter = new StringWriter();
  788. OpenmrsUtil.getSerializer().write(implementationId, stringWriter);
  789. Context.getAdministrationService().saveGlobalProperty(
  790. new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_IMPLEMENTATION_ID, stringWriter.toString()));
  791. }
  792. catch (APIException e) {
  793. throw e;
  794. }
  795. catch (Exception e) {
  796. // pass any other exceptions on up the train
  797. throw new APIException(e);
  798. }
  799. finally {
  800. // save an empty concept source to the database when something fails?
  801. }
  802. }
  803. /**
  804. * Checks the remote server for this exact implementation id. Returns the description if 1)
  805. * there is no implementation id or 2) there is a implementation id and this passphrase matches
  806. * it. In the case of 1), this implementation id and passphrase are saved to the remote server's
  807. * database
  808. *
  809. * @param implementationId
  810. * @param description
  811. * @param passphrase
  812. * @return the stored description on the remote server
  813. * @throws APIException
  814. * @throws UnsupportedEncodingException
  815. */
  816. private String checkImplementationIdValidity(String implementationId, String description, String passphrase)
  817. throws APIException {
  818. if (!StringUtils.hasLength(implementationId))
  819. throw new APIException("The implementationid cannot be empty");
  820. if (!StringUtils.hasLength(description))
  821. throw new APIException("The description cannot be empty");
  822. if (!StringUtils.hasLength(passphrase))
  823. throw new APIException("The passphrase cannot be empty");
  824. // set up the data map to post to the openmrs server
  825. Map<String, String> data = new HashMap<String, String>();
  826. data.put("implementationId", implementationId);
  827. data.put("description", description);
  828. data.put("passphrase", passphrase);
  829. String response = OpenmrsUtil.postToUrl(OpenmrsConstants.IMPLEMENTATION_ID_REMOTE_CONNECTION_URL, data);
  830. response = response.trim();
  831. if ("".equals(response)) {
  832. String ms = Context.getMessageSourceService().getMessage("ImplementationId.connectionError",
  833. new String[] { implementationId }, Context.getLocale());
  834. throw new APIException(ms);
  835. }
  836. if (log.isDebugEnabled())
  837. log.debug("Response: " + response);
  838. if (response.startsWith("Success")) {
  839. response = response.replace("Success", "");
  840. return response.trim();
  841. }
  842. String ms = Context.getMessageSourceService().getMessage("ImplementationId.invalidIdorPassphrase",
  843. new String[] { description }, Context.getLocale());
  844. throw new APIException(ms);
  845. }
  846. /**
  847. * @see org.openmrs.api.AdministrationService#getAllowedLocales()
  848. */
  849. public List<Locale> getAllowedLocales() {
  850. // lazy-load the global locale list and initialize with current global property value
  851. if (globalLocaleList == null) {
  852. globalLocaleList = new GlobalLocaleList();
  853. addGlobalPropertyListener(globalLocaleList);
  854. }
  855. Set<Locale> allowedLocales = globalLocaleList.getAllowedLocales();
  856. // update the GlobalLocaleList.allowedLocales by faking a global property change
  857. if (allowedLocales == null) {
  858. // use a default language of "english" if they have cleared this GP for some reason
  859. String currentPropertyValue = getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST,
  860. LocaleUtility.getDefaultLocale().toString());
  861. GlobalProperty allowedLocalesProperty = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST,
  862. currentPropertyValue);
  863. globalLocaleList.globalPropertyChanged(allowedLocalesProperty);
  864. allowedLocales = globalLocaleList.getAllowedLocales();
  865. }
  866. // allowedLocales is guaranteed to not be null at this point
  867. return new ArrayList<Locale>(allowedLocales);
  868. }
  869. /**
  870. * Used by spring to set the GlobalLocaleList on this implementation
  871. *
  872. * @param gll the GlobalLocaleList object that is registered to the GlobalPropertyListeners as
  873. * well
  874. */
  875. public void setGlobalLocaleList(GlobalLocaleList gll) {
  876. globalLocaleList = gll;
  877. }
  878. /**
  879. * @see org.openmrs.api.AdministrationService#getPresentationLocales()
  880. */
  881. public Set<Locale> getPresentationLocales() {
  882. if (presentationLocales == null) {
  883. presentationLocales = new HashSet<Locale>();
  884. Collection<Locale> messageLocales = Context.getMessageSourceService().getLocales();
  885. List<Locale> allowedLocales = getAllowedLocales();
  886. for (Locale possibleLocale : messageLocales) {
  887. if (allowedLocales.contains(possibleLocale)) {
  888. presentationLocales.add(possibleLocale);
  889. } else {
  890. // to be sure, check for language-only matches
  891. for (Locale allowedLocale : allowedLocales) {
  892. if (("".equals(allowedLocale.getCountry()) || "".equals(possibleLocale.getCountry()))
  893. && (allowedLocale.getLanguage().equals(possibleLocale.getLanguage()))) {
  894. presentationLocales.add(possibleLocale);
  895. break;
  896. }
  897. }
  898. }
  899. }
  900. }
  901. return presentationLocales;
  902. }
  903. /**
  904. * @see org.openmrs.api.GlobalPropertyListener#globalPropertyChanged(org.openmrs.GlobalProperty)
  905. */
  906. public void globalPropertyChanged(GlobalProperty newValue) {
  907. if (newValue.getProperty().equals(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST)) {
  908. // reset the calculated locale values
  909. presentationLocales = null;
  910. }
  911. }
  912. /**
  913. * @see org.openmrs.api.GlobalPropertyListener#globalPropertyDeleted(java.lang.String)
  914. */
  915. public void globalPropertyDeleted(String propertyName) {
  916. // TODO Auto-generated method stub
  917. }
  918. /**
  919. * @see org.openmrs.api.GlobalPropertyListener#supportsPropertyName(java.lang.String)
  920. */
  921. public boolean supportsPropertyName(String propertyName) {
  922. return propertyName.equals(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST);
  923. }
  924. /**
  925. * @see org.openmrs.api.AdministrationService#getGlobalPropertyByUuid(java.lang.String)
  926. */
  927. public GlobalProperty getGlobalPropertyByUuid(String uuid) {
  928. return dao.getGlobalPropertyByUuid(uuid);
  929. }
  930. /**
  931. * @see org.openmrs.api.AdministrationService#getGlobalPropertyValue(java.lang.String,
  932. * java.lang.Object)
  933. */
  934. @SuppressWarnings("unchecked")
  935. public <T> T getGlobalPropertyValue(String propertyName, T defaultValue) throws APIException {
  936. if (defaultValue == null)
  937. throw new IllegalArgumentException("The defaultValue argument cannot be null");
  938. String propVal = getGlobalProperty(propertyName);
  939. if (!StringUtils.hasLength(propVal))
  940. return defaultValue;
  941. try {
  942. return (T) defaultValue.getClass().getDeclaredConstructor(String.class).newInstance(propVal);
  943. }
  944. catch (InstantiationException e) {
  945. throw new APIException(defaultValue.getClass().getName() + " is not able to be instantiated with value: "
  946. + propVal, e);
  947. }
  948. catch (NoSuchMethodException e) {
  949. throw new APIException(defaultValue.getClass().getName() + " does not have a string constructor", e);
  950. }
  951. catch (Exception e) {
  952. log.error("Unable to turn value '" + propVal + "' into type " + defaultValue.getClass().getName(), e);
  953. return defaultValue;
  954. }
  955. }
  956. /**
  957. * @see org.openmrs.api.AdministrationService#getSystemInformation()
  958. */
  959. public Map<String, Map<String, String>> getSystemInformation() throws APIException {
  960. Map<String, Map<String, String>> systemInfoMap = new LinkedHashMap<String, Map<String, String>>();
  961. systemInfoMap.put("SystemInfo.title.openmrsInformation", new LinkedHashMap<String, String>() {
  962. private static final long serialVersionUID = 1L;
  963. {
  964. put("SystemInfo.OpenMRSInstallation.systemDate", new SimpleDateFormat("yyyy-MM-dd").format(Calendar
  965. .getInstance().getTime()));
  966. put("SystemInfo.OpenMRSInstallation.systemTime", new SimpleDateFormat("HH:mm:ss").format(Calendar
  967. .getInstance().getTime()));
  968. put("SystemInfo.OpenMRSInstallation.openmrsVersion", OpenmrsConstants.OPENMRS_VERSION);
  969. try {
  970. put("SystemInfo.hostname", InetAddress.getLocalHost().getCanonicalHostName());
  971. }
  972. catch (UnknownHostException e) {
  973. put("SystemInfo.hostname", "Unknown host: " + e.getMessage());
  974. }
  975. }
  976. });
  977. systemInfoMap.put("SystemInfo.title.javaRuntimeEnvironmentInformation", new LinkedHashMap<String, String>() {
  978. Properties properties = System.getProperties();
  979. private static final long serialVersionUID = 1L;
  980. {
  981. put("SystemInfo.JavaRuntimeEnv.operatingSystem", properties.getProperty("os.name"));
  982. put("SystemInfo.JavaRuntimeEnv.operatingSystemArch", properties.getProperty("os.arch"));
  983. put("SystemInfo.JavaRuntimeEnv.operatingSystemVersion", properties.getProperty("os.version"));
  984. put("SystemInfo.JavaRuntimeEnv.javaVersion", properties.getProperty("java.version"));
  985. put("SystemInfo.JavaRuntimeEnv.javaVendor", properties.getProperty("java.vendor"));
  986. put("SystemInfo.JavaRuntimeEnv.jvmVersion", properties.getProperty("java.vm.version"));
  987. put("SystemInfo.JavaRuntimeEnv.jvmVendor", properties.getProperty("java.vm.vendor"));
  988. put("SystemInfo.JavaRuntimeEnv.javaRuntimeName", properties.getProperty("java.runtime.name"));
  989. put("SystemInfo.JavaRuntimeEnv.javaRuntimeVersion", properties.getProperty("java.runtime.version"));
  990. put("SystemInfo.JavaRuntimeEnv.userName", properties.getProperty("user.name"));
  991. put("SystemInfo.JavaRuntimeEnv.systemLanguage", properties.getProperty("user.language"));
  992. put("SystemInfo.JavaRuntimeEnv.systemTimezone", properties.getProperty("user.timezone"));
  993. put("SystemInfo.JavaRuntimeEnv.fileSystemEncoding", properties.getProperty("sun.jnu.encoding"));
  994. put("SystemInfo.JavaRuntimeEnv.userDirectory", properties.getProperty("user.dir"));
  995. put("SystemInfo.JavaRuntimeEnv.tempDirectory", properties.getProperty("java.io.tmpdir"));
  996. }
  997. });
  998. systemInfoMap.put("SystemInfo.title.memoryInformation", new LinkedHashMap<String, String>() {
  999. private static final long serialVersionUID = 1L;
  1000. Runtime runtime = Runtime.getRuntime();
  1001. {
  1002. put("SystemInfo.Memory.totalMemory", convertToMegaBytes(runtime.totalMemory()));
  1003. put("SystemInfo.Memory.freeMemory", convertToMegaBytes(runtime.freeMemory()));
  1004. put("SystemInfo.Memory.maximumHeapSize", convertToMegaBytes(runtime.maxMemory()));
  1005. }
  1006. });
  1007. systemInfoMap.put("SystemInfo.title.dataBaseInformation", new LinkedHashMap<String, String>() {
  1008. Properties properties = Context.getRuntimeProperties();
  1009. private static final long serialVersionUID = 1L;
  1010. {
  1011. put("SystemInfo.Database.name", OpenmrsConstants.DATABASE_NAME);
  1012. put("SystemInfo.Database.connectionURL", properties.getProperty("connection.url"));
  1013. put("SystemInfo.Database.userName", properties.getProperty("connection.username"));
  1014. put("SystemInfo.Database.driver", properties.getProperty("hibernate.connection.driver_class"));
  1015. put("SystemInfo.Database.dialect", properties.getProperty("hibernate.dialect"));
  1016. }
  1017. });
  1018. systemInfoMap.put("SystemInfo.title.moduleInformation", new LinkedHashMap<String, String>() {
  1019. private static final long serialVersionUID = 1L;
  1020. {
  1021. put("SystemInfo.Module.repositoryPath", ModuleUtil.getModuleRepository().getAbsolutePath());
  1022. Collection<Module> loadedModules = ModuleFactory.getLoadedModules();
  1023. for (Module module : loadedModules) {
  1024. String moduleInfo = module.getVersion() + " "
  1025. + (module.isStarted() ? "" : Context.getMessageSourceService().getMessage("Module.notStarted"));
  1026. put(module.getName(), moduleInfo);
  1027. }
  1028. }
  1029. });
  1030. return systemInfoMap;
  1031. }
  1032. /**
  1033. * @param bytes to be converted into mega bytes
  1034. * @return memory in mega bytes
  1035. */
  1036. private String convertToMegaBytes(long bytes) {
  1037. int ONE_KILO_BYTE = 1024;
  1038. return String.valueOf(bytes / ONE_KILO_BYTE / ONE_KILO_BYTE) + " MB";
  1039. }
  1040. /**
  1041. * @see org.openmrs.api.AdministrationService#purgeGlobalProperties(java.util.List)
  1042. */
  1043. @Override
  1044. public void purgeGlobalProperties(List<GlobalProperty> globalProperties) throws APIException {
  1045. for (GlobalProperty globalProperty : globalProperties) {
  1046. Context.getAdministrationService().purgeGlobalProperty(globalProperty);
  1047. }
  1048. }
  1049. /**
  1050. * @see AdministrationService#getMaximumPropertyLength(Class, String)
  1051. */
  1052. @Override
  1053. public int getMaximumPropertyLength(Class<? extends OpenmrsObject> aClass, String fieldName) {
  1054. return dao.getMaximumPropertyLength(aClass, fieldName);
  1055. }
  1056. }