Search in sources :

Example 21 with IDataProvider

use of com.servoy.j2db.persistence.IDataProvider in project servoy-client by Servoy.

the class FormatTypeSabloValue method getSabloValue.

protected ComponentFormat getSabloValue(String formatValue, String dataproviderId, Object valuelistId, String foundsetId, IWebObjectContext webObjectCntxt) {
    INGApplication application = ((WebFormComponent) webObjectCntxt.getUnderlyingWebObject()).getDataConverterContext().getApplication();
    IDataProviderLookup dataProviderLookup = null;
    // IMPORTANT: here we use the for: configs in .spec file
    // 
    // if you have for: [valuelist, dataprovider] then 2 things can happen:
    // - valuelist if it has both real and display values - forces the type; it is either TEXT (custom vl., global method vl.) or the 'display' column type in case it's a DB valuelist
    // - valuelist if not real/display but only one kind of values: here it is required in docs in the spec file that the valuelist property also defines "for": dataprovider if format
    // defines both "for" valuelist and dataprovider => valuelist doesn't force the type and then the dataprovider will decide the type
    // 
    // if you have just for: dataprovider the the dataprovider property determines the type
    // if you have just for: valuelist (TODO) - this is currently not properly supported - as here we should get the type always from the VL (for both display and real values) - as we don't have a dataprovider to fall back on
    isValuelistFormatSet = false;
    if (valuelistId != null) {
        // if we have a "for" valuelist, see if this valuelist forces the format type due to display values (when they are separate from real values)
        // otherwise it will do nothing and loop/fallback to the other if clause below which checks the "for" dataprovider
        ValueList valuelistPersist = ValueListTypeSabloValue.getValuelistPersist(valuelistId, application);
        if (valuelistPersist != null) {
            IDataProvider dataProvider = null;
            ITable table;
            try {
                if (valuelistPersist.getRelationName() != null) {
                    Relation[] relations = application.getFlattenedSolution().getRelationSequence(valuelistPersist.getRelationName());
                    table = application.getFlattenedSolution().getTable(relations[relations.length - 1].getForeignDataSource());
                } else {
                    table = application.getFlattenedSolution().getTable(valuelistPersist.getDataSource());
                }
                if (table != null) {
                    // if the format is for a table valuelist - the type to be used is the one of the dp chosen as 'display' in the valuelist
                    String dp = null;
                    // if show == real then we can use show anyway cause there is only one value for both real and display; if show != real we care about show
                    int showDataProviders = valuelistPersist.getShowDataProviders();
                    if ((showDataProviders & 1) != 0) {
                        dp = valuelistPersist.getDataProviderID1();
                    }
                    if ((showDataProviders & 2) != 0) {
                        // display value is a concat of multiple columns, so a string; not even sure if format property makes sense, but it is for a String then
                        if (dp != null)
                            return ComponentFormat.getComponentFormat(formatValue, IColumnTypes.TEXT, application);
                        dp = valuelistPersist.getDataProviderID2();
                    }
                    if ((showDataProviders & 4) != 0) {
                        // display value is a concat of multiple columns, so a string; not even sure if format property makes sense, but it is for a String then
                        if (dp != null)
                            return ComponentFormat.getComponentFormat(formatValue, IColumnTypes.TEXT, application);
                        dp = valuelistPersist.getDataProviderID3();
                    }
                    if (dp != null) {
                        dataProvider = application.getFlattenedSolution().getDataProviderForTable(table, dp);
                    }
                    isValuelistFormatSet = true;
                    return ComponentFormat.getComponentFormat(formatValue, dataProvider, application, true);
                } else if (valuelistPersist.getValueListType() == IValueListConstants.CUSTOM_VALUES) {
                    IValueList realValuelist = null;
                    ValueListTypeSabloValue valuelistSabloValue = (ValueListTypeSabloValue) FoundsetLinkedTypeSabloValue.unwrapIfNeeded(webObjectContext.getProperty(propertyDependencies.valueListPropertyName));
                    if (valuelistSabloValue != null) {
                        // take it from property, may not be the shared instance in case setvaluelistitems on component was used
                        realValuelist = valuelistSabloValue.getValueList();
                    }
                    if (realValuelist == null) {
                        realValuelist = com.servoy.j2db.component.ComponentFactory.getRealValueList(application, valuelistPersist, true, Types.OTHER, ComponentFormat.getComponentFormat(formatValue, dataproviderId, null, application, true).parsedFormat, null, true);
                    }
                    if (realValuelist.hasRealValues()) {
                        // if custom vl has both real and display values, the display values are TEXT (format is for those)
                        // of if it has displayValueType set, use that
                        isValuelistFormatSet = true;
                        int realValueDisplayType = realValuelist.getValueList().getDisplayValueType();
                        return ComponentFormat.getComponentFormat(formatValue, realValueDisplayType != 0 ? realValueDisplayType : IColumnTypes.TEXT, application);
                    }
                } else if (valuelistPersist.getValueListType() == IValueListConstants.GLOBAL_METHOD_VALUES) {
                    PropertyDescription vlPD = webObjectCntxt.getPropertyDescription(propertyDependencies.valueListPropertyName);
                    Object vlPDConfig = null;
                    if (vlPD != null) {
                        vlPDConfig = vlPD.getConfig();
                        if (vlPDConfig instanceof FoundsetLinkedConfig)
                            vlPDConfig = ((FoundsetLinkedConfig) vlPDConfig).getWrappedConfig();
                    }
                    boolean lazyLoad = valuelistPersist.getLazyLoading() && vlPDConfig instanceof ValueListConfig && ((ValueListConfig) vlPDConfig).getLazyLoading();
                    if (!lazyLoad) {
                        IValueList realValuelist = com.servoy.j2db.component.ComponentFactory.getRealValueList(application, valuelistPersist, true, Types.OTHER, null, null, true);
                        if (realValuelist instanceof GlobalMethodValueList) {
                            ((GlobalMethodValueList) realValuelist).fill(null, "", null);
                            if (realValuelist.hasRealValues() || realValuelist.getSize() == 0 || (realValuelist.getSize() == 1 && valuelistPersist.getAddEmptyValue() == IValueListConstants.EMPTY_VALUE_ALWAYS)) {
                                // if global method vl has both real and display values, the display values are TEXT (format is for those)
                                // of if it has displayValueType set, use that
                                isValuelistFormatSet = true;
                                int realValueDisplayType = realValuelist.getValueList().getDisplayValueType();
                                return ComponentFormat.getComponentFormat(formatValue, realValueDisplayType != 0 ? realValueDisplayType : IColumnTypes.TEXT, application);
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                Debug.error(ex);
            }
        }
    // here - we want to fall back to the dataprovider if available in for: [ ..., dataprovider] if valuelist didn't force a certain display type on the format
    }
    if (dataproviderId != null && foundsetId != null) {
        ITable table = null;
        Form form = ((IContextProvider) webObjectCntxt.getUnderlyingWebObject()).getDataConverterContext().getForm().getForm();
        // always assume now that the the properties has the foundset property name.
        FoundsetTypeSabloValue runtimeValOfFoundset = (FoundsetTypeSabloValue) webObjectCntxt.getUnderlyingWebObject().getProperty(this.propertyDependencies.foundsetPropertyName);
        if (runtimeValOfFoundset != null && runtimeValOfFoundset.getFoundset() != null && runtimeValOfFoundset.getFoundset().getDataSource().equals(foundsetId)) {
            table = runtimeValOfFoundset.getFoundset().getTable();
        }
        if (table == null)
            table = FoundsetTypeSabloValue.getTableBasedOfFoundsetPropertyFromFoundsetIdentifier(foundsetId, application, form);
        if (table != null) {
            dataProviderLookup = new FormAndTableDataProviderLookup(application.getFlattenedSolution(), form, table);
        }
    // else it will be searched for in form's context and table as below
    }
    if (dataProviderLookup == null) {
        WebObjectSpecification spec = ((WebFormComponent) webObjectCntxt.getUnderlyingWebObject()).getParent().getSpecification();
        if (spec != null) {
            Collection<PropertyDescription> formComponentProperties = spec.getProperties(FormComponentPropertyType.INSTANCE);
            if (formComponentProperties != null) {
                for (PropertyDescription property : formComponentProperties) {
                    if (property.getConfig() instanceof ComponentTypeConfig && ((ComponentTypeConfig) property.getConfig()).forFoundset != null) {
                        FoundsetTypeSabloValue runtimeValOfFoundset = (FoundsetTypeSabloValue) ((WebFormComponent) webObjectCntxt.getUnderlyingWebObject()).getParent().getProperty(((ComponentTypeConfig) property.getConfig()).forFoundset);
                        ITable table = null;
                        Form form = ((IContextProvider) webObjectCntxt.getUnderlyingWebObject()).getDataConverterContext().getForm().getForm();
                        if (runtimeValOfFoundset.getFoundset() != null)
                            table = runtimeValOfFoundset.getFoundset().getTable();
                        if (table == null)
                            table = FoundsetTypeSabloValue.getTableBasedOfFoundsetPropertyFromFoundsetIdentifier(runtimeValOfFoundset.getFoundsetSelector(), application, form);
                        if (table != null) {
                            dataProviderLookup = new FormAndTableDataProviderLookup(application.getFlattenedSolution(), form, table);
                        }
                        break;
                    }
                }
            }
        }
    }
    if (dataProviderLookup == null && application != null)
        dataProviderLookup = application.getFlattenedSolution().getDataproviderLookup(application.getFoundSetManager(), ((IContextProvider) webObjectCntxt.getUnderlyingWebObject()).getDataConverterContext().getForm().getForm());
    ComponentFormat format = ComponentFormat.getComponentFormat(formatValue, dataproviderId, dataProviderLookup, application, true);
    return format;
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) ValueList(com.servoy.j2db.persistence.ValueList) GlobalMethodValueList(com.servoy.j2db.dataprocessing.GlobalMethodValueList) IValueList(com.servoy.j2db.dataprocessing.IValueList) Form(com.servoy.j2db.persistence.Form) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) FoundsetLinkedConfig(com.servoy.j2db.server.ngclient.property.FoundsetLinkedConfig) GlobalMethodValueList(com.servoy.j2db.dataprocessing.GlobalMethodValueList) IDataProvider(com.servoy.j2db.persistence.IDataProvider) Relation(com.servoy.j2db.persistence.Relation) INGApplication(com.servoy.j2db.server.ngclient.INGApplication) IContextProvider(com.servoy.j2db.server.ngclient.IContextProvider) FormAndTableDataProviderLookup(com.servoy.j2db.FormAndTableDataProviderLookup) ITable(com.servoy.j2db.persistence.ITable) ComponentTypeConfig(com.servoy.j2db.server.ngclient.property.ComponentTypeConfig) IValueList(com.servoy.j2db.dataprocessing.IValueList) ComponentFormat(com.servoy.j2db.component.ComponentFormat) ValueListConfig(com.servoy.j2db.server.ngclient.property.ValueListConfig) PropertyDescription(org.sablo.specification.PropertyDescription) FoundsetTypeSabloValue(com.servoy.j2db.server.ngclient.property.FoundsetTypeSabloValue) IDataProviderLookup(com.servoy.j2db.persistence.IDataProviderLookup)

Example 22 with IDataProvider

use of com.servoy.j2db.persistence.IDataProvider in project servoy-client by Servoy.

the class ComponentFactory method createField.

private static IComponent createField(IApplication application, Form form, Field field, IDataProviderLookup dataProviderLookup, IScriptExecuter el, boolean printing) {
    ValueList valuelist = application.getFlattenedSolution().getValueList(field.getValuelistID());
    ComponentFormat fieldFormat = ComponentFormat.getComponentFormat(field.getFormat(), field.getDataProviderID(), dataProviderLookup, application);
    IDataProvider dp = null;
    if (field.getDataProviderID() != null && dataProviderLookup != null) {
        try {
            dp = dataProviderLookup.getDataProvider(field.getDataProviderID());
        } catch (RepositoryException e) {
            Debug.error(e);
        }
    }
    // apply any style
    Insets style_margin = null;
    int style_halign = -1;
    boolean hasBorder = false;
    Pair<IStyleSheet, IStyleRule> styleInfo = getStyleForBasicComponent(application, field, form);
    if (styleInfo != null) {
        IStyleSheet ss = styleInfo.getLeft();
        IStyleRule s = styleInfo.getRight();
        if (ss != null && s != null) {
            style_margin = ss.getMargin(s);
            style_halign = ss.getHAlign(s);
            hasBorder = ss.hasBorder(s);
        }
    }
    IStylePropertyChangesRecorder jsChangeRecorder = application.getItemFactory().createChangesRecorder();
    IFieldComponent fl;
    AbstractRuntimeField<? extends IFieldComponent> scriptable;
    switch(field.getDisplayType()) {
        case Field.PASSWORD:
            {
                RuntimeDataPassword so;
                scriptable = so = new RuntimeDataPassword(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataPassword(so, getWebID(form, field));
                so.setComponent(fl, field);
            }
            break;
        case Field.RTF_AREA:
            {
                RuntimeRtfArea so;
                scriptable = so = new RuntimeRtfArea(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataTextEditor(so, getWebID(form, field), RTF_AREA, field.getEditable());
                so.setComponent(fl, field);
                if (fl instanceof IScrollPane) {
                    applyScrollBarsProperty((IScrollPane) fl, field);
                }
            }
            break;
        case Field.HTML_AREA:
            {
                RuntimeHTMLArea so;
                scriptable = so = new RuntimeHTMLArea(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataTextEditor(so, getWebID(form, field), HTML_AREA, field.getEditable());
                so.setComponent(fl, field);
                if (fl instanceof IScrollPane) {
                    applyScrollBarsProperty((IScrollPane) fl, field);
                }
            }
            break;
        case Field.TEXT_AREA:
            {
                RuntimeTextArea so;
                scriptable = so = new RuntimeTextArea(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataTextArea(so, getWebID(form, field));
                so.setComponent(fl, field);
                if (fl instanceof IScrollPane) {
                    applyScrollBarsProperty((IScrollPane) fl, field);
                }
            }
            break;
        case Field.CHECKS:
            {
                AbstractRuntimeValuelistComponent<IFieldComponent> so;
                if (valuelist != null) {
                    IValueList list = getRealValueList(application, valuelist, true, fieldFormat.dpType, fieldFormat.parsedFormat, field.getDataProviderID());
                    if (isSingleValue(valuelist)) {
                        scriptable = so = new RuntimeCheckbox(jsChangeRecorder, application);
                        fl = application.getItemFactory().createCheckBox((RuntimeCheckbox) so, getWebID(form, field), application.getI18NMessageIfPrefixed(field.getText()), list);
                    } else {
                        scriptable = so = new RuntimeCheckBoxChoice(jsChangeRecorder, application);
                        fl = application.getItemFactory().createDataChoice((RuntimeCheckBoxChoice) so, getWebID(form, field), list, false, fieldFormat == null || fieldFormat.dpType == IColumnTypes.TEXT);
                        if (fl instanceof IScrollPane) {
                            applyScrollBarsProperty((IScrollPane) fl, field);
                        }
                    }
                } else {
                    scriptable = so = new RuntimeCheckbox(jsChangeRecorder, application);
                    fl = application.getItemFactory().createCheckBox((RuntimeCheckbox) so, getWebID(form, field), application.getI18NMessageIfPrefixed(field.getText()), null);
                }
                so.setComponent(fl, field);
            }
            break;
        case Field.RADIOS:
            {
                AbstractRuntimeValuelistComponent<IFieldComponent> so;
                IValueList list = getRealValueList(application, valuelist, true, fieldFormat.dpType, fieldFormat.parsedFormat, field.getDataProviderID());
                if (isSingleValue(valuelist)) {
                    scriptable = so = new RuntimeRadioButton(jsChangeRecorder, application);
                    fl = application.getItemFactory().createRadioButton((RuntimeRadioButton) so, getWebID(form, field), application.getI18NMessageIfPrefixed(field.getText()), list);
                } else {
                    scriptable = so = new RuntimeRadioChoice(jsChangeRecorder, application);
                    fl = application.getItemFactory().createDataChoice((RuntimeRadioChoice) so, getWebID(form, field), list, true, false);
                    if (fl instanceof IScrollPane) {
                        applyScrollBarsProperty((IScrollPane) fl, field);
                    }
                }
                so.setComponent(fl, field);
            }
            break;
        case Field.COMBOBOX:
            {
                RuntimeDataCombobox so;
                scriptable = so = new RuntimeDataCombobox(jsChangeRecorder, application);
                IValueList list = getRealValueList(application, valuelist, true, fieldFormat.dpType, fieldFormat.parsedFormat, field.getDataProviderID());
                fl = application.getItemFactory().createDataComboBox(so, getWebID(form, field), list);
                so.setComponent(fl, field);
            }
            break;
        case Field.CALENDAR:
            {
                RuntimeDataCalendar so;
                scriptable = so = new RuntimeDataCalendar(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataCalendar(so, getWebID(form, field));
                so.setComponent(fl, field);
            }
            break;
        case Field.IMAGE_MEDIA:
            {
                RuntimeMediaField so;
                scriptable = so = new RuntimeMediaField(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataImgMediaField(so, getWebID(form, field));
                if (fl instanceof IScrollPane) {
                    applyScrollBarsProperty((IScrollPane) fl, field);
                }
                so.setComponent(fl, field);
            }
            break;
        case Field.TYPE_AHEAD:
            if (field.getValuelistID() > 0) {
                fl = createTypeAheadWithValueList(application, form, field, dataProviderLookup, fieldFormat.uiType, fieldFormat.parsedFormat, jsChangeRecorder);
                if (fl == null)
                    return null;
                scriptable = (AbstractRuntimeField<? extends IFieldComponent>) fl.getScriptObject();
                break;
            }
            if (// only allow plain columns
            dp != null && dp.getColumnWrapper() != null && dp.getColumnWrapper().getRelations() == null) {
                RuntimeDataLookupField so;
                scriptable = so = new RuntimeDataLookupField(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataLookupField(so, getWebID(form, field), form.getServerName(), form.getTableName(), dp == null ? field.getDataProviderID() : dp.getDataProviderID());
                so.setComponent(fl, field);
                break;
            } else {
                RuntimeDataField so;
                scriptable = so = new RuntimeDataField(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataField(so, getWebID(form, field));
                so.setComponent(fl, field);
                break;
            }
        // $FALL-THROUGH$
        case Field.LIST_BOX:
        case Field.MULTISELECT_LISTBOX:
            {
                boolean multiSelect = (field.getDisplayType() == Field.MULTISELECT_LISTBOX);
                RuntimeListBox so;
                scriptable = so = new RuntimeListBox(jsChangeRecorder, application, multiSelect);
                IValueList list = getRealValueList(application, valuelist, true, fieldFormat.dpType, fieldFormat.parsedFormat, field.getDataProviderID());
                fl = application.getItemFactory().createListBox(so, getWebID(form, field), list, multiSelect);
                so.setComponent(fl, field);
            }
            break;
        case Field.SPINNER:
            {
                RuntimeSpinner so;
                scriptable = so = new RuntimeSpinner(jsChangeRecorder, application);
                IValueList list = getRealValueList(application, valuelist, true, fieldFormat.dpType, fieldFormat.parsedFormat, field.getDataProviderID());
                fl = application.getItemFactory().createSpinner(so, getWebID(form, field), list);
                so.setComponent(fl, field);
                break;
            }
        // else treat as the default case: TEXT_FIELD
        default:
            // Field.TEXT_FIELD
            if (field.getValuelistID() > 0) {
                fl = createTypeAheadWithValueList(application, form, field, dataProviderLookup, fieldFormat.uiType, fieldFormat.parsedFormat, jsChangeRecorder);
                if (fl == null)
                    return null;
                scriptable = (AbstractRuntimeField<? extends IFieldComponent>) fl.getScriptObject();
            } else {
                RuntimeDataField so;
                scriptable = so = new RuntimeDataField(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataField(so, getWebID(form, field));
                so.setComponent(fl, field);
            }
    }
    if (fl instanceof ISupportAsyncLoading) {
        ((ISupportAsyncLoading) fl).setAsyncLoadingEnabled(!printing);
    }
    fl.setSelectOnEnter(field.getSelectOnEnter());
    fl.setEditable(field.getEditable());
    try {
        int halign = field.getHorizontalAlignment();
        if (halign != -1) {
            fl.setHorizontalAlignment(halign);
        } else if (style_halign != -1) {
            fl.setHorizontalAlignment(style_halign);
        }
    } catch (RuntimeException e) {
    // just ignore...Debug.error(e);
    }
    fl.setToolTipText(application.getI18NMessageIfPrefixed(field.getToolTipText()));
    fl.setTitleText(application.getI18NMessageIfPrefixed(field.getText()));
    fl.setDataProviderID(dp == null ? field.getDataProviderID() : dp.getDataProviderID());
    if (field.getDataProviderID() != null && dataProviderLookup != null) {
        if (scriptable instanceof IFormatScriptComponent) {
            ((IFormatScriptComponent) scriptable).setComponentFormat(fieldFormat);
        }
        if (dp != null) {
            // if (valuelist != null && valuelist.getValueListType() != ValueList.CUSTOM_VALUES) type = valuelist.getDisplayValueType();
            int l = dp.getLength();
            int defaultType = Column.mapToDefaultType(fieldFormat.dpType);
            boolean skipMaxLength = false;
            if (valuelist != null) {
                // if the display values are different than the real values, then maxlength should be skipped
                IValueList vl = getRealValueList(application, valuelist, true, fieldFormat.dpType, fieldFormat.parsedFormat, dp.getDataProviderID());
                skipMaxLength = vl.hasRealValues();
            }
            if (l > 0 && (defaultType == IColumnTypes.TEXT || defaultType == IColumnTypes.MEDIA) && !skipMaxLength) {
                fl.setMaxLength(l);
            }
        }
    }
    // fl.setOpaque(!field.getTransparent());
    if (field.getDisplaysTags()) {
        fl.setNeedEntireState(true);
        if (field.getDataProviderID() == null && field.getText() != null && fl instanceof IDisplayTagText) {
            ((IDisplayTagText) fl).setTagText(field.getText());
        }
    }
    if (// el is an ActionListener
    el != null) {
        fl.addScriptExecuter(el);
        Object[] cmds = combineMethodsToCommands(form, form.getOnElementFocusGainedMethodID(), "onElementFocusGainedMethodID", field, field.getOnFocusGainedMethodID(), "onFocusGainedMethodID");
        if (cmds != null)
            fl.setEnterCmds((String[]) cmds[0], (Object[][]) cmds[1]);
        cmds = combineMethodsToCommands(form, form.getOnElementFocusLostMethodID(), "onElementFocusLostMethodID", field, field.getOnFocusLostMethodID(), "onFocusLostMethodID");
        if (cmds != null)
            fl.setLeaveCmds((String[]) cmds[0], (Object[][]) cmds[1]);
        if (field.getOnActionMethodID() > 0)
            fl.setActionCmd(Integer.toString(field.getOnActionMethodID()), Utils.parseJSExpressions(field.getFlattenedMethodArguments("onActionMethodID")));
        if (field.getOnDataChangeMethodID() > 0)
            fl.setChangeCmd(Integer.toString(field.getOnDataChangeMethodID()), Utils.parseJSExpressions(field.getFlattenedMethodArguments("onDataChangeMethodID")));
        if (field.getOnRightClickMethodID() > 0)
            fl.setRightClickCommand(Integer.toString(field.getOnRightClickMethodID()), Utils.parseJSExpressions(field.getFlattenedMethodArguments("onRightClickMethodID")));
    }
    int onRenderMethodID = field.getOnRenderMethodID();
    AbstractBase onRenderPersist = field;
    if (onRenderMethodID <= 0) {
        onRenderMethodID = form.getOnRenderMethodID();
        onRenderPersist = form;
    }
    if (onRenderMethodID > 0) {
        RenderEventExecutor renderEventExecutor = scriptable.getRenderEventExecutor();
        renderEventExecutor.setRenderCallback(Integer.toString(onRenderMethodID), Utils.parseJSExpressions(onRenderPersist.getFlattenedMethodArguments("onRenderMethodID")));
        IForm rendererForm = application.getFormManager().getForm(form.getName());
        IScriptExecuter rendererScriptExecuter = rendererForm instanceof FormController ? ((FormController) rendererForm).getScriptExecuter() : null;
        renderEventExecutor.setRenderScriptExecuter(rendererScriptExecuter);
    }
    applyBasicComponentProperties(application, fl, field, styleInfo);
    if (fl instanceof INullableAware) {
        INullableAware nullAware = (INullableAware) fl;
        boolean allowNull = true;
        // become 0 (because it is unchecked) so that the user does not need to check/uncheck it for save
        try {
            if (dataProviderLookup != null && dataProviderLookup.getTable() != null && field.getDataProviderID() != null) {
                String dataproviderId = dp == null ? field.getDataProviderID() : dp.getDataProviderID();
                if (dataProviderLookup.getTable().getColumn(dataproviderId) != null) {
                    allowNull = dataProviderLookup.getTable().getColumn(dataproviderId).getAllowNull();
                }
            }
        } catch (RepositoryException e) {
        // maybe this field is not linked to a table column... so leave it true
        }
        nullAware.setAllowNull(allowNull);
    }
    Insets m = field.getMargin();
    if (m == null)
        m = style_margin;
    if (m != null) {
        fl.setMargin(m);
        if (fl instanceof IMarginAwareBorder) {
            if (field.getBorderType() != null || hasBorder) {
                Border b = fl.getBorder();
                if (b != null) {
                    fl.setBorder(BorderFactory.createCompoundBorder(b, BorderFactory.createEmptyBorder(m.top, m.left, m.bottom, m.right)));
                }
            }
        }
    }
    if (fl.getScriptObject() instanceof HasRuntimePlaceholder) {
        ((HasRuntimePlaceholder) fl.getScriptObject()).setPlaceholderText(field.getPlaceholderText());
    }
    return fl;
}
Also used : RuntimeDataCombobox(com.servoy.j2db.ui.scripting.RuntimeDataCombobox) Insets(java.awt.Insets) IScrollPane(com.servoy.j2db.ui.IScrollPane) ValueList(com.servoy.j2db.persistence.ValueList) LookupValueList(com.servoy.j2db.dataprocessing.LookupValueList) CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) IValueList(com.servoy.j2db.dataprocessing.IValueList) RuntimeHTMLArea(com.servoy.j2db.ui.scripting.RuntimeHTMLArea) IForm(com.servoy.j2db.IForm) IDataProvider(com.servoy.j2db.persistence.IDataProvider) RuntimeMediaField(com.servoy.j2db.ui.scripting.RuntimeMediaField) RuntimeRtfArea(com.servoy.j2db.ui.scripting.RuntimeRtfArea) HasRuntimePlaceholder(com.servoy.j2db.ui.runtime.HasRuntimePlaceholder) RuntimeDataPassword(com.servoy.j2db.ui.scripting.RuntimeDataPassword) RuntimeDataCalendar(com.servoy.j2db.ui.scripting.RuntimeDataCalendar) IValueList(com.servoy.j2db.dataprocessing.IValueList) RuntimeDataField(com.servoy.j2db.ui.scripting.RuntimeDataField) FormController(com.servoy.j2db.FormController) RuntimeRadioButton(com.servoy.j2db.ui.scripting.RuntimeRadioButton) RuntimeDataLookupField(com.servoy.j2db.ui.scripting.RuntimeDataLookupField) IStyleSheet(com.servoy.j2db.util.IStyleSheet) RuntimeCheckbox(com.servoy.j2db.ui.scripting.RuntimeCheckbox) IScriptExecuter(com.servoy.j2db.IScriptExecuter) AbstractBase(com.servoy.j2db.persistence.AbstractBase) RepositoryException(com.servoy.j2db.persistence.RepositoryException) RuntimeRadioChoice(com.servoy.j2db.ui.scripting.RuntimeRadioChoice) RuntimeCheckBoxChoice(com.servoy.j2db.ui.scripting.RuntimeCheckBoxChoice) RuntimeTextArea(com.servoy.j2db.ui.scripting.RuntimeTextArea) AbstractRuntimeValuelistComponent(com.servoy.j2db.ui.scripting.AbstractRuntimeValuelistComponent) IStyleRule(com.servoy.j2db.util.IStyleRule) RuntimeListBox(com.servoy.j2db.ui.scripting.RuntimeListBox) RuntimeSpinner(com.servoy.j2db.ui.scripting.RuntimeSpinner) IFormatScriptComponent(com.servoy.j2db.ui.scripting.IFormatScriptComponent) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) RenderEventExecutor(com.servoy.j2db.ui.RenderEventExecutor) IFieldComponent(com.servoy.j2db.ui.IFieldComponent) IStylePropertyChangesRecorder(com.servoy.j2db.ui.IStylePropertyChangesRecorder) Border(javax.swing.border.Border) TitledBorder(javax.swing.border.TitledBorder) IDisplayTagText(com.servoy.j2db.ui.IDisplayTagText)

Example 23 with IDataProvider

use of com.servoy.j2db.persistence.IDataProvider in project servoy-client by Servoy.

the class JSDatabaseManager method js_convertToDataSet.

/**
 * @clonedesc js_convertToDataSet(IFoundSetInternal)
 *
 * @sampleas js_convertToDataSet(IFoundSetInternal)
 *
 * @param foundset The foundset to be converted.
 * @param dataproviderNames Array with column names.
 *
 * @return JSDataSet with the data.
 */
public JSDataSet js_convertToDataSet(IFoundSetInternal foundset, String[] dataproviderNames) throws RepositoryException {
    if (foundset == null) {
        return null;
    }
    // $NON-NLS-1$
    String[] dpnames = { "id" };
    ColumnType[] dptypes = { ColumnType.getInstance(IColumnTypes.INTEGER, Integer.MAX_VALUE, 0) };
    List<Object[]> lst = new ArrayList<Object[]>();
    FoundSet fs = (FoundSet) foundset;
    if (fs.getTable() != null) {
        if (dataproviderNames != null) {
            dpnames = dataproviderNames;
        } else {
            dpnames = fs.getSQLSheet().getPKColumnDataProvidersAsArray();
        }
        FoundSetManager fsm = (FoundSetManager) application.getFoundSetManager();
        boolean getInOneQuery = !fs.isInFindMode() && (fs.hadMoreRows() || fs.getSize() > fsm.config.pkChunkSize()) && !fsm.getEditRecordList().hasEditedRecords(fs);
        dptypes = new ColumnType[dpnames.length];
        Table table = fs.getSQLSheet().getTable();
        Map<String, Column> columnMap = new HashMap<String, Column>();
        for (int i = 0; i < dpnames.length; i++) {
            IDataProvider dp = application.getFlattenedSolution().getDataProviderForTable(table, dpnames[i]);
            dptypes[i] = dp == null ? ColumnType.getInstance(0, 0, 0) : ColumnType.getInstance(dp instanceof Column ? ((Column) dp).getType() : dp.getDataProviderType(), dp.getLength(), dp instanceof Column ? ((Column) dp).getScale() : 0);
            if (getInOneQuery) {
                // only columns and data we can get from the foundset (calculations only when stored)
                if (dp instanceof Column) {
                    columnMap.put(dpnames[i], (Column) dp);
                    // Blobs require special resultset handling
                    getInOneQuery = !SQLGenerator.isBlobColumn((Column) dp);
                } else {
                    // aggregates, globals
                    getInOneQuery = fs.containsDataProvider(dpnames[i]);
                }
            }
        }
        if (getInOneQuery && columnMap.size() > 0) {
            // large foundset, query the columns in 1 go
            QuerySelect sqlSelect = AbstractBaseQuery.deepClone(fs.getQuerySelectForReading());
            ArrayList<IQuerySelectValue> cols = new ArrayList<IQuerySelectValue>(columnMap.size());
            ArrayList<String> distinctColumns = new ArrayList<String>(columnMap.size());
            for (String dpname : dpnames) {
                Column column = columnMap.get(dpname);
                if (column != null && !distinctColumns.contains(dpname)) {
                    distinctColumns.add(dpname);
                    cols.add(column.queryColumn(sqlSelect.getTable()));
                }
            }
            boolean hasJoins = sqlSelect.getJoins() != null;
            if (hasJoins) {
                // add pk columns so distinct-in-memory can be used
                List<Column> rowIdentColumns = ((Table) fs.getTable()).getRowIdentColumns();
                for (Column column : rowIdentColumns) {
                    if (!columnMap.containsKey(column.getDataProviderID())) {
                        cols.add(column.queryColumn(sqlSelect.getTable()));
                    }
                }
            }
            sqlSelect.setColumns(cols);
            try {
                SQLSheet sheet = fs.getSQLSheet();
                IConverterManager<IColumnConverter> columnConverterManager = ((FoundSetManager) fs.getFoundSetManager()).getColumnConverterManager();
                SQLStatement trackingInfo = null;
                if (fsm.getEditRecordList().hasAccess(sheet.getTable(), IRepository.TRACKING_VIEWS)) {
                    trackingInfo = new SQLStatement(ISQLActionTypes.SELECT_ACTION, sheet.getServerName(), sheet.getTable().getName(), null, null);
                    trackingInfo.setTrackingData(sqlSelect.getColumnNames(), new Object[][] {}, new Object[][] {}, fsm.getApplication().getUserUID(), fsm.getTrackingInfo(), fsm.getApplication().getClientID());
                }
                IDataSet dataSet = fsm.getDataServer().performQuery(fsm.getApplication().getClientID(), sheet.getServerName(), fsm.getTransactionID(sheet), sqlSelect, null, fsm.getTableFilterParams(sheet.getServerName(), sqlSelect), hasJoins, 0, -1, IDataServer.FOUNDSET_LOAD_QUERY, trackingInfo);
                lst = new ArrayList<Object[]>(dataSet.getRowCount());
                for (int i = 0; i < dataSet.getRowCount(); i++) {
                    Object[] row = new Object[dpnames.length];
                    // may contain more data: pk columns for distinct-in-memory
                    Object[] dataseRow = dataSet.getRow(i);
                    for (int j = 0; j < dpnames.length; j++) {
                        Column column = columnMap.get(dpnames[j]);
                        if (column == null) {
                            // fs.containsDataProvider returned true for this dpname
                            row[j] = fs.getDataProviderValue(dpnames[j]);
                        } else {
                            row[j] = sheet.convertValueToObject(dataseRow[distinctColumns.indexOf(dpnames[j])], sheet.getColumnIndex(dpnames[j]), columnConverterManager);
                        }
                    }
                    lst.add(row);
                }
            } catch (RepositoryException e) {
                throw e;
            } catch (Exception e) {
                Debug.error(e);
                throw new RepositoryException(e.getMessage());
            }
        } else {
            // loop over the records
            for (int i = 0; i < fs.getSize(); i++) {
                IRecordInternal record = fs.getRecord(i);
                Object[] pk = new Object[dpnames.length];
                for (int j = 0; j < dpnames.length; j++) {
                    pk[j] = record.getValue(dpnames[j]);
                }
                lst.add(pk);
            }
        }
    }
    return new JSDataSet(application, BufferedDataSetInternal.createBufferedDataSet(dpnames, dptypes, lst, false));
}
Also used : ColumnType(com.servoy.j2db.query.ColumnType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QueryString(com.servoy.j2db.persistence.QueryString) IDataProvider(com.servoy.j2db.persistence.IDataProvider) QueryColumn(com.servoy.j2db.query.QueryColumn) IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) QBColumn(com.servoy.j2db.querybuilder.impl.QBColumn) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) Table(com.servoy.j2db.persistence.Table) IJSFoundSet(com.servoy.base.scripting.api.IJSFoundSet) RepositoryException(com.servoy.j2db.persistence.RepositoryException) QuerySelect(com.servoy.j2db.query.QuerySelect) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) SQLException(java.sql.SQLException) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) NativeObject(org.mozilla.javascript.NativeObject) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 24 with IDataProvider

use of com.servoy.j2db.persistence.IDataProvider in project servoy-client by Servoy.

the class DataAdapterList method addDataAdaptersForRelationSequence.

/**
 * Walk over the relation sequence to add data adapters.
 *
 * @param dataAdapter
 * @param relations
 * @param rel
 * @throws RepositoryException
 */
protected void addDataAdaptersForRelationSequence(IDataAdapter dataAdapter, Relation[] relations, int rel) throws RepositoryException {
    Relation relation = relations[rel];
    IDataProvider[] dps = relation.getPrimaryDataProviders(application.getFlattenedSolution());
    if (dps != null) {
        for (IDataProvider dp : dps) {
            if (dp instanceof LiteralDataprovider)
                continue;
            StringBuilder prefix = new StringBuilder();
            for (int r = 0; r < rel; r++) {
                prefix.append(relations[r].getName());
                prefix.append('.');
            }
            String dataProviderID;
            if (ScopesUtils.isVariableScope(dp.getDataProviderID())) {
                dataProviderID = dp.getDataProviderID();
            } else {
                dataProviderID = prefix.toString() + dp.getDataProviderID();
            }
            IDataAdapter da = dataAdapters.get(dataProviderID);
            if (da == null) {
                da = new DataAdapter(dataProviderID);
                dataAdapters.put(dataProviderID, da);
            }
            da.addDataListener(dataAdapter);
            if (rel > 0) {
                addDataAdaptersForRelationSequence(da, relations, rel - 1);
            }
        }
    }
}
Also used : Relation(com.servoy.j2db.persistence.Relation) IDataProvider(com.servoy.j2db.persistence.IDataProvider) LiteralDataprovider(com.servoy.j2db.persistence.LiteralDataprovider)

Example 25 with IDataProvider

use of com.servoy.j2db.persistence.IDataProvider in project servoy-client by Servoy.

the class FoundSetManager method getRelationWhereArgs.

/**
 * Get relation where-args, not using column converters
 * @param state
 * @param relation
 * @param testForCalcs
 * @return
 * @throws RepositoryException
 */
public Object[] getRelationWhereArgs(IRecordInternal state, Relation relation, boolean testForCalcs) throws RepositoryException {
    boolean isNull = true;
    IDataProvider[] args = relation.getPrimaryDataProviders(application.getFlattenedSolution());
    Column[] columns = relation.getForeignColumns(application.getFlattenedSolution());
    Object[] array = new Object[args.length];
    for (int i = 0; i < args.length; i++) {
        Object value = null;
        if (args[i] instanceof LiteralDataprovider) {
            value = ((LiteralDataprovider) args[i]).getValue();
        } else if (args[i] instanceof EnumDataProvider) {
            value = getScopesScopeProvider().getDataProviderValue(args[i].getDataProviderID());
        } else {
            String dataProviderID = args[i].getDataProviderID();
            if (testForCalcs && state.getRawData().containsCalculation(dataProviderID) && state.getRawData().mustRecalculate(dataProviderID, true)) {
                // else this can just cascade through..
                return null;
            }
            // unconverted (todb value)
            value = state.getValue(dataProviderID, false);
        }
        if (value != Scriptable.NOT_FOUND) {
            array[i] = columns[i].getAsRightType(value);
        }
        if (array[i] != null) {
            isNull = false;
        } else {
            // Because null columns can't have a relation.
            if (args[i] instanceof IColumn) {
                return null;
            }
            if (isNull) {
                isNull = !(args[i] instanceof ScriptVariable);
            }
        }
    }
    // optimize for null keys (multiple all null!) but not empty pk (db ident)
    if (isNull)
        return null;
    return array;
}
Also used : IDataProvider(com.servoy.j2db.persistence.IDataProvider) LiteralDataprovider(com.servoy.j2db.persistence.LiteralDataprovider) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) IBaseColumn(com.servoy.base.persistence.IBaseColumn) IColumn(com.servoy.j2db.persistence.IColumn) EnumDataProvider(com.servoy.j2db.persistence.EnumDataProvider) ScriptVariable(com.servoy.j2db.persistence.ScriptVariable) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject)

Aggregations

IDataProvider (com.servoy.j2db.persistence.IDataProvider)25 RepositoryException (com.servoy.j2db.persistence.RepositoryException)13 Column (com.servoy.j2db.persistence.Column)11 IColumn (com.servoy.j2db.persistence.IColumn)11 Relation (com.servoy.j2db.persistence.Relation)8 QueryColumn (com.servoy.j2db.query.QueryColumn)6 FormController (com.servoy.j2db.FormController)5 LiteralDataprovider (com.servoy.j2db.persistence.LiteralDataprovider)5 ColumnInfo (com.servoy.j2db.persistence.ColumnInfo)4 ColumnWrapper (com.servoy.j2db.persistence.ColumnWrapper)4 Form (com.servoy.j2db.persistence.Form)4 ITable (com.servoy.j2db.persistence.ITable)4 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)4 FormAndTableDataProviderLookup (com.servoy.j2db.FormAndTableDataProviderLookup)3 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)3 AbstractBase (com.servoy.j2db.persistence.AbstractBase)3 IPersist (com.servoy.j2db.persistence.IPersist)3 Table (com.servoy.j2db.persistence.Table)3 IBaseColumn (com.servoy.base.persistence.IBaseColumn)2 BaseQueryColumn (com.servoy.base.query.BaseQueryColumn)2