1- /**
1+ /*
22 * This Source Code Form is subject to the terms of the Mozilla Public License,
33 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
44 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
3030import org .openmrs .module .xforms .util .XformsUtil ;
3131import org .openmrs .util .OpenmrsConstants ;
3232
33-
3433/**
3534 * Builds the basic form.
3635 */
3736public class BasicFormBuilder {
3837
3938 private static final int FIELD_TYPE_CONCEPT = 1 ;
39+
4040 private static final int FIELD_TYPE_SECTION = 5 ;
41+
4142 private static final int FIELD_TYPE_DATABASE_ELEMENT = 2 ;
4243
4344 private static final String SECTION_NAME_PATIENT = "PATIENT" ;
45+
4446 private static final String SECTION_NAME_ENCOUNTER = "ENCOUNTER" ;
47+
4548 private static final String SECTION_NAME_OBS = "OBS" ;
4649
4750 private static final String TABLE_NAME_PATIENT_NAME = "patient_name" ;
51+
4852 private static final String TABLE_NAME_PATIENT = "patient" ;
53+
4954 private static final String TABLE_NAME_PATIENT_IDENTIFIER = "patient_identifier" ;
55+
5056 private static final String TABLE_NAME_ENCOUNTER = "encounter" ;
5157
5258 private static final String FIELD_FAMILY_NAME = "PATIENT.FAMILY_NAME" ;
59+
5360 private static final String FIELD_GIVEN_NAME = "PATIENT.GIVEN_NAME" ;
61+
5462 private static final String FIELD_MIDDLE_NAME = "PATIENT.MIDDLE_NAME" ;
63+
5564 private static final String FIELD_MEDICAL_RECORD_NUMBER = "PATIENT.MEDICAL_RECORD_NUMBER" ;
65+
5666 private static final String FIELD_PATIENT_ID = "PATIENT.PATIENT_ID" ;
67+
5768 private static final String FIELD_SEX = "PATIENT.SEX" ;
69+
5870 private static final String FIELD_BIRTHDATE = "PATIENT.BIRTHDATE" ;
71+
5972 private static final String FIELD_ENCOUNTER_DATETIME = "ENCOUNTER.ENCOUNTER_DATETIME" ;
73+
6074 private static final String FIELD_LOCATION_ID = "ENCOUNTER.LOCATION_ID" ;
75+
6176 private static final String FIELD_PROVIDER_ID = "ENCOUNTER.PROVIDER_ID" ;
6277
6378 private static final int CONCEPT_CLASS_MISC = 11 ;
79+
6480 private static final int CONCEPT_DATATYPE_NA = 4 ;
6581
6682 private static final String CONCEPT_NAME_MEDICAL_RECORD_OBSERVATIONS = "MEDICAL RECORD OBSERVATIONS" ;
6783
68-
69- public static void addDefaultFields (Form form ){
84+ public static void addDefaultFields (Form form ) {
7085
7186 FormService formService = Context .getFormService ();
7287
7388 //Add patient section.
7489 Field patientSection = getPatientSection (formService );
75- FormField patientFormField = getNewFormField (); patientSection .toString ();
90+ FormField patientFormField = getNewFormField ();
91+ patientSection .toString ();
7692 patientFormField .setField (patientSection );
7793 patientFormField .setFieldNumber (1 );
7894 form .addFormField (patientFormField );
79-
95+
8096 //Add encounter section.
8197 Field encounterSection = getEncounterSection (formService );
8298 FormField encounterFormField = getNewFormField ();
@@ -92,27 +108,36 @@ public static void addDefaultFields(Form form){
92108 form .addFormField (obsFormField );
93109
94110 //Add patient section fields.
95- addDatabaseElementField (formService , form , FIELD_FAMILY_NAME , TABLE_NAME_PATIENT_NAME , "family_name" , "$!{patient.getFamilyName()}" , patientFormField );
96- addDatabaseElementField (formService , form , FIELD_GIVEN_NAME , TABLE_NAME_PATIENT_NAME , "given_name" , "$!{patient.getGivenName()}" , patientFormField );
97- addDatabaseElementField (formService , form , FIELD_MIDDLE_NAME , TABLE_NAME_PATIENT_NAME , "middle_name" , "$!{patient.getMiddleName()}" , patientFormField );
98- addDatabaseElementField (formService , form , FIELD_BIRTHDATE , TABLE_NAME_PATIENT , "birthdate" , "$!{date.format($patient.getBirthdate())}" , patientFormField );
99- addDatabaseElementField (formService , form , FIELD_SEX , TABLE_NAME_PATIENT , "gender" , "$!{patient.getGender()}" , patientFormField );
100- addDatabaseElementField (formService , form , FIELD_PATIENT_ID , TABLE_NAME_PATIENT , "patient_id" , "$!{patient.getPatientId()}" , patientFormField );
101- addDatabaseElementField (formService , form , FIELD_MEDICAL_RECORD_NUMBER , TABLE_NAME_PATIENT_IDENTIFIER , "identifier" , "$!{patient.getPatientIdentifier(1).getIdentifier()}" , patientFormField );
111+ addDatabaseElementField (formService , form , FIELD_FAMILY_NAME , TABLE_NAME_PATIENT_NAME , "family_name" ,
112+ "$!{patient.getFamilyName()}" , patientFormField );
113+ addDatabaseElementField (formService , form , FIELD_GIVEN_NAME , TABLE_NAME_PATIENT_NAME , "given_name" ,
114+ "$!{patient.getGivenName()}" , patientFormField );
115+ addDatabaseElementField (formService , form , FIELD_MIDDLE_NAME , TABLE_NAME_PATIENT_NAME , "middle_name" ,
116+ "$!{patient.getMiddleName()}" , patientFormField );
117+ addDatabaseElementField (formService , form , FIELD_BIRTHDATE , TABLE_NAME_PATIENT , "birthdate" ,
118+ "$!{date.format($patient.getBirthdate())}" , patientFormField );
119+ addDatabaseElementField (formService , form , FIELD_SEX , TABLE_NAME_PATIENT , "gender" , "$!{patient.getGender()}" ,
120+ patientFormField );
121+ addDatabaseElementField (formService , form , FIELD_PATIENT_ID , TABLE_NAME_PATIENT , "patient_id" ,
122+ "$!{patient.getPatientId()}" , patientFormField );
123+ addDatabaseElementField (formService , form , FIELD_MEDICAL_RECORD_NUMBER , TABLE_NAME_PATIENT_IDENTIFIER , "identifier" ,
124+ "$!{patient.getPatientIdentifier(1).getIdentifier()}" , patientFormField );
102125
103126 //Add encounter section fields.
104- addDatabaseElementField (formService , form , FIELD_ENCOUNTER_DATETIME , TABLE_NAME_ENCOUNTER , "encounter_datetime" , null , encounterFormField );
105- addDatabaseElementField (formService , form , FIELD_LOCATION_ID , TABLE_NAME_ENCOUNTER , "location_id" , null , encounterFormField );
106- addDatabaseElementField (formService , form , FIELD_PROVIDER_ID , TABLE_NAME_ENCOUNTER , "provider_id" , null , encounterFormField );
107-
127+ addDatabaseElementField (formService , form , FIELD_ENCOUNTER_DATETIME , TABLE_NAME_ENCOUNTER , "encounter_datetime" ,
128+ null , encounterFormField );
129+ addDatabaseElementField (formService , form , FIELD_LOCATION_ID , TABLE_NAME_ENCOUNTER , "location_id" , null ,
130+ encounterFormField );
131+ addDatabaseElementField (formService , form , FIELD_PROVIDER_ID , TABLE_NAME_ENCOUNTER , "provider_id" , null ,
132+ encounterFormField );
108133
109134 //Finally save everything.
110135 formService .saveForm (form );
111136 }
112137
113- private static Field getPatientSection (FormService formService ){
138+ private static Field getPatientSection (FormService formService ) {
114139 Field field = getField (formService , SECTION_NAME_PATIENT , FIELD_TYPE_SECTION );
115- if (field == null ){
140+ if (field == null ) {
116141 field = getNewField ();
117142 field .setName (SECTION_NAME_PATIENT );
118143 field .setFieldType (formService .getFieldType (FIELD_TYPE_SECTION ));
@@ -122,9 +147,9 @@ private static Field getPatientSection(FormService formService){
122147 return field ;
123148 }
124149
125- private static Field getEncounterSection (FormService formService ){
150+ private static Field getEncounterSection (FormService formService ) {
126151 Field field = getField (formService , SECTION_NAME_ENCOUNTER , FIELD_TYPE_SECTION );
127- if (field == null ){
152+ if (field == null ) {
128153 field = getNewField ();
129154 field .setName (SECTION_NAME_ENCOUNTER );
130155 field .setFieldType (formService .getFieldType (FIELD_TYPE_SECTION ));
@@ -134,20 +159,19 @@ private static Field getEncounterSection(FormService formService){
134159 return field ;
135160 }
136161
137- private static Field getObsSection (FormService formService ){
162+ private static Field getObsSection (FormService formService ) {
138163 Field field = getField (formService , SECTION_NAME_OBS , FIELD_TYPE_CONCEPT );
139- if (field == null ) {
164+ if (field == null ) {
140165 field = getField (formService , SECTION_NAME_OBS , FIELD_TYPE_CONCEPT );
141166 }
142167
143- if (field == null ){
168+ if (field == null ) {
144169 field = getNewField ();
145170 field .setName (SECTION_NAME_OBS );
146171 field .setFieldType (formService .getFieldType (FIELD_TYPE_CONCEPT ));
147172 field .setConcept (getObsSectionConcept ());
148173 field .setDescription ("Obs section of form" );
149- }
150- else if (field .getConcept () == null ) {
174+ } else if (field .getConcept () == null ) {
151175 field .setConcept (getObsSectionConcept ());
152176 }
153177
@@ -160,8 +184,9 @@ private static Concept getObsSectionConcept() {
160184 return concept ;
161185 }
162186
163- String conceptId = Context .getAdministrationService ().getGlobalProperty (OpenmrsConstants .GLOBAL_PROPERTY_MEDICAL_RECORD_OBSERVATIONS );
164- if (conceptId != null && StringUtils .isNumeric (conceptId )) {
187+ String conceptId = Context .getAdministrationService ()
188+ .getGlobalProperty (OpenmrsConstants .GLOBAL_PROPERTY_MEDICAL_RECORD_OBSERVATIONS );
189+ if (conceptId != null && StringUtils .isNumeric (conceptId )) {
165190 concept = Context .getConceptService ().getConcept (Integer .parseInt (conceptId ));
166191 if (concept != null ) {
167192 return concept ;
@@ -170,20 +195,22 @@ private static Concept getObsSectionConcept() {
170195
171196 concept = new Concept ();
172197 concept .addName (new ConceptName (CONCEPT_NAME_MEDICAL_RECORD_OBSERVATIONS , Context .getLocale ()));
173- concept .addDescription (new ConceptDescription ("General description for clinical observations entered into the system." , Context .getLocale ()));
198+ concept .addDescription (new ConceptDescription (
199+ "General description for clinical observations entered into the system." , Context .getLocale ()));
174200 concept .setConceptClass (new ConceptClass (CONCEPT_CLASS_MISC ));
175201 concept .setDatatype (new ConceptDatatype (CONCEPT_DATATYPE_NA ));
176202 concept = Context .getConceptService ().saveConcept (concept );
177203
178- GlobalProperty globalProperty = Context .getAdministrationService ().getGlobalPropertyObject (OpenmrsConstants .GLOBAL_PROPERTY_MEDICAL_RECORD_OBSERVATIONS );
204+ GlobalProperty globalProperty = Context .getAdministrationService ()
205+ .getGlobalPropertyObject (OpenmrsConstants .GLOBAL_PROPERTY_MEDICAL_RECORD_OBSERVATIONS );
179206
180207 if (globalProperty == null ) {
181- globalProperty = new GlobalProperty (OpenmrsConstants .GLOBAL_PROPERTY_MEDICAL_RECORD_OBSERVATIONS ,
182- concept .getConceptId ().toString (), "The concept id of the MEDICAL_RECORD_OBSERVATIONS concept. " +
183- "This concept_id is presumed to be the generic grouping (obr) concept in hl7 messages . " +
184- "An obs_group row is not created for this concept." );
185- }
186- else {
208+ globalProperty = new GlobalProperty (OpenmrsConstants .GLOBAL_PROPERTY_MEDICAL_RECORD_OBSERVATIONS ,
209+ concept .getConceptId ().toString (),
210+ "The concept id of the MEDICAL_RECORD_OBSERVATIONS concept . "
211+ + "This concept_id is presumed to be the generic grouping (obr) concept in hl7 messages. "
212+ + "An obs_group row is not created for this concept." );
213+ } else {
187214 globalProperty .setPropertyValue (concept .getConceptId ().toString ());
188215 }
189216
@@ -192,13 +219,12 @@ private static Concept getObsSectionConcept() {
192219 return concept ;
193220 }
194221
195- private static Field getField (FormService formService , String name , int type ){
222+ private static Field getField (FormService formService , String name , int type ) {
196223 List <Field > fields = formService .getFields (name );
197- if (fields != null ){
198- for (Field field : fields ){
199- if (field .getFieldType () != null &&
200- field .getFieldType ().getFieldTypeId () == type &&
201- field .getName ().equalsIgnoreCase (name )){
224+ if (fields != null ) {
225+ for (Field field : fields ) {
226+ if (field .getFieldType () != null && field .getFieldType ().getFieldTypeId () == type
227+ && field .getName ().equalsIgnoreCase (name )) {
202228 return field ;
203229 }
204230 }
@@ -207,9 +233,10 @@ private static Field getField(FormService formService, String name, int type){
207233 return null ;
208234 }
209235
210- private static void addDatabaseElementField (FormService formService , Form form , String name , String tableName , String attributeName , String defaultValue , FormField parentFormField ){
236+ private static void addDatabaseElementField (FormService formService , Form form , String name , String tableName ,
237+ String attributeName , String defaultValue , FormField parentFormField ) {
211238 Field field = getField (formService , name , FIELD_TYPE_DATABASE_ELEMENT );
212- if (field == null ){
239+ if (field == null ) {
213240 field = getNewField ();
214241 field .setName (name );
215242 field .setFieldType (formService .getFieldType (FIELD_TYPE_DATABASE_ELEMENT ));
@@ -224,7 +251,7 @@ private static void addDatabaseElementField(FormService formService, Form form,
224251 form .addFormField (formField );
225252 }
226253
227- private static Field getNewField (){
254+ private static Field getNewField () {
228255 Field field = new Field ();
229256 field .setUuid (UUID .randomUUID ().toString ());
230257 field .setCreator (Context .getAuthenticatedUser ());
@@ -233,7 +260,7 @@ private static Field getNewField(){
233260 return field ;
234261 }
235262
236- private static FormField getNewFormField (){
263+ private static FormField getNewFormField () {
237264 FormField formField = new FormField ();
238265 formField .setUuid (UUID .randomUUID ().toString ());
239266 formField .setCreator (Context .getAuthenticatedUser ());
@@ -243,12 +270,13 @@ private static FormField getNewFormField(){
243270 return formField ;
244271 }
245272
246- public static String getFormXslt (){
273+ public static String getFormXslt () {
247274
248- try {
275+ try {
249276 String fileName = XformsUtil .isOnePointNineAndAbove () ? "form_xslt.xml" : "form_xslt_pre19.xml" ;
250277 StringBuffer fileData = new StringBuffer (1000 );
251- BufferedReader reader = new BufferedReader (new InputStreamReader (BasicFormBuilder .class .getResourceAsStream (fileName ), XformConstants .DEFAULT_CHARACTER_ENCODING ));
278+ BufferedReader reader = new BufferedReader (new InputStreamReader (
279+ BasicFormBuilder .class .getResourceAsStream (fileName ), XformConstants .DEFAULT_CHARACTER_ENCODING ));
252280
253281 char [] buf = new char [1024 ];
254282 int numRead = 0 ;
@@ -260,7 +288,7 @@ public static String getFormXslt(){
260288 reader .close ();
261289 return fileData .toString ();
262290 }
263- catch (Exception ex ){
291+ catch (Exception ex ) {
264292 ex .printStackTrace ();
265293 }
266294
0 commit comments