Search in sources :

Example 1 with CampaignFormElementType

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;
}
Also used : HashMap(java.util.HashMap) CampaignFormElement(de.symeda.sormas.api.campaign.form.CampaignFormElement) ControlPropertyField(de.symeda.sormas.app.component.controls.ControlPropertyField) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) CampaignFormMeta(de.symeda.sormas.app.backend.campaign.form.CampaignFormMeta) CampaignFormElementType(de.symeda.sormas.api.campaign.form.CampaignFormElementType) CampaignFormDataEntry(de.symeda.sormas.api.campaign.data.CampaignFormDataEntry) CampaignFormDataFragmentUtils.getOrCreateCampaignFormDataEntry(de.symeda.sormas.app.campaign.CampaignFormDataFragmentUtils.getOrCreateCampaignFormDataEntry) TextView(android.widget.TextView) ImageView(android.widget.ImageView) LinearLayout(android.widget.LinearLayout)

Example 2 with CampaignFormElementType

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;
}
Also used : HashMap(java.util.HashMap) CampaignFormElement(de.symeda.sormas.api.campaign.form.CampaignFormElement) ControlPropertyField(de.symeda.sormas.app.component.controls.ControlPropertyField) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) CampaignFormMeta(de.symeda.sormas.app.backend.campaign.form.CampaignFormMeta) CampaignFormElementType(de.symeda.sormas.api.campaign.form.CampaignFormElementType) CampaignFormDataEntry(de.symeda.sormas.api.campaign.data.CampaignFormDataEntry) CampaignFormDataFragmentUtils.getOrCreateCampaignFormDataEntry(de.symeda.sormas.app.campaign.CampaignFormDataFragmentUtils.getOrCreateCampaignFormDataEntry) TextView(android.widget.TextView) ImageView(android.widget.ImageView) LinearLayout(android.widget.LinearLayout)

Example 3 with CampaignFormElementType

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;
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) HashMap(java.util.HashMap) CampaignFormElement(de.symeda.sormas.api.campaign.form.CampaignFormElement) ControlPropertyField(de.symeda.sormas.app.component.controls.ControlPropertyField) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) CampaignFormMeta(de.symeda.sormas.app.backend.campaign.form.CampaignFormMeta) CampaignFormElementType(de.symeda.sormas.api.campaign.form.CampaignFormElementType) CampaignFormDataEntry(de.symeda.sormas.api.campaign.data.CampaignFormDataEntry) TextView(android.widget.TextView) ImageView(android.widget.ImageView) LinearLayout(android.widget.LinearLayout)

Example 4 with CampaignFormElementType

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);
            }
        }
    }
}
Also used : CampaignFormElement(de.symeda.sormas.api.campaign.form.CampaignFormElement) Label(com.vaadin.v7.ui.Label) Field(com.vaadin.v7.ui.Field) TextField(com.vaadin.v7.ui.TextField) AbstractComponent(com.vaadin.ui.AbstractComponent) GridLayout(com.vaadin.ui.GridLayout) MarginInfo(com.vaadin.shared.ui.MarginInfo) CampaignFormElementStyle(de.symeda.sormas.api.campaign.form.CampaignFormElementStyle) CampaignFormElementType(de.symeda.sormas.api.campaign.form.CampaignFormElementType)

Aggregations

CampaignFormElement (de.symeda.sormas.api.campaign.form.CampaignFormElement)4 CampaignFormElementType (de.symeda.sormas.api.campaign.form.CampaignFormElementType)4 View (android.view.View)3 ImageView (android.widget.ImageView)3 LinearLayout (android.widget.LinearLayout)3 TextView (android.widget.TextView)3 CampaignFormDataEntry (de.symeda.sormas.api.campaign.data.CampaignFormDataEntry)3 CampaignFormMeta (de.symeda.sormas.app.backend.campaign.form.CampaignFormMeta)3 ControlPropertyField (de.symeda.sormas.app.component.controls.ControlPropertyField)3 HashMap (java.util.HashMap)3 CampaignFormDataFragmentUtils.getOrCreateCampaignFormDataEntry (de.symeda.sormas.app.campaign.CampaignFormDataFragmentUtils.getOrCreateCampaignFormDataEntry)2 MarginInfo (com.vaadin.shared.ui.MarginInfo)1 AbstractComponent (com.vaadin.ui.AbstractComponent)1 GridLayout (com.vaadin.ui.GridLayout)1 Field (com.vaadin.v7.ui.Field)1 Label (com.vaadin.v7.ui.Label)1 TextField (com.vaadin.v7.ui.TextField)1 CampaignFormElementStyle (de.symeda.sormas.api.campaign.form.CampaignFormElementStyle)1 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)1