Search in sources :

Example 1 with SelectField

use of com.manydesigns.elements.fields.SelectField in project Portofino by ManyDesigns.

the class ManyToManyAction method init.

@Override
public Object init() {
    preparePage();
    if (m2mConfiguration == null || m2mConfiguration.getActualRelationTable() == null || m2mConfiguration.getActualManyTable() == null) {
        logger.error("Configuration is null or relation/many table not found (check previous log messages)");
        // TODO WebApplicationException instead?
        return this;
    }
    Table table = m2mConfiguration.getActualRelationTable();
    relationTableAccessor = new TableAccessor(table);
    manyTableAccessor = new TableAccessor(m2mConfiguration.getActualManyTable());
    if (StringUtils.isBlank(m2mConfiguration.getActualOnePropertyName())) {
        logger.error("One property name not set");
        return this;
    }
    String expression = m2mConfiguration.getOneExpression();
    if (!StringUtils.isBlank(expression)) {
        // Set primary key
        OgnlContext ognlContext = ElementsThreadLocals.getOgnlContext();
        // TODO handle exception
        onePk = OgnlUtils.getValueQuietly(expression, ognlContext, this);
        correctlyConfigured = true;
    } else {
        assert !StringUtils.isBlank(m2mConfiguration.getActualOnePropertyName());
        // Setup "one" selection
        SelectionProviderReference oneSelectionProvider = m2mConfiguration.getOneSelectionProvider();
        if (oneSelectionProvider != null) {
            ModelSelectionProvider actualSelectionProvider = oneSelectionProvider.getActualSelectionProvider();
            if (!(actualSelectionProvider instanceof DatabaseSelectionProvider)) {
                logger.warn("Selection provider {} not supported", actualSelectionProvider);
                return this;
            }
            TableAccessor tableAccessor = new TableAccessor(m2mConfiguration.getActualRelationTable());
            PropertyAccessor onePkAccessor = null;
            try {
                onePkAccessor = tableAccessor.getProperty(m2mConfiguration.getActualOnePropertyName());
            } catch (NoSuchFieldException e) {
                throw new RuntimeException(e);
            }
            if (onePkAccessor == null) {
                logger.warn("Not a property: {}", m2mConfiguration.getActualOnePropertyName());
                return this;
            }
            String databaseName = m2mConfiguration.getActualOneDatabase().getDatabaseName();
            DatabaseSelectionProvider sp = (DatabaseSelectionProvider) actualSelectionProvider;
            DefaultSelectionProvider selectionProvider;
            String name = sp.getName();
            String hql = sp.getHql();
            if (StringUtils.isNotEmpty(hql)) {
                selectionProvider = createSelectionProviderFromHql(name, databaseName, hql, DisplayMode.DROPDOWN, SearchDisplayMode.DROPDOWN);
                if (sp instanceof ForeignKey) {
                    selectionProvider.sortByLabel();
                }
            } else {
                logger.warn("ModelSelection provider '{}': unsupported query", name);
                return this;
            }
            Object myInstance = tableAccessor.newInstance();
            oneSelectField = new SelectField(onePkAccessor, selectionProvider, Mode.EDIT, "__");
            oneSelectField.setRequired(false);
            oneSelectField.readFromRequest(context.getRequest());
            oneSelectField.writeToObject(myInstance);
            onePk = onePkAccessor.get(myInstance);
            correctlyConfigured = true;
        }
    }
    return this;
}
Also used : PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor) SelectField(com.manydesigns.elements.fields.SelectField) OgnlContext(ognl.OgnlContext) TableAccessor(com.manydesigns.portofino.reflection.TableAccessor) SelectionProviderReference(com.manydesigns.portofino.resourceactions.m2m.configuration.SelectionProviderReference) JSONObject(org.json.JSONObject)

Example 2 with SelectField

use of com.manydesigns.elements.fields.SelectField 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)

Example 3 with SelectField

use of com.manydesigns.elements.fields.SelectField in project Portofino by ManyDesigns.

the class TableFormBuilder method setupSelectionProvidersForRow.

protected void setupSelectionProvidersForRow(TableForm tableForm, TableForm.Row row, Map.Entry<String[], SelectionProvider> current) {
    String[] fieldNames = current.getKey();
    SelectionProvider selectionProvider = current.getValue();
    SelectionModel selectionModel = selectionProvider.createSelectionModel();
    SelectField previousField = null;
    for (int i = 0; i < fieldNames.length; i++) {
        int fieldIndex = findFieldIndexByName(tableForm, fieldNames[i]);
        SelectField selectField = (SelectField) row.get(fieldIndex);
        selectField.setSelectionModel(selectionModel);
        selectField.setSelectionModelIndex(i);
        if (previousField != null) {
            selectField.setPreviousSelectField(previousField);
            previousField.setNextSelectField(selectField);
        }
        previousField = selectField;
    }
}
Also used : SelectField(com.manydesigns.elements.fields.SelectField) SelectionModel(com.manydesigns.elements.options.SelectionModel) SelectionProvider(com.manydesigns.elements.options.SelectionProvider)

Example 4 with SelectField

use of com.manydesigns.elements.fields.SelectField in project Portofino by ManyDesigns.

the class ManyToManyAction method addSpRefSelectionProvider.

protected void addSpRefSelectionProvider(final List<ModelSelectionProvider> sps, Form form, String fieldName) throws NoSuchFieldException {
    SelectionProvider sp = SelectionProviderLogic.createSelectionProvider(fieldName, sps, ModelSelectionProvider.class, null, new String[] { "name" });
    JavaClassAccessor acc = JavaClassAccessor.getClassAccessor(ManyToManyConfiguration.class);
    form.get(0).add(new SelectField(acc.getProperty(fieldName), sp, Mode.EDIT, "") {

        @Override
        public void readFromRequest(HttpServletRequest req) {
            String stringValue = req.getParameter(inputName);
            if (!StringUtils.isEmpty(stringValue)) {
                for (ModelSelectionProvider msp : sps) {
                    if (msp.getName().equals(stringValue)) {
                        SelectionProviderReference ref = new SelectionProviderReference();
                        if (msp instanceof ForeignKey) {
                            ref.setForeignKeyName(msp.getName());
                        } else {
                            ref.setSelectionProviderName(msp.getName());
                        }
                        selectionModel.setValue(selectionModelIndex, ref);
                    }
                }
            } else {
                selectionModel.setValue(selectionModelIndex, null);
            }
        }
    });
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SelectField(com.manydesigns.elements.fields.SelectField) SelectionProviderReference(com.manydesigns.portofino.resourceactions.m2m.configuration.SelectionProviderReference) JavaClassAccessor(com.manydesigns.elements.reflection.JavaClassAccessor)

Aggregations

SelectField (com.manydesigns.elements.fields.SelectField)4 SelectionModel (com.manydesigns.elements.options.SelectionModel)2 SelectionProvider (com.manydesigns.elements.options.SelectionProvider)2 PropertyAccessor (com.manydesigns.elements.reflection.PropertyAccessor)2 SelectionProviderReference (com.manydesigns.portofino.resourceactions.m2m.configuration.SelectionProviderReference)2 Field (com.manydesigns.elements.fields.Field)1 JavaClassAccessor (com.manydesigns.elements.reflection.JavaClassAccessor)1 TableAccessor (com.manydesigns.portofino.reflection.TableAccessor)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 OgnlContext (ognl.OgnlContext)1 JSONObject (org.json.JSONObject)1