Search in sources :

Example 16 with WalkHelper

use of com.twinsoft.convertigo.engine.helpers.WalkHelper in project convertigo by convertigo.

the class ProjectLoadingJob method run.

protected IStatus run(IProgressMonitor monitor) {
    this.monitor = monitor;
    synchronized (unloadedProjectTreeObject) {
        if (unloadedProjectTreeObject.getParent() == null) {
            Display.getDefault().asyncExec(() -> viewer.refresh());
            return Status.OK_STATUS;
        }
        try {
            int worksNumber = 2 * ConvertigoPlugin.projectManager.getNumberOfObjects(projectName);
            monitor.beginTask("Opening project " + projectName + "...", worksNumber);
            if (monitor.isCanceled()) {
                Status status = new Status(Status.CANCEL, ConvertigoPlugin.PLUGIN_UNIQUE_ID, 0, "Project " + projectName + " not loaded because of user abort", null);
                return status;
            }
            Project project;
            try {
                monitor.subTask("Importing the project...");
                Engine.theApp.databaseObjectsManager.clearCacheIfSymbolError(projectName);
                project = Engine.theApp.databaseObjectsManager.getOriginalProjectByName(projectName, false);
                if (project == null) {
                    unloadedProjectTreeObject.getParent().removeChild(unloadedProjectTreeObject);
                    Status status = new Status(Status.CANCEL, ConvertigoPlugin.PLUGIN_UNIQUE_ID, 0, "Project " + projectName + " doesn't exists", null);
                    return status;
                }
                monitor.subTask("Refreshing project ressources...");
                String projectDir = Engine.projectDir(projectName);
                ConvertigoPlugin.projectManager.getProjectExplorerView().createDir(projectName);
                ConvertigoPlugin.getDefault().createProjectPluginResource(projectName, projectDir, monitor);
                Engine.theApp.databaseObjectsManager.addDatabaseObjectListener(this);
                if (project.undefinedGlobalSymbols) {
                    synchronized (Engine.theApp.databaseObjectsManager) {
                        // parallel projects opening with undefined symbols, check after the first wizard
                        project = Engine.theApp.databaseObjectsManager.getOriginalProjectByName(projectName, false);
                        if (project.undefinedGlobalSymbols) {
                            final boolean[] created = { false };
                            new WalkHelper() {

                                boolean create = false;

                                boolean forAll = false;

                                @Override
                                protected void walk(DatabaseObject databaseObject) throws Exception {
                                    if (databaseObject.isSymbolError()) {
                                        for (Entry<String, Set<String>> entry : databaseObject.getSymbolsErrors().entrySet()) {
                                            Set<String> undefinedSymbols = Engine.theApp.databaseObjectsManager.symbolsSetCheckUndefined(entry.getValue());
                                            if (!undefinedSymbols.isEmpty()) {
                                                if (!forAll) {
                                                    boolean[] response = ConvertigoPlugin.warningGlobalSymbols(projectName, databaseObject.getName(), databaseObject.getDatabaseType(), entry.getKey(), "" + databaseObject.getCompilablePropertySourceValue(entry.getKey()), undefinedSymbols, true);
                                                    create = response[0];
                                                    forAll = response[1];
                                                    created[0] |= create;
                                                }
                                                if (create) {
                                                    Engine.theApp.databaseObjectsManager.symbolsCreateUndefined(undefinedSymbols);
                                                }
                                            }
                                        }
                                    }
                                    super.walk(databaseObject);
                                }
                            }.init(project);
                            if (created[0]) {
                                project = Engine.theApp.databaseObjectsManager.getOriginalProjectByName(projectName, false);
                            }
                        }
                    }
                }
            } finally {
                Engine.theApp.databaseObjectsManager.removeDatabaseObjectListener(this);
            }
            if (monitor.isCanceled()) {
                Status status = new Status(Status.CANCEL, ConvertigoPlugin.PLUGIN_UNIQUE_ID, 0, "Project " + projectName + " not loaded because of user abort", null);
                return status;
            }
            monitor.subTask("Creating connectors...");
            monitor.subTask("Updating tree view...");
            TreeParent invisibleRoot = unloadedProjectTreeObject.getParent();
            projectTreeObject = new ProjectTreeObject(viewer, project);
            defaultConnectorTreeObject = null;
            demoTraceTreeObject = null;
            invisibleRoot.removeChild(unloadedProjectTreeObject);
            invisibleRoot.addChild(projectTreeObject);
            ConvertigoPlugin.projectManager.setCurrentProject((ProjectTreeObject) projectTreeObject);
            ConvertigoPlugin.getDefault().getProjectPluginResource(projectName, monitor).refreshLocal(IResource.DEPTH_INFINITE, monitor);
            loadDatabaseObject(projectTreeObject, project);
            // Comment out the following line to disable the resources tree part
            // loadResource(projectTreeObject, "Resources", resourceProject.members());
            Status status = new Status(Status.OK, ConvertigoPlugin.PLUGIN_UNIQUE_ID, 0, "Project " + projectName + " loaded", null);
            return status;
        } catch (Exception e) {
            Status status = null;
            unloadedProjectTreeObject.isLoadable = false;
            if (e.getCause() instanceof ProjectInMigrationProcessException)
                status = new Status(Status.WARNING, ConvertigoPlugin.PLUGIN_UNIQUE_ID, 0, "Could not open project \"" + projectName + "\" while it was still in migration check process", e);
            else
                status = new Status(Status.ERROR, ConvertigoPlugin.PLUGIN_UNIQUE_ID, 0, "Error while loading project " + projectName, e);
            return status;
        } finally {
            // Updating the tree viewer
            Display.getDefault().asyncExec(new Runnable() {

                public void run() {
                    if (projectTreeObject != null) {
                        viewer.refresh();
                        ISelection selection = new StructuredSelection(projectTreeObject);
                        viewer.setSelection(selection, true);
                        if (defaultConnectorTreeObject != null && ConvertigoPlugin.getAutoOpenDefaultConnector())
                            defaultConnectorTreeObject.launchEditor();
                        TreeObjectEvent treeObjectEvent = null;
                        if (isCopy)
                            treeObjectEvent = new TreeObjectEvent(projectTreeObject, "name", originalName, projectName, 0);
                        else
                            treeObjectEvent = new TreeObjectEvent(projectTreeObject);
                        ConvertigoPlugin.projectManager.getProjectExplorerView().fireTreeObjectAdded(treeObjectEvent);
                        viewer.getControl().getDisplay().asyncExec(new Runnable() {

                            public void run() {
                                if (demoTraceTreeObject != null)
                                    demoTraceTreeObject.play(false);
                                Engine.execute(() -> {
                                    Project.executeAutoStartSequences(projectName);
                                });
                            }
                        });
                    }
                }
            });
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) Set(java.util.Set) ProjectInMigrationProcessException(com.twinsoft.convertigo.engine.ProjectInMigrationProcessException) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) WalkHelper(com.twinsoft.convertigo.engine.helpers.WalkHelper) CoreException(org.eclipse.core.runtime.CoreException) ProjectInMigrationProcessException(com.twinsoft.convertigo.engine.ProjectInMigrationProcessException) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException) Project(com.twinsoft.convertigo.beans.core.Project) Entry(java.util.Map.Entry) ISelection(org.eclipse.jface.viewers.ISelection) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) UnloadedProjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.UnloadedProjectTreeObject) ProjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.ProjectTreeObject)

Example 17 with WalkHelper

use of com.twinsoft.convertigo.engine.helpers.WalkHelper in project convertigo by convertigo.

the class MobileSmartSource method findDatabaseObject.

private DatabaseObject findDatabaseObject(final String dboName, final long priority) throws Exception {
    Project project = Engine.theApp.databaseObjectsManager.getOriginalProjectByName(getProjectName());
    DatabaseObject root = null;
    if (dboName != null) {
        ApplicationComponent app = (ApplicationComponent) project.getMobileApplication().getApplicationComponent();
        try {
            root = app.getPageComponentByName(dboName);
        } catch (Exception e1) {
            try {
                root = app.getMenuComponentByName(dboName);
            } catch (Exception e2) {
                try {
                    root = app;
                } catch (Exception e3) {
                    ;
                }
            }
        }
    }
    if (root == null) {
        root = project;
    }
    final List<DatabaseObject> list = new ArrayList<DatabaseObject>();
    new WalkHelper() {

        @Override
        protected void walk(DatabaseObject databaseObject) throws Exception {
            if (databaseObject.priority == priority) {
                list.add(databaseObject);
            }
            if (list.isEmpty()) {
                super.walk(databaseObject);
            }
        }
    }.init(root);
    return list.isEmpty() ? null : list.get(0);
}
Also used : Project(com.twinsoft.convertigo.beans.core.Project) ArrayList(java.util.ArrayList) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) WalkHelper(com.twinsoft.convertigo.engine.helpers.WalkHelper) InvalidSourceException(com.twinsoft.convertigo.engine.InvalidSourceException) JSONException(org.codehaus.jettison.json.JSONException)

Example 18 with WalkHelper

use of com.twinsoft.convertigo.engine.helpers.WalkHelper in project convertigo by convertigo.

the class ComponentObjectWizard method doFinish.

private void doFinish(IProgressMonitor monitor) throws CoreException {
    String dboName, name;
    boolean bContinue = true;
    int index = 0;
    try {
        newBean = getCreatedBean();
        if (newBean != null) {
            monitor.setTaskName("Object created");
            monitor.worked(1);
            dboName = newBean.getName();
            if (!StringUtils.isNormalized(dboName))
                throw new EngineException("Bean name is not normalized : \"" + dboName + "\".");
            // Verify if a child object with same name exist and change name
            while (bContinue) {
                if (index == 0)
                    name = dboName;
                else
                    name = dboName + index;
                newBean.setName(name);
                newBean.hasChanged = true;
                newBean.bNew = true;
                try {
                    new WalkHelper() {

                        boolean root = true;

                        boolean find = false;

                        @Override
                        protected boolean before(DatabaseObject dbo, Class<? extends DatabaseObject> dboClass) {
                            boolean isInstance = dboClass.isInstance(newBean);
                            find |= isInstance;
                            return isInstance;
                        }

                        @Override
                        protected void walk(DatabaseObject dbo) throws Exception {
                            if (root) {
                                root = false;
                                super.walk(dbo);
                                if (!find) {
                                    throw new EngineException("You cannot add to a " + parentObject.getClass().getSimpleName() + " a database object of type " + newBean.getClass().getSimpleName());
                                }
                            } else {
                                if (newBean.getName().equalsIgnoreCase(dbo.getName())) {
                                    throw new ObjectWithSameNameException("Unable to add the object because an object with the same name already exists in target.");
                                }
                            }
                        }
                    }.init(parentObject);
                    bContinue = false;
                } catch (ObjectWithSameNameException owsne) {
                    // Silently ignore
                    index++;
                } catch (EngineException ee) {
                    throw ee;
                } catch (Exception e) {
                    throw new EngineException("Exception in create", e);
                }
            }
            // Now add bean to target
            try {
                parentObject.add(newBean);
                monitor.setTaskName("Object added");
                monitor.worked(1);
                ConvertigoPlugin.logInfo("New object class '" + this.className + "' named '" + newBean.getName() + "' has been added");
                monitor.setTaskName("Object setted up");
                monitor.worked(1);
                bContinue = false;
            } catch (com.twinsoft.convertigo.engine.ObjectWithSameNameException owsne) {
                if (newBean instanceof HandlerStatement) {
                    throw owsne;
                }
                index++;
            }
        } else {
            throw new Exception("Could not instantiate bean!");
        }
    } catch (Exception e) {
        String message = "Unable to create a new object from class '" + this.className + "'.";
        ConvertigoPlugin.logException(e, message);
        if (objectExplorerPage != null) {
            objectExplorerPage.doCancel();
        }
        newBean = null;
    }
}
Also used : EngineException(com.twinsoft.convertigo.engine.EngineException) WalkHelper(com.twinsoft.convertigo.engine.helpers.WalkHelper) HandlerStatement(com.twinsoft.convertigo.beans.statements.HandlerStatement) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) EngineException(com.twinsoft.convertigo.engine.EngineException) ObjectWithSameNameException(com.twinsoft.convertigo.engine.ObjectWithSameNameException) ObjectWithSameNameException(com.twinsoft.convertigo.engine.ObjectWithSameNameException) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) ObjectWithSameNameException(com.twinsoft.convertigo.engine.ObjectWithSameNameException)

Example 19 with WalkHelper

use of com.twinsoft.convertigo.engine.helpers.WalkHelper 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 addMainVariable(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 getMainVariables() {
                try {
                    List<String> declarations = new ArrayList<String>();
                    List<String> functions = new ArrayList<String>();
                    String c8o_Declarations = "", c8o_Functions = "", prefix = "";
                    if (isInPage(origin)) {
                        prefix = "Page";
                        c8o_Declarations += origin.getPage().getComputedDeclarations() + System.lineSeparator();
                        String c8o_UserCustoms = origin.getPage().getScriptContent().getString();
                        c8o_Declarations += NgxBuilder.getMarker(c8o_UserCustoms, prefix + "Declaration");
                        c8o_Functions += NgxBuilder.getMarker(c8o_UserCustoms, prefix + "Function");
                    } else if (isInSharedComponent(origin)) {
                        prefix = "Comp";
                        for (UICompVariable var : origin.getSharedComponent().getVariables()) {
                            c8o_Declarations += "let " + var.getVariableName() + " = " + var.getVariableValue() + ";" + System.lineSeparator();
                        }
                        c8o_Declarations += origin.getSharedComponent().getComputedDeclarations() + System.lineSeparator();
                        String c8o_UserCustoms = origin.getSharedComponent().getScriptContent().getString();
                        c8o_Declarations += NgxBuilder.getMarker(c8o_UserCustoms, prefix + "Declaration");
                        c8o_Functions += NgxBuilder.getMarker(c8o_UserCustoms, prefix + "Function");
                    } else if (isInApplication(origin)) {
                        prefix = "App";
                        c8o_Declarations += origin.getApplication().getComputedDeclarations() + System.lineSeparator();
                        String c8o_UserCustoms = origin.getApplication().getComponentScriptContent().getString();
                        c8o_Declarations += NgxBuilder.getMarker(c8o_UserCustoms, prefix + "Declaration");
                        c8o_Functions += NgxBuilder.getMarker(c8o_UserCustoms, prefix + "Function");
                    }
                    // class variables
                    if (!c8o_Declarations.isEmpty()) {
                        for (String line : Arrays.asList(c8o_Declarations.split(System.lineSeparator()))) {
                            line = line.trim();
                            if (!line.isEmpty() && line.indexOf(prefix + "Declaration") == -1) {
                                if (line.indexOf(prefix + "@ViewChild") != -1 || line.indexOf(prefix + "@ViewChildren") != -1) {
                                    line = line.substring(line.indexOf(')'));
                                }
                                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);
                                }
                                addMainVariable(var_name, var_value);
                            }
                        }
                    }
                    // class functions
                    if (!c8o_Functions.isEmpty()) {
                        c8o_Functions = simplify(c8o_Functions);
                        for (String line : Arrays.asList(c8o_Functions.split(System.lineSeparator()))) {
                            line = line.trim();
                            if (!line.isEmpty() && line.indexOf(prefix + "Function") == -1) {
                                // "((\\w+)\\s*(\\([^\\(]*\\))\\s*\\{)"
                                Matcher matcher = d_func.matcher(line);
                                while (matcher.find()) {
                                    functions.add(matcher.group(2));
                                }
                            }
                        }
                        for (String func_name : functions) {
                            addMainVariable(func_name, "() => {}");
                        }
                    }
                } 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;
                        addMainVariable(item, "[]");
                        addMapVariable(item, item, "this._params_." + item);
                        addMapVariable(item, uicd.toString() + " : found variable which stands for the iterator's item");
                        String itemName = uicd.getDirectiveItemName();
                        addMainVariable(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();
                        addMainVariable(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);
                                    addMainVariable(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);
                                    addMainVariable(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;
                }
                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;
                try {
                    if (Mode.SCRIPT.equals(msst.getMode())) {
                        s = msst.getValue(extended);
                    }
                    if (Mode.SOURCE.equals(msst.getMode())) {
                        s = msst.getSmartSource().toJsonString();
                    }
                } catch (Exception e) {
                }
                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 {
                getMainVariables();
                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()) {
                        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("NgxSmartSourcePropertyDescriptor")) {
                                    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.ngx.components.UICompVariable) IonProperty(com.twinsoft.convertigo.beans.ngx.components.dynamic.IonProperty) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MobileSmartSourceType(com.twinsoft.convertigo.beans.ngx.components.MobileSmartSourceType) IonBean(com.twinsoft.convertigo.beans.ngx.components.dynamic.IonBean) UIComponent(com.twinsoft.convertigo.beans.ngx.components.UIComponent) UIControlDirective(com.twinsoft.convertigo.beans.ngx.components.UIControlDirective) UIDynamicElement(com.twinsoft.convertigo.beans.ngx.components.UIDynamicElement) WalkHelper(com.twinsoft.convertigo.engine.helpers.WalkHelper) Method(java.lang.reflect.Method) 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 20 with WalkHelper

use of com.twinsoft.convertigo.engine.helpers.WalkHelper in project convertigo by convertigo.

the class ComponentObjectWizard method doFinish.

private void doFinish(IProgressMonitor monitor) throws CoreException {
    String dboName, name;
    boolean bContinue = true;
    int index = 0;
    try {
        newBean = getCreatedBean();
        if (newBean != null) {
            monitor.setTaskName("Object created");
            monitor.worked(1);
            dboName = newBean.getName();
            if (!StringUtils.isNormalized(dboName))
                throw new EngineException("Bean name is not normalized : \"" + dboName + "\".");
            // Verify if a child object with same name exist and change name
            while (bContinue) {
                if (index == 0)
                    name = dboName;
                else
                    name = dboName + index;
                newBean.setName(name);
                newBean.hasChanged = true;
                newBean.bNew = true;
                try {
                    new WalkHelper() {

                        boolean root = true;

                        boolean find = false;

                        @Override
                        protected boolean before(DatabaseObject dbo, Class<? extends DatabaseObject> dboClass) {
                            boolean isInstance = dboClass.isInstance(newBean);
                            find |= isInstance;
                            return isInstance;
                        }

                        @Override
                        protected void walk(DatabaseObject dbo) throws Exception {
                            if (root) {
                                root = false;
                                super.walk(dbo);
                                if (!find) {
                                    throw new EngineException("You cannot add to a " + newBean.getClass().getSimpleName() + " a database object of type " + parentObject.getClass().getSimpleName());
                                }
                            } else {
                                if (newBean.getName().equalsIgnoreCase(dbo.getName())) {
                                    throw new ObjectWithSameNameException("Unable to add the object because an object with the same name already exists in target.");
                                }
                            }
                        }
                    }.init(parentObject);
                    bContinue = false;
                } catch (ObjectWithSameNameException owsne) {
                    // Silently ignore
                    index++;
                } catch (EngineException ee) {
                    throw ee;
                } catch (Exception e) {
                    throw new EngineException("Exception in create", e);
                }
            }
            // Now add bean to target
            try {
                parentObject.add(newBean);
                monitor.setTaskName("Object added");
                monitor.worked(1);
                ConvertigoPlugin.logInfo("New object class '" + this.className + "' named '" + newBean.getName() + "' has been added");
                monitor.setTaskName("Object setted up");
                monitor.worked(1);
                bContinue = false;
            } catch (com.twinsoft.convertigo.engine.ObjectWithSameNameException owsne) {
                if (newBean instanceof HandlerStatement) {
                    throw owsne;
                }
                index++;
            }
        } else {
            throw new Exception("Could not instantiate bean!");
        }
    } catch (Exception e) {
        String message = "Unable to create a new object from class '" + this.className + "'.";
        ConvertigoPlugin.logException(e, message);
        if (objectExplorerPage != null) {
            objectExplorerPage.doCancel();
        }
        newBean = null;
    }
}
Also used : EngineException(com.twinsoft.convertigo.engine.EngineException) WalkHelper(com.twinsoft.convertigo.engine.helpers.WalkHelper) HandlerStatement(com.twinsoft.convertigo.beans.statements.HandlerStatement) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) EngineException(com.twinsoft.convertigo.engine.EngineException) ObjectWithSameNameException(com.twinsoft.convertigo.engine.ObjectWithSameNameException) ObjectWithSameNameException(com.twinsoft.convertigo.engine.ObjectWithSameNameException) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) ObjectWithSameNameException(com.twinsoft.convertigo.engine.ObjectWithSameNameException)

Aggregations

WalkHelper (com.twinsoft.convertigo.engine.helpers.WalkHelper)26 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)25 Project (com.twinsoft.convertigo.beans.core.Project)14 EngineException (com.twinsoft.convertigo.engine.EngineException)10 IOException (java.io.IOException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 CoreException (org.eclipse.core.runtime.CoreException)9 ArrayList (java.util.ArrayList)8 Transaction (com.twinsoft.convertigo.beans.core.Transaction)7 Step (com.twinsoft.convertigo.beans.core.Step)6 JSONException (org.codehaus.jettison.json.JSONException)6 Connector (com.twinsoft.convertigo.beans.core.Connector)5 Criteria (com.twinsoft.convertigo.beans.core.Criteria)5 ScreenClass (com.twinsoft.convertigo.beans.core.ScreenClass)5 Sequence (com.twinsoft.convertigo.beans.core.Sequence)5 Sheet (com.twinsoft.convertigo.beans.core.Sheet)5 Statement (com.twinsoft.convertigo.beans.core.Statement)5 TestCase (com.twinsoft.convertigo.beans.core.TestCase)5 TreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject)5 HashMap (java.util.HashMap)5