Search in sources :

Example 1 with AutoApi

use of org.talend.uniserv.AutoApi in project tdi-studio-se by Talend.

the class RetrieveSchemaHelper method retrieveSchemasCommand.

public static Command retrieveSchemasCommand(Node node) {
    IElementParameter param = node.getElementParameter("SCHEMA");
    IMetadataTable inputMeta = node.getMetadataFromConnector("FLOW");
    IMetadataTable inputMetaCopy = inputMeta.clone(true);
    IElementParameter outParam = node.getElementParameter("SCHEMA_OUT");
    IMetadataTable outputMeta = node.getMetadataFromConnector(outParam.getContext());
    IMetadataTable outputMetaCopy = outputMeta.clone(true);
    File xmlFile = new File(TalendTextUtils.removeQuotes(node.getElementParameter("PATH_JOBDEF").getValue().toString()));
    if (!xmlFile.exists())
        try {
            xmlFile.createNewFile();
        } catch (IOException e1) {
            ExceptionHandler.process(e1);
        }
    SAXReader saxReader = new SAXReader();
    Document document;
    AutoApi a = null;
    try {
        // get the schema file from server
        a = new AutoApi();
        String hostName = TalendTextUtils.removeQuotes(node.getElementParameter("HOSTNAME").getValue().toString());
        int port = Integer.parseInt(TalendTextUtils.removeQuotes(node.getElementParameter("PORT").getValue().toString()));
        String mandant = TalendTextUtils.removeQuotes(node.getElementParameter("MANDANT").getValue().toString());
        String userName = TalendTextUtils.removeQuotes(node.getElementParameter("USERNAME").getValue().toString());
        String passWord = TalendTextUtils.removeQuotes(node.getElementParameter("PASSWORD").getValue().toString());
        String jobDir = TalendTextUtils.removeQuotes(node.getElementParameter("JOB_DIR").getValue().toString());
        String jobName = TalendTextUtils.removeQuotes(node.getElementParameter("JOB_NAME").getValue().toString());
        String jobDef = TalendTextUtils.removeQuotes(node.getElementParameter("PATH_JOBDEF").getValue().toString());
        a.openConnection(hostName, port, mandant, userName, passWord);
        a.getJobDefinitionFile(jobDir, jobName, jobDef);
        document = saxReader.read(xmlFile);
        List inputList = document.selectNodes("//Job//Lines//Line//Steps//Input//Sources//Source//Format//Fields//Field");
        List inputMetaColumnList = new ArrayList<MetadataColumn>();
        for (int i = 0; i < inputList.size(); i++) {
            IMetadataColumn imc = new MetadataColumn();
            DefaultElement de = (DefaultElement) inputList.get(i);
            Element nameElement = de.element("Name");
            imc.setLabel(nameElement.getData().toString());
            Element lengthElement = de.element("Length");
            if (!"".equals(lengthElement.getData().toString()) && !"0".equals(lengthElement.getData().toString())) {
                imc.setLength(Integer.parseInt(lengthElement.getData().toString()));
            }
            Element typeElement = de.element("Type");
            JavaType javaType = JavaTypesManager.getJavaTypeFromName(typeElement.getData().toString());
            if (javaType != null) {
                imc.setTalendType(javaType.getId());
            } else {
                imc.setTalendType(JavaTypesManager.STRING.getId());
            }
            inputMetaColumnList.add(imc);
        }
        inputMetaCopy.setListColumns(inputMetaColumnList);
        List outputList = document.selectNodes("//Job//Lines//Line//Steps//Output//Targets//Target//Format//Fields//Field");
        List outputMetaColumnList = new ArrayList<MetadataColumn>();
        for (int i = 0; i < outputList.size(); i++) {
            IMetadataColumn imc = new MetadataColumn();
            DefaultElement de = (DefaultElement) outputList.get(i);
            Element nameElement = de.element("Name");
            imc.setLabel(nameElement.getData().toString());
            Element lengthElement = de.element("Length");
            if (!"".equals(lengthElement.getData().toString()) && !"0".equals(lengthElement.getData().toString())) {
                imc.setLength(Integer.parseInt(lengthElement.getData().toString()));
            }
            Element typeElement = de.element("Type");
            JavaType javaType = JavaTypesManager.getJavaTypeFromName(typeElement.getData().toString());
            if (javaType != null) {
                imc.setTalendType(javaType.getId());
            } else {
                imc.setTalendType(JavaTypesManager.STRING.getId());
            }
            outputMetaColumnList.add(imc);
        }
        outputMetaCopy.setListColumns(outputMetaColumnList);
        // set advanced setting info
        DefaultElement de = (DefaultElement) document.selectObject("//Job//Lines//Line//Steps//Input//Sources//Source//Format//FileInfo//Record//FieldSeparator");
        int separator = Integer.parseInt(de.getData().toString());
        node.getElementParameter("IN_FIELD_SEP").setValue(TalendTextUtils.addQuotes(new Character((char) separator).toString()));
        de = (DefaultElement) document.selectObject("//Job//Lines//Line//Steps//Input//Sources//Source//Format//FileInfo//Record//HeaderRecordCount");
        node.getElementParameter("IN_HEADER_COUNT").setValue(de.getData().toString());
        de = (DefaultElement) document.selectObject("//Job//Lines//Line//Steps//Input//Sources//Source//Format//FileInfo//FileLocation//Directory");
        node.getElementParameter("IN_DIR").setValue(TalendTextUtils.addQuotes(de.getData().toString()));
        de = (DefaultElement) document.selectObject("//Job//Lines//Line//Steps//Input//Sources//Source//Format//FileInfo//FileLocation//FileName");
        node.getElementParameter("IN_FILENAME").setValue(TalendTextUtils.addQuotes(de.getData().toString()));
        de = (DefaultElement) document.selectObject("//Job//Lines//Line//Steps//Input//Sources//Source//Format//FileInfo//FileLocation");
        node.getElementParameter("IN_MODE").setValue(de.attribute("Mode").getValue());
        de = (DefaultElement) document.selectObject("//Job//Lines//Line//Steps//Output//Targets//Target//Format//FileInfo//Record//FieldSeparator");
        separator = Integer.parseInt(de.getData().toString());
        node.getElementParameter("OUT_FIELD_SEP").setValue(TalendTextUtils.addQuotes(new Character((char) separator).toString()));
        de = (DefaultElement) document.selectObject("//Job//Lines//Line//Steps//Output//Targets//Target//Format//FileInfo//Record//HeaderRecordCount");
        node.getElementParameter("OUT_HEADER_COUNT").setValue(de.getData().toString());
        de = (DefaultElement) document.selectObject("//Job//Lines//Line//Steps//Output//Targets//Target//Format//FileInfo//FileLocation//Directory");
        node.getElementParameter("OUT_DIR").setValue(TalendTextUtils.addQuotes(de.getData().toString()));
        de = (DefaultElement) document.selectObject("//Job//Lines//Line//Steps//Output//Targets//Target//Format//FileInfo//FileLocation//FileName");
        node.getElementParameter("OUT_FILENAME").setValue(TalendTextUtils.addQuotes(de.getData().toString()));
        de = (DefaultElement) document.selectObject("//Job//Lines//Line//Steps//Output//Targets//Target//Format//FileInfo//FileLocation");
        node.getElementParameter("OUT_MODE").setValue(de.attribute("Mode").getValue());
    } catch (Exception e) {
        ExceptionHandler.process(e);
    } finally {
        try {
            a.closeConnection();
        } catch (Exception e) {
            ExceptionHandler.process(e);
        }
    }
    CompoundCommand cc = new CompoundCommand();
    cc.add(new ChangeMetadataCommand(node, param, inputMeta, inputMetaCopy));
    cc.add(new ChangeMetadataCommand(node, param, outputMeta, outputMetaCopy));
    return cc;
}
Also used : DefaultElement(org.dom4j.tree.DefaultElement) SAXReader(org.dom4j.io.SAXReader) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IMetadataColumn(org.talend.core.model.metadata.IMetadataColumn) Document(org.dom4j.Document) IOException(java.io.IOException) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) IMetadataTable(org.talend.core.model.metadata.IMetadataTable) IMetadataColumn(org.talend.core.model.metadata.IMetadataColumn) MetadataColumn(org.talend.core.model.metadata.MetadataColumn) JavaType(org.talend.core.model.metadata.types.JavaType) ChangeMetadataCommand(org.talend.designer.core.ui.editor.cmd.ChangeMetadataCommand) IElementParameter(org.talend.core.model.process.IElementParameter) AutoApi(org.talend.uniserv.AutoApi) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Document (org.dom4j.Document)1 Element (org.dom4j.Element)1 SAXReader (org.dom4j.io.SAXReader)1 DefaultElement (org.dom4j.tree.DefaultElement)1 CompoundCommand (org.eclipse.gef.commands.CompoundCommand)1 IMetadataColumn (org.talend.core.model.metadata.IMetadataColumn)1 IMetadataTable (org.talend.core.model.metadata.IMetadataTable)1 MetadataColumn (org.talend.core.model.metadata.MetadataColumn)1 JavaType (org.talend.core.model.metadata.types.JavaType)1 IElementParameter (org.talend.core.model.process.IElementParameter)1 ChangeMetadataCommand (org.talend.designer.core.ui.editor.cmd.ChangeMetadataCommand)1 AutoApi (org.talend.uniserv.AutoApi)1