Search in sources :

Example 1 with SelectionProvider

use of com.manydesigns.elements.options.SelectionProvider in project Portofino by ManyDesigns.

the class TableFormBuilder method setupRows.

protected void setupRows(TableForm tableForm) {
    int index = 0;
    for (TableForm.Row row : tableForm.getRows()) {
        String rowPrefix = StringUtils.join(new Object[] { prefix, "row", index, "_" });
        for (PropertyAccessor propertyAccessor : propertyAccessors) {
            Field field = buildField(propertyAccessor, rowPrefix);
            if (field == null) {
                logger.warn("Cannot instantiate field for property {}", propertyAccessor);
            } else {
                row.add(field);
            }
        }
        // handle cascaded select fields
        for (Map.Entry<String[], SelectionProvider> current : selectionProviders.entrySet()) {
            setupSelectionProvidersForRow(tableForm, row, current);
        }
        for (Map.Entry<String[], SelectionProvider> current : rowSelectionProviders.get(index).entrySet()) {
            setupSelectionProvidersForRow(tableForm, row, current);
        }
        index++;
    }
}
Also used : Field(com.manydesigns.elements.fields.Field) SelectField(com.manydesigns.elements.fields.SelectField) PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor) SelectionProvider(com.manydesigns.elements.options.SelectionProvider)

Example 2 with SelectionProvider

use of com.manydesigns.elements.options.SelectionProvider in project Portofino by ManyDesigns.

the class AbstractCrudAction method configureSearchFormBuilder.

protected SearchFormBuilder configureSearchFormBuilder(SearchFormBuilder searchFormBuilder) {
    // setup option providers
    if (selectionProviderSupport != null) {
        for (CrudSelectionProvider current : selectionProviderSupport.getCrudSelectionProviders()) {
            SelectionProvider selectionProvider = current.getSelectionProvider();
            if (selectionProvider == null || !current.isEnforced()) {
                continue;
            }
            String[] fieldNames = current.getFieldNames();
            searchFormBuilder.configSelectionProvider(selectionProvider, fieldNames);
        }
    }
    return searchFormBuilder.configPrefix(searchPrefix);
}
Also used : SelectionProvider(com.manydesigns.elements.options.SelectionProvider)

Example 3 with SelectionProvider

use of com.manydesigns.elements.options.SelectionProvider in project Portofino by ManyDesigns.

the class AbstractCrudAction method jsonOptions.

/**
 * Returns values to update multiple related select fields or a single autocomplete text field, in JSON form.
 * @param selectionProviderName name of the selection provider. See {@link #selectionProviders()}.
 * @param selectionProviderIndex index of the selection field (in case of multiple-valued selection providers,
 *                               otherwise it is always 0 and you can use
 *                               {@link #jsonOptions(String, String, String, boolean)}).
 * @param labelSearch for autocomplete fields, the text entered by the user.
 * @param prefix form prefix, to read values from the request.
 * @param includeSelectPrompt controls if the first option is a label with no value indicating
 * what field is being selected. For combo boxes you would generally pass true as the value of
 * this parameter; for autocomplete fields, you would likely pass false.
 * @return a Response with the JSON.
 */
@GET
@Path(":selectionProvider/{selectionProviderName}/{selectionProviderIndex : (\\d+)}")
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "The values a given selection provider can assume")
public Response jsonOptions(@Parameter(description = "The name of the selection provider") @PathParam("selectionProviderName") String selectionProviderName, @Parameter(description = "For multi-field selection providers, the index of the field") @PathParam("selectionProviderIndex") int selectionProviderIndex, @Parameter(description = "The searched substring (for autocomplete search)") @QueryParam("labelSearch") String labelSearch, @Parameter(description = "The form prefix (advanced and generally not used)") @QueryParam("prefix") String prefix, @Parameter(description = "Whether the returned values include a default option \"Please choose one\"") @QueryParam("includeSelectPrompt") boolean includeSelectPrompt) {
    checkAccessorPermissions(new String[] { PERMISSION_READ });
    CrudSelectionProvider crudSelectionProvider = null;
    for (CrudSelectionProvider current : selectionProviderSupport.getCrudSelectionProviders()) {
        SelectionProvider selectionProvider = current.getSelectionProvider();
        if (selectionProvider.getName().equals(selectionProviderName)) {
            crudSelectionProvider = current;
            break;
        }
    }
    if (crudSelectionProvider == null) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    SelectionProvider selectionProvider = crudSelectionProvider.getSelectionProvider();
    String[] fieldNames = crudSelectionProvider.getFieldNames();
    Form form = buildForm(createFormBuilder().configFields(fieldNames).configSelectionProvider(selectionProvider, fieldNames).configPrefix(prefix).configMode(Mode.EDIT));
    // Guaranteed to be the only one per the code above
    FieldSet fieldSet = form.get(0);
    // Ensure the value is actually read from the request
    for (Field field : fieldSet.fields()) {
        field.setUpdatable(true);
    }
    form.readFromRequest(context.getRequest());
    // The form only contains fields from the selection provider, so the index matches that of the field
    if (selectionProviderIndex < 0 || selectionProviderIndex >= fieldSet.size()) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Invalid index").build();
    }
    SelectField targetField = (SelectField) fieldSet.get(selectionProviderIndex);
    targetField.setLabelSearch(labelSearch);
    String text = targetField.jsonSelectFieldOptions(includeSelectPrompt);
    logger.debug("jsonOptions: {}", text);
    return Response.ok(text, MimeTypes.APPLICATION_JSON_UTF8).build();
}
Also used : SelectionProvider(com.manydesigns.elements.options.SelectionProvider) Operation(io.swagger.v3.oas.annotations.Operation)

Example 4 with SelectionProvider

use of com.manydesigns.elements.options.SelectionProvider in project Portofino by ManyDesigns.

the class AbstractCrudAction method configureFormSelectionProviders.

protected void configureFormSelectionProviders(FormBuilder formBuilder) {
    if (selectionProviderSupport == null || selectionProviderLoadStrategy == SelectionProviderLoadStrategy.NONE) {
        return;
    }
    // setup option providers
    for (CrudSelectionProvider current : selectionProviderSupport.getCrudSelectionProviders()) {
        SelectionProvider selectionProvider = current.getSelectionProvider();
        if (selectionProvider == null || (selectionProviderLoadStrategy == SelectionProviderLoadStrategy.ONLY_ENFORCED && !current.isEnforced())) {
            continue;
        }
        String[] fieldNames = current.getFieldNames();
        if (object != null) {
            Object[] values = new Object[fieldNames.length];
            boolean valuesRead = true;
            for (int i = 0; i < fieldNames.length; i++) {
                String fieldName = fieldNames[i];
                try {
                    PropertyAccessor propertyAccessor = classAccessor.getProperty(fieldName);
                    values[i] = propertyAccessor.get(object);
                } catch (Exception e) {
                    logger.error("Couldn't read property " + fieldName, e);
                    valuesRead = false;
                }
            }
            if (valuesRead) {
                selectionProvider.ensureActive(values);
            }
        }
        formBuilder.configSelectionProvider(selectionProvider, fieldNames);
    }
}
Also used : PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor) JSONObject(org.json.JSONObject) SelectionProvider(com.manydesigns.elements.options.SelectionProvider) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Example 5 with SelectionProvider

use of com.manydesigns.elements.options.SelectionProvider in project Portofino by ManyDesigns.

the class FormBuilder method build.

// **************************************************************************
// Building
// **************************************************************************
public Form build() {
    logger.debug("build");
    Form form = new Form(mode);
    if (groupedPropertyAccessors == null) {
        configReflectiveFields();
    }
    // remove unused (or partially used) selection providers
    List<PropertyAccessor> allPropertyAccessors = new ArrayList<PropertyAccessor>();
    for (ArrayList<PropertyAccessor> group : groupedPropertyAccessors) {
        allPropertyAccessors.addAll(group);
    }
    removeUnusedSelectionProviders(allPropertyAccessors);
    // create the form/fieldset/field sructure
    Map<String, Field> fieldMap = new HashMap<String, Field>();
    for (int i = 0; i < groupedPropertyAccessors.size(); i++) {
        buildFieldGroup(form, i, fieldMap);
    }
    // handle cascaded select fields
    for (Map.Entry<String[], SelectionProvider> current : selectionProviders.entrySet()) {
        String[] fieldNames = current.getKey();
        SelectionProvider selectionProvider = current.getValue();
        SelectionModel selectionModel = selectionProvider.createSelectionModel();
        SelectField previousField = null;
        for (int i = 0; i < fieldNames.length; i++) {
            SelectField selectField = (SelectField) fieldMap.get(fieldNames[i]);
            selectField.setSelectionModel(selectionModel);
            selectField.setSelectionModelIndex(i);
            if (previousField != null) {
                selectField.setPreviousSelectField(previousField);
                previousField.setNextSelectField(selectField);
            }
            previousField = selectField;
        }
    }
    return form;
}
Also used : PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor) SelectField(com.manydesigns.elements.fields.SelectField) Field(com.manydesigns.elements.fields.Field) SelectField(com.manydesigns.elements.fields.SelectField) SelectionModel(com.manydesigns.elements.options.SelectionModel) SelectionProvider(com.manydesigns.elements.options.SelectionProvider)

Aggregations

SelectionProvider (com.manydesigns.elements.options.SelectionProvider)12 SelectField (com.manydesigns.elements.fields.SelectField)5 Field (com.manydesigns.elements.fields.Field)4 PropertyAccessor (com.manydesigns.elements.reflection.PropertyAccessor)4 SelectionModel (com.manydesigns.elements.options.SelectionModel)3 Operation (io.swagger.v3.oas.annotations.Operation)3 FieldsManager (com.manydesigns.elements.fields.helpers.FieldsManager)1 SearchField (com.manydesigns.elements.fields.search.SearchField)1 SelectSearchField (com.manydesigns.elements.fields.search.SelectSearchField)1 DisplayMode (com.manydesigns.elements.options.DisplayMode)1 SearchDisplayMode (com.manydesigns.elements.options.SearchDisplayMode)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1