Search in sources :

Example 16 with FormScope

use of com.servoy.j2db.scripting.FormScope in project servoy-client by Servoy.

the class RecordItemModel method getValue.

/**
 * Gets the value for dataProviderID in the context of this record and the form determined using given component. The component is only used for getting the
 * form, otherwise it does not affect the returned value.
 *
 * @param component the component used to determine the form (in case of global or form variables).
 * @param dataProviderID the data provider id pointing to a data provider in the record, a form or a global variable.
 * @return the value.
 */
public Object getValue(Component component, String dataProviderID) {
    Object value = null;
    WebForm webForm = component.findParent(WebForm.class);
    if (webForm == null) {
        // component.toString() may cause stackoverflow here
        // $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
        Debug.error(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
        "Component " + component.getClass() + " with dp: " + dataProviderID + "  already removed from its parent form.", new RuntimeException());
        return null;
    }
    FormScope fs = webForm.getController().getFormScope();
    IRecord record = (IRecord) RecordItemModel.this.getObject();
    if (ScopesUtils.isVariableScope(dataProviderID)) {
        value = webForm.getController().getApplication().getScriptEngine().getSolutionScope().getScopesScope().get(null, dataProviderID);
    } else if (record != null) {
        value = record.getValue(dataProviderID);
    }
    if (value == Scriptable.NOT_FOUND && fs != null && fs.has(dataProviderID, fs)) {
        value = fs.get(dataProviderID);
    }
    if (value instanceof DbIdentValue) {
        value = ((DbIdentValue) value).getPkValue();
    }
    if (value == Scriptable.NOT_FOUND) {
        value = null;
    } else if (!(record instanceof FindState)) {
        // use UI converter to convert from record value to UI value
        value = ComponentFormat.applyUIConverterToObject(component, value, dataProviderID, webForm.getController().getApplication().getFoundSetManager());
    }
    return value;
}
Also used : FindState(com.servoy.j2db.dataprocessing.FindState) WebForm(com.servoy.j2db.server.headlessclient.WebForm) DbIdentValue(com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue) IRecord(com.servoy.j2db.dataprocessing.IRecord) FormScope(com.servoy.j2db.scripting.FormScope)

Example 17 with FormScope

use of com.servoy.j2db.scripting.FormScope in project servoy-client by Servoy.

the class FormPropertyType method toJSON.

@Override
public JSONWriter toJSON(JSONWriter writer, String key, Object sabloValue, PropertyDescription pd, DataConversion clientConversion, IBrowserConverterContext dataConverterContext) throws JSONException {
    if (key != null) {
        writer.key(key);
    }
    String formName = null;
    if (sabloValue instanceof String) {
        formName = (String) sabloValue;
        if (dataConverterContext != null && dataConverterContext.getWebObject() instanceof IContextProvider) {
            FlattenedSolution flattenedSolution = ((IContextProvider) dataConverterContext.getWebObject()).getDataConverterContext().getApplication().getFlattenedSolution();
            Form form = flattenedSolution.getForm(formName);
            // form name
            if (form == null) {
                form = (Form) flattenedSolution.searchPersist((String) sabloValue);
            }
            if (form != null) {
                formName = form.getName();
            }
        }
    } else if (sabloValue instanceof CharSequence) {
        formName = ((CharSequence) sabloValue).toString();
    } else if (sabloValue instanceof Form) {
        formName = ((Form) sabloValue).getName();
    } else if (sabloValue instanceof FormController) {
        formName = ((FormController) sabloValue).getName();
    } else if (sabloValue instanceof FormScope) {
        formName = ((FormScope) sabloValue).getFormController().getName();
    } else {
        formName = null;
        Debug.error("Cannot handle unknown value for Form type: " + sabloValue);
    }
    writer.value(formName);
    if (CurrentWindow.get() instanceof INGClientWindow) {
        // if this is a web component that triggered it, register to only allow this form for that component
        if (dataConverterContext != null && dataConverterContext.getWebObject() instanceof WebFormComponent)
            ((INGClientWindow) CurrentWindow.get()).registerAllowedForm(formName, ((WebFormComponent) dataConverterContext.getWebObject()).getFormElement());
        else
            // else register it for null then this form is allowed globally (a form in dialog of popup)
            ((INGClientWindow) CurrentWindow.get()).registerAllowedForm(formName, null);
    }
    return writer;
}
Also used : FormController(com.servoy.j2db.FormController) IContextProvider(com.servoy.j2db.server.ngclient.IContextProvider) INGClientWindow(com.servoy.j2db.server.ngclient.INGClientWindow) Form(com.servoy.j2db.persistence.Form) JSForm(com.servoy.j2db.scripting.solutionmodel.JSForm) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) FlattenedSolution(com.servoy.j2db.FlattenedSolution) FormScope(com.servoy.j2db.scripting.FormScope)

Example 18 with FormScope

use of com.servoy.j2db.scripting.FormScope in project servoy-client by Servoy.

the class JSEventType method fillJSEvent.

/**
 * @param event Event that needs to be filled
 * @param jsonObject The json data for that has the event data.
 * @param webObject The webObject element (WEbFormComponent or WebFormUI)
 * @param controller Optional the controller object if the caller knows this already
 * @return
 */
@SuppressWarnings("nls")
public static void fillJSEvent(JSBaseEvent event, JSONObject jsonObject, BaseWebObject webObject, IWebFormController controller) {
    event.setType(jsonObject.optString("eventType"));
    String formName = controller != null ? controller.getName() : "";
    String elementName = "";
    if (webObject instanceof WebFormComponent) {
        elementName = ((WebFormComponent) webObject).getFormElement().getRawName();
        if (elementName == null)
            elementName = "";
        if (formName.isEmpty()) {
            BaseWebObject parentWebObject = ((WebFormComponent) webObject).getParent();
            while (parentWebObject != null && !(parentWebObject instanceof WebFormUI)) {
                parentWebObject = ((WebFormComponent) parentWebObject).getParent();
            }
            if (parentWebObject instanceof WebFormUI) {
                formName = parentWebObject.getName();
            }
        }
    }
    if (formName.isEmpty()) {
        formName = jsonObject.optString("formName");
    }
    if (formName.isEmpty() && webObject instanceof WebFormUI) {
        // executeInlineScript with an event in params tells a DAL to execute it and it gives a formui as context to the fromJSON conversion
        // for JSEventType problem is that it can give any formName from the client (the component/service can give anything there as an arg); or, if the function
        // (sent to client before through "function" type) that will be used to execute the script is a global/scope function
        // then formName can be null and in that case the WebFormUI will be the main form or the window (not the most nested one)
        // so this is just a fallback and we do give priority to the form name determined on client through $window.createJSEvent(...) an put into
        // "jsonObject" (see if above) - to target the correct form - closest one to event
        formName = webObject.getName();
    }
    if (elementName.isEmpty()) {
        elementName = jsonObject.optString("elementName");
    }
    if (!formName.isEmpty())
        event.setFormName(formName);
    if (!elementName.isEmpty())
        event.setElementName(elementName);
    if (!formName.isEmpty()) {
        INGApplication application = ((IContextProvider) webObject).getDataConverterContext().getApplication();
        IWebFormController formController = controller != null ? controller : application.getFormManager().getForm(formName);
        if (formController != null) {
            FormScope formScope = formController.getFormScope();
            if (formScope != null) {
                ElementScope elementsScope = (ElementScope) formScope.get("elements", null);
                if (elementsScope != null) {
                    Object scriptableElement = !elementName.isEmpty() ? elementsScope.get(elementName, null) : null;
                    if (scriptableElement != null && scriptableElement != Scriptable.NOT_FOUND) {
                        event.setSource(scriptableElement);
                    } else if (webObject instanceof WebFormComponent) {
                        // quickly create a scriptable wrapper around the component so that the source can be set to a value that we expect.
                        FormElement fe = ((WebFormComponent) webObject).getFormElement();
                        RuntimeWebComponent runtimeComponent = new RuntimeWebComponent((WebFormComponent) webObject, webObject.getSpecification());
                        if (fe.isLegacy() || ((fe.getForm().getView() == IForm.LIST_VIEW || fe.getForm().getView() == FormController.LOCKED_LIST_VIEW || fe.getForm().getView() == FormController.TABLE_VIEW || fe.getForm().getView() == FormController.LOCKED_TABLE_VIEW) && fe.getTypeName().startsWith("svy-"))) {
                            // add legacy behavior
                            runtimeComponent.setPrototype(new RuntimeLegacyComponent((WebFormComponent) webObject));
                        }
                        event.setSource(runtimeComponent);
                    }
                }
            }
        }
    }
    try {
        if (jsonObject.has("x"))
            event.setLocation(new Point(jsonObject.optInt("x"), jsonObject.optInt("y")));
        if (jsonObject.has("modifiers"))
            event.setModifiers(jsonObject.optInt("modifiers"));
        if (jsonObject.has("data"))
            event.setData(jsonObject.opt("data"));
        if (jsonObject.has("timestamp"))
            event.setTimestamp(new Timestamp(jsonObject.getLong("timestamp")));
        else
            event.setTimestamp(new Date());
    } catch (Exception e) {
        Debug.error("error setting event properties from " + jsonObject + ", for component: " + elementName + " on form " + formName, e);
    }
}
Also used : WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) ElementScope(com.servoy.j2db.scripting.ElementScope) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) Point(java.awt.Point) RuntimeWebComponent(com.servoy.j2db.server.ngclient.component.RuntimeWebComponent) Timestamp(java.sql.Timestamp) FormElement(com.servoy.j2db.server.ngclient.FormElement) Date(java.util.Date) JSONException(org.json.JSONException) BaseWebObject(org.sablo.BaseWebObject) FormScope(com.servoy.j2db.scripting.FormScope) WebFormUI(com.servoy.j2db.server.ngclient.WebFormUI) INGApplication(com.servoy.j2db.server.ngclient.INGApplication) JSONObject(org.json.JSONObject) BaseWebObject(org.sablo.BaseWebObject) RuntimeLegacyComponent(com.servoy.j2db.server.ngclient.component.RuntimeLegacyComponent)

Example 19 with FormScope

use of com.servoy.j2db.scripting.FormScope in project servoy-client by Servoy.

the class DebugUtils method getScopesAndFormsToReload.

public static Set<IFormController>[] getScopesAndFormsToReload(final ClientState clientState, Collection<IPersist> changes) {
    Set<IFormController> scopesToReload = new HashSet<IFormController>();
    final Set<IFormController> formsToReload = new HashSet<IFormController>();
    final SpecProviderState specProviderState = WebComponentSpecProvider.getSpecProviderState();
    final Set<Form> formsUpdated = new HashSet<Form>();
    for (IPersist persist : changes) {
        clientState.getFlattenedSolution().updatePersistInSolutionCopy(persist);
        if (persist instanceof ScriptMethod) {
            if (persist.getParent() instanceof Form) {
                Form form = (Form) persist.getParent();
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers(form);
                for (IFormController formController : cachedFormControllers) {
                    scopesToReload.add(formController);
                }
            } else if (persist.getParent() instanceof Solution) {
                LazyCompilationScope scope = clientState.getScriptEngine().getScopesScope().getGlobalScope(((ScriptMethod) persist).getScopeName());
                scope.remove((IScriptProvider) persist);
                scope.put((IScriptProvider) persist, (IScriptProvider) persist);
            } else if (persist.getParent() instanceof TableNode) {
                clientState.getFoundSetManager().reloadFoundsetMethod(((TableNode) persist.getParent()).getDataSource(), (IScriptProvider) persist);
            }
            if (clientState instanceof DebugJ2DBClient) {
                // ((DebugJ2DBClient)clientState).clearUserWindows();  no need for this as window API was refactored and it allows users to clean up dialogs
                ((DebugSwingFormMananger) ((DebugJ2DBClient) clientState).getFormManager()).fillScriptMenu();
            }
        } else if (persist instanceof ScriptVariable) {
            ScriptVariable sv = (ScriptVariable) persist;
            if (persist.getParent() instanceof Solution) {
                clientState.getScriptEngine().getScopesScope().getGlobalScope(sv.getScopeName()).put(sv);
            }
            if (persist.getParent() instanceof Form) {
                Form form = (Form) persist.getParent();
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers(form);
                for (IFormController formController : cachedFormControllers) {
                    FormScope scope = formController.getFormScope();
                    scope.put(sv);
                }
            }
        } else if (persist.getAncestor(IRepository.FORMS) != null) {
            final Form form = (Form) persist.getAncestor(IRepository.FORMS);
            if (form != null && form.isFormComponent().booleanValue()) {
                // if the changed form is a reference form we need to check if that is referenced by a loaded form..
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
                for (IFormController fc : cachedFormControllers) {
                    fc.getForm().acceptVisitor(new IPersistVisitor() {

                        @Override
                        public Object visit(IPersist o) {
                            if (o instanceof WebComponent) {
                                WebComponent wc = (WebComponent) o;
                                WebObjectSpecification spec = FormTemplateGenerator.getWebObjectSpecification(wc);
                                Collection<PropertyDescription> properties = spec != null ? spec.getProperties(FormComponentPropertyType.INSTANCE) : null;
                                if (properties != null && properties.size() > 0) {
                                    Form persistForm = (Form) wc.getAncestor(IRepository.FORMS);
                                    for (PropertyDescription pd : properties) {
                                        Form frm = FormComponentPropertyType.INSTANCE.getForm(wc.getProperty(pd.getName()), clientState.getFlattenedSolution());
                                        if (frm != null && (form.equals(frm) || FlattenedForm.hasFormInHierarchy(frm, form) || isReferenceFormUsedInForm(clientState, form, frm)) && !formsUpdated.contains(persistForm)) {
                                            formsUpdated.add(persistForm);
                                            List<IFormController> cfc = clientState.getFormManager().getCachedFormControllers(persistForm);
                                            for (IFormController formController : cfc) {
                                                formsToReload.add(formController);
                                            }
                                        }
                                    }
                                }
                            }
                            return IPersistVisitor.CONTINUE_TRAVERSAL;
                        }
                    });
                }
            } else if (!formsUpdated.contains(form)) {
                formsUpdated.add(form);
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers(form);
                for (IFormController formController : cachedFormControllers) {
                    formsToReload.add(formController);
                }
            }
            if (persist instanceof Form && clientState.getFormManager() instanceof DebugUtils.DebugUpdateFormSupport) {
                ((DebugUtils.DebugUpdateFormSupport) clientState.getFormManager()).updateForm((Form) persist);
            }
        } else if (persist instanceof ScriptCalculation) {
            ScriptCalculation sc = (ScriptCalculation) persist;
            if (((RemoteDebugScriptEngine) clientState.getScriptEngine()).recompileScriptCalculation(sc)) {
                List<String> al = new ArrayList<String>();
                al.add(sc.getDataProviderID());
                try {
                    String dataSource = clientState.getFoundSetManager().getDataSource(sc.getTable());
                    ((FoundSetManager) clientState.getFoundSetManager()).getRowManager(dataSource).clearCalcs(null, al);
                    ((FoundSetManager) clientState.getFoundSetManager()).flushSQLSheet(dataSource);
                } catch (Exception e) {
                    Debug.error(e);
                }
            }
        // if (clientState instanceof DebugJ2DBClient)
        // {
        // ((DebugJ2DBClient)clientState).clearUserWindows(); no need for this as window API was refactored and it allows users to clean up dialogs
        // }
        } else if (persist instanceof Relation) {
            ((FoundSetManager) clientState.getFoundSetManager()).flushSQLSheet((Relation) persist);
            List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
            try {
                String primary = ((Relation) persist).getPrimaryDataSource();
                for (IFormController formController : cachedFormControllers) {
                    if (primary.equals(formController.getDataSource())) {
                        final IFormController finalController = formController;
                        final Relation finalRelation = (Relation) persist;
                        formController.getForm().acceptVisitor(new IPersistVisitor() {

                            @Override
                            public Object visit(IPersist o) {
                                if (o instanceof Tab && Utils.equalObjects(finalRelation.getName(), ((Tab) o).getRelationName())) {
                                    formsToReload.add(finalController);
                                    return o;
                                }
                                if (o instanceof Field && ((Field) o).getValuelistID() > 0) {
                                    ValueList vl = clientState.getFlattenedSolution().getValueList(((Field) o).getValuelistID());
                                    if (vl != null && Utils.equalObjects(finalRelation.getName(), vl.getRelationName())) {
                                        formsToReload.add(finalController);
                                        return o;
                                    }
                                }
                                if (o instanceof WebComponent) {
                                    WebComponent webComponent = (WebComponent) o;
                                    WebObjectSpecification spec = specProviderState == null ? null : specProviderState.getWebComponentSpecification(webComponent.getTypeName());
                                    if (spec != null) {
                                        Collection<PropertyDescription> properties = spec.getProperties(RelationPropertyType.INSTANCE);
                                        for (PropertyDescription pd : properties) {
                                            if (Utils.equalObjects(webComponent.getFlattenedJson().opt(pd.getName()), finalRelation.getName())) {
                                                formsToReload.add(finalController);
                                                return o;
                                            }
                                        }
                                    }
                                }
                                return CONTINUE_TRAVERSAL;
                            }
                        });
                    }
                }
            } catch (Exception e) {
                Debug.error(e);
            }
        } else if (persist instanceof ValueList) {
            ComponentFactory.flushValueList(clientState, (ValueList) persist);
            List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
            for (IFormController formController : cachedFormControllers) {
                final IFormController finalController = formController;
                final ValueList finalValuelist = (ValueList) persist;
                formController.getForm().acceptVisitor(new IPersistVisitor() {

                    @Override
                    public Object visit(IPersist o) {
                        if (o instanceof Field && ((Field) o).getValuelistID() > 0 && ((Field) o).getValuelistID() == finalValuelist.getID()) {
                            formsToReload.add(finalController);
                            return o;
                        }
                        if (o instanceof WebComponent) {
                            WebComponent webComponent = (WebComponent) o;
                            WebObjectSpecification spec = specProviderState == null ? null : specProviderState.getWebComponentSpecification(webComponent.getTypeName());
                            if (spec != null) {
                                Collection<PropertyDescription> properties = spec.getProperties(ValueListPropertyType.INSTANCE);
                                for (PropertyDescription pd : properties) {
                                    if (Utils.equalObjects(webComponent.getFlattenedJson().opt(pd.getName()), finalValuelist.getUUID().toString())) {
                                        formsToReload.add(finalController);
                                        return o;
                                    }
                                }
                            }
                        }
                        return CONTINUE_TRAVERSAL;
                    }
                });
            }
        } else if (persist instanceof Style) {
            ComponentFactory.flushStyle(null, ((Style) persist));
            List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
            String styleName = ((Style) persist).getName();
            for (IFormController formController : cachedFormControllers) {
                if (styleName.equals(formController.getForm().getStyleName())) {
                    formsToReload.add(formController);
                }
            }
        }
    }
    return new Set[] { scopesToReload, formsToReload };
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) Set(java.util.Set) HashSet(java.util.HashSet) Form(com.servoy.j2db.persistence.Form) FlattenedForm(com.servoy.j2db.persistence.FlattenedForm) ValueList(com.servoy.j2db.persistence.ValueList) LazyCompilationScope(com.servoy.j2db.scripting.LazyCompilationScope) FormScope(com.servoy.j2db.scripting.FormScope) WebComponent(com.servoy.j2db.persistence.WebComponent) Field(com.servoy.j2db.persistence.Field) Relation(com.servoy.j2db.persistence.Relation) Style(com.servoy.j2db.persistence.Style) ValueList(com.servoy.j2db.persistence.ValueList) List(java.util.List) ArrayList(java.util.ArrayList) Solution(com.servoy.j2db.persistence.Solution) HashSet(java.util.HashSet) FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) DebugSwingFormMananger(com.servoy.j2db.debug.DebugJ2DBClient.DebugSwingFormMananger) RhinoException(org.mozilla.javascript.RhinoException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServoyException(com.servoy.j2db.util.ServoyException) PropertyDescription(org.sablo.specification.PropertyDescription) ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) SpecProviderState(org.sablo.specification.SpecProviderState) Tab(com.servoy.j2db.persistence.Tab) IPersist(com.servoy.j2db.persistence.IPersist) IScriptProvider(com.servoy.j2db.persistence.IScriptProvider) TableNode(com.servoy.j2db.persistence.TableNode) ScriptVariable(com.servoy.j2db.persistence.ScriptVariable) IPersistVisitor(com.servoy.j2db.persistence.IPersistVisitor) Collection(java.util.Collection) IFormController(com.servoy.j2db.IFormController) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod)

Example 20 with FormScope

use of com.servoy.j2db.scripting.FormScope in project servoy-client by Servoy.

the class WebFormUI method initContainerScopeIfNeeded.

/**
 * @param formController2
 */
private void initContainerScopeIfNeeded(IWebFormController fc) {
    if (fc.getForm().isResponsiveLayout()) {
        FormScope formScope = fc.getFormScope();
        ContainersScope containersScope = new ContainersScope(fc);
        // $NON-NLS-1$
        formScope.putWithoutFireChange("containers", containersScope);
    }
}
Also used : ContainersScope(com.servoy.j2db.server.ngclient.scripting.ContainersScope) FormScope(com.servoy.j2db.scripting.FormScope)

Aggregations

FormScope (com.servoy.j2db.scripting.FormScope)23 IFormController (com.servoy.j2db.IFormController)6 Form (com.servoy.j2db.persistence.Form)5 ArrayList (java.util.ArrayList)4 JSONObject (org.json.JSONObject)4 IRecord (com.servoy.j2db.dataprocessing.IRecord)3 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)3 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)3 GlobalScope (com.servoy.j2db.scripting.GlobalScope)3 IWebFormController (com.servoy.j2db.server.ngclient.IWebFormController)3 WebFormComponent (com.servoy.j2db.server.ngclient.WebFormComponent)3 ServoyException (com.servoy.j2db.util.ServoyException)3 JSONException (org.json.JSONException)3 Scriptable (org.mozilla.javascript.Scriptable)3 ApplicationException (com.servoy.j2db.ApplicationException)2 IServiceProvider (com.servoy.j2db.IServiceProvider)2 FindState (com.servoy.j2db.dataprocessing.FindState)2 IServoyAwareBean (com.servoy.j2db.dataui.IServoyAwareBean)2 IPersist (com.servoy.j2db.persistence.IPersist)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2