Search in sources :

Example 1 with MethodTemplate

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

the class BasicFormController method executeFunction.

@SuppressWarnings("nls")
protected Object executeFunction(Function f, Object[] args, Scriptable scope, Scriptable thisObject, boolean saveData, Object src, boolean testFindMode, boolean focusEvent, String methodKey, boolean executeWhenFieldValidationFailed, boolean useFormAsEventSourceEventually, boolean throwException) throws Exception {
    if (// only run certain methods in find
    !(testFindMode && isInFindMode())) {
        // this is a semi saveData , we do NOT want the record go out of edit(!) and is updated in db
        if (saveData) {
            application.getFoundSetManager().getEditRecordList().prepareForSave(false);
        }
        if (f != null) {
            if (!executeWhenFieldValidationFailed && Boolean.TRUE.equals(application.getRuntimeProperties().get(IServiceProvider.RT_LASTFIELDVALIDATIONFAILED_FLAG))) {
                if (Debug.tracing()) {
                    Debug.trace("Function not executed because a field is marked invalid");
                }
                return null;
            }
            FormAndComponent formAndComponent = getJSApplicationNames(src, f, useFormAsEventSourceEventually);
            try {
                currentFormExecutingFunctionCount.incrementAndGet();
                Object[] newArgs = args;
                if (formAndComponent != null) {
                    // for use of deprecated aplication.getMethodTriggerElementName() and aplication.getMethodTriggerFormName()
                    IExecutingEnviroment scriptEngine = application.getScriptEngine();
                    if (scriptEngine instanceof ScriptEngine) {
                        ((ScriptEngine) scriptEngine).getJSApplication().pushLastNames(formAndComponent);
                    }
                    if (methodKey != null) {
                        // add form event if needed
                        MethodTemplate methodTemplate = MethodTemplate.getTemplate(null, methodKey);
                        if (methodTemplate != null) {
                            MethodArgument[] methodArguments = methodTemplate.getArguments();
                            for (int i = 0; methodArguments != null && i < methodArguments.length; i++) {
                                if (methodArguments[i].getType() == ArgumentType.JSEvent) {
                                    // method template declares an event argument
                                    if (args == null || args.length <= i || args[i] == null) {
                                        // no event argument there yet, insert a form event
                                        JSEvent event = getJSEvent(formAndComponent.src, methodKey);
                                        if (args == null || args.length <= i) {
                                            newArgs = new Object[i + 1];
                                            if (args != null) {
                                                System.arraycopy(args, 0, newArgs, 0, args.length);
                                            }
                                        }
                                        newArgs[i] = event;
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                if (newArgs != null && newArgs.length > 0) {
                    for (Object newArg : newArgs) {
                        if (newArg instanceof JSEvent) {
                            JSEvent event = (JSEvent) newArg;
                            if (formScope != null && event.getSource() instanceof IComponent && ((IComponent) event.getSource()).getName() != null) {
                                Object elementScope = formScope.get("elements");
                                if (elementScope instanceof Scriptable) {
                                    Object elementSrc = ((Scriptable) elementScope).get(((IComponent) event.getSource()).getName(), (Scriptable) elementScope);
                                    if (elementSrc != null) {
                                        if (elementSrc instanceof ITwoNativeJavaObject) {
                                            Object scriptable = event.getSource();
                                            if (scriptable instanceof IScriptableProvider) {
                                                scriptable = ((IScriptableProvider) scriptable).getScriptObject();
                                            }
                                            ((ITwoNativeJavaObject) elementSrc).setRealObject(scriptable);
                                        }
                                        event.setSource(elementSrc);
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
                return application.getScriptEngine().executeFunction(f, scope, thisObject, newArgs, focusEvent, throwException);
            } finally {
                currentFormExecutingFunctionCount.decrementAndGet();
                if (formAndComponent != null) {
                    IExecutingEnviroment scriptEngine = application.getScriptEngine();
                    if (scriptEngine instanceof ScriptEngine) {
                        ((ScriptEngine) scriptEngine).getJSApplication().popLastStackNames(formAndComponent);
                    }
                }
                // after a script clear the unchanged records so that no records keep hanging around.
                if (!focusEvent && !"onRecordEditStopMethodID".equals(methodKey) && !"onRenderMethodID".equals(methodKey) && application.getFoundSetManager() != null) {
                    application.getFoundSetManager().getEditRecordList().removeUnChangedRecords(false, false);
                }
            }
        }
    }
    return null;
}
Also used : IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) JSEvent(com.servoy.j2db.scripting.JSEvent) MethodArgument(com.servoy.j2db.persistence.MethodArgument) IComponent(com.servoy.j2db.ui.IComponent) MethodTemplate(com.servoy.j2db.persistence.MethodTemplate) Scriptable(org.mozilla.javascript.Scriptable) FormAndComponent(com.servoy.j2db.scripting.JSApplication.FormAndComponent) ScriptEngine(com.servoy.j2db.scripting.ScriptEngine) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject) IScriptableProvider(com.servoy.j2db.scripting.IScriptableProvider)

Example 2 with MethodTemplate

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

the class MethodTemplatesLoader method loadMethodTemplatesFromXML.

public static void loadMethodTemplatesFromXML() {
    synchronized (ClientMethodTemplatesLoader.getLock()) {
        // even if it was loaded before with "ClientMethodTemplatesLoader.loadClientMethodTemplatesIfNeeded" we must load the XML, as we are in developer and we need all the method template info
        ClientMethodTemplatesLoader.setLoaded();
        try {
            InputStream is = new FileInputStream("../../servoy-eclipse/com.servoy.eclipse.core/src/com/servoy/eclipse/core/doc/methodtemplates.xml");
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = builder.parse(is);
            Element rootElement = doc.getDocumentElement();
            NodeList eventElements = rootElement.getElementsByTagName("event");
            int counter = 0;
            for (int i = 0; i < eventElements.getLength(); i++) {
                Element eventElem = (Element) eventElements.item(i);
                String name = eventElem.getAttribute("name");
                NodeList templElements = eventElem.getElementsByTagName("methodtemplate");
                if (templElements.getLength() > 0) {
                    Element methTempl = (Element) templElements.item(0);
                    MethodTemplate mt = MethodTemplate.fromXML(methTempl);
                    MethodTemplate.COMMON_TEMPLATES.put(name, mt);
                    counter++;
                }
            }
            System.out.println("Loaded " + counter + " method templates.");
        } catch (Throwable e) {
            Debug.error("Exception while loading method templates.", e);
        }
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) MethodTemplate(com.servoy.j2db.persistence.MethodTemplate) FileInputStream(java.io.FileInputStream)

Example 3 with MethodTemplate

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

the class SpecGenerator method readModelAndHandlers.

private void readModelAndHandlers(List<SpecTemplateModel> specTemplateList) {
    MethodTemplatesLoader.loadMethodTemplatesFromXML();
    ContentSpec spec = StaticContentSpecLoader.getContentSpec();
    for (SpecTemplateModel componentSpec : specTemplateList) {
        List<Element> props = Utils.asList(spec.getPropertiesForObjectType(componentSpec.getRepositoryType()));
        List<Element> model = new ArrayList<Element>();
        List<ApiMethod> handlers = new ArrayList<ApiMethod>();
        for (Element element : props) {
            if (isAllowedProperty(componentSpec.getName(), element.getName())) {
                if (BaseComponent.isEventProperty(element.getName())) {
                    if (element.getDeprecatedMoveContentID() == 0) {
                        MethodTemplate template = MethodTemplate.getTemplate(ScriptMethod.class, element.getName());
                        List<String> parametersNames = new ArrayList<String>();
                        List<String> parameterTypes = new ArrayList<String>();
                        List<String> optionalParameters = new ArrayList<String>();
                        if (template.getArguments() != null) {
                            for (MethodArgument arg : template.getArguments()) {
                                parametersNames.add(arg.getName());
                                parameterTypes.add(arg.getType().getName());
                                if (arg.isOptional())
                                    optionalParameters.add(arg.getName());
                            }
                        }
                        String returnType = template.getReturnType() != null ? template.getReturnType().getName() : null;
                        handlers.add(new ApiMethod(element.getName(), returnType, parametersNames, parameterTypes, optionalParameters, metaDataForApi.get(template.getName())));
                    }
                } else if (getSpecTypeFromRepoType(componentSpec.getName(), element) != null) {
                    model.add(element);
                }
            }
        }
        if ("listbox".equals(componentSpec.getName())) {
            ContentSpec cs = new ContentSpec();
            model.add(cs.new Element(-1, IRepository.FIELDS, "multiselectListbox", IRepository.BOOLEAN, Boolean.FALSE));
        }
        if ("splitpane".equals(componentSpec.getName())) {
            ContentSpec cs = new ContentSpec();
            model.add(cs.new Element(-1, IRepository.FIELDS, "divLocation", IRepository.INTEGER, Integer.valueOf(-1)));
            model.add(cs.new Element(-1, IRepository.FIELDS, "divSize", IRepository.INTEGER, Integer.valueOf(5)));
            model.add(cs.new Element(-1, IRepository.FIELDS, "resizeWeight", IRepository.INTEGER, Integer.valueOf(-1)));
            model.add(cs.new Element(-1, IRepository.FIELDS, "pane1MinSize", IRepository.INTEGER, Integer.valueOf(-1)));
            model.add(cs.new Element(-1, IRepository.FIELDS, "pane2MinSize", IRepository.INTEGER, Integer.valueOf(-1)));
        }
        if ("portal".equals(componentSpec.getName())) {
            ContentSpec cs = new ContentSpec();
            model.add(cs.new Element(-1, IRepository.FIELDS, "relatedFoundset", -1, null));
            model.add(cs.new Element(-1, IRepository.FIELDS, "childElements", -1, null));
            model.add(cs.new Element(-1, IRepository.FIELDS, "columnHeaders", -1, null));
            model.add(cs.new Element(-1, IRepository.FIELDS, "headersClasses", -1, null));
            model.add(cs.new Element(-1, IRepository.INTEGER, "headerHeight", IRepository.INTEGER, 32));
        }
        if (componentSpec.getRepositoryType() == IRepository.TABPANELS) {
            ContentSpec cs = new ContentSpec();
            Element el = cs.new Element(-1, IRepository.FIELDS, "tabIndex", IRepository.SERVERS, "");
            if (isAllowedProperty(componentSpec.getName(), el.getName()) && getSpecTypeFromRepoType(componentSpec.getName(), el) != null) {
                model.add(el);
            }
            el = cs.new Element(-1, IRepository.TABPANELS, "tabs", IRepository.SERVERS, null);
            model.add(el);
            el = cs.new Element(-1, IRepository.TABPANELS, "readOnly", IRepository.BOOLEAN, Boolean.FALSE);
            model.add(el);
        }
        if (addFindmodeModelEntries.contains(componentSpec.getName())) {
            ContentSpec cs = new ContentSpec();
            Element el = cs.new Element(-1, IRepository.FIELDS, "findmode", IRepository.STRING, null);
            model.add(el);
        }
        if (addReadOnlyModelEntries.contains(componentSpec.getName())) {
            ContentSpec cs = new ContentSpec();
            Element el = cs.new Element(-1, IRepository.FIELDS, "readOnly", IRepository.STRING, null);
            model.add(el);
        }
        componentSpec.setModel(model);
        componentSpec.setHandlers(handlers);
    }
}
Also used : ContentSpec(com.servoy.j2db.persistence.ContentSpec) MethodArgument(com.servoy.j2db.persistence.MethodArgument) Element(com.servoy.j2db.persistence.ContentSpec.Element) ArrayList(java.util.ArrayList) MethodTemplate(com.servoy.j2db.persistence.MethodTemplate)

Aggregations

MethodTemplate (com.servoy.j2db.persistence.MethodTemplate)3 MethodArgument (com.servoy.j2db.persistence.MethodArgument)2 ContentSpec (com.servoy.j2db.persistence.ContentSpec)1 Element (com.servoy.j2db.persistence.ContentSpec.Element)1 IExecutingEnviroment (com.servoy.j2db.scripting.IExecutingEnviroment)1 IScriptableProvider (com.servoy.j2db.scripting.IScriptableProvider)1 ITwoNativeJavaObject (com.servoy.j2db.scripting.ITwoNativeJavaObject)1 FormAndComponent (com.servoy.j2db.scripting.JSApplication.FormAndComponent)1 JSEvent (com.servoy.j2db.scripting.JSEvent)1 ScriptEngine (com.servoy.j2db.scripting.ScriptEngine)1 IComponent (com.servoy.j2db.ui.IComponent)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)1 Scriptable (org.mozilla.javascript.Scriptable)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1