use of org.openmrs.FormField in project openmrs-core by openmrs.
the class FormServiceImpl method saveFormField.
/**
* @see org.openmrs.api.FormService#saveFormField(org.openmrs.FormField)
*/
@Override
public FormField saveFormField(FormField formField) throws APIException {
Field field = formField.getField();
if (field.getCreator() == null) {
field.setCreator(Context.getAuthenticatedUser());
}
if (field.getDateCreated() == null) {
field.setDateCreated(new Date());
}
// set the uuid here because the RequiredDataAdvice only looks at child lists
if (field.getUuid() == null) {
field.setUuid(UUID.randomUUID().toString());
}
FormField tmpFormField = dao.saveFormField(formField);
// Include all formfields from all serializable complex obs handlers
Concept concept = tmpFormField.getField().getConcept();
if (concept != null && concept.isComplex()) {
ComplexObsHandler handler = Context.getObsService().getHandler(((ConceptComplex) concept).getHandler());
if (handler instanceof SerializableComplexObsHandler) {
SerializableComplexObsHandler sHandler = (SerializableComplexObsHandler) handler;
if (sHandler.getFormFields() != null) {
for (FormField ff : sHandler.getFormFields()) {
ff.setParent(tmpFormField);
ff.setForm(tmpFormField.getForm());
ff.setCreator(tmpFormField.getCreator());
ff.setDateCreated(tmpFormField.getDateCreated());
dao.saveFormField(ff);
}
}
}
}
return tmpFormField;
}
use of org.openmrs.FormField in project openmrs-core by openmrs.
the class FormServiceImpl method mergeDuplicateFields.
/**
* @see FormService#mergeDuplicateFields()
*/
@Override
public int mergeDuplicateFields() throws APIException {
List<Field> fields = dao.getAllFields(true);
Set<Field> fieldsToDelete = new HashSet<>();
Map<String, Integer> fieldNameAsKeyAndFieldIdAsValueMap = new HashMap<>();
for (Field field : fields) {
if (fieldNameAsKeyAndFieldIdAsValueMap.containsKey(field.getName())) {
Field fieldToCompareTo = dao.getField(fieldNameAsKeyAndFieldIdAsValueMap.get(field.getName()));
if (fieldsAreSimilar(field, fieldToCompareTo)) {
// get the formFields that use this duplicate field
List<FormField> formFields = dao.getFormFieldsByField(field);
// replace with field from outer loop
for (FormField formField : formFields) {
formField.setField(fieldToCompareTo);
dao.saveFormField(formField);
fieldsToDelete.add(field);
}
} else {
fieldNameAsKeyAndFieldIdAsValueMap.put(field.getName(), field.getId());
}
} else {
fieldNameAsKeyAndFieldIdAsValueMap.put(field.getName(), field.getId());
}
}
for (Field field : fieldsToDelete) {
dao.deleteField(field);
}
return fieldsToDelete.size();
}
use of org.openmrs.FormField in project openmrs-core by openmrs.
the class FormServiceImpl method saveForm.
/**
* @see org.openmrs.api.FormService#saveForm(org.openmrs.Form)
*/
@Override
public Form saveForm(Form form) throws APIException {
checkIfFormsAreLocked();
BindException errors = new BindException(form, "form");
formValidator.validate(form, errors);
if (errors.hasErrors()) {
throw new APIException(errors);
}
if (form.getFormFields() != null) {
for (FormField ff : form.getFormFields()) {
if (ff.getForm() == null) {
ff.setForm(form);
} else if (!ff.getForm().equals(form)) {
throw new APIException("Form.contains.FormField.error", new Object[] { ff });
}
}
}
return dao.saveForm(form);
}
use of org.openmrs.FormField in project openmrs-core by openmrs.
the class FormServiceTest method getFormField_shouldIgnoreFormFieldsPassedToIgnoreFormFields.
/**
* @see FormService#getFormField(Form,Concept,Collection<QFormField;>,null)
*/
@Test
public void getFormField_shouldIgnoreFormFieldsPassedToIgnoreFormFields() {
executeDataSet(INITIAL_FIELDS_XML);
executeDataSet("org/openmrs/api/include/FormServiceTest-formFields.xml");
FormField ff = Context.getFormService().getFormField(new Form(1), new Concept(1), null, false);
// sanity check
assertNotNull(ff);
// test that the first formfield is ignored when a second fetch
// is done on the same form and same concept
List<FormField> ignoreFormFields = new ArrayList<>();
ignoreFormFields.add(ff);
FormField ff2 = Context.getFormService().getFormField(new Form(1), new Concept(1), ignoreFormFields, false);
assertNotNull(ff2);
assertNotSame(ff, ff2);
}
use of org.openmrs.FormField in project openmrs-core by openmrs.
the class FormServiceTest method saveFormField_shouldInjectFormFieldsFromSerializableComplexObsHandlers.
/**
* @see FormService#saveFormField(FormField)
*/
@SuppressWarnings("unchecked")
@Test
public void saveFormField_shouldInjectFormFieldsFromSerializableComplexObsHandlers() {
executeDataSet("org/openmrs/api/include/ConceptComplex.xml");
Context.getObsService().registerHandler("NeigborHandler", new NeighborHandler());
Concept concept = Context.getConceptService().getConcept(6043);
Field field = new Field();
field.setName("neighbor");
field.setConcept(concept);
FormField formField = new FormField();
formField.setField(field);
FormService fs = Context.getFormService();
formField.setForm(fs.getForm(1));
List<FormField> originalFormFields = fs.getAllFormFields();
int initialFormFieldCount = originalFormFields.size();
formField = fs.saveFormField(formField);
List<FormField> updatedFormFields = fs.getAllFormFields();
// should have this and the two form fields from the handler
Assert.assertEquals(initialFormFieldCount += 3, updatedFormFields.size());
// get the formfields added by the handler and check their parent
List<FormField> childFormFields = ListUtils.subtract(updatedFormFields, originalFormFields);
// exclude this form field
childFormFields.remove(formField);
for (FormField ff : childFormFields) {
Assert.assertEquals(formField, ff.getParent());
}
}
Aggregations