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

openmrs / openmrs-core / 41717837886

03 Nov 2026 08:59PM UTC coverage: 68.971% (+0.05%) from 68.922%
41717837886

push

github

ibacher
TRUNK-6863: Remove the generation token before clearing a node's cache locally (#6791)

1 of 1 new or added line in 1 file covered. (100.0%)

413 existing lines in 30 files now uncovered.

26445 of 38342 relevant lines covered (68.97%)

0.69 hits per line

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

77.42
/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
                form.setUuid(null);
1 ✔
110
                form.setFormId(null);
1 ✔
111
                form.setCreator(null);
1 ✔
112
                form.setDateCreated(null);
1 ✔
113
                form.setChangedBy(null);
1 ✔
114
                form.setDateChanged(null);
1 ✔
115
                
116
                Context.clearSession();
1 ✔
117
                
118
                Form originalForm = Context.getFormService().getForm(originalFormId);
1 ✔
119
                //On upgrading from hibernate 4.3.10.Final to 4.3.11.Final, 
120
                //calling getFormResourcesForForm results into a flush which finds the form as dirty,
121
                //resulting into the failure of this test
122
                //FormServiceTest.duplicateForm_shouldClearChangedDetailsAndUpdateCreationDetails:401 expected null, but was:<admin>
123
                //That is why we call getFormResourcesForForm before dao.duplicateForm(form) below.
124
                Collection<FormResource> formResources = Context.getFormService().getFormResourcesForForm(originalForm);
1 ✔
125
                
126
                RequiredDataAdvice.recursivelyHandle(SaveHandler.class, form, null);
1 ✔
127
                Form newForm = dao.duplicateForm(form);
1 ✔
128
                
129
                // duplicate form resources from the old form to the new one
130
                duplicateFormResources(originalForm, newForm, formResources);
1 ✔
131
                
132
                return newForm;
1 ✔
133
        }
134
        
135
        /**
136
         * @see org.openmrs.api.FormService#retireForm(org.openmrs.Form, java.lang.String)
137
         */
138
        @Override
139
        public void retireForm(Form form, String reason) throws APIException {
140
                form.setRetired(true);
1 ✔
141
                form.setRetireReason(reason);
1 ✔
142
                Context.getFormService().saveForm(form);
1 ✔
143
        }
1 ✔
144
        
145
        /**
146
         * @see org.openmrs.api.FormService#unretireForm(org.openmrs.Form)
147
         */
148
        @Override
149
        public void unretireForm(Form form) throws APIException {
150
                form.setRetired(false);
1 ✔
151
                Context.getFormService().saveForm(form);
1 ✔
152
        }
1 ✔
153
        
154
        /**
155
         * @see org.openmrs.api.FormService#getAllFieldTypes()
156
         */
157
        @Override
158
        @Transactional(readOnly = true)
159
        public List<FieldType> getAllFieldTypes() throws APIException {
160
                return Context.getFormService().getAllFieldTypes(true);
1 ✔
161
        }
162
        
163
        /**
164
         * @see org.openmrs.api.FormService#getAllFieldTypes(boolean)
165
         */
166
        @Override
167
        @Transactional(readOnly = true)
168
        public List<FieldType> getAllFieldTypes(boolean includeRetired) throws APIException {
169
                return dao.getAllFieldTypes(includeRetired);
1 ✔
170
        }
171
        
172
        /**
173
         * @see org.openmrs.api.FormService#getFieldType(java.lang.Integer)
174
         */
175
        @Override
176
        @Transactional(readOnly = true)
177
        public FieldType getFieldType(Integer fieldTypeId) throws APIException {
178
                return dao.getFieldType(fieldTypeId);
1 ✔
179
        }
180
        
181
        /**
182
         * @see org.openmrs.api.FormService#getField(java.lang.Integer)
183
         */
184
        @Override
185
        @Transactional(readOnly = true)
186
        public Field getField(Integer fieldId) throws APIException {
187
                return dao.getField(fieldId);
1 ✔
188
        }
189
                
190
        /**
191
         * @see org.openmrs.api.FormService#getFormField(java.lang.Integer)
192
         */
193
        @Override
194
        @Transactional(readOnly = true)
195
        public FormField getFormField(Integer formFieldId) throws APIException {
196
                return dao.getFormField(formFieldId);
1 ✔
197
        }
198
        
199
        /**
200
         * @see org.openmrs.api.FormService#getFormField(org.openmrs.Form, org.openmrs.Concept,
201
         *      java.util.Collection, boolean)
202
         */
203
        @Override
204
        @Transactional(readOnly = true)
205
        public FormField getFormField(Form form, Concept concept, Collection<FormField> ignoreFormFields, boolean force)
206
                throws APIException {
207
                // create an empty ignoreFormFields list if none was passed in
208
                Collection<FormField> tmpIgnoreFormFields = ignoreFormFields;
1 ✔
209
                if (tmpIgnoreFormFields == null) {
1 ✔
210
                        tmpIgnoreFormFields = Collections.emptyList();
1 ✔
211
                }
212
                
213
                return dao.getFormField(form, concept, tmpIgnoreFormFields, force);
1 ✔
214
        }
215
        
216
        /**
217
         * @see org.openmrs.api.FormService#getFieldByUuid(java.lang.String)
218
         */
219
        @Override
220
        @Transactional(readOnly = true)
221
        public Field getFieldByUuid(String uuid) throws APIException {
222
                return dao.getFieldByUuid(uuid);
1 ✔
223
        }
224
        
225
        @Override
226
        @Transactional(readOnly = true)
227
        public FieldAnswer getFieldAnswerByUuid(String uuid) throws APIException {
228
                return dao.getFieldAnswerByUuid(uuid);
1 ✔
229
        }
230
        
231
        /**
232
         * @see org.openmrs.api.FormService#getFieldTypeByUuid(java.lang.String)
233
         */
234
        @Override
235
        @Transactional(readOnly = true)
236
        public FieldType getFieldTypeByUuid(String uuid) throws APIException {
237
                return dao.getFieldTypeByUuid(uuid);
1 ✔
238
        }
239
        
240
        /**
241
         * @see org.openmrs.api.FormService#getFieldTypeByName(java.lang.String)
242
         */
243
        @Override
244
        @Transactional(readOnly = true)
245
        public FieldType getFieldTypeByName(String name) throws APIException {
246
                return dao.getFieldTypeByName(name);
1 ✔
247
        }
248
        
249
        /**
250
         * @see org.openmrs.api.FormService#getFormByUuid(java.lang.String)
251
         */
252
        @Override
253
        @Transactional(readOnly = true)
254
        public Form getFormByUuid(String uuid) throws APIException {
255
                return dao.getFormByUuid(uuid);
1 ✔
256
        }
257
        
258
        /**
259
         * @see org.openmrs.api.FormService#getFormFieldByUuid(java.lang.String)
260
         */
261
        @Override
262
        @Transactional(readOnly = true)
263
        public FormField getFormFieldByUuid(String uuid) throws APIException {
264
                return dao.getFormFieldByUuid(uuid);
1 ✔
265
        }
266
        
267
        /**
268
         * @see org.openmrs.api.FormService#getAllFields()
269
         */
270
        @Override
271
        @Transactional(readOnly = true)
272
        public List<Field> getAllFields() throws APIException {
273
                return Context.getFormService().getAllFields(true);
1 ✔
274
        }
275
        
276
        /**
277
         * @see org.openmrs.api.FormService#getAllFields(boolean)
278
         */
279
        @Override
280
        @Transactional(readOnly = true)
281
        public List<Field> getAllFields(boolean includeRetired) throws APIException {
282
                return dao.getAllFields(includeRetired);
1 ✔
283
        }
284
        
285
        /**
286
         * @see org.openmrs.api.FormService#getAllFormFields()
287
         */
288
        @Override
289
        @Transactional(readOnly = true)
290
        public List<FormField> getAllFormFields() throws APIException {
291
                return dao.getAllFormFields();
1 ✔
292
        }
293
        
294
        /**
295
         * @see org.openmrs.api.FormService#getAllForms()
296
         */
297
        @Override
298
        @Transactional(readOnly = true)
299
        public List<Form> getAllForms() throws APIException {
300
                return Context.getFormService().getAllForms(true);
1 ✔
301
        }
302
        
303
        /**
304
         * @see org.openmrs.api.FormService#getAllForms(boolean)
305
         */
306
        @Override
307
        @Transactional(readOnly = true)
308
        public List<Form> getAllForms(boolean includeRetired) throws APIException {
309
                return dao.getAllForms(includeRetired);
1 ✔
310
        }
311
        
312
        /**
313
         * @see org.openmrs.api.FormService#getFields(java.util.Collection, java.util.Collection,
314
         *      java.util.Collection, java.util.Collection, java.util.Collection, java.lang.Boolean,
315
         *      java.util.Collection, java.util.Collection, java.lang.Boolean)
316
         */
317
        @Override
318
        @Transactional(readOnly = true)
319
        public List<Field> getFields(Collection<Form> forms, Collection<FieldType> fieldTypes, Collection<Concept> concepts,
320
                Collection<String> tableNames, Collection<String> attributeNames, Boolean selectMultiple,
321
                Collection<FieldAnswer> containsAllAnswers, Collection<FieldAnswer> containsAnyAnswer, Boolean retired)
322
                throws APIException {
323

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

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

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

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

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