• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmrs / openmrs-core / 31703336740

13 Aug 2026 01:07PM UTC coverage: 66.487% (-0.004%) from 66.491%
31703336740

push

github

ibacher
TRUNK-6733: Use java-uuid-generator for entity UUIDs instead of UUID.randomUUID() (#6391)

15 of 27 new or added lines in 10 files covered. (55.56%)

5 existing lines in 3 files now uncovered.

24799 of 37299 relevant lines covered (66.49%)

0.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.52
/api/src/main/java/org/openmrs/api/impl/FormServiceImpl.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 *
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs.api.impl;
11

12
import java.util.Collection;
13
import java.util.Collections;
14
import java.util.Date;
15
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.Iterator;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.Set;
21

22
import org.hibernate.exception.ConstraintViolationException;
23
import org.openmrs.Concept;
24
import org.openmrs.ConceptComplex;
25
import org.openmrs.EncounterType;
26
import org.openmrs.Field;
27
import org.openmrs.FieldAnswer;
28
import org.openmrs.FieldType;
29
import org.openmrs.Form;
30
import org.openmrs.FormField;
31
import org.openmrs.FormResource;
32
import org.openmrs.aop.RequiredDataAdvice;
33
import org.openmrs.api.APIException;
34
import org.openmrs.api.FormService;
35
import org.openmrs.api.FormsLockedException;
36
import org.openmrs.api.InvalidFileTypeException;
37
import org.openmrs.api.context.Context;
38
import org.openmrs.api.db.FormDAO;
39
import org.openmrs.api.handler.SaveHandler;
40
import org.openmrs.customdatatype.CustomDatatypeUtil;
41
import org.openmrs.obs.ComplexObsHandler;
42
import org.openmrs.obs.SerializableComplexObsHandler;
43
import org.openmrs.util.OpenmrsConstants;
44
import org.openmrs.util.OpenmrsUtil;
45
import org.openmrs.util.UuidUtil;
46
import org.openmrs.validator.FormValidator;
47
import org.springframework.transaction.annotation.Transactional;
48
import org.springframework.validation.BindException;
49

50
/**
51
 * Default implementation of the {@link FormService}
52
 * <p>
53
 * This class should not be instantiated alone, get a service class from the Context:
54
 * Context.getFormService();
55
 * 
56
 * @see org.openmrs.api.context.Context
57
 * @see org.openmrs.api.FormService
58
 */
59
@Transactional
60
public class FormServiceImpl extends BaseOpenmrsService implements FormService {
61
        
62
        private FormDAO dao;
63
        
64
        private final FormValidator formValidator;
65
        
66
        /**
67
         * Default empty constructor
68
         */
69
        public FormServiceImpl() {
1✔
70
                formValidator = new FormValidator();
1✔
71
        }
1✔
72
        
73
        /**
74
         * Method used to inject the data access object.
75
         * 
76
         * @param dao
77
         */
78
        public void setFormDAO(FormDAO dao) {
79
                this.dao = dao;
1✔
80
        }
1✔
81
        
82
        /**
83
         * @see org.openmrs.api.FormService#getForm(java.lang.Integer)
84
         */
85
        @Override
86
        @Transactional(readOnly = true)
87
        public Form getForm(Integer formId) throws APIException {
88
                return dao.getForm(formId);
1✔
89
        }
90
        
91
        /**
92
         * Duplicate this form and form_fields associated with this form
93
         * 
94
         * @param form
95
         * @return New duplicated form
96
         * @throws APIException
97
         * @see org.openmrs.api.FormService#duplicateForm(org.openmrs.Form)
98
         */
99
        @Override
100
        public Form duplicateForm(Form form) throws APIException {
101
                checkIfFormsAreLocked();
1✔
102
                // get original form id for reference later
103
                Integer originalFormId = form.getFormId();
1✔
104
                
105
                for (FormField formField : form.getFormFields()) {
1✔
106
                        formField.setUuid(null);
1✔
107
                        formField.setFormFieldId(null);
1✔
108
                }
1✔
109
                // this is required because Hibernate would recognize the original collection
110
                form.setFormFields(new HashSet<>(form.getFormFields()));
1✔
111
                
112
                form.setUuid(null);
1✔
113
                form.setFormId(null);
1✔
114
                form.setCreator(null);
1✔
115
                form.setDateCreated(null);
1✔
116
                form.setChangedBy(null);
1✔
117
                form.setDateChanged(null);
1✔
118
                
119
                Context.clearSession();
1✔
120
                
121
                Form originalForm = Context.getFormService().getForm(originalFormId);
1✔
122
                //On upgrading from hibernate 4.3.10.Final to 4.3.11.Final, 
123
                //calling getFormResourcesForForm results into a flush which finds the form as dirty,
124
                //resulting into the failure of this test
125
                //FormServiceTest.duplicateForm_shouldClearChangedDetailsAndUpdateCreationDetails:401 expected null, but was:<admin>
126
                //That is why we call getFormResourcesForForm before dao.duplicateForm(form) below.
127
                Collection<FormResource> formResources = Context.getFormService().getFormResourcesForForm(originalForm);
1✔
128
                
129
                RequiredDataAdvice.recursivelyHandle(SaveHandler.class, form, null);
1✔
130
                Form newForm = dao.duplicateForm(form);
1✔
131
                
132
                // duplicate form resources from the old form to the new one
133
                duplicateFormResources(originalForm, newForm, formResources);
1✔
134
                
135
                return newForm;
1✔
136
        }
137
        
138
        /**
139
         * @see org.openmrs.api.FormService#retireForm(org.openmrs.Form, java.lang.String)
140
         */
141
        @Override
142
        public void retireForm(Form form, String reason) throws APIException {
143
                form.setRetired(true);
1✔
144
                form.setRetireReason(reason);
1✔
145
                Context.getFormService().saveForm(form);
1✔
146
        }
1✔
147
        
148
        /**
149
         * @see org.openmrs.api.FormService#unretireForm(org.openmrs.Form)
150
         */
151
        @Override
152
        public void unretireForm(Form form) throws APIException {
153
                form.setRetired(false);
1✔
154
                Context.getFormService().saveForm(form);
1✔
155
        }
1✔
156
        
157
        /**
158
         * @see org.openmrs.api.FormService#getAllFieldTypes()
159
         */
160
        @Override
161
        @Transactional(readOnly = true)
162
        public List<FieldType> getAllFieldTypes() throws APIException {
163
                return Context.getFormService().getAllFieldTypes(true);
1✔
164
        }
165
        
166
        /**
167
         * @see org.openmrs.api.FormService#getAllFieldTypes(boolean)
168
         */
169
        @Override
170
        @Transactional(readOnly = true)
171
        public List<FieldType> getAllFieldTypes(boolean includeRetired) throws APIException {
172
                return dao.getAllFieldTypes(includeRetired);
1✔
173
        }
174
        
175
        /**
176
         * @see org.openmrs.api.FormService#getFieldType(java.lang.Integer)
177
         */
178
        @Override
179
        @Transactional(readOnly = true)
180
        public FieldType getFieldType(Integer fieldTypeId) throws APIException {
181
                return dao.getFieldType(fieldTypeId);
1✔
182
        }
183
        
184
        /**
185
         * @see org.openmrs.api.FormService#getField(java.lang.Integer)
186
         */
187
        @Override
188
        @Transactional(readOnly = true)
189
        public Field getField(Integer fieldId) throws APIException {
190
                return dao.getField(fieldId);
1✔
191
        }
192
                
193
        /**
194
         * @see org.openmrs.api.FormService#getFormField(java.lang.Integer)
195
         */
196
        @Override
197
        @Transactional(readOnly = true)
198
        public FormField getFormField(Integer formFieldId) throws APIException {
199
                return dao.getFormField(formFieldId);
1✔
200
        }
201
        
202
        /**
203
         * @see org.openmrs.api.FormService#getFormField(org.openmrs.Form, org.openmrs.Concept,
204
         *      java.util.Collection, boolean)
205
         */
206
        @Override
207
        @Transactional(readOnly = true)
208
        public FormField getFormField(Form form, Concept concept, Collection<FormField> ignoreFormFields, boolean force)
209
                throws APIException {
210
                // create an empty ignoreFormFields list if none was passed in
211
                Collection<FormField> tmpIgnoreFormFields = ignoreFormFields;
1✔
212
                if (tmpIgnoreFormFields == null) {
1✔
213
                        tmpIgnoreFormFields = Collections.emptyList();
1✔
214
                }
215
                
216
                return dao.getFormField(form, concept, tmpIgnoreFormFields, force);
1✔
217
        }
218
        
219
        /**
220
         * @see org.openmrs.api.FormService#getFieldByUuid(java.lang.String)
221
         */
222
        @Override
223
        @Transactional(readOnly = true)
224
        public Field getFieldByUuid(String uuid) throws APIException {
225
                return dao.getFieldByUuid(uuid);
1✔
226
        }
227
        
228
        @Override
229
        @Transactional(readOnly = true)
230
        public FieldAnswer getFieldAnswerByUuid(String uuid) throws APIException {
231
                return dao.getFieldAnswerByUuid(uuid);
1✔
232
        }
233
        
234
        /**
235
         * @see org.openmrs.api.FormService#getFieldTypeByUuid(java.lang.String)
236
         */
237
        @Override
238
        @Transactional(readOnly = true)
239
        public FieldType getFieldTypeByUuid(String uuid) throws APIException {
240
                return dao.getFieldTypeByUuid(uuid);
1✔
241
        }
242
        
243
        /**
244
         * @see org.openmrs.api.FormService#getFieldTypeByName(java.lang.String)
245
         */
246
        @Override
247
        @Transactional(readOnly = true)
248
        public FieldType getFieldTypeByName(String name) throws APIException {
249
                return dao.getFieldTypeByName(name);
1✔
250
        }
251
        
252
        /**
253
         * @see org.openmrs.api.FormService#getFormByUuid(java.lang.String)
254
         */
255
        @Override
256
        @Transactional(readOnly = true)
257
        public Form getFormByUuid(String uuid) throws APIException {
258
                return dao.getFormByUuid(uuid);
1✔
259
        }
260
        
261
        /**
262
         * @see org.openmrs.api.FormService#getFormFieldByUuid(java.lang.String)
263
         */
264
        @Override
265
        @Transactional(readOnly = true)
266
        public FormField getFormFieldByUuid(String uuid) throws APIException {
267
                return dao.getFormFieldByUuid(uuid);
1✔
268
        }
269
        
270
        /**
271
         * @see org.openmrs.api.FormService#getAllFields()
272
         */
273
        @Override
274
        @Transactional(readOnly = true)
275
        public List<Field> getAllFields() throws APIException {
276
                return Context.getFormService().getAllFields(true);
1✔
277
        }
278
        
279
        /**
280
         * @see org.openmrs.api.FormService#getAllFields(boolean)
281
         */
282
        @Override
283
        @Transactional(readOnly = true)
284
        public List<Field> getAllFields(boolean includeRetired) throws APIException {
285
                return dao.getAllFields(includeRetired);
1✔
286
        }
287
        
288
        /**
289
         * @see org.openmrs.api.FormService#getAllFormFields()
290
         */
291
        @Override
292
        @Transactional(readOnly = true)
293
        public List<FormField> getAllFormFields() throws APIException {
294
                return dao.getAllFormFields();
1✔
295
        }
296
        
297
        /**
298
         * @see org.openmrs.api.FormService#getAllForms()
299
         */
300
        @Override
301
        @Transactional(readOnly = true)
302
        public List<Form> getAllForms() throws APIException {
303
                return Context.getFormService().getAllForms(true);
1✔
304
        }
305
        
306
        /**
307
         * @see org.openmrs.api.FormService#getAllForms(boolean)
308
         */
309
        @Override
310
        @Transactional(readOnly = true)
311
        public List<Form> getAllForms(boolean includeRetired) throws APIException {
312
                return dao.getAllForms(includeRetired);
1✔
313
        }
314
        
315
        /**
316
         * @see org.openmrs.api.FormService#getFields(java.util.Collection, java.util.Collection,
317
         *      java.util.Collection, java.util.Collection, java.util.Collection, java.lang.Boolean,
318
         *      java.util.Collection, java.util.Collection, java.lang.Boolean)
319
         */
320
        @Override
321
        @Transactional(readOnly = true)
322
        public List<Field> getFields(Collection<Form> forms, Collection<FieldType> fieldTypes, Collection<Concept> concepts,
323
                Collection<String> tableNames, Collection<String> attributeNames, Boolean selectMultiple,
324
                Collection<FieldAnswer> containsAllAnswers, Collection<FieldAnswer> containsAnyAnswer, Boolean retired)
325
                throws APIException {
326

327
                Collection<Form> tmpForms = forms == null ? Collections.emptyList() : forms;
×
328
                Collection<Concept> tmpConcepts = concepts == null ? Collections.emptyList() : concepts;
×
329
                Collection<FieldType> tmpFieldTypes = fieldTypes == null ? Collections.emptyList() : fieldTypes;
×
330
                Collection<String> tmpTableNames = tableNames == null ? Collections.emptyList() : tableNames;
×
331
                Collection<String> tmpAttributeNames = attributeNames == null ? Collections.emptyList() : attributeNames;
×
332
                Collection<FieldAnswer> tmpContainsAllAnswers = containsAllAnswers == null ? Collections.emptyList() : containsAllAnswers;
×
333
                Collection<FieldAnswer> tmpContainsAnyAnswer = containsAnyAnswer == null ? Collections.emptyList() : containsAnyAnswer;
×
334
                
335
                return dao.getFields(tmpForms, tmpFieldTypes, tmpConcepts, tmpTableNames, tmpAttributeNames, selectMultiple,
×
336
                                tmpContainsAllAnswers, tmpContainsAnyAnswer, retired);
337
        }
338
        
339
        /**
340
         * @see org.openmrs.api.FormService#getForm(java.lang.String)
341
         * <strong>Should</strong> return the form with the highest version, if more than one form with the given name
342
         *         exists
343
         */
344
        @Override
345
        @Transactional(readOnly = true)
346
        public Form getForm(String name) throws APIException {
347
                List<Form> forms = dao.getFormsByName(name);
1✔
348
                if (forms == null || forms.isEmpty()) {
1✔
349
                        return null;
1✔
350
                } else {
351
                        return forms.get(0);
1✔
352
                }
353
        }
354
        
355
        /**
356
         * @see org.openmrs.api.FormService#getForm(java.lang.String, java.lang.String)
357
         */
358
        @Override
359
        @Transactional(readOnly = true)
360
        public Form getForm(String name, String version) throws APIException {
361
                return dao.getForm(name, version);
1✔
362
        }
363
        
364
        /**
365
         * @see org.openmrs.api.FormService#getForms(java.lang.String, boolean)
366
         */
367
        @Override
368
        @Transactional(readOnly = true)
369
        public List<Form> getForms(String fuzzyName, boolean onlyLatestVersion) {
370
                // get all forms including unpublished and including retired
371
                List<Form> forms = Context.getFormService().getForms(fuzzyName, null, null, null, null, null, null);
×
372
                
373
                Set<String> namesAlreadySeen = new HashSet<>();
×
374
                for (Iterator<Form> i = forms.iterator(); i.hasNext();) {
×
375
                        Form form = i.next();
×
376
                        if (namesAlreadySeen.contains(form.getName())) {
×
377
                                i.remove();
×
378
                        } else {
379
                                namesAlreadySeen.add(form.getName());
×
380
                        }
381
                }
×
382
                return forms;
×
383
        }
384

385
        /**
386
         * @see org.openmrs.api.FormService#getForms(java.lang.String, java.lang.Boolean,
387
         *      java.util.Collection, java.lang.Boolean, java.util.Collection, java.util.Collection,
388
         *      java.util.Collection)
389
         */
390
        @Override
391
        @Transactional(readOnly = true)
392
        public List<Form> getForms(String partialName, Boolean published, Collection<EncounterType> encounterTypes,
393
                Boolean retired, Collection<FormField> containingAnyFormField, Collection<FormField> containingAllFormFields,
394
                Collection<Field> fields) {
395

396
                Collection<EncounterType> tmpEncounterTypes = encounterTypes == null ? Collections.emptyList() : encounterTypes;
1✔
397
                Collection<FormField> tmpContainingAllFormFields = containingAllFormFields == null ? Collections.emptyList() : containingAllFormFields;
1✔
398
                Collection<FormField> tmpContainingAnyFormField = containingAnyFormField == null ? Collections.emptyList() : containingAnyFormField;
1✔
399
                Collection<Field> tmpFields = fields == null ? Collections.emptyList() : fields;
1✔
400
                
401
                return dao.getForms(partialName, published, tmpEncounterTypes, retired, tmpContainingAnyFormField,
1✔
402
                    tmpContainingAllFormFields, tmpFields);
403
        }
404
        
405
        /**
406
         * @see org.openmrs.api.FormService#getFormCount(java.lang.String, java.lang.Boolean,
407
         *      java.util.Collection, java.lang.Boolean, java.util.Collection, java.util.Collection,
408
         *      java.util.Collection)
409
         */
410
        @Override
411
        @Transactional(readOnly = true)
412
        public Integer getFormCount(String partialName, Boolean published, Collection<EncounterType> encounterTypes,
413
                Boolean retired, Collection<FormField> containingAnyFormField, Collection<FormField> containingAllFormFields,
414
                Collection<Field> fields) {
415

416
                Collection<EncounterType> tmpEncounterTypes = encounterTypes == null ? Collections.emptyList() : encounterTypes;
×
417
                Collection<FormField> tmpContainingAllFormFields = containingAllFormFields == null ? Collections.emptyList() : containingAllFormFields;
×
418
                Collection<FormField> tmpContainingAnyFormField = containingAnyFormField == null ? Collections.emptyList() : containingAnyFormField;
×
419
                Collection<Field> tmpFields = fields == null ? Collections.emptyList() : fields;
×
420
                
421
                return dao.getFormCount(partialName, published, tmpEncounterTypes, retired, tmpContainingAnyFormField,
×
422
                    tmpContainingAllFormFields, tmpFields);
423
        }
424
        
425
        /**
426
         * @see org.openmrs.api.FormService#getPublishedForms()
427
         */
428
        @Override
429
        @Transactional(readOnly = true)
430
        public List<Form> getPublishedForms() throws APIException {
431
                return Context.getFormService().getForms(null, true, null, false, null, null, null);
×
432
        }
433
        
434
        /**
435
         * @see org.openmrs.api.FormService#purgeField(org.openmrs.Field)
436
         */
437
        @Override
438
        public void purgeField(Field field) throws APIException {
439
                Context.getFormService().purgeField(field, false);
1✔
440
        }
1✔
441
        
442
        /**
443
         * @see org.openmrs.api.FormService#purgeField(org.openmrs.Field, boolean)
444
         */
445
        @Override
446
        public void purgeField(Field field, boolean cascade) throws APIException {
447
                if (cascade) {
1✔
448
                        throw new APIException("general.not.yet.implemented", (Object[]) null);
×
449
                } else {
450
                        dao.deleteField(field);
1✔
451
                }
452
        }
1✔
453
        
454
        /**
455
         * @see org.openmrs.api.FormService#purgeForm(org.openmrs.Form)
456
         */
457
        @Override
458
        public void purgeForm(Form form) throws APIException {
459
                checkIfFormsAreLocked();
1✔
460
                Context.getFormService().purgeForm(form, false);
1✔
461
        }
1✔
462
        
463
        /**
464
         * @see org.openmrs.api.FormService#purgeForm(org.openmrs.Form, boolean)
465
         */
466
        @Override
467
        public void purgeForm(Form form, boolean cascade) throws APIException {
468
                if (cascade) {
1✔
469
                        throw new APIException("general.not.yet.implemented", (Object[]) null);
×
470
                }
471
                
472
                // remove resources
473
                for (FormResource resource : Context.getFormService().getFormResourcesForForm(form)) {
1✔
474
                        Context.getFormService().purgeFormResource(resource);
1✔
475
                }
1✔
476
                
477
                dao.deleteForm(form);
1✔
478
        }
1✔
479
        
480
        /**
481
         * @see org.openmrs.api.FormService#purgeFormField(org.openmrs.FormField)
482
         */
483
        @Override
484
        public void purgeFormField(FormField formField) throws APIException {
485
                dao.deleteFormField(formField);
×
486
        }
×
487
        
488
        /**
489
         * @see org.openmrs.api.FormService#retireField(org.openmrs.Field)
490
         */
491
        @Override
492
        public Field retireField(Field field) throws APIException {
493
                if (!field.getRetired()) {
×
494
                        field.setRetired(true);
×
495
                        return Context.getFormService().saveField(field);
×
496
                } else {
497
                        return field;
×
498
                }
499
        }
500
        
501
        /**
502
         * @see org.openmrs.api.FormService#saveField(org.openmrs.Field)
503
         */
504
        @Override
505
        public Field saveField(Field field) throws APIException {
506
                return dao.saveField(field);
1✔
507
        }
508
        
509
        /**
510
         * @see org.openmrs.api.FormService#saveForm(org.openmrs.Form)
511
         */
512
        @Override
513
        public Form saveForm(Form form) throws APIException {
514
                checkIfFormsAreLocked();
1✔
515
                BindException errors = new BindException(form, "form");
1✔
516
                formValidator.validate(form, errors);
1✔
517
                if (errors.hasErrors()) {
1✔
518
                        throw new APIException(errors);
×
519
                }
520
                
521
                if (form.getFormFields() != null) {
1✔
522
                        for (FormField ff : form.getFormFields()) {
1✔
523
                                if (ff.getForm() == null) {
1✔
524
                                        ff.setForm(form);
×
525
                                } else if (!ff.getForm().equals(form)) {
1✔
526
                                        throw new APIException("Form.contains.FormField.error", new Object[] { ff });
×
527
                                }
528
                        }
1✔
529
                }
530
                
531
                return dao.saveForm(form);
1✔
532
        }
533
        
534
        /**
535
         * @see org.openmrs.api.FormService#saveFormField(org.openmrs.FormField)
536
         */
537
        @Override
538
        public FormField saveFormField(FormField formField) throws APIException {
539
                Field field = formField.getField();
1✔
540
                if (field.getCreator() == null) {
1✔
541
                        field.setCreator(Context.getAuthenticatedUser());
1✔
542
                }
543
                if (field.getDateCreated() == null) {
1✔
544
                        field.setDateCreated(new Date());
1✔
545
                }
546
                
547
                // don't change the changed by and date changed on field for
548
                // form field updates
549
                
550
                // set the uuid here because the RequiredDataAdvice only looks at child lists
551
                if (field.getUuid() == null) {
1✔
NEW
552
                        field.setUuid(UuidUtil.newUuidString());
×
553
                }
554

555
                FormField tmpFormField = dao.saveFormField(formField);
1✔
556
                
557
                //Include all formfields from all serializable complex obs handlers
558
                Concept concept = tmpFormField.getField().getConcept();
1✔
559
                if (concept != null && concept.isComplex()) {
1✔
560
                        ComplexObsHandler handler = Context.getObsService().getHandler(((ConceptComplex) concept).getHandler());
1✔
561
                        if (handler instanceof SerializableComplexObsHandler) {
1✔
562
                                SerializableComplexObsHandler sHandler = (SerializableComplexObsHandler) handler;
1✔
563
                                if (sHandler.getFormFields() != null) {
1✔
564
                                        for (FormField ff : sHandler.getFormFields()) {
1✔
565
                                                ff.setParent(tmpFormField);
1✔
566
                                                ff.setForm(tmpFormField.getForm());
1✔
567
                                                ff.setCreator(tmpFormField.getCreator());
1✔
568
                                                ff.setDateCreated(tmpFormField.getDateCreated());
1✔
569
                                                dao.saveFormField(ff);
1✔
570
                                        }
1✔
571
                                }
572
                        }
573
                }
574
                
575
                return tmpFormField;
1✔
576
        }
577
        
578
        /**
579
         * @see org.openmrs.api.FormService#unretireField(org.openmrs.Field)
580
         */
581
        @Override
582
        public Field unretireField(Field field) throws APIException {
583
                if (field.getRetired()) {
×
584
                        field.setRetired(false);
×
585
                        return Context.getFormService().saveField(field);
×
586
                } else {
587
                        return field;
×
588
                }
589
        }
590
        
591
        /**
592
         * @see org.openmrs.api.FormService#getFields(java.lang.String)
593
         */
594
        @Override
595
        public List<Field> getFields(String fuzzySearchPhrase) throws APIException {
596
                return dao.getFields(fuzzySearchPhrase);
×
597
        }
598
        
599
        /**
600
         * @see org.openmrs.api.FormService#getFieldsByConcept(org.openmrs.Concept)
601
         */
602
        @Override
603
        @Transactional(readOnly = true)
604
        public List<Field> getFieldsByConcept(Concept concept) throws APIException {
605
                return Context.getFormService().getFields(null, null, Collections.singleton(concept), null, null, null, null, null,
×
606
                    null);
607
        }
608
        
609
        /**
610
         * @see org.openmrs.api.FormService#getFormsContainingConcept(org.openmrs.Concept)
611
         */
612
        @Override
613
        @Transactional(readOnly = true)
614
        public List<Form> getFormsContainingConcept(Concept concept) throws APIException {
615
                if (concept.getConceptId() == null) {
1✔
616
                        return Collections.emptyList();
×
617
                }
618
                
619
                return dao.getFormsContainingConcept(concept);
1✔
620
        }
621
        
622
        /**
623
         * @see org.openmrs.api.FormService#purgeFieldType(org.openmrs.FieldType)
624
         */
625
        @Override
626
        public void purgeFieldType(FieldType fieldType) throws APIException {
627
                dao.deleteFieldType(fieldType);
×
628
        }
×
629
        
630
        /**
631
         * @see org.openmrs.api.FormService#saveFieldType(org.openmrs.FieldType)
632
         */
633
        @Override
634
        public FieldType saveFieldType(FieldType fieldType) throws APIException {
635
                return dao.saveFieldType(fieldType);
1✔
636
        }
637
        
638
        /**
639
         * @see FormService#mergeDuplicateFields()
640
         */
641
        @Override
642
        public int mergeDuplicateFields() throws APIException {
643
                
644
                List<Field> fields = dao.getAllFields(true);
1✔
645
                Set<Field> fieldsToDelete = new HashSet<>();
1✔
646
                
647
                Map<String, Integer> fieldNameAsKeyAndFieldIdAsValueMap = new HashMap<>();
1✔
648
                
649
                for (Field field : fields) {
1✔
650
                        if (fieldNameAsKeyAndFieldIdAsValueMap.containsKey(field.getName())) {
1✔
651
                                Field fieldToCompareTo = dao.getField(fieldNameAsKeyAndFieldIdAsValueMap.get(field.getName()));
1✔
652
                                if (fieldsAreSimilar(field, fieldToCompareTo)) {
1✔
653
                                        
654
                                        //get the formFields that use this duplicate field
655
                                        List<FormField> formFields = dao.getFormFieldsByField(field);
1✔
656
                                        
657
                                        //for each of the formFields that use this duplicate field
658
                                        //replace with field from outer loop
659
                                        for (FormField formField : formFields) {
1✔
660
                                                formField.setField(fieldToCompareTo);
1✔
661
                                                dao.saveFormField(formField);
1✔
662
                                                
663
                                                fieldsToDelete.add(field);
1✔
664
                                        }
1✔
665
                                } else {
1✔
666
                                        fieldNameAsKeyAndFieldIdAsValueMap.put(field.getName(), field.getId());
×
667
                                }
668
                                
669
                        } else {
1✔
670
                                fieldNameAsKeyAndFieldIdAsValueMap.put(field.getName(), field.getId());
1✔
671
                        }
672
                        
673
                }
1✔
674
                
675
                for (Field field : fieldsToDelete) {
1✔
676
                        dao.deleteField(field);
1✔
677
                }
1✔
678
                
679
                return fieldsToDelete.size();
1✔
680
        }
681
        
682
        private boolean fieldsAreSimilar(Field field, Field fieldToBeReplaced) {
683
                
684
                return (OpenmrsUtil.nullSafeEquals(field.getName(), fieldToBeReplaced.getName())
1✔
685
                        && OpenmrsUtil.nullSafeEquals(field.getSelectMultiple(), fieldToBeReplaced.getSelectMultiple())
1✔
686
                        && OpenmrsUtil.nullSafeEquals(field.getFieldType(), fieldToBeReplaced.getFieldType())
1✔
687
                        && OpenmrsUtil.nullSafeEquals(field.getConcept(), fieldToBeReplaced.getConcept())
1✔
688
                        && OpenmrsUtil.nullSafeEquals(field.getTableName(), fieldToBeReplaced.getTableName())
1✔
689
                        && OpenmrsUtil.nullSafeEquals(field.getDefaultValue(), fieldToBeReplaced.getDefaultValue())
1✔
690
                        && field.getRetired() != null && !field.getRetired());
1✔
691
        }
692
        
693
        /**
694
         * @see org.openmrs.api.FormService#getFormResource(java.lang.Integer)
695
         */
696
        @Override
697
        @Transactional(readOnly = true)
698
        public FormResource getFormResource(Integer formResourceId) throws APIException {
699
                return dao.getFormResource(formResourceId);
1✔
700
        }
701
        
702
        /**
703
         * @see org.openmrs.api.FormService#getFormResourceByUuid(java.lang.String)
704
         */
705
        @Override
706
        @Transactional(readOnly = true)
707
        public FormResource getFormResourceByUuid(String uuid) throws APIException {
708
                return dao.getFormResourceByUuid(uuid);
×
709
        }
710
        
711
        /**
712
         * @see org.openmrs.api.FormService#getFormResource(org.openmrs.Form, java.lang.String)
713
         */
714
        @Override
715
        @Transactional(readOnly = true)
716
        public FormResource getFormResource(Form form, String name) throws APIException {
717
                return dao.getFormResource(form, name);
1✔
718
        }
719
        
720
        /**
721
         * @see org.openmrs.api.FormService#saveFormResource(org.openmrs.FormResource)
722
         */
723
        @Override
724
        public FormResource saveFormResource(FormResource formResource) throws APIException {
725
                    if (formResource == null) {
1✔
726
                        return null;
×
727
                }
728
                // If a form resource with same name exists, replace it with current value
729
                FormResource toPersist = formResource;
1✔
730
                FormResource original = Context.getFormService().getFormResource(formResource.getForm(), formResource.getName());
1✔
731
                if (original != null) {
1✔
732
                        original.setName(formResource.getName());
1✔
733
                        original.setValue(formResource.getValue());
1✔
734
                        original.setDatatypeClassname(formResource.getDatatypeClassname());
1✔
735
                        original.setDatatypeConfig(formResource.getDatatypeConfig());
1✔
736
                        original.setPreferredHandlerClassname(formResource.getPreferredHandlerClassname());
1✔
737
                        toPersist = original;
1✔
738
                }
739
                try {
740
                    CustomDatatypeUtil.saveIfDirty(toPersist);
1✔
741
                }
742
                catch (ConstraintViolationException ex) {
×
743
                    throw new InvalidFileTypeException(ex.getMessage(), ex);
×
744
                }
1✔
745
                
746
                return dao.saveFormResource(toPersist);
1✔
747
        }
748
        
749
        /**
750
         * @see org.openmrs.api.FormService#purgeFormResource(org.openmrs.FormResource)
751
         */
752
        @Override
753
        public void purgeFormResource(FormResource formResource) throws APIException {
754
                dao.deleteFormResource(formResource);
1✔
755
        }
1✔
756
        
757
        /**
758
         * @see org.openmrs.api.FormService#getFormResourcesForForm(org.openmrs.Form)
759
         */
760
        @Override
761
        @Transactional(readOnly = true)
762
        public Collection<FormResource> getFormResourcesForForm(Form form) throws APIException {
763
                return dao.getFormResourcesForForm(form);
1✔
764
        }
765
        
766
        /**
767
         * duplicates form resources from one form to another
768
         * 
769
         * @param source the form to copy resources from
770
         * @param destination the form to copy resources to
771
         * @param formResources the form resources from the source form
772
         */
773
        private void duplicateFormResources(Form source, Form destination, Collection<FormResource> formResources) {
774
                FormService service = Context.getFormService();
1✔
775
                for (FormResource resource : formResources) {
1✔
776
                        FormResource newResource = new FormResource(resource);
1✔
777
                        newResource.setForm(destination);
1✔
778
                        service.saveFormResource(newResource);
1✔
779
                }
1✔
780
        }
1✔
781
        
782
        /*
783
         * @see org.openmrs.api.FormService#checkIfFormsAreLocked()
784
         * @see FormsLockedException
785
         */
786
        @Override
787
        public void checkIfFormsAreLocked() {
788
                String locked = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_FORMS_LOCKED,
1✔
789
                    "false");
790
                if (Boolean.valueOf(locked)) {
1✔
791
                        throw new FormsLockedException();
1✔
792
                }
793
        }
1✔
794
        
795
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc