Search in sources :

Example 1 with IElementParameterDefaultValue

use of org.talend.core.model.process.IElementParameterDefaultValue in project tdi-studio-se by Talend.

the class SapSchemaTypeController method createButtonCommand.

/*
     * (non-Javadoc)
     *
     * @see
     * org.talend.designer.core.ui.editor.properties.controllers.AbstractRepositoryController#createButtonCommand(org
     * .eclipse.swt.widgets.Button)
     */
@Override
protected Command createButtonCommand(Button button) {
    // see 0003766: Problems with the read only mode of the properties on repository mode.
    if (checkForRepositoryShema(button)) {
        return null;
    }
    Button inputButton = button;
    IElementParameter switchParam = elem.getElementParameter(EParameterName.REPOSITORY_ALLOW_AUTO_SWITCH.getName());
    if (inputButton.getData(NAME).equals(SCHEMA)) {
        // this map wil hold the all input connection for the tUnite component
        Map<INode, Map<IMetadataTable, Boolean>> inputInfos = new HashMap<INode, Map<IMetadataTable, Boolean>>();
        INode node;
        if (elem instanceof Node) {
            node = (INode) elem;
        } else {
            // else instanceof Connection
            node = ((IConnection) elem).getSource();
        }
        IMetadataTable inputMetadata = null, inputMetaCopy = null;
        Connection inputConec = null;
        String propertyName = (String) inputButton.getData(PARAMETER_NAME);
        IElementParameter param = node.getElementParameter(propertyName);
        IElementParameter connectionParam = param.getChildParameters().get(EParameterName.CONNECTION.getName());
        String connectionName = null;
        if (connectionParam != null) {
            connectionName = (String) connectionParam.getValue();
        }
        Object obj = button.getData(FORCE_READ_ONLY);
        boolean forceReadOnly = false;
        if (obj != null) {
            forceReadOnly = (Boolean) obj;
        }
        boolean inputReadOnly = false, outputReadOnly = false, inputReadOnlyNode = false, inputReadOnlyParam = false;
        for (Connection connec : (List<Connection>) node.getIncomingConnections()) {
            if (connec.isActivate() && (connec.getLineStyle().equals(EConnectionType.FLOW_MAIN) || connec.getLineStyle().equals(EConnectionType.TABLE) || connec.getLineStyle().equals(EConnectionType.FLOW_MERGE) || connec.getLineStyle() == EConnectionType.FLOW_REF)) {
                if (connectionName != null && !connec.getName().equals(connectionName)) {
                    continue;
                }
                inputMetadata = connec.getMetadataTable();
                inputMetaCopy = inputMetadata.clone();
                inputConec = connec;
                if (connec.getSource().isReadOnly()) {
                    inputReadOnlyNode = true;
                } else {
                    for (IElementParameter curParam : connec.getSource().getElementParameters()) {
                        if (curParam.getFieldType() == EParameterFieldType.SCHEMA_TYPE) {
                            if (curParam.isReadOnly()) {
                                inputReadOnlyParam = true;
                            }
                        }
                    }
                }
                // check if the inputMetadata is readonly
                if (inputMetadata != null) {
                    for (IMetadataColumn column : inputMetadata.getListColumns(true)) {
                        IMetadataColumn columnCopied = inputMetaCopy.getColumn(column.getLabel());
                        columnCopied.setCustom(column.isCustom());
                        columnCopied.setReadOnly(column.isReadOnly());
                    }
                    inputMetaCopy.setReadOnly(inputMetadata.isReadOnly());
                    inputReadOnly = prepareReadOnlyTable(inputMetaCopy, inputReadOnlyParam, inputReadOnlyNode);
                }
                // store the value for Dialog
                Map<IMetadataTable, Boolean> oneInput = new HashMap<IMetadataTable, Boolean>();
                oneInput.put(inputMetaCopy, inputReadOnly);
                inputInfos.put(connec.getSource(), oneInput);
            }
        }
        if (connectionParam != null && inputMetadata == null) {
            //$NON-NLS-1$
            MessageDialog.openError(//$NON-NLS-1$
            button.getShell(), //$NON-NLS-1$
            Messages.getString("SchemaTypeController.inputNotSet"), //$NON-NLS-1$
            Messages.getString("SchemaTypeController.connectionNotAvaliable"));
            return null;
        }
        IMetadataTable originaleMetadataTable = getMetadataTableFromXml(node);
        // check if the outputMetadata is readonly
        IMetadataTable originaleOutputTable = node.getMetadataFromConnector(param.getContext());
        // when several schema_type button ,need get the right one which is opening
        IElementParameter schemaParam = param.getChildParameters().get("SCHEMA_TYPE");
        // need setRepository here
        if (!param.getContext().equals(schemaParam.getContext())) {
            schemaParam = param.getChildParameters().get("SCHEMA_TYPE");
        }
        if (schemaParam != null && EmfComponent.REPOSITORY.equals(schemaParam.getValue())) {
            if (originaleOutputTable != null && originaleOutputTable instanceof MetadataTable) {
                ((MetadataTable) originaleOutputTable).setRepository(true);
            }
        } else if (schemaParam != null && EmfComponent.BUILTIN.equals(schemaParam.getValue())) {
            if (originaleOutputTable != null && originaleOutputTable instanceof MetadataTable) {
                ((MetadataTable) originaleOutputTable).setRepository(false);
            }
        }
        if ("tUniservBTGeneric".equals(node.getComponent().getName())) {
            originaleOutputTable = node.getMetadataTable("OUTPUT_SCHEMA");
        }
        IMetadataTable outputMetaCopy = originaleOutputTable.clone(true);
        for (IMetadataColumn column : originaleOutputTable.getListColumns(true)) {
            IMetadataColumn columnCopied = outputMetaCopy.getColumn(column.getLabel());
            columnCopied.setCustom(column.isCustom());
            columnCopied.setReadOnly(column.isReadOnly());
            if (//$NON-NLS-1$ //$NON-NLS-2$
            ("tLogCatcher".equals(node.getComponent().getName()) || "tStatCatcher".equals(node.getComponent().getName())) && !outputMetaCopy.sameMetadataAs(originaleMetadataTable, IMetadataColumn.OPTIONS_NONE)) {
                columnCopied.setReadOnly(false);
            }
        // setColumnLength(node, param, columnCopied);
        }
        outputMetaCopy.setReadOnly(originaleOutputTable.isReadOnly() || param.isReadOnly(node.getElementParametersWithChildrens()));
        if (//$NON-NLS-1$ //$NON-NLS-2$
        ("tLogCatcher".equals(node.getComponent().getName()) || "tStatCatcher".equals(node.getComponent().getName())) && !outputMetaCopy.sameMetadataAs(originaleMetadataTable, IMetadataColumn.OPTIONS_NONE)) {
            outputMetaCopy.setReadOnly(false);
        }
        //$NON-NLS-1$
        IElementParameter schemaTypeParam = param.getChildParameters().get("SCHEMA_TYPE");
        List<IElementParameterDefaultValue> defaultValues = schemaTypeParam.getDefaultValues();
        for (IElementParameterDefaultValue elementParameterDefaultValue : defaultValues) {
            if (elementParameterDefaultValue.getDefaultValue() instanceof MetadataTable) {
                MetadataTable table = (MetadataTable) elementParameterDefaultValue.getDefaultValue();
                outputMetaCopy.setReadOnlyColumnPosition(table.getReadOnlyColumnPosition());
                break;
            }
        }
        outputMetaCopy.sortCustomColumns();
        if (!forceReadOnly) {
            outputReadOnly = prepareReadOnlyTable(outputMetaCopy, param.isReadOnly(), node.isReadOnly());
        } else {
            outputReadOnly = true;
        }
        // create the MetadataDialog
        MetadataDialog metaDialog = null;
        if (inputMetadata != null) {
            if (inputInfos != null && inputInfos.size() > 1 && connectionName == null) {
                MetadataDialogForMerge metaDialogForMerge = new MetadataDialogForMerge(composite.getShell(), inputInfos, outputMetaCopy, node, getCommandStack());
                //$NON-NLS-1$
                metaDialogForMerge.setText(Messages.getString("SchemaController.schemaOf") + node.getLabel());
                metaDialogForMerge.setInputReadOnly(inputReadOnly);
                metaDialogForMerge.setOutputReadOnly(outputReadOnly);
                if (metaDialogForMerge.open() == MetadataDialogForMerge.OK) {
                    // inputMetaCopy = metaDialog.getInputMetaData();
                    outputMetaCopy = metaDialogForMerge.getOutputMetaData();
                    // check if the metadata is modified
                    boolean modified = false;
                    if (!outputMetaCopy.sameMetadataAs(originaleOutputTable, IMetadataColumn.OPTIONS_NONE)) {
                        modified = true;
                    } else {
                        if (inputMetadata != null) {
                            // Notice: the Map inputInfos maybe is modified by the dialog.
                            Set<INode> inputNodes = inputInfos.keySet();
                            for (INode inputNode : inputNodes) {
                                Map<IMetadataTable, Boolean> oneInput = inputInfos.get(inputNode);
                                inputMetaCopy = (IMetadataTable) oneInput.keySet().toArray()[0];
                                if (!inputMetaCopy.sameMetadataAs(inputNode.getMetadataList().get(0), IMetadataColumn.OPTIONS_NONE)) {
                                    modified = true;
                                }
                            }
                        }
                    }
                    // create the changeMetadataCommand
                    if (modified) {
                        if (switchParam != null) {
                            switchParam.setValue(Boolean.FALSE);
                        }
                        Command changeMetadataCommand = null;
                        // only output, no input
                        if (inputInfos.isEmpty()) {
                            changeMetadataCommand = new ChangeMetadataCommand(node, param, null, null, null, originaleOutputTable, outputMetaCopy);
                        } else {
                            Set<INode> inputNodes = inputInfos.keySet();
                            int count = 0;
                            for (INode inputNode : inputNodes) {
                                Map<IMetadataTable, Boolean> oneInput = inputInfos.get(inputNode);
                                inputMetaCopy = (IMetadataTable) oneInput.keySet().toArray()[0];
                                if (count == 0) {
                                    changeMetadataCommand = new ChangeMetadataCommand(node, param, inputNode, inputNode.getMetadataList().get(0), inputMetaCopy, originaleOutputTable, outputMetaCopy);
                                } else {
                                    changeMetadataCommand = changeMetadataCommand.chain(new ChangeMetadataCommand(node, param, inputNode, inputNode.getMetadataList().get(0), inputMetaCopy, originaleOutputTable, outputMetaCopy));
                                }
                                count++;
                            }
                        }
                        return changeMetadataCommand;
                    }
                }
            } else {
                INode inputNode = (inputConec.getSource());
                if (inputMetaCopy.getAttachedConnector() == null) {
                    INodeConnector mainConnector;
                    if (inputNode.isELTComponent()) {
                        mainConnector = inputNode.getConnectorFromType(EConnectionType.TABLE);
                    } else {
                        mainConnector = inputNode.getConnectorFromType(EConnectionType.FLOW_MAIN);
                    }
                    inputMetaCopy.setAttachedConnector(mainConnector.getName());
                }
                // INodeConnector outputConnector = node.getConnectorFromName(param.getContext());
                // if (outputConnector.getMaxLinkOutput() == 0 && (originaleOutputTable.getListColumns().size() ==
                // 0)) {
                // metaDialog = new MetadataDialog(composite.getShell(), inputMetaCopy, inputNode,
                // getCommandStack());
                // } else {
                metaDialog = new MetadataDialog(composite.getShell(), inputMetaCopy, inputNode, outputMetaCopy, node, getCommandStack());
            // }
            }
        } else {
            metaDialog = new MetadataDialog(composite.getShell(), outputMetaCopy, node, getCommandStack());
        }
        if (metaDialog != null) {
            //$NON-NLS-1$
            metaDialog.setText(Messages.getString("AbstractSchemaController.schema.title", node.getLabel()));
            metaDialog.setInputReadOnly(inputReadOnly);
            metaDialog.setOutputReadOnly(outputReadOnly);
            if (metaDialog.open() == MetadataDialog.OK) {
                inputMetaCopy = metaDialog.getInputMetaData();
                outputMetaCopy = metaDialog.getOutputMetaData();
                boolean modified = false;
                if (!outputMetaCopy.sameMetadataAs(originaleOutputTable, IMetadataColumn.OPTIONS_NONE)) {
                    modified = true;
                } else {
                    if (inputMetadata != null) {
                        if (!inputMetaCopy.sameMetadataAs(inputMetadata, IMetadataColumn.OPTIONS_NONE)) {
                            modified = true;
                        }
                    }
                }
                if (modified) {
                    if (switchParam != null) {
                        switchParam.setValue(Boolean.FALSE);
                    }
                    INode inputNode = null;
                    if (inputConec != null) {
                        inputNode = inputConec.getSource();
                    }
                    ChangeMetadataCommand changeMetadataCommand = new ChangeMetadataCommand(node, param, inputNode, inputMetadata, inputMetaCopy, originaleOutputTable, outputMetaCopy);
                    return changeMetadataCommand;
                }
            }
        }
    } else if (inputButton.getData(NAME).equals(RETRIEVE_SCHEMA)) {
        Node node = (Node) elem;
        // String propertyName = (String) inputButton.getData(PARAMETER_NAME);
        final Command cmd = RetrieveSchemaHelper.retrieveSchemasCommand(node);
        if (switchParam != null) {
            switchParam.setValue(Boolean.FALSE);
        }
        return cmd;
    } else if (inputButton.getData(NAME).equals(RESET_COLUMNS)) {
        Node node = (Node) elem;
        String propertyName = (String) inputButton.getData(PARAMETER_NAME);
        IElementParameter param = node.getElementParameter(propertyName);
        final Command cmd = SynchronizeSchemaHelper.createCommand(node, param);
        if (switchParam != null) {
            switchParam.setValue(Boolean.FALSE);
        }
        return cmd;
    } else if (button.getData(NAME).equals(REPOSITORY_CHOICE)) {
        String paramName = (String) button.getData(PARAMETER_NAME);
        IElementParameter schemaParam = elem.getElementParameter(paramName);
        ERepositoryObjectType type = ERepositoryObjectType.METADATA_CON_TABLE;
        String filter = schemaParam.getFilter();
        if (elem instanceof Node) {
            Node sapNode = (Node) elem;
            if (sapNode.getComponent().getName().startsWith("tSAP") && !sapNode.getComponent().getName().startsWith("tSAPHana")) {
                //$NON-NLS-1$ //$NON-NLS-2$
                type = ERepositoryObjectType.METADATA_SAP_FUNCTION;
            } else if (sapNode.getComponent().getName().startsWith("tESB")) {
                //$NON-NLS-1$
                filter = ERepositoryObjectType.SERVICESOPERATION.getType();
            }
        }
        RepositoryReviewDialog dialog = new RepositoryReviewDialog(button.getShell(), type, filter);
        if (dialog.open() == RepositoryReviewDialog.OK) {
            RepositoryNode node = dialog.getResult();
            while (node.getObject().getProperty().getItem() == null || (!(node.getObject().getProperty().getItem() instanceof ConnectionItem))) {
                node = node.getParent();
            }
            String id = dialog.getResult().getObject().getProperty().getId();
            // The name is Table Name.
            String name = dialog.getResult().getObject().getLabel();
            if (name != null) {
                if (elem instanceof Node) {
                    Node nodeElement = (Node) elem;
                    //$NON-NLS-1$
                    String value = id + " - " + name;
                    IMetadataTable repositoryMetadata = MetadataToolHelper.getMetadataFromRepository(value);
                    if (nodeElement.getComponent().getName().equals("tSQLTemplateMerge")) {
                        if (paramName.equals("SCHEMA")) {
                            paramName = "SOURCE_TABLE";
                            Command dbSelectorCommand = new PropertyChangeCommand(elem, paramName, TalendTextUtils.addQuotes(repositoryMetadata.getTableName()));
                            executeCommand(dbSelectorCommand);
                            Text labelText = (Text) hashCurControls.get(paramName);
                            labelText.setText(TalendTextUtils.addQuotes(repositoryMetadata.getTableName()));
                            paramName = "SCHEMA:REPOSITORY_SCHEMA_TYPE";
                            dbSelectorCommand = new PropertyChangeCommand(elem, paramName, TalendTextUtils.addQuotes(name));
                            executeCommand(dbSelectorCommand);
                            labelText = (Text) hashCurControls.get(paramName);
                            labelText.setText(TalendTextUtils.addQuotes(name));
                            paramName = "SCHEMA";
                        } else if (paramName.equals("SCHEMA_TARGET")) {
                            paramName = "TARGET_TABLE";
                            Command dbSelectorCommand = new PropertyChangeCommand(elem, paramName, TalendTextUtils.addQuotes(repositoryMetadata.getTableName()));
                            executeCommand(dbSelectorCommand);
                            Text labelText = (Text) hashCurControls.get(paramName);
                            labelText.setText(TalendTextUtils.addQuotes(repositoryMetadata.getTableName()));
                            paramName = "SCHEMA_TARGET:REPOSITORY_SCHEMA_TYPE";
                            dbSelectorCommand = new PropertyChangeCommand(elem, paramName, TalendTextUtils.addQuotes(name));
                            executeCommand(dbSelectorCommand);
                            labelText = (Text) hashCurControls.get(paramName);
                            labelText.setText(TalendTextUtils.addQuotes(name));
                            paramName = "SCHEMA_TARGET";
                        }
                    } else if (nodeElement.getComponent().getName().startsWith("tSQLTemplate")) {
                        if (paramName.equals("SCHEMA")) {
                            paramName = "TABLE_NAME";
                            Command dbSelectorCommand = new PropertyChangeCommand(elem, paramName, TalendTextUtils.addQuotes(repositoryMetadata.getTableName()));
                            executeCommand(dbSelectorCommand);
                            Text labelText = (Text) hashCurControls.get(paramName);
                            labelText.setText(TalendTextUtils.addQuotes(repositoryMetadata.getTableName()));
                            paramName = "SCHEMA:REPOSITORY_SCHEMA_TYPE";
                            dbSelectorCommand = new PropertyChangeCommand(elem, paramName, TalendTextUtils.addQuotes(name));
                            executeCommand(dbSelectorCommand);
                            labelText = (Text) hashCurControls.get(paramName);
                            labelText.setText(TalendTextUtils.addQuotes(name));
                            paramName = "SCHEMA";
                        } else if (paramName.equals("SCHEMA_TARGET")) {
                            paramName = "TABLE_NAME_TARGET";
                            Command dbSelectorCommand = new PropertyChangeCommand(elem, paramName, TalendTextUtils.addQuotes(repositoryMetadata.getTableName()));
                            executeCommand(dbSelectorCommand);
                            Text labelText = (Text) hashCurControls.get(paramName);
                            labelText.setText(TalendTextUtils.addQuotes(repositoryMetadata.getTableName()));
                            paramName = "SCHEMA_TARGET:REPOSITORY_SCHEMA_TYPE";
                            dbSelectorCommand = new PropertyChangeCommand(elem, paramName, TalendTextUtils.addQuotes(name));
                            executeCommand(dbSelectorCommand);
                            labelText = (Text) hashCurControls.get(paramName);
                            labelText.setText(TalendTextUtils.addQuotes(name));
                            paramName = "SCHEMA_TARGET";
                        }
                    } else {
                        Command dbSelectorCommand = new PropertyChangeCommand(elem, paramName, TalendTextUtils.addQuotes(repositoryMetadata.getTableName()));
                        executeCommand(dbSelectorCommand);
                        Text labelText = (Text) hashCurControls.get(paramName);
                        if (labelText != null) {
                            labelText.setText(TalendTextUtils.addQuotes(repositoryMetadata.getTableName()));
                        }
                    }
                }
            }
            //$NON-NLS-1$
            String value = id + " - " + name;
            //$NON-NLS-1$
            String fullParamName = paramName + ":" + getRepositoryChoiceParamName();
            org.talend.core.model.metadata.builder.connection.Connection connection = null;
            if (elem instanceof Node) {
                IMetadataTable repositoryMetadata = MetadataToolHelper.getMetadataFromRepository(value);
                connection = MetadataToolHelper.getConnectionFromRepository(value);
                // For SAP see bug 5423
                String functionId = node.getParent().getId();
                if (//$NON-NLS-1$//$NON-NLS-2$
                ((Node) elem).getUniqueName().startsWith("tSAP") && !((Node) elem).getUniqueName().startsWith("tSAPHana") && functionId != "-1") {
                    //$NON-NLS-1$
                    Node sapNode = (Node) elem;
                    repositoryMetadata = getMetadataFromRepository(id, functionId, name);
                    String functionName = node.getParent().getObject().getLabel();
                    for (IElementParameter param : sapNode.getElementParameters()) {
                        SAPParametersUtils.retrieveSAPParams(elem, connection, param, functionName);
                    }
                }
                // else {
                // repositoryMetadata = MetadataToolHelper.getMetadataFromRepository(value);
                // }
                // connection = MetadataTool.getConnectionFromRepository(value);
                // For validation rule.
                boolean isValRulesLost = false;
                IRepositoryViewObject currentValRuleObj = ValidationRulesUtil.getCurrentValidationRuleObjs(elem);
                if (currentValRuleObj != null) {
                    List<IRepositoryViewObject> valRuleObjs = ValidationRulesUtil.getRelatedValidationRuleObjs(value);
                    if (!ValidationRulesUtil.isCurrentValRuleObjInList(valRuleObjs, currentValRuleObj)) {
                        if (!MessageDialog.openConfirm(button.getShell(), //$NON-NLS-1$
                        Messages.getString("SchemaTypeController.validationrule.title.confirm"), Messages.getString("SchemaTypeController.validationrule.selectMetadataMsg"))) {
                            //$NON-NLS-1$
                            return null;
                        } else {
                            isValRulesLost = true;
                        }
                    }
                }
                if (repositoryMetadata == null) {
                    repositoryMetadata = new MetadataTable();
                }
                if (switchParam != null) {
                    switchParam.setValue(Boolean.FALSE);
                }
                CompoundCommand cc = new CompoundCommand();
                RepositoryChangeMetadataCommand changeMetadataCommand = new RepositoryChangeMetadataCommand((Node) elem, fullParamName, value, repositoryMetadata, null, null);
                changeMetadataCommand.setConnection(connection);
                cc.add(changeMetadataCommand);
                if (isValRulesLost) {
                    ValidationRulesUtil.appendRemoveValidationRuleCommands(cc, elem);
                }
                return cc;
            }
        }
    } else if (button.getData(NAME).equals(COPY_CHILD_COLUMNS)) {
        // 0004322: tRunJob can import the tBufferOutput schema from the son job
        // 0010489 modify
        String paramName = (String) button.getData(PARAMETER_NAME);
        IElementParameter param = elem.getElementParameter(paramName);
        IElementParameter processParam = elem.getElementParameterFromField(EParameterFieldType.PROCESS_TYPE);
        IElementParameter processIdParam = processParam.getChildParameters().get(EParameterName.PROCESS_TYPE_PROCESS.getName());
        String id = (String) processIdParam.getValue();
        Item item = ItemCacheManager.getProcessItem(id);
        Node node = (Node) elem;
        copySchemaFromChildJob(node, item);
        // pop up the schema dialog
        MetadataDialog metaDialog = new MetadataDialog(composite.getShell(), node.getMetadataList().get(0), node, getCommandStack());
        //$NON-NLS-1$
        metaDialog.setText(Messages.getString("SchemaController.schemaOf") + node.getLabel());
        if (metaDialog.open() == MetadataDialog.OK) {
            IMetadataTable outputMetaData = metaDialog.getOutputMetaData();
            return new ChangeMetadataCommand(node, param, null, outputMetaData);
        }
    }
    return null;
}
Also used : INode(org.talend.core.model.process.INode) HashMap(java.util.HashMap) ConnectionItem(org.talend.core.model.properties.ConnectionItem) Node(org.talend.designer.core.ui.editor.nodes.Node) INode(org.talend.core.model.process.INode) RepositoryNode(org.talend.repository.model.RepositoryNode) IRepositoryNode(org.talend.repository.model.IRepositoryNode) INodeConnector(org.talend.core.model.process.INodeConnector) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) Item(org.talend.core.model.properties.Item) ConnectionItem(org.talend.core.model.properties.ConnectionItem) ProcessItem(org.talend.core.model.properties.ProcessItem) Button(org.eclipse.swt.widgets.Button) IElementParameterDefaultValue(org.talend.core.model.process.IElementParameterDefaultValue) IMetadataTable(org.talend.core.model.metadata.IMetadataTable) MetadataTable(org.talend.core.model.metadata.MetadataTable) MetadataDialog(org.talend.core.ui.metadata.dialog.MetadataDialog) RepositoryChangeMetadataCommand(org.talend.designer.core.ui.editor.cmd.RepositoryChangeMetadataCommand) ChangeMetadataCommand(org.talend.designer.core.ui.editor.cmd.ChangeMetadataCommand) IElementParameter(org.talend.core.model.process.IElementParameter) List(java.util.List) ERepositoryObjectType(org.talend.core.model.repository.ERepositoryObjectType) RepositoryChangeMetadataCommand(org.talend.designer.core.ui.editor.cmd.RepositoryChangeMetadataCommand) Connection(org.talend.designer.core.ui.editor.connections.Connection) IConnection(org.talend.core.model.process.IConnection) Text(org.eclipse.swt.widgets.Text) IMetadataColumn(org.talend.core.model.metadata.IMetadataColumn) RepositoryNode(org.talend.repository.model.RepositoryNode) IRepositoryNode(org.talend.repository.model.IRepositoryNode) RepositoryReviewDialog(org.talend.repository.ui.dialog.RepositoryReviewDialog) Point(org.eclipse.swt.graphics.Point) IMetadataTable(org.talend.core.model.metadata.IMetadataTable) PropertyChangeCommand(org.talend.designer.core.ui.editor.cmd.PropertyChangeCommand) RepositoryChangeMetadataCommand(org.talend.designer.core.ui.editor.cmd.RepositoryChangeMetadataCommand) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) Command(org.eclipse.gef.commands.Command) ChangeMetadataCommand(org.talend.designer.core.ui.editor.cmd.ChangeMetadataCommand) PropertyChangeCommand(org.talend.designer.core.ui.editor.cmd.PropertyChangeCommand) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) Map(java.util.Map) HashMap(java.util.HashMap) MetadataDialogForMerge(org.talend.core.ui.metadata.dialog.MetadataDialogForMerge)

Example 2 with IElementParameterDefaultValue

use of org.talend.core.model.process.IElementParameterDefaultValue in project tdi-studio-se by Talend.

the class ChangeValuesFromRepository method setDefaultValues.

/**
     * Reset the values to default only for combo boxes, if the values set are not valid.<br>
     * This avoids to have some invalid setup after drag&drop / setup a component from repository.
     *
     * @param currentParam Current parameter that has been modified in the interface
     * @param testedParam Tested parameter, to know if there is a link for the default values between this parameter and
     * the current.
     */
private void setDefaultValues(IElementParameter testedParam, IElement referenceNode) {
    List<? extends IElementParameter> elementParameters = referenceNode.getElementParameters();
    if (elementParameters == null) {
        return;
    }
    // if not a combo box or if this parameter is linked to repository, just skip
    if (!testedParam.getFieldType().equals(EParameterFieldType.CLOSED_LIST) || testedParam.getRepositoryValue() != null) {
        return;
    }
    boolean contains = false;
    for (IElementParameter currentParam : elementParameters) {
        for (IElementParameterDefaultValue value : testedParam.getDefaultValues()) {
            if (value.getIfCondition() != null) {
                if (value.getIfCondition().contains(currentParam.getName())) {
                    contains = true;
                    break;
                }
            }
            if (value.getNotIfCondition() != null) {
                if (value.getNotIfCondition().contains(currentParam.getName())) {
                    contains = true;
                    break;
                }
            }
        }
        if (contains) {
            // check if current parameter for combo is valid for new parameters value
            // if not, it will try to choose another value from combo box list.
            boolean isCurrentComboValid = true;
            if (testedParam.getListItemsShowIf() != null || testedParam.getListItemsNotShowIf() != null) {
                String value = (String) testedParam.getValue();
                int index = ArrayUtils.indexOf(testedParam.getListItemsValue(), value);
                // TUP-671:if find this testParam's value,just do show if
                if (index != -1) {
                    if (testedParam.getListItemsShowIf() != null) {
                        String conditionShowIf = testedParam.getListItemsShowIf()[index];
                        if (conditionShowIf != null) {
                            isCurrentComboValid = Expression.evaluate(conditionShowIf, elementParameters);
                        }
                    }
                    if (testedParam.getListItemsNotShowIf() != null) {
                        String conditionNotShowIf = testedParam.getListItemsNotShowIf()[index];
                        if (conditionNotShowIf != null) {
                            isCurrentComboValid = !Expression.evaluate(conditionNotShowIf, elementParameters);
                        }
                    }
                }
            }
            if (!isCurrentComboValid && testedParam.getListItemsShowIf() != null) {
                for (String condition : testedParam.getListItemsShowIf()) {
                    if (condition != null && condition.contains(currentParam.getName())) {
                        boolean isValid = Expression.evaluate(condition, elementParameters);
                        if (isValid) {
                            int index = ArrayUtils.indexOf(testedParam.getListItemsShowIf(), condition);
                            testedParam.setValue(testedParam.getListItemsValue()[index]);
                            break;
                        }
                    }
                }
            }
            if (!isCurrentComboValid && !contains && testedParam.getListItemsNotShowIf() != null) {
                for (String condition : testedParam.getListItemsNotShowIf()) {
                    if (condition != null && condition.contains(currentParam.getName())) {
                        boolean isValid = !Expression.evaluate(condition, elementParameters);
                        if (isValid) {
                            int index = ArrayUtils.indexOf(testedParam.getListItemsNotShowIf(), condition);
                            testedParam.setValue(testedParam.getListItemsValue()[index]);
                            break;
                        }
                    } else if (condition == null) {
                        int index = ArrayUtils.indexOf(testedParam.getListItemsNotShowIf(), condition);
                        testedParam.setValue(testedParam.getListItemsValue()[index]);
                        break;
                    }
                }
            }
        }
    }
}
Also used : IElementParameterDefaultValue(org.talend.core.model.process.IElementParameterDefaultValue) IElementParameter(org.talend.core.model.process.IElementParameter)

Example 3 with IElementParameterDefaultValue

use of org.talend.core.model.process.IElementParameterDefaultValue in project tdi-studio-se by Talend.

the class SchemaReferenceController method createButtonCommand.

@Override
protected Command createButtonCommand(Button button) {
    if (checkForRepositoryShema(button)) {
        return null;
    }
    Button inputButton = button;
    IElementParameter switchParam = elem.getElementParameter(EParameterName.REPOSITORY_ALLOW_AUTO_SWITCH.getName());
    if (inputButton.getData(NAME).equals(SCHEMA)) {
        // this map wil hold the all input connection for the tUnite component
        Map<INode, Map<IMetadataTable, Boolean>> inputInfos = new HashMap<INode, Map<IMetadataTable, Boolean>>();
        INode node;
        if (elem instanceof Node) {
            node = (INode) elem;
        } else {
            // else instanceof Connection
            node = ((IConnection) elem).getSource();
        }
        IMetadataTable inputMetadata = null, inputMetaCopy = null;
        Connection inputConec = null;
        String propertyName = (String) inputButton.getData(PARAMETER_NAME);
        IElementParameter param = node.getElementParameter(propertyName);
        IElementParameter connectionParam = param.getChildParameters().get(EParameterName.CONNECTION.getName());
        String connectionName = null;
        if (connectionParam != null) {
            connectionName = (String) connectionParam.getValue();
        }
        Object obj = button.getData(FORCE_READ_ONLY);
        boolean forceReadOnly = false;
        if (obj != null) {
            forceReadOnly = (Boolean) obj;
        }
        boolean inputReadOnly = false, outputReadOnly = false, inputReadOnlyNode = false, inputReadOnlyParam = false;
        for (Connection connec : (List<Connection>) node.getIncomingConnections()) {
            if (connec.isActivate() && (connec.getLineStyle().equals(EConnectionType.FLOW_MAIN) || connec.getLineStyle().equals(EConnectionType.TABLE) || connec.getLineStyle().equals(EConnectionType.FLOW_MERGE) || connec.getLineStyle() == EConnectionType.FLOW_REF)) {
                if (connectionName != null && !connec.getName().equals(connectionName)) {
                    continue;
                }
                inputMetadata = connec.getMetadataTable();
                inputMetaCopy = inputMetadata.clone();
                inputConec = connec;
                if (connec.getSource().isReadOnly()) {
                    inputReadOnlyNode = true;
                } else {
                    for (IElementParameter curParam : connec.getSource().getElementParameters()) {
                        if (curParam.getFieldType() == EParameterFieldType.SCHEMA_REFERENCE) {
                            if (curParam.isReadOnly()) {
                                inputReadOnlyParam = true;
                            }
                        }
                    }
                }
                // check if the inputMetadata is readonly
                if (inputMetadata != null) {
                    for (IMetadataColumn column : inputMetadata.getListColumns(true)) {
                        IMetadataColumn columnCopied = inputMetaCopy.getColumn(column.getLabel());
                        columnCopied.setCustom(column.isCustom());
                        columnCopied.setReadOnly(column.isReadOnly());
                    }
                    inputMetaCopy.setReadOnly(inputMetadata.isReadOnly());
                    inputReadOnly = prepareReadOnlyTable(inputMetaCopy, inputReadOnlyParam, inputReadOnlyNode);
                }
                // store the value for Dialog
                Map<IMetadataTable, Boolean> oneInput = new HashMap<IMetadataTable, Boolean>();
                oneInput.put(inputMetaCopy, inputReadOnly);
                inputInfos.put(connec.getSource(), oneInput);
            }
        }
        if (connectionParam != null && inputMetadata == null) {
            //$NON-NLS-1$
            MessageDialog.openError(//$NON-NLS-1$
            button.getShell(), //$NON-NLS-1$
            Messages.getString("SchemaReferenceController.inputNotSet"), //$NON-NLS-1$
            Messages.getString("SchemaReferenceController.connectionNotAvaliable"));
            return null;
        }
        IMetadataTable originaleMetadataTable = getMetadataTableFromXml(node);
        // check if the outputMetadata is readonly
        IMetadataTable originaleOutputTable = node.getMetadataFromConnector(param.getContext());
        // when several schema_type button ,need get the right one which is opening
        //$NON-NLS-1$
        IElementParameter schemaParam = param.getChildParameters().get("SCHEMA_TYPE");
        // need setRepository here
        if (!param.getContext().equals(schemaParam.getContext())) {
            //$NON-NLS-1$
            schemaParam = param.getChildParameters().get("SCHEMA_TYPE");
        }
        if (schemaParam != null && EmfComponent.REPOSITORY.equals(schemaParam.getValue())) {
            if (originaleOutputTable != null && originaleOutputTable instanceof MetadataTable) {
                ((MetadataTable) originaleOutputTable).setRepository(true);
            }
        } else if (schemaParam != null && EmfComponent.BUILTIN.equals(schemaParam.getValue())) {
            if (originaleOutputTable != null && originaleOutputTable instanceof MetadataTable) {
                ((MetadataTable) originaleOutputTable).setRepository(false);
            }
        }
        IMetadataTable outputMetaCopy = originaleOutputTable.clone(true);
        for (IMetadataColumn column : originaleOutputTable.getListColumns(true)) {
            IMetadataColumn columnCopied = outputMetaCopy.getColumn(column.getLabel());
            columnCopied.setCustom(column.isCustom());
            columnCopied.setReadOnly(column.isReadOnly());
        }
        outputMetaCopy.setReadOnly(originaleOutputTable.isReadOnly() || param.isReadOnly(node.getElementParametersWithChildrens()));
        //$NON-NLS-1$
        IElementParameter schemaTypeParam = param.getChildParameters().get("SCHEMA_TYPE");
        List<IElementParameterDefaultValue> defaultValues = schemaTypeParam.getDefaultValues();
        for (IElementParameterDefaultValue elementParameterDefaultValue : defaultValues) {
            if (elementParameterDefaultValue.getDefaultValue() instanceof MetadataTable) {
                MetadataTable table = (MetadataTable) elementParameterDefaultValue.getDefaultValue();
                outputMetaCopy.setReadOnlyColumnPosition(table.getReadOnlyColumnPosition());
                break;
            }
        }
        outputMetaCopy.sortCustomColumns();
        if (!forceReadOnly) {
            outputReadOnly = prepareReadOnlyTable(outputMetaCopy, param.isReadOnly(), node.isReadOnly());
        } else {
            outputReadOnly = true;
        }
        MetadataDialog metaDialog = null;
        if (inputMetadata != null) {
            if (inputInfos != null && inputInfos.size() > 1 && connectionName == null) {
                MetadataDialogForMerge metaDialogForMerge = new MetadataDialogForMerge(composite.getShell(), inputInfos, outputMetaCopy, node, getCommandStack());
                //$NON-NLS-1$
                metaDialogForMerge.setText(Messages.getString("SchemaReferenceController.schemaOf") + node.getLabel());
                metaDialogForMerge.setInputReadOnly(inputReadOnly);
                metaDialogForMerge.setOutputReadOnly(outputReadOnly);
                if (metaDialogForMerge.open() == MetadataDialogForMerge.OK) {
                    outputMetaCopy = metaDialogForMerge.getOutputMetaData();
                    // check if the metadata is modified
                    boolean modified = false;
                    if (!outputMetaCopy.sameMetadataAs(originaleOutputTable, IMetadataColumn.OPTIONS_NONE)) {
                        modified = true;
                    } else {
                        if (inputMetadata != null) {
                            // Notice: the Map inputInfos maybe is modified by the dialog.
                            Set<INode> inputNodes = inputInfos.keySet();
                            for (INode inputNode : inputNodes) {
                                Map<IMetadataTable, Boolean> oneInput = inputInfos.get(inputNode);
                                inputMetaCopy = (IMetadataTable) oneInput.keySet().toArray()[0];
                                if (!inputMetaCopy.sameMetadataAs(inputNode.getMetadataList().get(0), IMetadataColumn.OPTIONS_NONE)) {
                                    modified = true;
                                }
                            }
                        }
                    }
                    // create the changeMetadataCommand
                    if (modified) {
                        if (switchParam != null) {
                            switchParam.setValue(Boolean.FALSE);
                        }
                        Command changeMetadataCommand = null;
                        // only output, no input
                        if (inputInfos.isEmpty()) {
                            changeMetadataCommand = new ChangeMetadataCommand(node, param, null, null, null, originaleOutputTable, outputMetaCopy);
                        } else {
                            Set<INode> inputNodes = inputInfos.keySet();
                            int count = 0;
                            for (INode inputNode : inputNodes) {
                                Map<IMetadataTable, Boolean> oneInput = inputInfos.get(inputNode);
                                inputMetaCopy = (IMetadataTable) oneInput.keySet().toArray()[0];
                                if (count == 0) {
                                    changeMetadataCommand = new ChangeMetadataCommand(node, param, inputNode, inputNode.getMetadataList().get(0), inputMetaCopy, originaleOutputTable, outputMetaCopy);
                                } else {
                                    changeMetadataCommand = changeMetadataCommand.chain(new ChangeMetadataCommand(node, param, inputNode, inputNode.getMetadataList().get(0), inputMetaCopy, originaleOutputTable, outputMetaCopy));
                                }
                                count++;
                            }
                        }
                        return changeMetadataCommand;
                    }
                }
            } else {
                INode inputNode = (inputConec.getSource());
                if (inputMetaCopy.getAttachedConnector() == null) {
                    INodeConnector mainConnector;
                    if (inputNode.isELTComponent()) {
                        mainConnector = inputNode.getConnectorFromType(EConnectionType.TABLE);
                    } else {
                        mainConnector = inputNode.getConnectorFromType(EConnectionType.FLOW_MAIN);
                    }
                    inputMetaCopy.setAttachedConnector(mainConnector.getName());
                }
                metaDialog = new MetadataDialog(composite.getShell(), inputMetaCopy, inputNode, outputMetaCopy, node, getCommandStack());
            }
        } else {
            metaDialog = new MetadataDialog(composite.getShell(), outputMetaCopy, node, getCommandStack());
        }
        if (metaDialog != null) {
            //$NON-NLS-1$
            metaDialog.setText(Messages.getString("SchemaReferenceController.schema.title", node.getLabel()));
            metaDialog.setInputReadOnly(inputReadOnly);
            metaDialog.setOutputReadOnly(outputReadOnly);
            if (metaDialog.open() == MetadataDialog.OK) {
                inputMetaCopy = metaDialog.getInputMetaData();
                outputMetaCopy = metaDialog.getOutputMetaData();
                boolean modified = false;
                if (!outputMetaCopy.sameMetadataAs(originaleOutputTable, IMetadataColumn.OPTIONS_NONE)) {
                    modified = true;
                } else {
                    if (inputMetadata != null) {
                        if (!inputMetaCopy.sameMetadataAs(inputMetadata, IMetadataColumn.OPTIONS_NONE)) {
                            modified = true;
                        }
                    }
                }
                if (modified) {
                    if (switchParam != null) {
                        switchParam.setValue(Boolean.FALSE);
                    }
                    INode inputNode = null;
                    if (inputConec != null) {
                        inputNode = inputConec.getSource();
                    }
                    // update the component schema
                    if (param instanceof GenericElementParameter) {
                        GenericElementParameter genericElementParameter = (GenericElementParameter) param;
                        String paramName = genericElementParameter.getName();
                        ComponentProperties componentProperties = node.getComponentProperties();
                        if (componentProperties != null) {
                            org.talend.daikon.properties.property.Property schemaProperty = componentProperties.getValuedProperty(paramName);
                            if (schemaProperty != null) {
                                SchemaUtils.updateComponentSchema(node, outputMetaCopy, null);
                            }
                        }
                    }
                    if (node.getComponent().isSchemaAutoPropagated()) {
                        ChangeMetadataCommand changeMetadataCommand = new ChangeMetadataCommand(node, param, inputNode, inputMetadata, inputMetaCopy, originaleOutputTable, outputMetaCopy);
                        return changeMetadataCommand;
                    } else {
                        ChangeMetadataCommand changeMetadataCommand = new ChangeMetadataCommand(node, param, inputNode, inputMetadata, inputMetaCopy, originaleOutputTable, outputMetaCopy);
                        changeMetadataCommand.setPropagate(Boolean.FALSE);
                        return changeMetadataCommand;
                    }
                }
            }
        }
    } else if (inputButton.getData(NAME).equals(RETRIEVE_SCHEMA)) {
        Node node = (Node) elem;
        // String propertyName = (String) inputButton.getData(PARAMETER_NAME);
        final Command cmd = RetrieveSchemaHelper.retrieveSchemasCommand(node);
        if (switchParam != null) {
            switchParam.setValue(Boolean.FALSE);
        }
        return cmd;
    } else if (inputButton.getData(NAME).equals(RESET_COLUMNS)) {
        Node node = (Node) elem;
        String propertyName = (String) inputButton.getData(PARAMETER_NAME);
        IElementParameter param = node.getElementParameter(propertyName);
        final Command cmd = SynchronizeSchemaHelper.createCommand(node, param);
        if (switchParam != null) {
            switchParam.setValue(Boolean.FALSE);
        }
        return cmd;
    } else if (button.getData(NAME).equals(REPOSITORY_CHOICE)) {
        String paramName = (String) button.getData(PARAMETER_NAME);
        IElementParameter schemaParam = elem.getElementParameter(paramName);
        ERepositoryObjectType type = ERepositoryObjectType.METADATA_CON_TABLE;
        String filter = schemaParam.getFilter();
        RepositoryReviewDialog dialog = new RepositoryReviewDialog(button.getShell(), type, filter);
        if (dialog.open() == RepositoryReviewDialog.OK) {
            RepositoryNode node = dialog.getResult();
            while (node.getObject().getProperty().getItem() == null || (!(node.getObject().getProperty().getItem() instanceof ConnectionItem))) {
                node = node.getParent();
            }
            IRepositoryViewObject object = dialog.getResult().getObject();
            Property property = object.getProperty();
            String id = property.getId();
            // The name is Table Name.
            String name = object.getLabel();
            if (name != null) {
                if (elem instanceof Node) {
                    //$NON-NLS-1$
                    String value = id + " - " + name;
                    //$NON-NLS-1$
                    paramName = paramName + ":" + EParameterName.REPOSITORY_SCHEMA_TYPE.getName();
                    Command selectorCommand = new PropertyChangeCommand(elem, paramName, TalendTextUtils.addQuotes(value));
                    executeCommand(selectorCommand);
                }
            }
            //$NON-NLS-1$
            String value = id + " - " + name;
            //$NON-NLS-1$
            String fullParamName = paramName + ":" + getRepositoryChoiceParamName();
            org.talend.core.model.metadata.builder.connection.Connection connection = null;
            if (elem instanceof Node) {
                IMetadataTable repositoryMetadata = MetadataToolHelper.getMetadataFromRepository(value);
                connection = MetadataToolHelper.getConnectionFromRepository(value);
                // For validation rule.
                boolean isValRulesLost = false;
                IRepositoryViewObject currentValRuleObj = ValidationRulesUtil.getCurrentValidationRuleObjs(elem);
                if (currentValRuleObj != null) {
                    List<IRepositoryViewObject> valRuleObjs = ValidationRulesUtil.getRelatedValidationRuleObjs(value);
                    if (!ValidationRulesUtil.isCurrentValRuleObjInList(valRuleObjs, currentValRuleObj)) {
                        if (!MessageDialog.openConfirm(button.getShell(), //$NON-NLS-1$
                        Messages.getString("SchemaReferenceController.validationrule.title.confirm"), Messages.getString("SchemaReferenceController.validationrule.selectMetadataMsg"))) {
                            //$NON-NLS-1$
                            return null;
                        } else {
                            isValRulesLost = true;
                        }
                    }
                }
                if (repositoryMetadata == null) {
                    repositoryMetadata = new MetadataTable();
                }
                if (switchParam != null) {
                    switchParam.setValue(Boolean.FALSE);
                }
                CompoundCommand cc = new CompoundCommand();
                RepositoryChangeMetadataCommand changeMetadataCommand = new RepositoryChangeMetadataCommand((Node) elem, fullParamName, value, repositoryMetadata, null, null);
                changeMetadataCommand.setConnection(connection);
                cc.add(changeMetadataCommand);
                if (isValRulesLost) {
                    ValidationRulesUtil.appendRemoveValidationRuleCommands(cc, elem);
                }
                return cc;
            }
        }
    } else if (button.getData(NAME).equals(COPY_CHILD_COLUMNS)) {
        String paramName = (String) button.getData(PARAMETER_NAME);
        IElementParameter param = elem.getElementParameter(paramName);
        IElementParameter processParam = elem.getElementParameterFromField(EParameterFieldType.PROCESS_TYPE);
        IElementParameter processIdParam = processParam.getChildParameters().get(EParameterName.PROCESS_TYPE_PROCESS.getName());
        String id = (String) processIdParam.getValue();
        Item item = ItemCacheManager.getProcessItem(id);
        Node node = (Node) elem;
        copySchemaFromChildJob(node, item);
        // pop up the schema dialog
        MetadataDialog metaDialog = new MetadataDialog(composite.getShell(), node.getMetadataList().get(0), node, getCommandStack());
        //$NON-NLS-1$
        metaDialog.setText(Messages.getString("SchemaReferenceController.schemaOf") + node.getLabel());
        if (metaDialog.open() == MetadataDialog.OK) {
            IMetadataTable outputMetaData = metaDialog.getOutputMetaData();
            return new ChangeMetadataCommand(node, param, null, outputMetaData);
        }
    }
    return null;
}
Also used : INode(org.talend.core.model.process.INode) ComponentProperties(org.talend.components.api.properties.ComponentProperties) HashMap(java.util.HashMap) ConnectionItem(org.talend.core.model.properties.ConnectionItem) Node(org.talend.designer.core.ui.editor.nodes.Node) RepositoryNode(org.talend.repository.model.RepositoryNode) INode(org.talend.core.model.process.INode) INodeConnector(org.talend.core.model.process.INodeConnector) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) ConnectionItem(org.talend.core.model.properties.ConnectionItem) Item(org.talend.core.model.properties.Item) Button(org.eclipse.swt.widgets.Button) IElementParameterDefaultValue(org.talend.core.model.process.IElementParameterDefaultValue) IMetadataTable(org.talend.core.model.metadata.IMetadataTable) MetadataTable(org.talend.core.model.metadata.MetadataTable) MetadataDialog(org.talend.core.ui.metadata.dialog.MetadataDialog) ChangeMetadataCommand(org.talend.designer.core.ui.editor.cmd.ChangeMetadataCommand) RepositoryChangeMetadataCommand(org.talend.designer.core.ui.editor.cmd.RepositoryChangeMetadataCommand) GenericElementParameter(org.talend.designer.core.generic.model.GenericElementParameter) IElementParameter(org.talend.core.model.process.IElementParameter) List(java.util.List) ERepositoryObjectType(org.talend.core.model.repository.ERepositoryObjectType) IDynamicProperty(org.talend.core.ui.properties.tab.IDynamicProperty) Property(org.talend.core.model.properties.Property) RepositoryChangeMetadataCommand(org.talend.designer.core.ui.editor.cmd.RepositoryChangeMetadataCommand) IConnection(org.talend.core.model.process.IConnection) Connection(org.talend.designer.core.ui.editor.connections.Connection) IMetadataColumn(org.talend.core.model.metadata.IMetadataColumn) RepositoryNode(org.talend.repository.model.RepositoryNode) RepositoryReviewDialog(org.talend.repository.ui.dialog.RepositoryReviewDialog) IMetadataTable(org.talend.core.model.metadata.IMetadataTable) PropertyChangeCommand(org.talend.designer.core.ui.editor.cmd.PropertyChangeCommand) ChangeMetadataCommand(org.talend.designer.core.ui.editor.cmd.ChangeMetadataCommand) RepositoryChangeMetadataCommand(org.talend.designer.core.ui.editor.cmd.RepositoryChangeMetadataCommand) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) Command(org.eclipse.gef.commands.Command) PropertyChangeCommand(org.talend.designer.core.ui.editor.cmd.PropertyChangeCommand) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) HashMap(java.util.HashMap) Map(java.util.Map) MetadataDialogForMerge(org.talend.core.ui.metadata.dialog.MetadataDialogForMerge)

Example 4 with IElementParameterDefaultValue

use of org.talend.core.model.process.IElementParameterDefaultValue in project tdi-studio-se by Talend.

the class ComponentsUtils method getParametersFromForm.

/**
     * DOC ycbai Comment method "loadParametersFromForm".
     * <p>
     * Get element parameters of <code>element</code> from <code>form</code>.
     *
     * @param node optional, used if there is a component setting up the properties
     * @param element
     * @param category
     * @param form
     * @return parameters list
     */
private static List<ElementParameter> getParametersFromForm(IElement element, boolean isInitializing, EComponentCategory category, ComponentProperties rootProperty, Properties compProperties, String parentPropertiesPath, Form form, Widget parentWidget, AtomicInteger lastRowNum) {
    List<ElementParameter> elementParameters = new ArrayList<>();
    List<String> parameterNames = new ArrayList<>();
    EComponentCategory compCategory = category;
    if (compCategory == null) {
        compCategory = EComponentCategory.BASIC;
    }
    AtomicInteger lastRN = lastRowNum;
    if (lastRN == null) {
        lastRN = new AtomicInteger();
    }
    if (form == null) {
        return elementParameters;
    }
    Properties componentProperties = compProperties;
    if (componentProperties == null) {
        componentProperties = form.getProperties();
    }
    if (element instanceof INode) {
        INode node = (INode) element;
        // Set the properties only one time to get the top-level properties object
        if (node.getComponentProperties() == null) {
            node.setComponentProperties(rootProperty);
        }
    }
    // Have to initialize for the messages
    Collection<Widget> formWidgets = form.getWidgets();
    for (Widget widget : formWidgets) {
        NamedThing widgetProperty = widget.getContent();
        String propertiesPath = getPropertiesPath(parentPropertiesPath, null);
        if (widgetProperty instanceof Form) {
            Form subForm = (Form) widgetProperty;
            Properties subProperties = subForm.getProperties();
            // Reset properties path
            if (!isSameComponentProperties(componentProperties, widgetProperty)) {
                propertiesPath = getPropertiesPath(parentPropertiesPath, subProperties.getName());
            }
            elementParameters.addAll(getParametersFromForm(element, isInitializing, compCategory, rootProperty, subProperties, propertiesPath, subForm, widget, lastRN));
            continue;
        }
        GenericElementParameter param = new GenericElementParameter(element, rootProperty, form, widget, getComponentService());
        String parameterName = propertiesPath.concat(param.getName());
        param.setName(parameterName);
        param.setCategory(compCategory);
        param.setShow(parentWidget == null ? !widget.isHidden() : !parentWidget.isHidden() && !widget.isHidden());
        int rowNum = 0;
        if (widget.getOrder() != 1) {
            rowNum = lastRN.get();
        } else {
            rowNum = widget.getRow();
            if (parentWidget != null) {
                rowNum += parentWidget.getRow();
            }
            rowNum = rowNum + lastRN.get();
        }
        param.setNumRow(rowNum);
        lastRN.set(rowNum);
        // handle form...
        EParameterFieldType fieldType = getFieldType(widget, widgetProperty);
        param.setFieldType(fieldType != null ? fieldType : EParameterFieldType.TEXT);
        if (widgetProperty instanceof SchemaProperty) {
            boolean found = false;
            // set a default connector
            param.setContext(EConnectionType.FLOW_MAIN.getName());
            for (Connector connector : rootProperty.getPossibleConnectors(true)) {
                if (!(((SchemaProperty) widgetProperty).getValue() instanceof Schema)) {
                    continue;
                }
                if (connector instanceof PropertyPathConnector) {
                    //$NON-NLS-1$
                    String linkedSchema = ((PropertyPathConnector) connector).getPropertyPath() + ".schema";
                    if (parameterName.equals(linkedSchema)) {
                        found = true;
                        param.setContext(connector.getName());
                        IElementParameterDefaultValue defaultValue = new ElementParameterDefaultValue();
                        Schema schema = ((SchemaProperty) widgetProperty).getValue();
                        defaultValue.setDefaultValue(new Schema.Parser().parse(schema.toString()));
                        param.getDefaultValues().add(defaultValue);
                    }
                }
            }
            if (!found) {
                // compatibility.
                for (Connector connector : rootProperty.getPossibleConnectors(false)) {
                    if (!(((SchemaProperty) widgetProperty).getValue() instanceof Schema)) {
                        continue;
                    }
                    if (connector instanceof PropertyPathConnector) {
                        //$NON-NLS-1$
                        String linkedSchema = ((PropertyPathConnector) connector).getPropertyPath() + ".schema";
                        if (parameterName.equals(linkedSchema)) {
                            if (GenericNodeConnector.INPUT_CONNECTOR.equals(connector.getName())) {
                                param.setContext(EConnectionType.FLOW_MAIN.getName());
                            } else {
                                param.setContext(connector.getName());
                            }
                            IElementParameterDefaultValue defaultValue = new ElementParameterDefaultValue();
                            Schema schema = ((SchemaProperty) widgetProperty).getValue();
                            defaultValue.setDefaultValue(new Schema.Parser().parse(schema.toString()));
                            param.getDefaultValues().add(defaultValue);
                        }
                    }
                }
            }
        }
        if (widgetProperty instanceof PresentationItem) {
            param.setValue(widgetProperty.getDisplayName());
        } else if (widgetProperty instanceof Property) {
            Property property = (Property) widgetProperty;
            param.setRequired(property.isRequired());
            param.setValue(getParameterValue(element, property, fieldType));
            boolean isNameProperty = IGenericConstants.NAME_PROPERTY.equals(param.getParameterName());
            if (EParameterFieldType.NAME_SELECTION_AREA.equals(fieldType) || EParameterFieldType.JSON_TABLE.equals(fieldType) || EParameterFieldType.CLOSED_LIST.equals(fieldType) || EParameterFieldType.CHECK.equals(fieldType) || isNameProperty) {
                // Disable context support for those filed types and name parameter.
                param.setSupportContext(false);
            } else {
                param.setSupportContext(isSupportContext(property));
            }
            property.setTaggedValue(IComponentConstants.SUPPORT_CONTEXT, param.isSupportContext());
            Object cmTV = property.getTaggedValue(IGenericConstants.IS_CONTEXT_MODE);
            param.setReadOnly(Boolean.valueOf(String.valueOf(cmTV)));
            boolean isDynamic = Boolean.valueOf(String.valueOf(property.getTaggedValue(IGenericConstants.IS_DYNAMIC)));
            param.setContextMode(isDynamic);
            List<?> values = property.getPossibleValues();
            if (values != null || EParameterFieldType.CLOSED_LIST.equals(fieldType)) {
                if (values == null) {
                    values = Collections.emptyList();
                }
                param.setPossibleValues(values);
                List<String> possVals = new ArrayList<>();
                List<String> possValsDisplay = new ArrayList<>();
                for (Object obj : values) {
                    String value = null;
                    String valueDisplay = null;
                    if (obj instanceof NamedThing) {
                        NamedThing nal = (NamedThing) obj;
                        value = nal.getName();
                        valueDisplay = nal.getDisplayName();
                    } else {
                        value = String.valueOf(obj);
                        valueDisplay = String.valueOf(obj);
                    }
                    String pvDisplayName = property.getPossibleValuesDisplayName(obj);
                    if (StringUtils.isNotBlank(pvDisplayName) && !"null".equals(pvDisplayName)) {
                        //$NON-NLS-1$
                        valueDisplay = pvDisplayName;
                    }
                    possVals.add(value);
                    possValsDisplay.add(valueDisplay);
                }
                param.setListItemsDisplayName(possValsDisplay.toArray(new String[0]));
                param.setListItemsDisplayCodeName(possValsDisplay.toArray(new String[0]));
                param.setListItemsValue(possVals.toArray(new String[0]));
            }
        } else if (fieldType != null && fieldType.equals(EParameterFieldType.TABLE) && widgetProperty instanceof Properties) {
            Properties table = (Properties) widgetProperty;
            Form mainForm = table.getForm(Form.MAIN);
            param.setDisplayName(mainForm.getTitle());
            //$NON-NLS-1$
            List<ElementParameter> parameters = getParametersFromForm(new FakeElement("table"), mainForm);
            // table is always empty by default
            param.setSupportContext(false);
            List<String> codeNames = new ArrayList<>();
            List<String> possValsDisplay = new ArrayList<>();
            for (ElementParameter curParam : parameters) {
                curParam.setFilter(null);
                curParam.setContext(null);
                curParam.setShowIf(null);
                curParam.setNotShowIf(null);
                curParam.setReadOnlyIf(null);
                curParam.setNotReadOnlyIf(null);
                curParam.setNoContextAssist(false);
                curParam.setRaw(false);
                curParam.setReadOnly(false);
                fillDefaultValsForListType(curParam);
                codeNames.add(curParam.getName());
                possValsDisplay.add(curParam.getDisplayName());
            }
            param.setListItemsDisplayName(possValsDisplay.toArray(new String[0]));
            param.setListItemsDisplayCodeName(codeNames.toArray(new String[0]));
            param.setListItemsValue(parameters.toArray(new ElementParameter[0]));
            String[] listItemsShowIf = new String[parameters.size()];
            String[] listItemsNotShowIf = new String[parameters.size()];
            param.setListItemsShowIf(listItemsShowIf);
            param.setListItemsNotShowIf(listItemsNotShowIf);
            param.setValue(GenericTableUtils.getTableValues(table, param));
            param.setBasedOnSchema(Boolean.valueOf(String.valueOf(widget.getConfigurationValue(Widget.HIDE_TOOLBAR_WIDGET_CONF))));
        }
        if (!param.isReadOnly()) {
            param.setReadOnly(widget.isReadonly() || element.isReadOnly());
        }
        // For issue TUP-16139
        if (EParameterFieldType.COMPONENT_REFERENCE.equals(fieldType) && param.getNumRow() == 2 && EComponentCategory.BASIC.equals(compCategory)) {
            param.setNumRow(1);
        }
        param.setSerialized(true);
        param.setDynamicSettings(true);
        // Avoid adding duplicate prameter.
        if (!parameterNames.contains(parameterName)) {
            elementParameters.add(param);
            parameterNames.add(parameterName);
        }
    }
    return elementParameters;
}
Also used : PropertyPathConnector(org.talend.components.api.component.PropertyPathConnector) INodeConnector(org.talend.core.model.process.INodeConnector) GenericNodeConnector(org.talend.designer.core.generic.model.GenericNodeConnector) Connector(org.talend.components.api.component.Connector) INode(org.talend.core.model.process.INode) PropertyPathConnector(org.talend.components.api.component.PropertyPathConnector) Form(org.talend.daikon.properties.presentation.Form) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) Widget(org.talend.daikon.properties.presentation.Widget) ComponentProperties(org.talend.components.api.properties.ComponentProperties) Properties(org.talend.daikon.properties.Properties) GenericElementParameter(org.talend.designer.core.generic.model.GenericElementParameter) ElementParameter(org.talend.designer.core.model.components.ElementParameter) IElementParameterDefaultValue(org.talend.core.model.process.IElementParameterDefaultValue) GenericElementParameter(org.talend.designer.core.generic.model.GenericElementParameter) List(java.util.List) ArrayList(java.util.ArrayList) Property(org.talend.daikon.properties.property.Property) SchemaProperty(org.talend.daikon.properties.property.SchemaProperty) EComponentCategory(org.talend.core.model.process.EComponentCategory) SchemaProperty(org.talend.daikon.properties.property.SchemaProperty) PresentationItem(org.talend.daikon.properties.PresentationItem) NamedThing(org.talend.daikon.NamedThing) SimpleNamedThing(org.talend.daikon.SimpleNamedThing) FakeElement(org.talend.designer.core.model.FakeElement) EParameterFieldType(org.talend.core.model.process.EParameterFieldType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IElementParameterDefaultValue(org.talend.core.model.process.IElementParameterDefaultValue) ElementParameterDefaultValue(org.talend.designer.core.model.components.ElementParameterDefaultValue)

Example 5 with IElementParameterDefaultValue

use of org.talend.core.model.process.IElementParameterDefaultValue in project tdi-studio-se by Talend.

the class Component method setupConnector.

/**
     * DOC nrousseau Comment method "setupConnector".
     *
     * @param node
     * @param rootProperty
     * @param paramName
     * @param schemaProperty
     * @param found
     * @return
     */
private boolean setupConnector(final INode node, List<ElementParameter> listParam, String paramName, SchemaProperty schemaProperty, boolean isOutput) {
    ComponentProperties rootProperty = node.getComponentProperties();
    boolean found = false;
    for (Connector connector : rootProperty.getPossibleConnectors(isOutput)) {
        if (!(schemaProperty.getValue() instanceof Schema)) {
            continue;
        }
        Schema schema = schemaProperty.getValue();
        if (connector instanceof PropertyPathConnector) {
            //$NON-NLS-1$
            String linkedSchema = ((PropertyPathConnector) connector).getPropertyPath() + ".schema";
            if (paramName.equals(linkedSchema)) {
                found = true;
                ElementParameter param = new ElementParameter(node);
                param.setName(paramName);
                param.setFieldType(EParameterFieldType.SCHEMA_REFERENCE);
                param.setShow(false);
                if (!isOutput) {
                    param.setContext(EConnectionType.FLOW_MAIN.getName());
                } else {
                    param.setContext(connector.getName());
                }
                param.setCategory(EComponentCategory.TECHNICAL);
                IElementParameterDefaultValue defaultValue = new ElementParameterDefaultValue();
                defaultValue.setDefaultValue(new Schema.Parser().parse(schema.toString()));
                param.getDefaultValues().add(defaultValue);
                listParam.add(param);
            }
        }
    }
    return found;
}
Also used : PropertyPathConnector(org.talend.components.api.component.PropertyPathConnector) INodeConnector(org.talend.core.model.process.INodeConnector) Connector(org.talend.components.api.component.Connector) NodeConnector(org.talend.designer.core.model.components.NodeConnector) IElementParameter(org.talend.core.model.process.IElementParameter) ElementParameter(org.talend.designer.core.model.components.ElementParameter) ComponentProperties(org.talend.components.api.properties.ComponentProperties) PropertyPathConnector(org.talend.components.api.component.PropertyPathConnector) IElementParameterDefaultValue(org.talend.core.model.process.IElementParameterDefaultValue) IElementParameterDefaultValue(org.talend.core.model.process.IElementParameterDefaultValue) ElementParameterDefaultValue(org.talend.designer.core.model.components.ElementParameterDefaultValue) Schema(org.apache.avro.Schema) ElementParameterParser(org.talend.core.model.process.ElementParameterParser)

Aggregations

IElementParameterDefaultValue (org.talend.core.model.process.IElementParameterDefaultValue)10 IElementParameter (org.talend.core.model.process.IElementParameter)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 INode (org.talend.core.model.process.INode)5 INodeConnector (org.talend.core.model.process.INodeConnector)5 IMetadataColumn (org.talend.core.model.metadata.IMetadataColumn)4 IMetadataTable (org.talend.core.model.metadata.IMetadataTable)4 Node (org.talend.designer.core.ui.editor.nodes.Node)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Command (org.eclipse.gef.commands.Command)3 CompoundCommand (org.eclipse.gef.commands.CompoundCommand)3 Button (org.eclipse.swt.widgets.Button)3 ComponentProperties (org.talend.components.api.properties.ComponentProperties)3 MetadataTable (org.talend.core.model.metadata.MetadataTable)3 IConnection (org.talend.core.model.process.IConnection)3 ConnectionItem (org.talend.core.model.properties.ConnectionItem)3 Item (org.talend.core.model.properties.Item)3 ERepositoryObjectType (org.talend.core.model.repository.ERepositoryObjectType)3