Search in sources :

Example 6 with DataproviderConfig

use of com.servoy.j2db.server.ngclient.property.DataproviderConfig in project servoy-client by Servoy.

the class ComponentFactory method createComponent.

@SuppressWarnings("nls")
public static WebFormComponent createComponent(IApplication application, IDataAdapterList dataAdapterList, FormElement fe, Container parentToAddTo, Form form) {
    // TODO anything to do here for custom special types?
    WebFormComponent webComponent = new WebFormComponent(fe.getName(), fe, dataAdapterList);
    if (parentToAddTo != null)
        parentToAddTo.add(webComponent);
    String name = fe.getName();
    IPersist persist = fe.getPersistIfAvailable();
    int elementSecurity = 0;
    if (persist != null) {
        boolean getItDirectlyBasedOnPersistAndForm = true;
        // FormComponent's child security is the security of the FormComponent
        if (fe.isFormComponentChild()) {
            String feName = fe.getName();
            // form component children security access is currently dictated by the root form component component security settings; currently one only has the Security tab in form editors not in form component editors;
            // for example if you have a form that contains a form component component A pointing to form component X that has in it a form component component B that points to form component Y
            // then the children of both X and Y in this case have the same security settings as 'root' form component component which is A;
            // so find the 'root' form component component persist and get it's access rights; this should always be found!
            String formComponentName = feName.substring(0, feName.indexOf('$'));
            for (IPersist p : form.getFlattenedFormElementsAndLayoutContainers()) {
                if (p instanceof IFormElement && formComponentName.equals(((IFormElement) p).getName())) {
                    elementSecurity = application.getFlattenedSolution().getSecurityAccess(p.getUUID(), form.getImplicitSecurityNoRights() ? IRepository.IMPLICIT_FORM_NO_ACCESS : IRepository.IMPLICIT_FORM_ACCESS);
                    getItDirectlyBasedOnPersistAndForm = false;
                    break;
                }
            }
            if (getItDirectlyBasedOnPersistAndForm)
                Debug.warn("'Root' form component including component on form " + form.getName() + " was not found when trying to determine access rights for a child of a form component: " + name);
        } else if (persist.getParent() instanceof Portal) {
            elementSecurity = application.getFlattenedSolution().getSecurityAccess(((Portal) persist.getParent()).getUUID(), form.getImplicitSecurityNoRights() ? IRepository.IMPLICIT_FORM_NO_ACCESS : IRepository.IMPLICIT_FORM_ACCESS);
            getItDirectlyBasedOnPersistAndForm = false;
        }
        if (getItDirectlyBasedOnPersistAndForm) {
            elementSecurity = application.getFlattenedSolution().getSecurityAccess(persist.getUUID(), form.getImplicitSecurityNoRights() ? IRepository.IMPLICIT_FORM_NO_ACCESS : IRepository.IMPLICIT_FORM_ACCESS);
        }
        if (!((elementSecurity & IRepository.VIEWABLE) != 0)) {
            webComponent.setVisible(false);
        }
    }
    WebObjectSpecification componentSpec = fe.getWebComponentSpec(false);
    // first convert formElement-to-Sablo and store them in the webComponent
    for (String propName : fe.getRawPropertyValues().keySet()) {
        // TODO this if should not be necessary. currently in the case of "printable" hidden property
        if (componentSpec.getProperty(propName) == null)
            continue;
        Object value = fe.getPropertyValueConvertedForWebComponent(propName, webComponent, (DataAdapterList) dataAdapterList);
        fillProperty(value, fe.getPropertyValue(propName), componentSpec.getProperty(propName), webComponent);
    }
    // then after all of them are converted above attach them to the webComponent (so that when attach is called on any ISmartPropertyValue at least all the other properties are converted
    // this could help initialize smart properties that depend on each other faster then if we would convert and then attach right away each value)
    webComponent.propertiesInitialized();
    // overwrite accessible
    if (persist != null) {
        if (// element not accessible
        !((elementSecurity & IRepository.ACCESSIBLE) != 0)) {
            webComponent.setProperty(WebFormUI.ENABLED, false);
            Object enableValue = webComponent.getRawPropertyValue(WebFormUI.ENABLED);
            if (enableValue instanceof NGEnabledSabloValue) {
                ((NGEnabledSabloValue) enableValue).setAccessible(false);
            }
        } else {
            int formSecurity = application.getFlattenedSolution().getSecurityAccess(form.getUUID(), form.getImplicitSecurityNoRights() ? IRepository.IMPLICIT_FORM_NO_ACCESS : IRepository.IMPLICIT_FORM_ACCESS);
            if (// form not accessible
            !((formSecurity & IRepository.ACCESSIBLE) != 0)) {
                webComponent.setProperty(WebFormUI.ENABLED, false);
                Object enableValue = webComponent.getRawPropertyValue(WebFormUI.ENABLED);
                if (enableValue instanceof NGEnabledSabloValue) {
                    ((NGEnabledSabloValue) enableValue).setAccessible(false);
                }
            }
        }
    }
    boolean[] foundOnDataChangeInDPConfigFromSpec = new boolean[] { false };
    componentSpec.getProperties(DataproviderPropertyType.INSTANCE, true).forEach((propertyFromSpec) -> {
        // the property type found here is for a 'dataprovider' property from the spec file of this component
        Object configOfDPOrFoundsetLinkedDP = propertyFromSpec.getConfig();
        DataproviderConfig dpConfig;
        if (configOfDPOrFoundsetLinkedDP instanceof FoundsetLinkedConfig)
            dpConfig = (DataproviderConfig) ((FoundsetLinkedConfig) configOfDPOrFoundsetLinkedDP).getWrappedConfig();
        else
            dpConfig = (DataproviderConfig) configOfDPOrFoundsetLinkedDP;
        if (dpConfig.getOnDataChange() != null && form.getOnElementDataChangeMethodID() > 0) {
            foundOnDataChangeInDPConfigFromSpec[0] = true;
            webComponent.add(dpConfig.getOnDataChange(), form.getOnElementDataChangeMethodID());
        }
    });
    // TODO should this be a part of type conversions for handlers instead?
    for (String eventName : componentSpec.getHandlers().keySet()) {
        Object eventValue = fe.getPropertyValue(eventName);
        if (eventValue instanceof String) {
            IPersist function = application.getFlattenedSolution().getScriptMethod((String) eventValue);
            if (function == null) {
                function = application.getFlattenedSolution().searchPersist((String) eventValue);
                if (function == null) {
                    Debug.warn("Script Method of value '" + eventValue + "' for handler " + eventName + " not found trying just the form " + form);
                    IPersist child = form.getChild(UUID.fromString((String) eventValue));
                    if (child != null) {
                        Debug.warn("Script Method " + child + " on the form " + form + " with uuid " + child.getUUID());
                        function = child;
                    } else {
                        Debug.warn("Still not found on Form " + form + " Script Method of value '" + eventValue + "' for handler " + eventName);
                    }
                }
            }
            if (function != null) {
                webComponent.add(eventName, function.getID());
            } else {
                Debug.warn("Event handler for " + eventName + " with value '" + eventValue + "' not found (form " + form + ", form element " + name + ")");
            }
        } else if (eventValue instanceof Number && ((Number) eventValue).intValue() > 0) {
            webComponent.add(eventName, ((Number) eventValue).intValue());
        } else if (Utils.equalObjects(eventName, StaticContentSpecLoader.PROPERTY_ONFOCUSGAINEDMETHODID.getPropertyName()) && (form.getOnElementFocusGainedMethodID() > 0)) {
            webComponent.add(eventName, form.getOnElementFocusGainedMethodID());
        } else if (Utils.equalObjects(eventName, StaticContentSpecLoader.PROPERTY_ONFOCUSLOSTMETHODID.getPropertyName()) && (form.getOnElementFocusLostMethodID() > 0)) {
            webComponent.add(eventName, form.getOnElementFocusLostMethodID());
        } else if (!foundOnDataChangeInDPConfigFromSpec[0] && Utils.equalObjects(eventName, StaticContentSpecLoader.PROPERTY_ONDATACHANGEMETHODID.getPropertyName()) && (form.getOnElementDataChangeMethodID() > 0)) {
            // legacy behavior - based on hard-coded handler name (of component)
            webComponent.add(eventName, form.getOnElementDataChangeMethodID());
        }
    }
    // just created, it should have no changes.
    webComponent.clearChanges();
    return webComponent;
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) FoundsetLinkedConfig(com.servoy.j2db.server.ngclient.property.FoundsetLinkedConfig) IFormElement(com.servoy.j2db.persistence.IFormElement) IPersist(com.servoy.j2db.persistence.IPersist) Portal(com.servoy.j2db.persistence.Portal) NGEnabledSabloValue(com.servoy.j2db.server.ngclient.property.types.NGEnabledSabloValue) DataproviderConfig(com.servoy.j2db.server.ngclient.property.DataproviderConfig)

Aggregations

DataproviderConfig (com.servoy.j2db.server.ngclient.property.DataproviderConfig)6 JSONObject (org.json.JSONObject)4 FoundsetLinkedConfig (com.servoy.j2db.server.ngclient.property.FoundsetLinkedConfig)2 WebObjectFunctionDefinition (org.sablo.specification.WebObjectFunctionDefinition)2 ApplicationException (com.servoy.j2db.ApplicationException)1 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)1 IFormElement (com.servoy.j2db.persistence.IFormElement)1 IPersist (com.servoy.j2db.persistence.IPersist)1 Portal (com.servoy.j2db.persistence.Portal)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 FoundsetLinkedPropertyType (com.servoy.j2db.server.ngclient.property.FoundsetLinkedPropertyType)1 FoundsetLinkedTypeSabloValue (com.servoy.j2db.server.ngclient.property.FoundsetLinkedTypeSabloValue)1 INGWebObjectContext (com.servoy.j2db.server.ngclient.property.INGWebObjectContext)1 ValueListConfig (com.servoy.j2db.server.ngclient.property.ValueListConfig)1 DataproviderTypeSabloValue (com.servoy.j2db.server.ngclient.property.types.DataproviderTypeSabloValue)1 NGEnabledSabloValue (com.servoy.j2db.server.ngclient.property.types.NGEnabledSabloValue)1 ServoyException (com.servoy.j2db.util.ServoyException)1 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1