Search in sources :

Example 16 with IonBean

use of com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean in project convertigo by convertigo.

the class UIDynamicElement method getContributor.

@Override
protected Contributor getContributor() {
    return new Contributor() {

        @Override
        public Map<String, String> getActionTsFunctions() {
            return new HashMap<String, String>();
        }

        @Override
        public Map<String, String> getActionTsImports() {
            return new HashMap<String, String>();
        }

        @Override
        public Map<String, File> getCompBeanDir() {
            return new HashMap<String, File>();
        }

        @Override
        public Map<String, String> getModuleTsImports() {
            Map<String, String> imports = new HashMap<String, String>();
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                Map<String, List<String>> map = ionBean.getConfig().getModuleTsImports();
                if (map.size() > 0) {
                    for (String from : map.keySet()) {
                        for (String component : map.get(from)) {
                            imports.put(component.trim(), from);
                        }
                    }
                }
            }
            return imports;
        }

        @Override
        public Set<String> getModuleNgImports() {
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                return ionBean.getConfig().getModuleNgImports();
            }
            return new HashSet<String>();
        }

        @Override
        public Set<String> getModuleNgProviders() {
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                return ionBean.getConfig().getModuleNgProviders();
            }
            return new HashSet<String>();
        }

        @Override
        public Set<String> getModuleNgDeclarations() {
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                return ionBean.getConfig().getModuleNgDeclarations();
            }
            return new HashSet<String>();
        }

        @Override
        public Set<String> getModuleNgComponents() {
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                return ionBean.getConfig().getModuleNgComponents();
            }
            return new HashSet<String>();
        }

        @Override
        public Map<String, String> getPackageDependencies() {
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                return ionBean.getConfig().getPackageDependencies();
            }
            return new HashMap<String, String>();
        }

        @Override
        public Map<String, String> getConfigPlugins() {
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                Map<String, String> map = ionBean.getConfig().getConfigPlugins();
                for (String plugin : map.keySet()) {
                    try {
                        JSONObject json = new JSONObject(map.get(plugin));
                        if (json.has("variables")) {
                            boolean hasChanged = false;
                            JSONObject jsonVars = json.getJSONObject("variables");
                            @SuppressWarnings("unchecked") Iterator<String> it = jsonVars.keys();
                            while (it.hasNext()) {
                                String varkey = it.next();
                                String varval = jsonVars.getString(varkey);
                                if (varval.startsWith("@")) {
                                    // value = @propertyName
                                    String propertyName = varval.substring(1);
                                    if (ionBean.hasProperty(propertyName)) {
                                        IonProperty ionProperty = ionBean.getProperty(propertyName);
                                        Object p_value = ionProperty.getValue();
                                        String value = "";
                                        if (!p_value.equals(false)) {
                                            MobileSmartSourceType msst = ionProperty.getSmartType();
                                            String smartValue = msst.getValue();
                                            if (Mode.PLAIN.equals(msst.getMode())) {
                                                value = smartValue;
                                            }
                                        }
                                        jsonVars.put(varkey, value);
                                        hasChanged = true;
                                    }
                                }
                            }
                            if (hasChanged) {
                                json.put("variables", jsonVars);
                                map.put(plugin, json.toString());
                            }
                        }
                    } catch (Exception e) {
                    }
                }
                return map;
            }
            return new HashMap<String, String>();
        }
    };
}
Also used : IonProperty(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonProperty) HashMap(java.util.HashMap) IonBean(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean) EvaluatorException(org.mozilla.javascript.EvaluatorException) JavaScriptException(org.mozilla.javascript.JavaScriptException) JSONException(org.codehaus.jettison.json.JSONException) EngineException(com.twinsoft.convertigo.engine.EngineException) JSONObject(org.codehaus.jettison.json.JSONObject) List(java.util.List) JSONObject(org.codehaus.jettison.json.JSONObject) File(java.io.File) HashSet(java.util.HashSet)

Example 17 with IonBean

use of com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean in project convertigo by convertigo.

the class UIDynamicIf method computeActionContent.

protected String computeActionContent() {
    if (isEnabled()) {
        IonBean ionBean = getIonBean();
        if (ionBean != null) {
            int numThen = numberOfActions();
            String actionName = getActionName();
            String inputs = computeActionInputs(false);
            StringBuilder sbElse = new StringBuilder();
            StringBuilder sbCatch = new StringBuilder();
            StringBuilder sbThen = new StringBuilder();
            Iterator<UIComponent> it = getUIComponentList().iterator();
            while (it.hasNext()) {
                UIComponent component = (UIComponent) it.next();
                if (component.isEnabled()) {
                    String sElse = "", sCatch = "", sThen = "";
                    if (component instanceof UIDynamicAction) {
                        sThen = ((UIDynamicAction) component).computeActionContent();
                    }
                    if (component instanceof UICustomAction) {
                        sThen = ((UICustomAction) component).computeActionContent();
                    }
                    if (component instanceof UIActionFailureEvent) {
                        sCatch = ((UIActionFailureEvent) component).computeEvent();
                    }
                    if (component instanceof UIActionElseEvent) {
                        sElse = ((UIActionElseEvent) component).computeEvent();
                    }
                    if (!sElse.isEmpty()) {
                        sbElse.append(sElse);
                    }
                    if (!sCatch.isEmpty()) {
                        sbCatch.append(sCatch);
                    }
                    if (!sThen.isEmpty()) {
                        sbThen.append(sbThen.length() > 0 && numThen > 1 ? "\t\t," + System.lineSeparator() : "").append(sThen);
                    }
                }
            }
            String tsCode = "";
            tsCode += "\t\tnew Promise((resolve, reject) => {" + System.lineSeparator();
            tsCode += "\t\t" + System.lineSeparator();
            // tsCode += "\t\tlet self: any = stack[\""+ getName() +"\"] = {};"+ System.lineSeparator();
            tsCode += "\t\tlet self: any = stack[\"" + getName() + "\"] = stack[\"" + priority + "\"] = {};" + System.lineSeparator();
            tsCode += "\t\tself.in = " + inputs + ";" + System.lineSeparator();
            tsCode += "\t\treturn this.actionBeans." + actionName + "(this, self.in.props, {...stack[\"root\"].in, ...self.in.vars})" + System.lineSeparator();
            tsCode += "\t\t.catch((error:any) => {" + System.lineSeparator();
            tsCode += "\t\tparent = self;" + System.lineSeparator();
            tsCode += "\t\tparent.out = error;" + System.lineSeparator();
            tsCode += "\t\tout = parent.out;" + System.lineSeparator();
            if (sbCatch.length() > 0) {
                tsCode += "\t\t" + sbCatch.toString();
            } else {
                tsCode += "\t\treturn Promise.reject(error);" + System.lineSeparator();
            }
            tsCode += "\t\t})" + System.lineSeparator();
            tsCode += "\t\t.then((res:any) => {" + System.lineSeparator();
            tsCode += "\t\tparent = self;" + System.lineSeparator();
            tsCode += "\t\tparent.out = res;" + System.lineSeparator();
            tsCode += "\t\tout = parent.out;" + System.lineSeparator();
            tsCode += "\t\tif (res == true) {" + System.lineSeparator();
            if (sbThen.length() > 0) {
                if (numThen > 1) {
                    tsCode += "\t\treturn Promise.all([" + System.lineSeparator();
                    tsCode += sbThen.toString();
                    tsCode += "\t\t])" + System.lineSeparator();
                } else {
                    tsCode += "\t\treturn " + sbThen.toString().replaceFirst("\t\t", "");
                }
            } else {
                tsCode += "\t\treturn Promise.resolve(res);" + System.lineSeparator();
            }
            tsCode += "\t\t} else if (res == false) {" + System.lineSeparator();
            if (sbElse.toString().isEmpty()) {
                tsCode += "\t\tthis.c8o.log.debug(\"For '" + getName() + "' condition is not verified. No Else handler, skipping and resolve false\");" + System.lineSeparator();
                tsCode += "\t\tresolve(false)" + System.lineSeparator();
            } else {
                tsCode += "\t\t" + sbElse.toString().replaceFirst("\t\t", "");
            }
            tsCode += "\t\t}" + System.lineSeparator();
            tsCode += "\t\t}, (error: any) => {if (\"c8oSkipError\" === error.message) {resolve(false);} else {this.c8o.log.debug(\"[MB] " + actionName + " : \", error.message);throw new Error(error);}})" + System.lineSeparator();
            tsCode += "\t\t.then((res:any) => {resolve(res)}).catch((error:any) => {reject(error)})" + System.lineSeparator();
            tsCode += "\t\t})" + System.lineSeparator();
            return tsCode;
        }
    }
    return "";
}
Also used : IonBean(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean)

Example 18 with IonBean

use of com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean in project convertigo by convertigo.

the class UIComponent method preconfigure.

@Override
public void preconfigure(Element element) throws Exception {
    super.preconfigure(element);
    String version = element.getAttribute("version");
    long priority = Long.valueOf(element.getAttribute("priority")).longValue();
    // TODO: REMOVE BEFORE RELEASE !!!
    boolean doMigration = false;
    if (!doMigration) {
        return;
    }
    if (VersionUtils.compare(version, "7.9.0") < 0) {
        try {
            NodeList properties = element.getElementsByTagName("property");
            int len = properties.getLength();
            Element propElement;
            for (int i = 0; i < len; i++) {
                propElement = (Element) properties.item(i);
                if (propElement != null && propElement.getParentNode().equals(element)) {
                    String propertyName = propElement.getAttribute("name");
                    Element valueElement = (Element) XMLUtils.findChildNode(propElement, Node.ELEMENT_NODE);
                    if (valueElement != null) {
                        Document document = valueElement.getOwnerDocument();
                        Object content = XMLUtils.readObjectFromXml(valueElement);
                        // This is data of the peusdo-bean
                        if ("beanData".equals(propertyName) && content instanceof String) {
                            try {
                                boolean needChange = false;
                                List<String> logList = new ArrayList<String>();
                                IonBean ionBean = new IonBean((String) content);
                                List<IonProperty> propertyList = new ArrayList<IonProperty>();
                                propertyList.addAll(ionBean.getProperties().values());
                                // Walk through properties
                                for (IonProperty ionProperty : propertyList) {
                                    String ionPropertyName = ionProperty.getName();
                                    String modeUpperCase = ionProperty.getMode().toUpperCase();
                                    if (Mode.SOURCE.equals(Mode.valueOf(modeUpperCase))) {
                                        MobileSmartSourceType msst = ionProperty.getSmartType();
                                        String smartValue = msst.getSmartValue();
                                        if (smartValue != null && !smartValue.isEmpty()) {
                                            try {
                                                MobileSmartSource mss = MobileSmartSource.migrate(smartValue);
                                                if (mss != null) {
                                                    boolean migrated = !smartValue.equals(mss.toJsonString());
                                                    if (migrated) {
                                                        msst.setSmartValue(mss.toJsonString());
                                                        ionBean.setPropertyValue(ionPropertyName, msst);
                                                        needChange = true;
                                                        logList.add("Done migration of \"" + ionPropertyName + "\" property for the object \"" + getName() + "\" (priority: " + priority + ")");
                                                    }
                                                }
                                            } catch (Exception e) {
                                                if (e instanceof InvalidSourceException) {
                                                    Engine.logBeans.warn("Failed to migrate \"" + ionPropertyName + "\" property for the object \"" + getName() + "\" (priority: " + priority + "): " + e.getMessage());
                                                } else {
                                                    Engine.logBeans.error("Failed to migrate \"" + ionPropertyName + "\" property for the object \"" + getName() + "\" (priority: " + priority + ")", e);
                                                }
                                            }
                                        }
                                    }
                                }
                                // Store new beandata property value
                                if (needChange) {
                                    String beanData = ionBean.toBeanData();
                                    Element newValueElement = (Element) XMLUtils.writeObjectToXml(document, beanData);
                                    propElement.replaceChild(newValueElement, valueElement);
                                    hasChanged = true;
                                    logList.forEach(s -> Engine.logBeans.warn(s));
                                }
                            } catch (Exception e) {
                                Engine.logBeans.error("Failed to migrate \"" + propertyName + "\" property for the object \"" + getName() + "\" (priority: " + priority + ")", e);
                            }
                        } else // This is a MobileSmartSourceType property
                        if (content instanceof MobileSmartSourceType) {
                            MobileSmartSourceType msst = (MobileSmartSourceType) content;
                            // Property is in 'SRC' mode
                            if (Mode.SOURCE.equals(msst.getMode())) {
                                try {
                                    String smartValue = msst.getSmartValue();
                                    if (smartValue != null && !smartValue.isEmpty()) {
                                        MobileSmartSource mss = MobileSmartSource.migrate(smartValue);
                                        if (mss != null) {
                                            boolean migrated = !smartValue.equals(mss.toJsonString());
                                            if (migrated) {
                                                msst.setSmartValue(mss.toJsonString());
                                                // Store new property value
                                                Element newValueElement = (Element) XMLUtils.writeObjectToXml(document, msst);
                                                propElement.replaceChild(newValueElement, valueElement);
                                                hasChanged = true;
                                                Engine.logBeans.warn("Done migration of \"" + propertyName + "\" property for the object \"" + getName() + "\" (priority: " + priority + ")");
                                            }
                                        }
                                    }
                                } catch (Exception e) {
                                    if (e instanceof InvalidSourceException) {
                                        Engine.logBeans.warn("Failed to migrate \"" + propertyName + "\" property for the object \"" + getName() + "\" (priority: " + priority + "): " + e.getMessage());
                                    } else {
                                        Engine.logBeans.error("Failed to migrate \"" + propertyName + "\" property for the object \"" + getName() + "\" (priority: " + priority + ")", e);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw new EngineException("Unable to preconfigure the mobile uicomponent \"" + getName() + "\".", e);
        }
    }
}
Also used : IonProperty(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonProperty) IonBean(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) EngineException(com.twinsoft.convertigo.engine.EngineException) Document(org.w3c.dom.Document) InvalidSourceException(com.twinsoft.convertigo.engine.InvalidSourceException) EngineException(com.twinsoft.convertigo.engine.EngineException) InvalidSourceException(com.twinsoft.convertigo.engine.InvalidSourceException) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 19 with IonBean

use of com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean in project convertigo by convertigo.

the class UIDynamicAction method computeActionInputs.

protected String computeActionInputs(boolean forTemplate) {
    boolean extended = !forTemplate;
    if (isEnabled()) {
        IonBean ionBean = getIonBean();
        if (ionBean != null) {
            StringBuilder sbProps = initProps(forTemplate);
            for (IonProperty property : ionBean.getProperties().values()) {
                String p_name = property.getName();
                Object p_value = property.getValue();
                sbProps.append(sbProps.length() > 0 ? ", " : "");
                sbProps.append(p_name).append(": ");
                // case value is set
                if (!p_value.equals(false)) {
                    MobileSmartSourceType msst = property.getSmartType();
                    String smartValue = msst.getValue(extended);
                    // Case plain string
                    if (Mode.PLAIN.equals(msst.getMode())) {
                        if (property.getType().equalsIgnoreCase("string")) {
                            smartValue = forTemplate ? "\'" + MobileSmartSourceType.escapeStringForTpl(smartValue) + "\'" : "\'" + MobileSmartSourceType.escapeStringForTs(smartValue) + "\'";
                        }
                    }
                    // Special case for ClearDataSourceAction
                    if ("ClearDataSourceAction".equals(getActionName())) {
                        if (Mode.SOURCE.equals(msst.getMode())) {
                            MobileSmartSource mss = msst.getSmartSource();
                            if (mss != null) {
                                smartValue = mss.getSources().toString();
                            }
                        }
                    }
                    // Case ts code in HTML template (single action)
                    if (forTemplate) {
                        smartValue = "" + smartValue;
                    } else // Case ts code in ActionBeans.service (stack of actions)
                    {
                        smartValue = smartValue.replaceAll("\\?\\.", ".");
                        smartValue = smartValue.replaceAll("this\\.", "c8oPage.");
                        if (paramsPattern.matcher(smartValue).lookingAt()) {
                            smartValue = "scope." + smartValue;
                        }
                        smartValue = "get('" + p_name + "', `" + smartValue + "`)";
                    }
                    sbProps.append(smartValue);
                } else // case value is not set
                {
                    sbProps.append("null");
                }
            }
            StringBuilder sbVars = new StringBuilder();
            Iterator<UIComponent> it = getUIComponentList().iterator();
            while (it.hasNext()) {
                UIComponent component = (UIComponent) it.next();
                if (component instanceof UIControlVariable) {
                    UIControlVariable uicv = (UIControlVariable) component;
                    if (uicv.isEnabled()) {
                        // Case code generated in HTML
                        if (forTemplate) {
                            String varValue = uicv.getVarValue();
                            if (!varValue.isEmpty()) {
                                sbVars.append(sbVars.length() > 0 ? ", " : "");
                                sbVars.append(uicv.getVarName()).append(": ");
                                sbVars.append(varValue);
                            }
                        } else // Case code generated in TS
                        {
                            MobileSmartSourceType msst = uicv.getVarSmartType();
                            String smartValue = msst.getValue(extended);
                            if (Mode.PLAIN.equals(msst.getMode())) {
                                smartValue = "\'" + MobileSmartSourceType.escapeStringForTs(smartValue) + "\'";
                            }
                            smartValue = smartValue.replaceAll("\\?\\.", ".");
                            smartValue = smartValue.replaceAll("this\\.", "c8oPage.");
                            if (paramsPattern.matcher(smartValue).lookingAt()) {
                                smartValue = "scope." + smartValue;
                            }
                            if (!smartValue.isEmpty()) {
                                sbVars.append(sbVars.length() > 0 ? ", " : "");
                                sbVars.append(uicv.getVarName()).append(": ");
                                sbVars.append("get('" + uicv.getVarName() + "', `" + smartValue + "`)");
                            }
                        }
                    }
                }
            }
            return "{props:{" + sbProps + "}, vars:{" + sbVars + "}}";
        }
    }
    return "";
}
Also used : IonProperty(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonProperty) IonBean(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 20 with IonBean

use of com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean in project convertigo by convertigo.

the class UIDynamicAction method computeTemplate.

@Override
public String computeTemplate() {
    if (isEnabled()) {
        String formGroupName = null;
        if (underSubmitEvent()) {
            UIForm uiForm = getUIForm();
            if (uiForm != null) {
                formGroupName = uiForm.getFormGroupName();
            }
        }
        String scope = getScope();
        String in = formGroupName == null ? "{}" : "merge({}," + formGroupName + ".value)";
        if (isStacked()) {
            return getFunctionName() + "({root: {scope:" + scope + ", in:" + in + ", out:$event}})";
        } else {
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                String actionName = getActionName();
                String props = "{}", vars = "{}";
                String inputs = computeActionInputs(true);
                Pattern pattern = Pattern.compile("\\{props:(\\{.*\\}), vars:(\\{.*\\})\\}");
                Matcher matcher = pattern.matcher(inputs);
                if (matcher.matches()) {
                    props = matcher.group(1);
                    vars = matcher.group(2);
                }
                if (formGroupName != null) {
                    vars = "merge(merge({}," + formGroupName + ".value), " + vars + ")";
                }
                String stack = "{stack:{root: {scope:" + scope + ", in:" + in + ", out:$event}}}";
                props = "merge(merge({}," + props + "), " + stack + ")";
                if (compareToTplVersion("1.0.91") >= 0) {
                    return "resolveError(actionBeans." + actionName + "(this," + props + "," + vars + ", $event))";
                } else {
                    return "actionBeans." + actionName + "(this," + props + "," + vars + ", $event)";
                }
            }
        }
    }
    return "";
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) IonBean(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean)

Aggregations

IonBean (com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean)22 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)10 IonProperty (com.twinsoft.convertigo.beans.mobile.components.dynamic.IonProperty)8 JSONObject (org.codehaus.jettison.json.JSONObject)7 EngineException (com.twinsoft.convertigo.engine.EngineException)6 HashMap (java.util.HashMap)6 UIDynamicAction (com.twinsoft.convertigo.beans.mobile.components.UIDynamicAction)4 UISharedComponent (com.twinsoft.convertigo.beans.mobile.components.UISharedComponent)4 File (java.io.File)4 JSONException (org.codehaus.jettison.json.JSONException)4 MobileSmartSourceType (com.twinsoft.convertigo.beans.mobile.components.MobileSmartSourceType)3 UIActionStack (com.twinsoft.convertigo.beans.mobile.components.UIActionStack)3 UICompVariable (com.twinsoft.convertigo.beans.mobile.components.UICompVariable)3 UIDynamicElement (com.twinsoft.convertigo.beans.mobile.components.UIDynamicElement)3 UIUseShared (com.twinsoft.convertigo.beans.mobile.components.UIUseShared)3 Project (com.twinsoft.convertigo.beans.core.Project)2 ApplicationComponent (com.twinsoft.convertigo.beans.mobile.components.ApplicationComponent)2 UIComponent (com.twinsoft.convertigo.beans.mobile.components.UIComponent)2 UIControlVariable (com.twinsoft.convertigo.beans.mobile.components.UIControlVariable)2 UIDynamicInvoke (com.twinsoft.convertigo.beans.mobile.components.UIDynamicInvoke)2