Search in sources :

Example 1 with IonBean

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

the class SharedComponentWizard method scanForVariables.

private void scanForVariables(final UIComponent origin) throws Exception {
    final Set<String> identifierSet = new HashSet<String>();
    try {
        new WalkHelper() {

            private void addDeclaration(String var_name, String var_value) {
                if (var_name != null && !var_name.isEmpty() && !main_map.containsKey(var_name)) {
                    main_map.put(var_name, var_value == null ? "''" : var_value);
                }
            }

            private void getMainDeclarations() {
                try {
                    List<String> declarations = new ArrayList<String>();
                    String c8o_Declarations = "", markerId = "";
                    if (isInPage(origin)) {
                        markerId = "PageDeclaration";
                        String c8o_UserCustoms = origin.getPage().getScriptContent().getString();
                        c8o_Declarations = Ionic3Builder.getMarker(c8o_UserCustoms, markerId);
                    } else if (isInSharedComponent(origin)) {
                        markerId = "SharedCompDeclaration";
                        UISharedComponent uisc = origin.getSharedComponent();
                        for (UICompVariable var : uisc.getVariables()) {
                            c8o_Declarations += "let " + var.getVariableName() + " = " + var.getVariableValue() + ";" + System.lineSeparator();
                        }
                    } else if (isInApplication(origin)) {
                        markerId = "AppDeclaration";
                        String c8o_UserCustoms = origin.getApplication().getComponentScriptContent().getString();
                        c8o_Declarations = Ionic3Builder.getMarker(c8o_UserCustoms, markerId);
                    }
                    if (!c8o_Declarations.isEmpty()) {
                        for (String line : Arrays.asList(c8o_Declarations.split(System.lineSeparator()))) {
                            line = line.trim();
                            if (!line.isEmpty() && line.indexOf(markerId) == -1) {
                                declarations.add(line);
                            }
                        }
                    }
                    for (String line : declarations) {
                        // "(((\\w+)\\s(\\w+)([^\\=]+))(\\=([^\\=]+))?)"
                        Matcher matcher = d_var.matcher(line);
                        while (matcher.find()) {
                            String var_name = matcher.group(4);
                            String var_value = matcher.group(7);
                            if (var_value != null) {
                                var_value = var_value.trim();
                                if (var_value.charAt(var_value.length() - 1) == ';') {
                                    var_value = var_value.substring(0, var_value.length() - 1);
                                }
                                var_value = escapeString(var_value);
                            }
                            addDeclaration(var_name, var_value);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            private boolean isInForDirective(UIComponent uic) {
                return getForDirective(uic) != null;
            }

            private UIControlDirective getForDirective(UIComponent uic) {
                DatabaseObject databaseObject = uic;
                while (databaseObject != null && (!(databaseObject instanceof UIControlDirective) || !AttrDirective.ForEach.name().equals(((UIControlDirective) databaseObject).getDirectiveName()))) {
                    databaseObject = databaseObject.getParent();
                }
                if (databaseObject == null)
                    return null;
                else
                    return (UIControlDirective) databaseObject;
            }

            private void getForDirectiveVariables(UIComponent uic) {
                UIComponent uicomponent = uic;
                while (isInForDirective(uicomponent)) {
                    UIControlDirective uicd = getForDirective(uicomponent);
                    if (!uic.equals(uicd)) {
                        String item = "item" + uicd.priority;
                        addDeclaration(item, "[]");
                        addMapVariable(item, item, "this._params_." + item);
                        addMapVariable(item, uicd.toString() + " : found  variable which stands for the iterator's item");
                        String itemName = uicd.getDirectiveItemName();
                        addDeclaration(itemName, "{}");
                        addMapVariable(itemName, itemName, "this._params_." + itemName);
                        addMapVariable(itemName, uicd.toString() + " : found variable which stands for the customized iterator's item");
                        String indexName = uicd.getDirectiveIndexName();
                        addDeclaration(indexName, "0");
                        addMapVariable(indexName, indexName, "this._params_." + indexName);
                        addMapVariable(indexName, uicd.toString() + " : found variable which stands for the customized iterator's index");
                        String expression = uicd.getDirectiveExpression();
                        if (!expression.isEmpty()) {
                            Matcher matcher = null;
                            List<String> list = Arrays.asList(expression.split("\\;"));
                            for (String s : list) {
                                matcher = d_var_let.matcher(s);
                                while (matcher.find()) {
                                    String expvar = matcher.group(3);
                                    addDeclaration(expvar, "''");
                                    addMapVariable(expvar, expvar, "this._params_." + expvar);
                                    addMapVariable(expvar, uicd.toString() + " : found variable used by the customized iterator's expression");
                                }
                                matcher = d_var_as.matcher(s);
                                while (matcher.find()) {
                                    String expvar = matcher.group(4);
                                    addDeclaration(expvar, "''");
                                    addMapVariable(expvar, expvar, "this._params_." + expvar);
                                    addMapVariable(expvar, uicd.toString() + " : found variable used by the customized iterator's expression");
                                }
                            }
                        }
                    }
                    DatabaseObject dbo = uicd.getParent();
                    uicomponent = dbo instanceof UIComponent ? (UIComponent) dbo : null;
                }
            }

            private boolean checkVariable(String name) {
                if (name == null || name.isEmpty())
                    return false;
                if (identifierSet.contains(name)) {
                    return false;
                }
                if ("global".equals(name))
                    return false;
                if ("router".equals(name))
                    return false;
                return true;
            }

            private void addMapVariable(String name, String target, String replacement) {
                if (checkVariable(name)) {
                    String normalized_name = StringUtils.normalize(name);
                    String var_name = normalized_name;
                    if (ovarMap.containsKey(var_name)) {
                    // System.out.println("var_name: "+ var_name + " already in ovarMap");
                    } else {
                        ovarMap.put(var_name, new HashMap<String, String>());
                    }
                    ovarMap.get(var_name).put(target, replacement.replace("_params_." + name, "_params_.") + var_name);
                }
            }

            private void addMapVariable(String name, String infos) {
                if (checkVariable(name)) {
                    String normalized_name = StringUtils.normalize(name);
                    String var_name = normalized_name;
                    if (infoMap.containsKey(var_name)) {
                    // System.out.println("var_name: "+ var_name + " already in infoMap");
                    } else {
                        infoMap.put(var_name, infos);
                    }
                }
            }

            private void scanSmartSource(UIComponent uic, String p_name, MobileSmartSourceType msst) throws Exception {
                boolean extended = !forTemplate(uic);
                String s = null;
                if (Mode.SCRIPT.equals(msst.getMode())) {
                    s = msst.getValue(extended);
                }
                if (Mode.SOURCE.equals(msst.getMode())) {
                    s = msst.getSmartSource().toJsonString();
                }
                if (s != null) {
                    String infos = uic.toString() + " : found variable used by '" + p_name + "' property";
                    Matcher matcher = p_var.matcher(s);
                    while (matcher.find()) {
                        String group1 = matcher.group(1);
                        String group2 = matcher.group(2);
                        // String group3 = matcher.group(3);
                        String group4 = matcher.group(4);
                        String name = group4;
                        String target = group1;
                        String replacement = group2 + "._params_." + name;
                        if (isInControlEvent(uic)) {
                            if (forTemplate(uic)) {
                                replacement = "_params_." + name;
                            } else {
                                replacement = "scope._params_." + name;
                            }
                        }
                        addMapVariable(name, target, replacement);
                        addMapVariable(name, infos);
                    }
                }
            }

            @Override
            public void init(DatabaseObject databaseObject) throws Exception {
                getMainDeclarations();
                if (isInForDirective(origin)) {
                    getForDirectiveVariables(origin);
                }
                super.init(databaseObject);
            }

            @Override
            protected void walk(DatabaseObject databaseObject) throws Exception {
                if (databaseObject instanceof UIComponent) {
                    UIComponent uic = (UIComponent) databaseObject;
                    if (uic.isEnabled() && !isInControlEvent(uic)) {
                        if (databaseObject instanceof UIDynamicElement) {
                            String identifier = ((UIDynamicElement) databaseObject).getIdentifier();
                            if (!identifier.isEmpty()) {
                                identifierSet.add(identifier);
                            }
                        }
                        for (java.beans.PropertyDescriptor pd : CachedIntrospector.getBeanInfo(databaseObject).getPropertyDescriptors()) {
                            if (pd.getPropertyEditorClass() != null) {
                                if (pd.getPropertyEditorClass().getSimpleName().equals("MobileSmartSourcePropertyDescriptor")) {
                                    Method getter = pd.getReadMethod();
                                    Object value = getter.invoke(databaseObject, new Object[] {});
                                    if (value != null && value instanceof MobileSmartSourceType) {
                                        MobileSmartSourceType msst = (MobileSmartSourceType) value;
                                        if (Mode.SCRIPT.equals(msst.getMode()) || Mode.SOURCE.equals(msst.getMode())) {
                                            scanSmartSource(uic, pd.getName(), msst);
                                        }
                                    }
                                }
                            }
                        }
                        if (databaseObject instanceof UIDynamicElement) {
                            UIDynamicElement uide = (UIDynamicElement) databaseObject;
                            IonBean ionBean = uide.getIonBean();
                            if (ionBean != null) {
                                for (IonProperty property : ionBean.getProperties().values()) {
                                    Object p_value = property.getValue();
                                    if (!p_value.equals(false)) {
                                        MobileSmartSourceType msst = property.getSmartType();
                                        if (Mode.SCRIPT.equals(msst.getMode()) || Mode.SOURCE.equals(msst.getMode())) {
                                            scanSmartSource(uide, property.getName(), msst);
                                        }
                                    }
                                }
                            }
                        }
                        super.walk(databaseObject);
                    }
                }
            }
        }.init(origin);
    } catch (Exception e) {
        throw new Exception("Unable to scan for variables", e);
    }
}
Also used : UICompVariable(com.twinsoft.convertigo.beans.mobile.components.UICompVariable) IonProperty(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonProperty) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MobileSmartSourceType(com.twinsoft.convertigo.beans.mobile.components.MobileSmartSourceType) IonBean(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean) UIComponent(com.twinsoft.convertigo.beans.mobile.components.UIComponent) UIControlDirective(com.twinsoft.convertigo.beans.mobile.components.UIControlDirective) UIDynamicElement(com.twinsoft.convertigo.beans.mobile.components.UIDynamicElement) WalkHelper(com.twinsoft.convertigo.engine.helpers.WalkHelper) Method(java.lang.reflect.Method) UISharedComponent(com.twinsoft.convertigo.beans.mobile.components.UISharedComponent) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) ArrayList(java.util.ArrayList) List(java.util.List) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) HashSet(java.util.HashSet)

Example 2 with IonBean

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

the class UIDynamicAction method computeActionContent.

protected String computeActionContent() {
    if (isEnabled()) {
        IonBean ionBean = getIonBean();
        if (ionBean != null) {
            int numThen = numberOfActions();
            String actionName = getActionName();
            String inputs = computeActionInputs(false);
            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 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 (!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\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();
            if ("InvokeAction".equals(ionBean.getName())) {
                if (isBroken()) {
                    tsCode += "\t\treturn this.actionBeans." + actionName + "(this, {...self.in.props, ...{message: 'Invoke source is broken'}}, " + "{...stack[\"root\"].in, ...self.in.vars})" + System.lineSeparator();
                } else {
                    if (getSharedAction() != null) {
                        tsCode += "\t\treturn this.actionBeans." + actionName + "(this, {...{stack: stack, parent: parent, out: out}, ...self.in.props}, " + "{...stack[\"root\"].in, ...params, ...self.in.vars}, event)" + System.lineSeparator();
                    } else {
                        tsCode += "\t\treturn this.actionBeans." + actionName + "(this, {...{stack: stack, parent: parent, out: out}, ...self.in.props}, " + "{...stack[\"root\"].in, ...self.in.vars}, event)" + System.lineSeparator();
                    }
                }
            } else {
                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();
            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();
            }
            if ("IfAction".equals(ionBean.getName())) {
                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();
            } else {
                tsCode += "\t\t}, (error: any) => {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 3 with IonBean

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

the class UIDynamicComponent method getContributor.

/* (non-Javadoc)
	 * @see com.twinsoft.convertigo.beans.mobile.components.UIDynamicElement#getContributor()
	 */
@Override
protected Contributor getContributor() {
    Contributor contributor = super.getContributor();
    return new Contributor() {

        @Override
        public Map<String, String> getActionTsFunctions() {
            return contributor.getActionTsFunctions();
        }

        @Override
        public Map<String, String> getActionTsImports() {
            return contributor.getActionTsImports();
        }

        @Override
        public Map<String, File> getCompBeanDir() {
            Map<String, File> map = new HashMap<String, File>();
            IonBean ionBean = getIonBean();
            if (ionBean != null) {
                String compName = ionBean.getName();
                File dir = ComponentManager.getCompBeanDir(compName);
                if (dir != null) {
                    map.put(compName, dir);
                }
            }
            return map;
        }

        @Override
        public Map<String, String> getModuleTsImports() {
            return contributor.getModuleTsImports();
        }

        @Override
        public Set<String> getModuleNgImports() {
            return contributor.getModuleNgImports();
        }

        @Override
        public Set<String> getModuleNgProviders() {
            return contributor.getModuleNgProviders();
        }

        @Override
        public Set<String> getModuleNgDeclarations() {
            return contributor.getModuleNgDeclarations();
        }

        @Override
        public Set<String> getModuleNgComponents() {
            return contributor.getModuleNgComponents();
        }

        @Override
        public Map<String, String> getPackageDependencies() {
            return contributor.getPackageDependencies();
        }

        @Override
        public Map<String, String> getConfigPlugins() {
            return contributor.getConfigPlugins();
        }
    };
}
Also used : HashMap(java.util.HashMap) IonBean(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean) File(java.io.File)

Example 4 with IonBean

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

the class UIDynamicAction method computeJsonModel.

@Override
public String computeJsonModel() {
    JSONObject jsonModel = new JSONObject();
    // if (isEnabled()) {
    try {
        jsonModel.put("in", new JSONObject().put("props", new JSONObject()).put("vars", new JSONObject())).put("out", new JSONObject());
        IonBean ionBean = getIonBean();
        if (ionBean != null) {
            JSONObject jsonProps = jsonModel.getJSONObject("in").getJSONObject("props");
            jsonProps.put("tplVersion", "");
            jsonProps.put("actionName", "");
            jsonProps.put("actionFunction", "");
            for (IonProperty property : ionBean.getProperties().values()) {
                jsonProps.put(property.getName(), "");
            }
        }
        JSONObject jsonVars = jsonModel.getJSONObject("in").getJSONObject("vars");
        Iterator<UIComponent> it = getUIComponentList().iterator();
        while (it.hasNext()) {
            UIComponent component = (UIComponent) it.next();
            if (component instanceof UIControlVariable) {
                UIControlVariable var = (UIControlVariable) component;
                jsonVars.put(var.getVarName(), "");
            }
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // }
    return jsonModel.toString();
}
Also used : IonProperty(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonProperty) JSONObject(org.codehaus.jettison.json.JSONObject) IonBean(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean) JSONException(org.codehaus.jettison.json.JSONException)

Example 5 with IonBean

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

the class UIDynamicAction method addInfos.

@Override
protected void addInfos(Set<UIComponent> done, Map<String, Set<String>> infoMap) {
    super.addInfos(done, infoMap);
    IonBean ionBean = getIonBean();
    if (ionBean != null) {
        String beanName = ionBean.getName();
        if (ionBean.hasProperty("marker")) {
            JSONObject json = new JSONObject();
            String key = null;
            for (IonProperty property : ionBean.getProperties().values()) {
                MobileSmartSourceType msst = property.getSmartType();
                String p_name = property.getName();
                Object p_value = property.getValue();
                if (!p_value.equals(false)) {
                    if (beanName.equals("FullSyncViewAction")) {
                        if (p_name.equals("fsview")) {
                            key = p_value.toString() + ".view";
                        }
                    } else if (beanName.equals("FullSyncGetAction")) {
                        if (p_name.equals("requestable")) {
                            key = p_value.toString() + ".get";
                        }
                    } else if (beanName.equals("CallSequenceAction")) {
                        if (p_name.equals("requestable")) {
                            key = p_value.toString();
                        }
                    } else if (beanName.equals("CallFullSyncAction")) {
                        if (p_name.equals("requestable")) {
                            key = p_value.toString();
                            Object p_verb = ionBean.getProperty("verb").getValue();
                            if (!p_verb.equals(false)) {
                                key += "." + p_verb.toString();
                            }
                        }
                    }
                }
                try {
                    if (p_name.equals("marker")) {
                        json.put(p_name, !p_value.equals(false) ? msst.getValue() : "");
                    }
                    if (p_name.equals("include_docs")) {
                        json.put(p_name, !p_value.equals(false) ? msst.getValue() : "false");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            if (key != null && !key.isEmpty()) {
                Set<String> infos = infoMap.get(key);
                if (infos == null) {
                    infos = new HashSet<String>();
                }
                String info = json.toString();
                if (!info.isEmpty()) {
                    infos.add(info);
                }
                infoMap.put(key, infos);
            }
        }
    }
}
Also used : IonProperty(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonProperty) JSONObject(org.codehaus.jettison.json.JSONObject) IonBean(com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean) JSONException(org.codehaus.jettison.json.JSONException) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) JSONObject(org.codehaus.jettison.json.JSONObject)

Aggregations

IonBean (com.twinsoft.convertigo.beans.mobile.components.dynamic.IonBean)21 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