Search in sources :

Example 6 with IonProperty

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

the class UIDynamicElement method initAttributes.

@Override
protected StringBuilder initAttributes() {
    StringBuilder attributes = super.initAttributes();
    IonBean ionBean = getIonBean();
    if (ionBean != null) {
        if (is("Tabs")) {
            attributes.append(" #c8oTabs");
        }
        String formControlVarName = getFormControlName();
        Map<String, String> vm = new HashMap<String, String>();
        for (IonProperty property : ionBean.getProperties().values()) {
            String name = property.getName();
            Object value = property.getValue();
            if (!value.equals(false)) {
                MobileSmartSourceType msst = property.getSmartType();
                String smartValue = msst.getValue();
                if (Mode.PLAIN.equals(msst.getMode())) {
                    if (property.getType().equalsIgnoreCase("string")) {
                        if (!isComposedValue(smartValue)) {
                            smartValue = "\'" + MobileSmartSourceType.escapeStringForTpl(smartValue) + "\'";
                        }
                    }
                }
                vm.put(name, smartValue);
            } else {
                vm.put(name, "null");
            }
        }
        StrSubstitutor sub = new StrSubstitutor(vm, "$[", "]");
        sub.setEnableSubstitutionInVariables(true);
        for (IonProperty property : ionBean.getProperties().values()) {
            String name = property.getName();
            String attr = property.getAttr();
            Object value = property.getValue();
            boolean isComposite = property.isComposite();
            // case value is set
            if (!value.equals(false)) {
                if (name.equals("AutoDisable")) {
                    UIForm form = getUIForm();
                    if (form != null) {
                        String formGroupName = form.getFormGroupName();
                        attributes.append(" [disabled]=\"!").append(formGroupName).append(".valid\"");
                    }
                } else {
                    if (name.equals("FormControlName") && formControlVarName.isEmpty()) {
                        continue;
                    }
                    if (attr.equals("[ngModel]")) {
                        String tagName = getTagName();
                        if (tagName.equals("ion-checkbox") || tagName.equals("ion-toggle")) {
                            attr = formControlVarName.isEmpty() ? "[checked]" : "[ngModel]";
                        } else if (tagName.equals("ion-datetime")) {
                            attr = formControlVarName.isEmpty() ? "value" : "[ngModel]";
                        }
                    }
                    String smartValue = property.getSmartValue();
                    smartValue = sub.replace(smartValue);
                    if (!isComposite) {
                        attributes.append(" ");
                        if (attr.isEmpty()) {
                            attributes.append(smartValue);
                        } else if (attr.indexOf("%%") != -1) {
                            attributes.append(attr.replaceFirst("%%", smartValue));
                        } else {
                            attributes.append(attr).append("=");
                            attributes.append("\"").append(smartValue).append("\"");
                        }
                    }
                }
            } else // case value is not set
            {
                if (!isComposite) {
                    if (attr.equals("[ngModel]")) {
                        String tagName = getTagName();
                        if (formControlVarName.isEmpty() || tagName.equals("ion-checkbox") || tagName.equals("ion-toggle")) {
                            continue;
                        } else {
                            attributes.append(" [ngModel]=\"null\"");
                        }
                    }
                }
            }
        }
    }
    return attributes;
}
Also used : StrSubstitutor(org.apache.commons.text.StrSubstitutor) IonProperty(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonProperty) HashMap(java.util.HashMap) IonBean(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 7 with IonProperty

use of com.twinsoft.convertigo.beans.mobile.components.dynamic.IonProperty 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 8 with IonProperty

use of com.twinsoft.convertigo.beans.mobile.components.dynamic.IonProperty 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 9 with IonProperty

use of com.twinsoft.convertigo.beans.mobile.components.dynamic.IonProperty 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)

Aggregations

IonProperty (com.twinsoft.convertigo.beans.mobile.components.dynamic.IonProperty)9 IonBean (com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean)8 JSONObject (org.codehaus.jettison.json.JSONObject)7 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)5 EngineException (com.twinsoft.convertigo.engine.EngineException)3 HashMap (java.util.HashMap)3 JSONException (org.codehaus.jettison.json.JSONException)3 UIDynamicElement (com.twinsoft.convertigo.beans.mobile.components.UIDynamicElement)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 CoreException (org.eclipse.core.runtime.CoreException)2 EvaluatorException (org.mozilla.javascript.EvaluatorException)2 JavaScriptException (org.mozilla.javascript.JavaScriptException)2 MobileSmartSourceType (com.twinsoft.convertigo.beans.mobile.components.MobileSmartSourceType)1 UICompVariable (com.twinsoft.convertigo.beans.mobile.components.UICompVariable)1 UIComponent (com.twinsoft.convertigo.beans.mobile.components.UIComponent)1 UIControlDirective (com.twinsoft.convertigo.beans.mobile.components.UIControlDirective)1 UISharedComponent (com.twinsoft.convertigo.beans.mobile.components.UISharedComponent)1