Search in sources :

Example 6 with JSONXPathLoopDescriptor

use of org.talend.repository.model.json.JSONXPathLoopDescriptor in project tdi-studio-se by Talend.

the class JSONConnectionContextHelper method revertJSONFilePropertiesForContextMode.

static void revertJSONFilePropertiesForContextMode(JSONFileConnection conn, ContextType contextType) {
    if (conn == null || contextType == null) {
        return;
    }
    if (!conn.isInputModel()) {
        String outputFilePath = getOriginalValue(contextType, conn.getOutputFilePath());
        outputFilePath = TalendQuoteUtils.removeQuotes(outputFilePath);
        conn.setOutputFilePath(outputFilePath);
    } else {
        String filePath = getOriginalValue(contextType, conn.getJSONFilePath());
        String encoding = getOriginalValue(contextType, conn.getEncoding());
        filePath = TalendQuoteUtils.removeQuotes(filePath);
        conn.setJSONFilePath(filePath);
        encoding = TalendQuoteUtils.removeQuotes(encoding);
        conn.setEncoding(encoding);
        EList schema = conn.getSchema();
        if (schema != null) {
            if (schema.get(0) instanceof JSONXPathLoopDescriptor) {
                JSONXPathLoopDescriptor descriptor = (JSONXPathLoopDescriptor) schema.get(0);
                String xPahtQuery = getOriginalValue(contextType, descriptor.getAbsoluteXPathQuery());
                xPahtQuery = TalendQuoteUtils.removeQuotes(xPahtQuery);
                descriptor.setAbsoluteXPathQuery(xPahtQuery);
            }
        }
    }
}
Also used : JSONXPathLoopDescriptor(org.talend.repository.model.json.JSONXPathLoopDescriptor) EList(org.eclipse.emf.common.util.EList)

Example 7 with JSONXPathLoopDescriptor

use of org.talend.repository.model.json.JSONXPathLoopDescriptor in project tdi-studio-se by Talend.

the class JSONWizard method performFinish.

/**
     * This method determine if the 'Finish' button is enable This method is called when 'Finish' button is pressed in
     * the wizard. We will create an operation and run it using wizard as execution context.
     */
@Override
public boolean performFinish() {
    boolean formIsPerformed = false;
    IWizardPage finalPage = getCurrentPage();
    if (finalPage == null) {
        finalPage = propertiesWizardPage;
    }
    // deleteTemFile();
    if (connection.isInputModel()) {
        if (finalPage instanceof JSONFileWizardPage) {
            int step = ((JSONFileWizardPage) finalPage).step;
            if (step == 2) {
                formIsPerformed = finalPage.isPageComplete();
                if (formIsPerformed) {
                    List schemas = connection.getSchema();
                    Set tables = ConnectionHelper.getTables(connection);
                    if (!schemas.isEmpty() && !tables.isEmpty()) {
                        JSONXPathLoopDescriptor currentSchema = (JSONXPathLoopDescriptor) schemas.get(0);
                        MetadataTable currentTable = (MetadataTable) tables.toArray(new MetadataTable[0])[0];
                        if (!currentSchema.getAbsoluteXPathQuery().equals(oldAbstractQueryPath)) {
                            resetMetadata(currentSchema.getSchemaTargets(), true);
                        } else {
                            resetMetadata(currentSchema.getSchemaTargets(), false);
                        }
                    }
                }
            } else {
                formIsPerformed = finalPage.isPageComplete();
            }
        } else {
            formIsPerformed = finalPage.isPageComplete();
        }
    } else {
        formIsPerformed = finalPage.isPageComplete();
    }
    if (formIsPerformed) {
        final IProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
        final IWorkspaceRunnable op = new IWorkspaceRunnable() {

            @Override
            public void run(IProgressMonitor monitor) throws CoreException {
                PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {

                    @Override
                    public void run() {
                        if (creation) {
                            String nextId = factory.getNextId();
                            connectionProperty.setId(nextId);
                            // changed by hqzhang for TDI-19527, label=displayName
                            connectionProperty.setLabel(connectionProperty.getDisplayName());
                            final RepositoryWorkUnit<Object> workUnit = new RepositoryWorkUnit<Object>("", this) {

                                @Override
                                protected void run() throws LoginException, PersistenceException {
                                    factory.create(connectionItem, propertiesWizardPage.getDestinationPath());
                                }
                            };
                            workUnit.setAvoidUnloadResources(true);
                            factory.executeRepositoryWorkUnit(workUnit);
                        } else {
                            // changed by hqzhang for TDI-19527, label=displayName
                            connectionProperty.setLabel(connectionProperty.getDisplayName());
                            // update schemas
                            Map<String, SchemaTarget> schemaTargetMap = new HashMap<String, SchemaTarget>();
                            EList<JSONXPathLoopDescriptor> schema = connection.getSchema();
                            if (schema != null && schema.size() > 0) {
                                JSONXPathLoopDescriptor jsonXPathLoopDescriptor = schema.get(0);
                                if (jsonXPathLoopDescriptor != null) {
                                    EList<SchemaTarget> schemaTargets = jsonXPathLoopDescriptor.getSchemaTargets();
                                    if (schemaTargets != null && schemaTargets.size() > 0) {
                                        for (SchemaTarget schemaTarget : schemaTargets) {
                                            schemaTargetMap.put(schemaTarget.getTagName(), schemaTarget);
                                        }
                                    }
                                }
                            }
                            Map<String, MetadataColumn> columnsMap = new HashMap<String, MetadataColumn>();
                            MetadataTable[] tables = ConnectionHelper.getTables(connectionItem.getConnection()).toArray(new MetadataTable[0]);
                            for (MetadataTable table : tables) {
                                EList<MetadataColumn> columns = table.getColumns();
                                Iterator<MetadataColumn> columnsIter = columns.iterator();
                                while (columnsIter.hasNext()) {
                                    MetadataColumn column = columnsIter.next();
                                    if (connection.isInputModel()) {
                                        if (schemaTargetMap.get(column.getLabel()) == null) {
                                            columnsIter.remove();
                                        } else {
                                            columnsMap.put(column.getLabel(), column);
                                        }
                                    } else {
                                        columnsMap.put(column.getLabel(), column);
                                    }
                                }
                            }
                            boolean hasAddedColumns = false;
                            Iterator<Entry<String, SchemaTarget>> schemaTargetIter = schemaTargetMap.entrySet().iterator();
                            while (schemaTargetIter.hasNext()) {
                                Map.Entry<String, SchemaTarget> entry = schemaTargetIter.next();
                                String key = entry.getKey();
                                if (columnsMap.get(key) == null) {
                                    hasAddedColumns = true;
                                    break;
                                }
                            }
                            if (hasAddedColumns) {
                                MessageDialog.openInformation(getShell(), "Detect new columns", "There are some new fields to extract, guess your schema manually if you want to apply the update.");
                            }
                            // update
                            RepositoryUpdateManager.updateFileConnection(connectionItem);
                            refreshInFinish(propertiesWizardPage.isNameModifiedByUser());
                            final RepositoryWorkUnit<Object> workUnit = new RepositoryWorkUnit<Object>("", this) {

                                @Override
                                protected void run() throws LoginException, PersistenceException {
                                    factory.save(connectionItem);
                                }
                            };
                            workUnit.setAvoidUnloadResources(true);
                            factory.executeRepositoryWorkUnit(workUnit);
                            closeLockStrategy();
                        }
                        final RepositoryWorkUnit<Object> workUnit = new RepositoryWorkUnit<Object>("", this) {

                            @Override
                            protected void run() throws LoginException, PersistenceException {
                                ProxyRepositoryFactory.getInstance().saveProject(ProjectManager.getInstance().getCurrentProject());
                            }
                        };
                        workUnit.setAvoidUnloadResources(true);
                        factory.executeRepositoryWorkUnit(workUnit);
                    }
                });
            }
        };
        IRunnableWithProgress iRunnableWithProgress = new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                IWorkspace workspace = ResourcesPlugin.getWorkspace();
                try {
                    ISchedulingRule schedulingRule = workspace.getRoot();
                    // the update the project files need to be done in the workspace runnable to avoid all
                    // notification
                    // of changes before the end of the modifications.
                    workspace.run(op, schedulingRule, IWorkspace.AVOID_UPDATE, monitor);
                } catch (CoreException e) {
                    throw new InvocationTargetException(e);
                }
            }
        };
        try {
            new ProgressMonitorDialog(null).run(true, true, iRunnableWithProgress);
        } catch (InvocationTargetException e) {
            ExceptionHandler.process(e);
        } catch (InterruptedException e) {
        //
        }
        return true;
    } else {
        return false;
    }
}
Also used : Set(java.util.Set) RepositoryWorkUnit(org.talend.repository.RepositoryWorkUnit) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) Entry(java.util.Map.Entry) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory) JSONXPathLoopDescriptor(org.talend.repository.model.json.JSONXPathLoopDescriptor) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) SchemaTarget(org.talend.repository.model.json.SchemaTarget) InvocationTargetException(java.lang.reflect.InvocationTargetException) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) EList(org.eclipse.emf.common.util.EList) CoreException(org.eclipse.core.runtime.CoreException) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IWorkspace(org.eclipse.core.resources.IWorkspace) PersistenceException(org.talend.commons.exception.PersistenceException) LoginException(org.talend.commons.exception.LoginException) IWizardPage(org.eclipse.jface.wizard.IWizardPage) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with JSONXPathLoopDescriptor

use of org.talend.repository.model.json.JSONXPathLoopDescriptor in project tdi-studio-se by Talend.

the class JSONWizard method addPages.

/**
     * Adding the page to the wizard.
     */
@Override
public void addPages() {
    if (isToolbar) {
        pathToSave = null;
    }
    propertiesWizardPage = new Step0WizardPage(connectionProperty, pathToSave, JSONRepositoryNodeType.JSON, !isRepositoryObjectEditable(), creation);
    jsonFileSelectPage = new JSONFileSelectWizardPage(creation, connectionItem, isRepositoryObjectEditable(), existingNames);
    setDefaultPageImageDescriptor(ImageProvider.getImageDesc(JSONImage.JSON_ICON32));
    if (connection != null) {
        List schemas = connection.getSchema();
        if (!schemas.isEmpty()) {
            JSONXPathLoopDescriptor currentSchema = (JSONXPathLoopDescriptor) schemas.get(0);
            oldAbstractQueryPath = currentSchema.getAbsoluteXPathQuery();
        }
    }
    if (creation) {
        setWindowTitle("New Json File");
        propertiesWizardPage.setTitle("File - Step" + " 1 " + "of" + " 5");
        propertiesWizardPage.setDescription("Add a Metadata File on repository\\nDefine the properties");
        addPage(propertiesWizardPage);
        propertiesWizardPage.setPageComplete(false);
        jsonFileSelectPage.setTitle("File - Step" + " 2 " + "of" + " 5");
        jsonFileSelectPage.setDescription("Select input or output model to create json metadata connection");
        addPage(jsonFileSelectPage);
        jsonFileSelectPage.setPageComplete(true);
    } else {
        setWindowTitle("Edit an existing Json File");
        propertiesWizardPage.setTitle("File - Step" + " 1 " + "of" + " 4");
        propertiesWizardPage.setDescription("Edit an existing Metadata File on repository\\nUpdate the properties");
        addPage(propertiesWizardPage);
        jsonFileSelectPage.setTitle("File - Step" + " 2 " + "of" + " 4");
        jsonFileSelectPage.setDescription("Select input or output model to create json metadata connection");
        addPage(jsonFileSelectPage);
        jsonFileSelectPage.setPageComplete(true);
        if (!isRepositoryObjectEditable()) {
            creation = true;
        }
    }
}
Also used : JSONXPathLoopDescriptor(org.talend.repository.model.json.JSONXPathLoopDescriptor) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) Step0WizardPage(org.talend.metadata.managment.ui.wizard.metadata.connection.Step0WizardPage)

Example 9 with JSONXPathLoopDescriptor

use of org.talend.repository.model.json.JSONXPathLoopDescriptor in project tdi-studio-se by Talend.

the class JSONToXPathLinker method initLoopListeners.

/**
     * DOC amaumont Comment method "initListeners".
     */
private void initLoopListeners() {
    JSONExtractorLoopModel loopModel = this.loopTableEditorView.getModel();
    final Table loopTable = this.loopTableEditorView.getTableViewerCreator().getTable();
    loopModelModifiedBeanListener = new IModifiedBeanListener<JSONXPathLoopDescriptor>() {

        @Override
        public void handleEvent(ModifiedBeanEvent<JSONXPathLoopDescriptor> event) {
            handleModifiedBeanEvent(event);
        }

        private void handleModifiedBeanEvent(ModifiedBeanEvent<JSONXPathLoopDescriptor> event) {
            if (event.column == loopTableEditorView.getXPathColumn()) {
                onXPathValueChanged(loopTable, (String) event.newValue, event.index);
            }
        }
    };
    loopModel.addModifiedBeanListener(loopModelModifiedBeanListener);
    loopTableExtendedControlListener = new IExtendedControlListener() {

        @Override
        public void handleEvent(ExtendedControlEvent event) {
            if (event.getType() == EVENT_TYPE.MODEL_CHANGED) {
                nodeRetriever.setCurrentLoopXPath(getCurrentLoopXPath());
            }
        }
    };
    this.loopTableEditorView.getExtendedTableViewer().addListener(loopTableExtendedControlListener);
    SelectionHelper selectionHelper = this.loopTableEditorView.getTableViewerCreator().getSelectionHelper();
    afterLineSelectionListener = new ILineSelectionListener() {

        @Override
        public void handle(LineSelectionEvent e) {
            updateLinksStyleAndControlsSelection(e.source.getTable(), true);
        }
    };
    selectionHelper.addAfterSelectionListener(afterLineSelectionListener);
}
Also used : JSONXPathLoopDescriptor(org.talend.repository.model.json.JSONXPathLoopDescriptor) LineSelectionEvent(org.talend.commons.ui.runtime.swt.tableviewer.selection.LineSelectionEvent) Table(org.eclipse.swt.widgets.Table) SelectionHelper(org.talend.commons.ui.runtime.swt.tableviewer.selection.SelectionHelper) ExtendedControlEvent(org.talend.commons.ui.swt.extended.table.ExtendedControlEvent) ILineSelectionListener(org.talend.commons.ui.runtime.swt.tableviewer.selection.ILineSelectionListener) IExtendedControlListener(org.talend.commons.ui.swt.extended.table.IExtendedControlListener)

Example 10 with JSONXPathLoopDescriptor

use of org.talend.repository.model.json.JSONXPathLoopDescriptor in project tdi-studio-se by Talend.

the class JSONToXPathLinker method getTreeItem.

@Override
protected TreeItem getTreeItem(Tree tree, Object dataOfTreeItem, Object dataOfTableItem) {
    String path = null;
    if (dataOfTableItem instanceof SchemaTarget) {
        SchemaTarget target = (SchemaTarget) dataOfTableItem;
        path = target.getRelativeXPathQuery();
    } else if (dataOfTableItem instanceof JSONXPathLoopDescriptor) {
        JSONXPathLoopDescriptor target = (JSONXPathLoopDescriptor) dataOfTableItem;
        path = target.getAbsoluteXPathQuery();
    }
    if (path == null) {
        return super.getTreeItem(tree, dataOfTreeItem, dataOfTableItem);
    }
    boolean expressionIsAbsolute = false;
    if (path.trim().startsWith("/")) {
        //$NON-NLS-1$
        expressionIsAbsolute = true;
    }
    //$NON-NLS-1$
    String fullPath = "";
    if (!expressionIsAbsolute) {
        if (loopXpathNodes.size() > 0) {
            //$NON-NLS-1$
            fullPath = loopXpathNodes.get(0) + "/";
        }
        // adapt relative path
        //$NON-NLS-1$
        String[] relatedSplitedPaths = path.split("\\.\\./");
        if (relatedSplitedPaths.length > 1) {
            int pathsToRemove = relatedSplitedPaths.length - 1;
            //$NON-NLS-1$
            String[] fullPathSplited = fullPath.split("/");
            //$NON-NLS-1$
            fullPath = "";
            for (int j = 1; j < (fullPathSplited.length - pathsToRemove); j++) {
                //$NON-NLS-1$
                fullPath += "/" + fullPathSplited[j];
            }
            //$NON-NLS-1$
            fullPath += "/" + relatedSplitedPaths[pathsToRemove];
        } else {
            fullPath += path;
        }
    } else {
        fullPath = path;
    }
    TreeItem treeItem = treePopulator.getTreeItem(fullPath);
    if (treeItem != null) {
        return treeItem;
    } else {
        return super.getTreeItem(tree, dataOfTreeItem, dataOfTableItem);
    }
}
Also used : JSONXPathLoopDescriptor(org.talend.repository.model.json.JSONXPathLoopDescriptor) TreeItem(org.eclipse.swt.widgets.TreeItem) SchemaTarget(org.talend.repository.model.json.SchemaTarget)

Aggregations

JSONXPathLoopDescriptor (org.talend.repository.model.json.JSONXPathLoopDescriptor)14 EList (org.eclipse.emf.common.util.EList)8 ArrayList (java.util.ArrayList)5 SchemaTarget (org.talend.repository.model.json.SchemaTarget)5 List (java.util.List)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ConvertJSONString (org.talend.repository.json.util.ConvertJSONString)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 TreeItem (org.eclipse.swt.widgets.TreeItem)2 ContextType (org.talend.designer.core.model.utils.emf.talendfile.ContextType)2 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 IWorkspace (org.eclipse.core.resources.IWorkspace)1 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)1 CoreException (org.eclipse.core.runtime.CoreException)1 Path (org.eclipse.core.runtime.Path)1 ISchedulingRule (org.eclipse.core.runtime.jobs.ISchedulingRule)1