public InternalField cloneInternalField() {
   FieldDescription copyFldDesc = fieldDesc.cloneField();
   InternalField newFld =
       new InternalField(copyFldDesc, fieldType, new Integer(fieldId.intValue()));
   newFld.setFieldIndex(fieldIndex);
   return newFld;
 }
Exemple #2
0
  private Value getValue(Object obj) {
    int typeField = fieldDescriptor.getFieldType();
    Value value = null; // ValueFactory.createNullValue();

    if (obj instanceof Number) {
      if (typeField == Types.DOUBLE || typeField == Types.NUMERIC) {
        double dv = ((Number) obj).doubleValue();
        value = ValueFactory.createValue(dv);
      } else if (typeField == Types.FLOAT) {
        float df = ((Number) obj).floatValue();
        value = ValueFactory.createValue(df);
      } else if (typeField == Types.INTEGER) {
        int di = ((Number) obj).intValue();
        value = ValueFactory.createValue(di);
      } else if (typeField == Types.BIGINT) {
        long di = ((Number) obj).longValue();
        value = ValueFactory.createValue(di);
      } else if (typeField == Types.VARCHAR) {
        String s = ((Number) obj).toString();
        value = ValueFactory.createValue(s);
      } else if (typeField == Types.BOOLEAN) {
        if (((Number) obj).intValue() == 0) {
          value = ValueFactory.createValue(false);
        } else {
          value = ValueFactory.createValue(true);
        }
      }
    } else if (obj instanceof Date) {
      if (typeField == Types.DATE) {
        Date date = (Date) obj;
        value = ValueFactory.createValue(date);
      } else if (typeField == Types.VARCHAR) {
        String s = ((Date) obj).toString();
        value = ValueFactory.createValue(s);
      }
    } else if (obj instanceof Boolean) {
      if (typeField == Types.BOOLEAN) {
        boolean b = ((Boolean) obj).booleanValue();
        value = ValueFactory.createValue(b);
      } else if (typeField == Types.VARCHAR) {
        String s = ((Boolean) obj).toString();
        value = ValueFactory.createValue(s);
      }
    } else if (obj instanceof String) {
      if (typeField == Types.VARCHAR) {
        String s = obj.toString();
        value = ValueFactory.createValue(s);
      }
    } else {
      value = ValueFactory.createNullValue();
    }

    return value;
  }
  public void doImportField(ImportFieldParams params) throws Exception {

    if (!params.isValid()) {
      // TODO: ver que excepcion a lanzar
      throw new Exception("invalid Paramenters: " + params.getValidationMsg());
    }

    IEditableSource edSource = null;
    IEditableSource edSourceToImport = params.getTableToImport().getModelo();

    SelectableDataSource rsSourceToImport = edSourceToImport.getRecordset();

    ArrayList fieldsToImport = new ArrayList();
    ArrayList fieldsToImport_des = new ArrayList();
    ArrayList fieldsToImport_pos = new ArrayList();
    Iterator iter;
    Map values;
    int i;

    try {
      rsSourceToImport.start();

      // Cargamos la lista con los campos que vamos a importar
      iter = params.getFieldsToImport().iterator();
      FielToImport fieldToImport;
      while (iter.hasNext()) {
        fieldToImport = (FielToImport) iter.next();
        if (fieldToImport.toImport) {
          fieldsToImport.add(fieldToImport);
        }
      }

      // Cargamos la lista de la definicio de capos desde la
      // tabla a importar
      iter = fieldsToImport.iterator();
      FieldDescription[] toImportAllFieldsDescription = edSourceToImport.getFieldsDescription();
      FieldDescription tmpFieldDesc, newFieldDesc;

      while (iter.hasNext()) {
        fieldToImport = (FielToImport) iter.next();
        for (i = 0; i < toImportAllFieldsDescription.length; i++) {
          tmpFieldDesc = toImportAllFieldsDescription[i];
          if (tmpFieldDesc.getFieldName().equals(fieldToImport.originalFieldName)) {
            newFieldDesc = tmpFieldDesc.cloneField();
            newFieldDesc.setFieldLength(tmpFieldDesc.getFieldLength());
            newFieldDesc.setFieldName(fieldToImport.fieldNameToUse);
            newFieldDesc.setDefaultValue(tmpFieldDesc.getDefaultValue());
            newFieldDesc.setFieldAlias(fieldToImport.fieldNameToUse);
            newFieldDesc.setFieldType(tmpFieldDesc.getFieldType());
            fieldsToImport_des.add(newFieldDesc);
            fieldsToImport_pos.add(new Integer(i));
          }
        }
      }

      // Cagamos los valores en un hash
      values =
          this.loadValuesFromSource(
              rsSourceToImport, params.getTableToImportField(), fieldsToImport_pos);
    } catch (Exception e) {
      throw e;
    } finally {

      rsSourceToImport.stop();
      rsSourceToImport = null;
      edSourceToImport = null;
    }

    FLyrVect layer = null;

    boolean changeEditing = false;
    // Ponemos en edicion si no lo esta
    if (params.getTable().getAssociatedTable() instanceof FLyrVect) {
      // Viene de una capa
      layer = (FLyrVect) params.getTable().getAssociatedTable();
      if (!layer.isEditing()) {
        layer.setEditing(true);
        changeEditing = true;
      }
      edSource = (VectorialEditableAdapter) layer.getSource();
    } else {
      // es una tabla normal
      edSource = params.getTable().getModelo();
      if (!edSource.isEditing()) {
        edSource.startEdition(EditionEvent.ALPHANUMERIC);
        changeEditing = true;
      }
    }

    edSource.startComplexRow();

    int originalFieldsCount = edSource.getRecordset().getFieldCount();
    int finalFieldsCount = originalFieldsCount + fieldsToImport.size();
    // Añadimos los campos
    iter = fieldsToImport_des.iterator();
    while (iter.hasNext()) {
      ((EditableAdapter) edSource).addField((FieldDescription) iter.next());
    }

    // Recorremos la fuente y vamos actualizando
    int rowCount = edSource.getRowCount();
    IRowEdited originalRow;
    IRow newRow;
    IRowEdited newRowEdited;
    Value[] finalValues;
    Value[] originalValues;
    Value[] valuesToUse;
    Value key;
    int column;
    int srcKeyPos = edSource.getRecordset().getFieldIndexByName(params.getTableField());
    for (i = 0; i < rowCount; i++) {
      originalRow = edSource.getRow(i);

      key = originalRow.getAttribute(srcKeyPos);
      valuesToUse = (Value[]) values.get(key);
      if (valuesToUse == null) {
        continue;
      }
      newRow = originalRow.getLinkedRow().cloneRow();
      originalValues = newRow.getAttributes();
      finalValues = new Value[finalFieldsCount];
      System.arraycopy(originalValues, 0, finalValues, 0, originalFieldsCount);
      for (column = 0; column < valuesToUse.length; column++) {
        finalValues[column + originalFieldsCount] = valuesToUse[column];
      }
      newRow.setAttributes(finalValues);
      newRowEdited = new DefaultRowEdited(newRow, IRowEdited.STATUS_MODIFIED, i);
      edSource.modifyRow(
          newRowEdited.getIndex(), newRowEdited.getLinkedRow(), "", EditionEvent.ALPHANUMERIC);
    }

    edSource.endComplexRow("Import fields");
    if (changeEditing) {
      if (layer == null) {
        IWriter writer = ((IWriteable) edSource).getWriter();
        writer.initialize(edSource.getTableDefinition());
        edSource.stopEdition(writer, EditionEvent.ALPHANUMERIC);
        edSource.getSelection().clear();

      } else {
        layer.setRecordset(edSource.getRecordset());
        ISpatialWriter spatialWriter =
            (ISpatialWriter) ((VectorialEditableAdapter) edSource).getWriter();
        ILayerDefinition lyrDef = EditionUtilities.createLayerDefinition(layer);
        spatialWriter.initialize(lyrDef);
        edSource.stopEdition(spatialWriter, EditionEvent.ALPHANUMERIC);
        layer.setEditing(false);
        edSource.getSelection().clear();
      }
    }
  }