Exemplo n.º 1
0
 @Override
 public int category(Object element) {
   if (element instanceof RepositoryNode) {
     RepositoryNode node = (RepositoryNode) element;
     if (ERepositoryObjectType.METADATA_CON_COLUMN.equals(
         node.getProperties(EProperties.CONTENT_TYPE))) {
       RepositoryNode parent = node.getParent().getParent();
       if (parent != null
           && ERepositoryObjectType.METADATA_CON_TABLE.equals(
               parent.getProperties(EProperties.CONTENT_TYPE))
           && parent.getObject() instanceof MetadataTableRepositoryObject) {
         MetadataTableRepositoryObject tableObject =
             (MetadataTableRepositoryObject) parent.getObject();
         MetadataColumnRepositoryObject columnObject =
             (MetadataColumnRepositoryObject) ((RepositoryNode) element).getObject();
         MetadataColumn tColumn = columnObject.getTdColumn();
         EList<MetadataColumn> columns = tableObject.getTable().getColumns();
         for (int i = 0; i < columns.size(); i++) {
           MetadataColumn column = columns.get(i);
           if (column.getName() != null && column.getName().equals(tColumn.getName())) {
             return i;
           }
         }
       }
     }
   }
   return super.category(element);
 }
  /**
   * DOC zli Comment method "guessSchema".
   *
   * @param processDescription
   */
  public void guessSchema(ProcessDescription processDescription) {
    informationLabel.setText(
        "   " + Messages.getString("FileStep3.guessIsDone")); // $NON-NLS-1$ //$NON-NLS-2$
    // clear all items
    tableEditorView.getMetadataEditor().removeAll();
    List<MetadataColumn> columns = new ArrayList<MetadataColumn>();

    List<IMetadataColumn> listColumns = processDescription.getSchema().get(0).getListColumns();
    int size = listColumns.size();
    if (size > 0) {
      for (int i = 0; i < size; i++) {
        MetadataColumn metadataColumn = ConnectionFactory.eINSTANCE.createMetadataColumn();
        metadataColumn.setNullable(listColumns.get(i).isNullable());
        metadataColumn.setLength(listColumns.get(i).getLength());
        metadataColumn.setPattern(listColumns.get(i).getPattern());
        metadataColumn.setTalendType(listColumns.get(i).getTalendType());
        metadataColumn.setPrecision(listColumns.get(i).getPrecision());
        metadataColumn.setLabel(listColumns.get(i).getLabel());
        columns.add(i, metadataColumn);
      }
    }
    tableEditorView.getMetadataEditor().addAll(columns);
    tableEditorView.getTableViewerCreator().layout();
    informationLabel.setText(Messages.getString("FileStep3.guessTip")); // $NON-NLS-1$
  }
 private void addColumn(String label, FOXTreeNode node, EList columnList) {
   if (StringUtils.isEmpty(label)) {
     return;
   }
   MetadataEmfTableEditor editor = new MetadataEmfTableEditor();
   String columnName = label;
   if (columnName.contains(":")) { // $NON-NLS-1$
     columnName = columnName.split(":")[1]; // $NON-NLS-1$
   }
   columnName = columnName.replaceAll("[^a-zA-Z0-9]", "_"); // $NON-NLS-1$
   String dataType = node.getDataType();
   MetadataColumn metadataColumn = ConnectionFactory.eINSTANCE.createMetadataColumn();
   metadataColumn.setLabel(editor.getNextGeneratedColumnName(columnName, columnList));
   metadataColumn.setOriginalField(label);
   metadataColumn.setTalendType(dataType);
   columnList.add(metadataColumn);
   node.setColumn(ConvertionHelper.convertToIMetaDataColumn(metadataColumn));
 }
 private boolean isColumnTaggedAsReadonly(
     org.talend.core.model.metadata.builder.connection.MetadataTable table, String columnName) {
   if (table == null || columnName == null) {
     return false;
   }
   EList<org.talend.core.model.metadata.builder.connection.MetadataColumn> columns =
       table.getColumns();
   for (org.talend.core.model.metadata.builder.connection.MetadataColumn newColumn : columns) {
     if (columnName.equals(newColumn.getLabel())) {
       EList<TaggedValue> taggedValues = newColumn.getTaggedValue();
       for (TaggedValue taggedValue : taggedValues) {
         if (DiSchemaConstants.TALEND6_IS_READ_ONLY.equals(taggedValue.getTag())) {
           return Boolean.valueOf(taggedValue.getValue());
         }
       }
     }
   }
   return false;
 }
  /**
   * DOC ocarbone Comment method "refreshMetaData".
   *
   * @param csvArray
   */
  public void refreshMetaDataTable(final CsvArray csvArray, ProcessDescription processDescription) {
    informationLabel.setText(
        "   " + Messages.getString("FileStep3.guessIsDone")); // $NON-NLS-1$ //$NON-NLS-2$

    // clear all items
    tableEditorView.getMetadataEditor().removeAll();

    List<MetadataColumn> columns = new ArrayList<MetadataColumn>();

    if (csvArray == null || csvArray.getRows().isEmpty()) {
      return;
    } else {

      List<String[]> csvRows = csvArray.getRows();
      String[] fields = csvRows.get(0);
      // int numberOfCol = fields.size();

      Integer numberOfCol = getRightFirstRow(csvRows);

      // 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.intValue()];
      for (int i = 0; i < numberOfCol; i++) {
        label[i] = DEFAULT_LABEL + i;
        if (firstRowToExtractMetadata == 0) {
          label[i] =
              "" + processDescription.getSchema().get(0).getListColumns().get(i); // $NON-NLS-1$
        }
      }

      for (int i = 0; i < numberOfCol.intValue(); 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) {
              globalType = "id_String"; // $NON-NLS-1$
            } 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) {
              globalType = "String"; // $NON-NLS-1$
            } 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("")) { // $NON-NLS-1$
              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 preferenceStore = null;
              if (GlobalServiceRegister.getDefault()
                  .isServiceRegistered(IDesignerCoreUIService.class)) {
                IDesignerCoreUIService designerCoreUiService =
                    (IDesignerCoreUIService)
                        GlobalServiceRegister.getDefault().getService(IDesignerCoreUIService.class);
                preferenceStore = designerCoreUiService.getPreferenceStore();
              }
              if (preferenceStore != null
                  && preferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_TYPE)
                      != null
                  && !preferenceStore
                      .getString(MetadataTypeLengthConstants.VALUE_DEFAULT_TYPE)
                      .equals("")) { // $NON-NLS-1$
                globalType =
                    preferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_TYPE);
                if (preferenceStore.getString(MetadataTypeLengthConstants.VALUE_DEFAULT_LENGTH)
                        != null
                    && !preferenceStore
                        .getString(MetadataTypeLengthConstants.VALUE_DEFAULT_LENGTH)
                        .equals("")) { // $NON-NLS-1$
                  lengthValue =
                      Integer.parseInt(
                          preferenceStore.getString(
                              MetadataTypeLengthConstants.VALUE_DEFAULT_LENGTH));
                }
              }
            }
          }
        }

        // define the metadataColumn to field i
        MetadataColumn metadataColumn = ConnectionFactory.eINSTANCE.createMetadataColumn();
        // 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);

        // Check the label and add it to the table
        metadataColumn.setLabel(
            tableEditorView.getMetadataEditor().getNextGeneratedColumnName(label[i]));
        columns.add(i, metadataColumn);
      }
    }
    tableEditorView.getMetadataEditor().addAll(columns);
    checkFieldsValue();
    tableEditorView.getTableViewerCreator().layout();
    tableEditorView.getTableViewerCreator().getTable().deselectAll();
    informationLabel.setText(Messages.getString("FileStep3.guessTip")); // $NON-NLS-1$
  }
  public void refreshMetaDataTable(final CsvArray csvArray, ProcessDescription processDescription) {
    informationLabel.setText(
        "   " + Messages.getString("FileStep3.guessIsDone")); // $NON-NLS-1$ //$NON-NLS-2$
    // clear all items
    tableEditorView.getMetadataEditor().removeAll();

    List<MetadataColumn> columns = new ArrayList<MetadataColumn>();
    if (csvArray == null || csvArray.getRows().isEmpty()) {
      return;
    } else {

      List<String[]> csvRows = csvArray.getRows();
      Integer numberOfCol = getRightFirstRow(csvRows);

      // define the label to the metadata width the content of the first row
      int firstRowToExtractMetadata = 0;
      List<IMetadataColumn> listColumns = processDescription.getSchema().get(0).getListColumns();
      // the first rows is used to define the label of any metadata
      String[] label = new String[numberOfCol.intValue()];
      for (int i = 0; i < numberOfCol; i++) {
        label[i] = DEFAULT_LABEL + i;
        if (firstRowToExtractMetadata == 0) {

          label[i] = "" + listColumns.get(i); // $NON-NLS-1$
        }
      }

      for (int i = 0; i < numberOfCol.intValue(); i++) {
        // define the first currentType and assimile it to globalType
        String globalType = null;
        int lengthValue = 0;
        int precisionValue = 0;
        boolean nullAble = true;

        int current = firstRowToExtractMetadata;
        while (globalType == null) {
          String value = csvRows.get(current)[i];
          if (LanguageManager.getCurrentLanguage() == ECodeLanguage.JAVA) {
            if (i >= csvRows.get(current).length) {
              globalType = "id_String"; // $NON-NLS-1$
            } else {
              if (value != null && !"".equals(value)) { // $NON-NLS-1$
                globalType = JavaDataTypeHelper.getTalendTypeOfValue(value);
              } else {
                globalType = listColumns.get(i).getTalendType();
              }
              current++;
            }
          } else {
            if (i >= csvRows.get(current).length) {
              globalType = "String"; // $NON-NLS-1$
            } else {
              globalType = PerlDataTypeHelper.getTalendTypeOfValue(value);
              current++;
            }
          }
        }
        nullAble = listColumns.get(i).isNullable();
        lengthValue = listColumns.get(i).getLength();
        precisionValue = listColumns.get(i).getPrecision();

        // define the metadataColumn to field i
        MetadataColumn metadataColumn = ConnectionFactory.eINSTANCE.createMetadataColumn();
        // Convert javaType to TalendType
        String talendType = null;
        if (LanguageManager.getCurrentLanguage() == ECodeLanguage.JAVA) {
          talendType = globalType;
          if (globalType.equals(JavaTypesManager.FLOAT.getId())
              || globalType.equals(JavaTypesManager.DOUBLE.getId())) {
            metadataColumn.setPrecision(precisionValue);
          } else {
            metadataColumn.setPrecision(0);
          }
        } else {
          talendType =
              PerlTypesManager.getNewTypeName(
                  MetadataTalendType.loadTalendType(
                      globalType, "TALENDDEFAULT", false)); // $NON-NLS-1$
          if (globalType.equals("FLOAT")
              || globalType.equals("DOUBLE")) { // $NON-NLS-1$ //$NON-NLS-2$
            metadataColumn.setPrecision(precisionValue);
          } else {
            metadataColumn.setPrecision(0);
          }
        }

        metadataColumn.setNullable(nullAble);
        metadataColumn.setTalendType(talendType);
        metadataColumn.setLength(lengthValue);
        // bug 6758
        if (talendType.equals("id_Date")) { // $NON-NLS-1$
          String pattern = listColumns.get(i).getPattern();
          metadataColumn.setPattern(pattern);
        }
        // Check the label and add it to the table
        metadataColumn.setLabel(
            tableEditorView.getMetadataEditor().getNextGeneratedColumnName(label[i]));
        columns.add(i, metadataColumn);
      }
    }
    // tableEditorView.getMetadataEditor().registerDataList(columns);
    tableEditorView.getMetadataEditor().addAll(columns);

    checkFieldsValue();
    tableEditorView.getTableViewerCreator().layout();
    informationLabel.setText(Messages.getString("FileStep3.guessTip")); // $NON-NLS-1$
  }