Search in sources :

Example 21 with IonBean

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

the class UIDynamicIterate method computeActionContent.

protected String computeActionContent() {
    if (isEnabled()) {
        IonBean ionBean = getIonBean();
        if (ionBean != null) {
            int numThen = numberOfActions();
            String actionName = getActionName();
            String inputs = computeActionInputs(false);
            StringBuilder sbLoop = 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 sLoop = "", 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 UIActionLoopEvent) {
                        sLoop = ((UIActionLoopEvent) component).computeEvent();
                    }
                    if (!sLoop.isEmpty()) {
                        sbLoop.append(sLoop);
                    }
                    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();
            if (sbLoop.length() > 0) {
                tsCode += sbLoop.toString();
            } else {
                tsCode += "\t\tconst doLoop = (c8oPage : C8oPageBase, item : any, index : number) : Promise<any> => {" + System.lineSeparator();
                tsCode += "\t\treturn Promise.reject('no loop handler');" + System.lineSeparator();
                tsCode += "\t\t}" + 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}, doLoop)" + 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();
            }
            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 22 with IonBean

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

the class UIDynamicSwitch method computeActionContent.

protected String computeActionContent() {
    if (isEnabled()) {
        IonBean ionBean = getIonBean();
        if (ionBean != null) {
            int numThen = numberOfActions();
            String actionName = getActionName();
            String inputs = computeActionInputs(false);
            StringBuilder sbCase = new StringBuilder();
            StringBuilder sbCatch = new StringBuilder();
            StringBuilder sbThen = new StringBuilder();
            List<String> passthroughCases = new ArrayList<String>();
            Iterator<UIComponent> it = getUIComponentList().iterator();
            while (it.hasNext()) {
                UIComponent component = (UIComponent) it.next();
                if (component.isEnabled()) {
                    String sCase = "", 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 UIActionCaseEvent) {
                        UIActionCaseEvent caseEvent = (UIActionCaseEvent) component;
                        String caseFn = caseEvent.getCaseFn();
                        String caseValue = caseEvent.getCaseValue();
                        int len = caseValue.length();
                        char c1 = caseValue.charAt(0);
                        char c2 = caseValue.charAt(len - 1);
                        if (c1 == c2 && Character.toString(c1).matches("[\'\"]")) {
                            caseValue = "'" + escapeStringForTs(caseValue.substring(1, len - 1)) + "'";
                        } else {
                            caseValue = escapeStringForTs(caseValue);
                            caseValue = "'" + caseValue + "'";
                        }
                        if (caseEvent.isEnabled()) {
                            sCase += System.lineSeparator();
                            sCase += caseEvent.computeEvent();
                            sCase += "\t\tcases[" + caseValue + "] = cases[" + caseValue + "] || []" + System.lineSeparator();
                            sCase += "\t\tcases[" + caseValue + "].push(" + caseFn + ")" + System.lineSeparator();
                            for (String cv : passthroughCases) {
                                if (!cv.equals(caseValue)) {
                                    sCase += "\t\tcases[" + cv + "].push(" + caseFn + ")" + System.lineSeparator();
                                }
                            }
                            if (caseEvent.isPassThrough()) {
                                if (!passthroughCases.contains(caseValue)) {
                                    passthroughCases.add(caseValue);
                                }
                            } else {
                                passthroughCases.clear();
                            }
                        }
                    }
                    if (!sCase.isEmpty()) {
                        sbCase.append(sCase);
                    }
                    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\t" + "let cases = {};" + System.lineSeparator();
            if (sbCase.length() > 0) {
                tsCode += sbCase.toString();
            } else {
                // does nothing
                ;
            }
            tsCode += "\t\t" + System.lineSeparator();
            tsCode += "\t\tlet self: any = stack[\"" + getName() + "\"] = stack[\"" + priority + "\"] = {};" + System.lineSeparator();
            tsCode += "\t\tself.in = " + inputs + ";" + System.lineSeparator();
            // "(this, self.in.props, {...stack[\"root\"].in, ...self.in.vars}, cases)"+ System.lineSeparator();
            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}, cases)" + 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}, cases)" + 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();
            }
            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) ArrayList(java.util.ArrayList)

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