use of de.symeda.sormas.api.campaign.form.CampaignFormElementType in project SORMAS-Project by hzi-braunschweig.
the class CampaignFormDataEditFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = super.onCreateView(inflater, container, savedInstanceState);
final LinearLayout dynamicLayout = view.findViewById(R.id.dynamicLayout);
final CampaignFormMeta campaignFormMeta = DatabaseHelper.getCampaignFormMetaDao().queryForId(record.getCampaignFormMeta().getId());
final List<CampaignFormDataEntry> formValues = record.getFormValues();
final Map<String, String> formValuesMap = new HashMap<>();
formValues.forEach(campaignFormDataEntry -> formValuesMap.put(campaignFormDataEntry.getId(), DataHelper.toStringNullable(campaignFormDataEntry.getValue())));
final Map<String, ControlPropertyField> fieldMap = new HashMap<>();
final Map<CampaignFormElement, ControlPropertyField> expressionMap = new HashMap<>();
for (CampaignFormElement campaignFormElement : campaignFormMeta.getCampaignFormElements()) {
CampaignFormElementType type = CampaignFormElementType.fromString(campaignFormElement.getType());
if (type != CampaignFormElementType.SECTION && type != CampaignFormElementType.LABEL) {
String value = formValuesMap.get(campaignFormElement.getId());
ControlPropertyField dynamicField;
if (type == CampaignFormElementType.YES_NO) {
dynamicField = createControlCheckBoxField(campaignFormElement, requireContext(), getUserTranslations(campaignFormMeta));
ControlCheckBoxField.setValue((ControlCheckBoxField) dynamicField, Boolean.valueOf(value));
} else {
dynamicField = createControlTextEditField(campaignFormElement, requireContext(), getUserTranslations(campaignFormMeta));
ControlTextEditField.setValue((ControlTextEditField) dynamicField, value);
}
fieldMap.put(campaignFormElement.getId(), dynamicField);
dynamicField.setShowCaption(true);
dynamicLayout.addView(dynamicField, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
dynamicField.addValueChangedListener(field -> {
final CampaignFormDataEntry campaignFormDataEntry = getOrCreateCampaignFormDataEntry(formValues, campaignFormElement);
campaignFormDataEntry.setValue(field.getValue());
if (campaignFormElement.getExpression() == null && fieldMap.get(campaignFormElement.getId()) != null) {
expressionMap.forEach((formElement, controlPropertyField) -> handleExpression(expressionParser, formValues, CampaignFormElementType.fromString(formElement.getType()), controlPropertyField, formElement.getExpression()));
}
});
handleDependingOn(fieldMap, campaignFormElement, dynamicField);
final String expressionString = campaignFormElement.getExpression();
if (expressionString != null) {
handleExpression(expressionParser, formValues, type, dynamicField, expressionString);
expressionMap.put(campaignFormElement, dynamicField);
}
} else if (type == CampaignFormElementType.SECTION) {
dynamicLayout.addView(new ImageView(requireContext(), null, R.style.FullHorizontalDividerStyle));
} else if (type == CampaignFormElementType.LABEL) {
TextView textView = new TextView(requireContext());
TextViewBindingAdapters.setHtmlValue(textView, getUserLanguageCaption(getUserTranslations(campaignFormMeta), campaignFormElement));
dynamicLayout.addView(textView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
return view;
}
use of de.symeda.sormas.api.campaign.form.CampaignFormElementType in project SORMAS-Project by hzi-braunschweig.
the class CampaignFormDataNewFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = super.onCreateView(inflater, container, savedInstanceState);
final LinearLayout dynamicLayout = view.findViewById(R.id.dynamicLayout);
final CampaignFormMeta campaignFormMeta = DatabaseHelper.getCampaignFormMetaDao().queryForId(record.getCampaignFormMeta().getId());
record.setFormValues(new ArrayList<>());
final List<CampaignFormDataEntry> formValues = record.getFormValues();
final Map<String, ControlPropertyField> fieldMap = new HashMap<>();
final Map<CampaignFormElement, ControlPropertyField> expressionMap = new HashMap<>();
for (CampaignFormElement campaignFormElement : campaignFormMeta.getCampaignFormElements()) {
CampaignFormElementType type = CampaignFormElementType.fromString(campaignFormElement.getType());
if (type != CampaignFormElementType.SECTION && type != CampaignFormElementType.LABEL) {
ControlPropertyField dynamicField;
if (type == CampaignFormElementType.YES_NO) {
dynamicField = createControlCheckBoxField(campaignFormElement, requireContext(), getUserTranslations(campaignFormMeta));
} else {
dynamicField = createControlTextEditField(campaignFormElement, requireContext(), getUserTranslations(campaignFormMeta));
}
fieldMap.put(campaignFormElement.getId(), dynamicField);
dynamicField.setShowCaption(true);
dynamicLayout.addView(dynamicField, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
dynamicField.addValueChangedListener(field -> {
final CampaignFormDataEntry campaignFormDataEntry = getOrCreateCampaignFormDataEntry(formValues, campaignFormElement);
campaignFormDataEntry.setValue(field.getValue());
if (campaignFormElement.getExpression() == null && fieldMap.get(campaignFormElement.getId()) != null) {
expressionMap.forEach((formElement, controlPropertyField) -> handleExpression(expressionParser, formValues, CampaignFormElementType.fromString(formElement.getType()), controlPropertyField, formElement.getExpression()));
}
});
formValues.add(new CampaignFormDataEntry(campaignFormElement.getId(), null));
handleDependingOn(fieldMap, campaignFormElement, dynamicField);
final String expressionString = campaignFormElement.getExpression();
if (expressionString != null) {
handleExpression(expressionParser, formValues, type, dynamicField, expressionString);
expressionMap.put(campaignFormElement, dynamicField);
}
} else if (type == CampaignFormElementType.SECTION) {
dynamicLayout.addView(new ImageView(requireContext(), null, R.style.FullHorizontalDividerStyle));
} else if (type == CampaignFormElementType.LABEL) {
TextView textView = new TextView(requireContext());
TextViewBindingAdapters.setHtmlValue(textView, getUserLanguageCaption(getUserTranslations(campaignFormMeta), campaignFormElement));
dynamicLayout.addView(textView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
return view;
}
use of de.symeda.sormas.api.campaign.form.CampaignFormElementType in project SORMAS-Project by hzi-braunschweig.
the class CampaignFormDataReadFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = super.onCreateView(inflater, container, savedInstanceState);
final LinearLayout dynamicLayout = view.findViewById(R.id.dynamicLayout);
final CampaignFormMeta campaignFormMeta = DatabaseHelper.getCampaignFormMetaDao().queryForId(record.getCampaignFormMeta().getId());
final List<CampaignFormDataEntry> formValues = record.getFormValues();
final Map<String, String> formValuesMap = new HashMap<>();
formValues.forEach(campaignFormDataEntry -> formValuesMap.put(campaignFormDataEntry.getId(), DataHelper.toStringNullable(campaignFormDataEntry.getValue())));
final Map<String, ControlPropertyField> fieldMap = new HashMap<>();
for (CampaignFormElement campaignFormElement : campaignFormMeta.getCampaignFormElements()) {
CampaignFormElementType type = CampaignFormElementType.fromString(campaignFormElement.getType());
if (type != CampaignFormElementType.SECTION && type != CampaignFormElementType.LABEL) {
String value = formValuesMap.get(campaignFormElement.getId());
ControlPropertyField dynamicField = createControlTextReadField(campaignFormElement, requireContext(), getUserTranslations(campaignFormMeta));
dynamicField.setShowCaption(true);
if (value != null) {
if (type == CampaignFormElementType.YES_NO) {
ControlTextReadField.setValue((ControlTextReadField) dynamicField, Boolean.valueOf(value), null, null);
} else {
ControlTextReadField.setValue((ControlTextReadField) dynamicField, value, null, null, null);
}
}
dynamicLayout.addView(dynamicField, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
fieldMap.put(campaignFormElement.getId(), dynamicField);
final String dependingOn = campaignFormElement.getDependingOn();
final String[] dependingOnValues = campaignFormElement.getDependingOnValues();
if (dependingOn != null && dependingOnValues != null) {
ControlPropertyField controlPropertyField = fieldMap.get(dependingOn);
setVisibilityDependency(dynamicField, dependingOnValues, controlPropertyField.getValue());
}
final String expressionString = campaignFormElement.getExpression();
if (expressionString != null) {
try {
final Object expressionValue = getExpressionValue(expressionParser, formValues, expressionString);
if (type == CampaignFormElementType.YES_NO) {
ControlTextReadField.setValue((ControlTextReadField) dynamicField, (Boolean) expressionValue, null, null);
} else {
ControlTextReadField.setValue((ControlTextReadField) dynamicField, expressionValue.toString(), null, null, null);
}
} catch (SpelEvaluationException e) {
Log.e("Error evaluating expression: " + expressionString, e.getMessage());
}
}
} else if (type == CampaignFormElementType.SECTION) {
dynamicLayout.addView(new ImageView(requireContext(), null, R.style.FullHorizontalDividerStyle));
} else if (type == CampaignFormElementType.LABEL) {
TextView textView = new TextView(requireContext());
TextViewBindingAdapters.setHtmlValue(textView, campaignFormElement.getCaption());
dynamicLayout.addView(textView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
return view;
}
use of de.symeda.sormas.api.campaign.form.CampaignFormElementType in project SORMAS-Project by hzi-braunschweig.
the class CampaignFormBuilder method buildForm.
public void buildForm() {
int currentCol = -1;
GridLayout currentLayout = campaignFormLayout;
int sectionCount = 0;
for (CampaignFormElement formElement : formElements) {
CampaignFormElementType type = CampaignFormElementType.fromString(formElement.getType());
List<CampaignFormElementStyle> styles;
if (formElement.getStyles() != null) {
styles = Arrays.stream(formElement.getStyles()).map(CampaignFormElementStyle::fromString).collect(Collectors.toList());
} else {
styles = new ArrayList<>();
}
String dependingOnId = formElement.getDependingOn();
Object[] dependingOnValues = formElement.getDependingOnValues();
Object value = formValuesMap.get(formElement.getId());
int occupiedColumns = getOccupiedColumns(type, styles);
if (type == CampaignFormElementType.SECTION) {
sectionCount++;
GridLayout sectionLayout = new GridLayout(12, 1);
sectionLayout.setMargin(new MarginInfo(true, true));
CssStyles.style(sectionLayout, CssStyles.GRID_LAYOUT_SECTION, sectionCount % 2 == 0 ? CssStyles.GRID_LAYOUT_EVEN : CssStyles.GRID_LAYOUT_ODD);
sectionLayout.setWidth(100, Unit.PERCENTAGE);
currentLayout = sectionLayout;
campaignFormLayout.addComponent(sectionLayout, 0, campaignFormLayout.getRows() - 1, 11, campaignFormLayout.getRows() - 1);
campaignFormLayout.insertRow(campaignFormLayout.getRows());
} else if (type == CampaignFormElementType.LABEL) {
if ((currentCol + 1) + (occupiedColumns - 1) > 11 || currentCol > -1 && styles.contains(CampaignFormElementStyle.FIRST)) {
currentLayout.insertRow(currentLayout.getRows());
currentCol = -1;
}
Label field = new Label(get18nCaption(formElement.getId(), formElement.getCaption()));
field.setId(formElement.getId());
prepareComponent(field, formElement.getId(), formElement.getCaption(), type, styles);
currentLayout.addComponent(field, (currentCol + 1), currentLayout.getRows() - 1, (currentCol + 1) + (occupiedColumns - 1), currentLayout.getRows() - 1);
if (styles.contains(CampaignFormElementStyle.INLINE)) {
currentCol += occupiedColumns;
} else {
currentLayout.insertRow(currentLayout.getRows());
currentCol = -1;
}
if (dependingOnId != null && dependingOnValues != null) {
setVisibilityDependency(field, dependingOnId, dependingOnValues);
}
} else {
if ((currentCol + 1) + (occupiedColumns - 1) > 11 || currentCol > -1 && styles.contains(CampaignFormElementStyle.FIRST)) {
currentLayout.insertRow(currentLayout.getRows());
currentCol = -1;
}
Field<?> field = createField(formElement.getId(), formElement.getCaption(), type, styles);
setFieldValue(field, type, value);
field.setId(formElement.getId());
field.setCaption(get18nCaption(formElement.getId(), formElement.getCaption()));
field.setSizeFull();
currentLayout.addComponent(field, (currentCol + 1), currentLayout.getRows() - 1, (currentCol + 1) + (occupiedColumns - 1), currentLayout.getRows() - 1);
if (styles.contains(CampaignFormElementStyle.ROW)) {
currentLayout.insertRow(currentLayout.getRows());
currentCol = -1;
} else {
currentCol += occupiedColumns;
}
fields.put(formElement.getId(), field);
if (dependingOnId != null && dependingOnValues != null) {
setVisibilityDependency((AbstractComponent) field, dependingOnId, dependingOnValues);
}
}
}
}
Aggregations