Search in sources :

Example 16 with CsvArray

use of org.talend.core.utils.CsvArray in project tdi-studio-se by Talend.

the class JSONWizard method resetMetadata.

private void resetMetadata(List<SchemaTarget> schemaTarget, boolean flag) {
    // TODO
    JSONFileConnection connection2 = JSONConnectionContextUtils.getJSONOriginalValueConnection(connection, this.connectionItem, connection.isContextMode(), true);
    ProcessDescription processDescription = JSONShadowProcessHelper.getProcessDescription(connection2, getTempJsonPath());
    CsvArray csvArray = null;
    try {
        if (EJsonReadbyMode.JSONPATH.getValue().equals(getReadbyMode())) {
            //$NON-NLS-1$
            csvArray = JSONShadowProcessHelper.getCsvArray(processDescription, "FILE_JSON");
        } else {
            //$NON-NLS-1$
            csvArray = JSONShadowProcessHelper.getCsvArray(processDescription, "FILE_XML");
        }
    } catch (CoreException e) {
        ExceptionHandler.process(e);
    }
    List<MetadataColumn> newColumns = new ArrayList<MetadataColumn>();
    String file = ((JSONFileConnection) this.connectionItem.getConnection()).getJSONFilePath();
    if (connection.isContextMode()) {
        ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connectionItem.getConnection(), true);
        file = TalendQuoteUtils.removeQuotes(ConnectionContextHelper.getOriginalValue(contextType, file));
    }
    if (file != null) {
        // prepareColumnsFromXSD(file, newColumns, schemaTarget);
        return;
    }
    if (csvArray == null || csvArray.getRows().isEmpty()) {
        return;
    } else {
        List<String[]> csvRows = csvArray.getRows();
        String[] fields = csvRows.get(0);
        int numberOfCol = fields.length;
        // define the label to the metadata width the content of the first row
        int firstRowToExtractMetadata = 0;
        // the first rows is used to define the label of any metadata
        String[] label = new String[numberOfCol];
        for (int i = 0; i < numberOfCol; i++) {
            label[i] = DEFAULT_LABEL + i;
            if (firstRowToExtractMetadata == 0) {
                if (schemaTarget.get(i).getTagName() != null && !schemaTarget.get(i).getTagName().equals("")) {
                    //$NON-NLS-1$
                    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    label[i] = "" + schemaTarget.get(i).getTagName().trim().replaceAll(" ", "_");
                    label[i] = MetadataToolHelper.validateColumnName(label[i], i);
                }
            }
        }
        for (int i = 0; i < numberOfCol; i++) {
            // define the first currentType and assimile it to globalType
            String globalType = null;
            int lengthValue = 0;
            int precisionValue = 0;
            int current = firstRowToExtractMetadata;
            while (globalType == null) {
                if (LanguageManager.getCurrentLanguage() == ECodeLanguage.JAVA) {
                    if (i >= csvRows.get(current).length) {
                        //$NON-NLS-1$
                        globalType = "id_String";
                    } else {
                        globalType = JavaDataTypeHelper.getTalendTypeOfValue(csvRows.get(current)[i]);
                        current++;
                    // if (current == csvRows.size()) {
                    // globalType = "id_String"; //$NON-NLS-1$
                    // }
                    }
                } else {
                    if (i >= csvRows.get(current).length) {
                        //$NON-NLS-1$
                        globalType = "String";
                    } else {
                        globalType = PerlDataTypeHelper.getTalendTypeOfValue(csvRows.get(current)[i]);
                        current++;
                    // if (current == csvRows.size()) {
                    // globalType = "String"; //$NON-NLS-1$
                    // }
                    }
                }
            }
            // for another lines
            for (int f = firstRowToExtractMetadata; f < csvRows.size(); f++) {
                fields = csvRows.get(f);
                if (fields.length > i) {
                    String value = fields[i];
                    if (!value.equals("")) {
                        if (LanguageManager.getCurrentLanguage() == ECodeLanguage.JAVA) {
                            if (!JavaDataTypeHelper.getTalendTypeOfValue(value).equals(globalType)) {
                                globalType = JavaDataTypeHelper.getCommonType(globalType, JavaDataTypeHelper.getTalendTypeOfValue(value));
                            }
                        } else {
                            if (!PerlDataTypeHelper.getTalendTypeOfValue(value).equals(globalType)) {
                                globalType = PerlDataTypeHelper.getCommonType(globalType, PerlDataTypeHelper.getTalendTypeOfValue(value));
                            }
                        }
                        if (lengthValue < value.length()) {
                            lengthValue = value.length();
                        }
                        int positionDecimal = 0;
                        if (value.indexOf(',') > -1) {
                            positionDecimal = value.lastIndexOf(',');
                            precisionValue = lengthValue - positionDecimal;
                        } else if (value.indexOf('.') > -1) {
                            positionDecimal = value.lastIndexOf('.');
                            precisionValue = lengthValue - positionDecimal;
                        }
                    } else {
                        IPreferenceStore corePreferenceStore = null;
                        if (GlobalServiceRegister.getDefault().isServiceRegistered(IDesignerCoreUIService.class)) {
                            IDesignerCoreUIService designerCoreUiService = (IDesignerCoreUIService) GlobalServiceRegister.getDefault().getService(IDesignerCoreUIService.class);
                            corePreferenceStore = designerCoreUiService.getPreferenceStore();
                        }
                        if (corePreferenceStore != null && corePreferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_TYPE) != null && !corePreferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_TYPE).equals("")) {
                            //$NON-NLS-1$
                            globalType = corePreferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_TYPE);
                            if (corePreferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_LENGTH) != null && !corePreferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_LENGTH).equals("")) {
                                //$NON-NLS-1$
                                lengthValue = Integer.parseInt(corePreferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_LENGTH));
                            }
                        }
                    }
                }
            }
            // define the metadataColumn to field i
            MetadataColumn metadataColumn = ConnectionFactory.eINSTANCE.createMetadataColumn();
            // hshen bug7249
            //$NON-NLS-1$
            metadataColumn.setPattern("\"dd-MM-yyyy\"");
            // Convert javaType to TalendType
            String talendType = null;
            talendType = globalType;
            if (globalType.equals(JavaTypesManager.FLOAT.getId()) || globalType.equals(JavaTypesManager.DOUBLE.getId())) {
                metadataColumn.setPrecision(precisionValue);
            } else {
                metadataColumn.setPrecision(0);
            }
            metadataColumn.setTalendType(talendType);
            metadataColumn.setLength(lengthValue);
            metadataColumn.setLabel(label[i]);
            newColumns.add(i, metadataColumn);
        }
    }
    EList columns = ConnectionHelper.getTables(connection).toArray(new MetadataTable[0])[0].getColumns();
    if (!flag) {
        for (int i = 0; i < newColumns.size(); i++) {
            for (int j = 0; j < columns.size(); j++) {
                if (newColumns.get(i).getLabel().equals(((MetadataColumn) columns.get(j)).getLabel())) {
                    newColumns.remove(i);
                    newColumns.add(i, (MetadataColumn) columns.get(j));
                }
            }
        }
    }
    columns.clear();
    columns.addAll(newColumns);
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) JSONFileConnection(org.talend.repository.model.json.JSONFileConnection) ArrayList(java.util.ArrayList) IDesignerCoreUIService(org.talend.core.ui.services.IDesignerCoreUIService) ProcessDescription(org.talend.metadata.managment.ui.preview.ProcessDescription) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) CsvArray(org.talend.core.utils.CsvArray) EList(org.eclipse.emf.common.util.EList) CoreException(org.eclipse.core.runtime.CoreException) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore)

Example 17 with CsvArray

use of org.talend.core.utils.CsvArray in project tdi-studio-se by Talend.

the class JSONShadowProcessHelper method getCsvArray.

/**
     * parse a file describe by a processDescription in XmlArray.
     * 
     * @param processDescription
     * @return xmlArray
     */
public static CsvArray getCsvArray(final ProcessDescription processDescription, String type) throws CoreException {
    CsvArray csvArray = null;
    IPreview preview = createPreview(type);
    if (preview != null) {
        csvArray = preview.preview(processDescription, type);
    }
    return csvArray;
}
Also used : CsvArray(org.talend.core.utils.CsvArray) IPreview(org.talend.core.repository.model.preview.IPreview)

Example 18 with CsvArray

use of org.talend.core.utils.CsvArray in project tdi-studio-se by Talend.

the class JSONShadowProcess method run.

public CsvArray run() throws ProcessorException {
    IProcess talendProcess = buildProcess();
    IProcessor processor = ProcessorUtilities.getProcessor(talendProcess, null);
    processor.setProxyParameters(getProxyParameters());
    // try {
    // Delete previous Perl generated file
    File previousFile = outPath.toFile();
    if (previousFile.exists()) {
        previousFile.delete();
    }
    // Process ps = processor.run(process.getContextManager().getDefaultContext(), Processor.NO_STATISTICS,
    // Processor.NO_TRACES,Processor.WATCH_ALLOWED);//Old
    IContext context = talendProcess.getContextManager().getDefaultContext();
    processor.setContext(context);
    process = processor.run(IProcessor.NO_STATISTICS, IProcessor.NO_TRACES, null);
    ProcessStreamTrashReader.readAndForget(process);
    if (!outPath.toFile().exists()) {
        //$NON-NLS-1$
        throw new ProcessorException(Messages.getString("ShadowProcess.notGeneratedOutputException"));
    }
    try {
        CsvArray array = new CsvArray();
        array = array.createFrom(outPath.toFile(), currentProcessEncoding);
        return array;
    } catch (IOException ioe) {
        throw new ProcessorException(ioe);
    }
}
Also used : IContext(org.talend.core.model.process.IContext) ProcessorException(org.talend.designer.runprocess.ProcessorException) CsvArray(org.talend.core.utils.CsvArray) IProcessor(org.talend.designer.runprocess.IProcessor) IOException(java.io.IOException) IProcess(org.talend.core.model.process.IProcess) File(java.io.File)

Example 19 with CsvArray

use of org.talend.core.utils.CsvArray in project tdi-studio-se by Talend.

the class JSONShadowProcessHelper method getCsvArray.

/**
     * parse a file describe by a processDescription in XmlArray.
     * 
     * @param processDescription
     * @return xmlArray
     */
public static CsvArray getCsvArray(final ProcessDescription processDescription, String type, boolean errorOutoutAsException) throws CoreException {
    CsvArray csvArray = null;
    IPreview preview = createPreview();
    if (preview != null) {
        csvArray = preview.preview(processDescription, type, errorOutoutAsException);
    }
    return csvArray;
}
Also used : CsvArray(org.talend.core.utils.CsvArray) IPreview(org.talend.core.repository.model.preview.IPreview)

Aggregations

CsvArray (org.talend.core.utils.CsvArray)19 ProcessorException (org.talend.designer.runprocess.ProcessorException)10 File (java.io.File)7 CoreException (org.eclipse.core.runtime.CoreException)7 IOException (java.io.IOException)6 IProcessor (org.talend.designer.runprocess.IProcessor)5 Status (org.eclipse.core.runtime.Status)4 IContext (org.talend.core.model.process.IContext)4 IProcess (org.talend.core.model.process.IProcess)4 IProcessDescription (org.talend.core.repository.model.preview.IProcessDescription)4 ArrayList (java.util.ArrayList)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 IPreview (org.talend.core.repository.model.preview.IPreview)2 EShadowProcessType (org.talend.designer.runprocess.shadow.ShadowProcess.EShadowProcessType)2 EJSONShadowProcessType (org.talend.repository.json.ui.shadow.JSONShadowProcess.EJSONShadowProcessType)2 JSONFileConnection (org.talend.repository.model.json.JSONFileConnection)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1