Exemplo n.º 1
0
  public void execute(String actionCommand) {
    ImportFieldParams param = new ImportFieldParams();

    IWindow v = PluginServices.getMDIManager().getActiveWindow();
    if (v instanceof Table) {
      Table table = (Table) v;
      param.setLockTable(true);
      try {
        param.setTable(table.getModel());
      } catch (ReadDriverException e) {
        NotificationManager.showMessageError(
            PluginServices.getText(null, "Failed_filling_table"), e);
      }
    }

    ImageIcon logo =
        new javax.swing.ImageIcon(
            this.getClass().getClassLoader().getResource("images/package_graphics.png"));

    WizardAndami wizard = new WizardAndami(logo);
    // Adds the wizard panels:

    LinkDefinitionPanel linkPanel = new LinkDefinitionPanel(wizard.getWizardComponents(), param);
    wizard.getWizardComponents().addWizardPanel(linkPanel);

    ImportFieldPanel fieldsPanel = new ImportFieldPanel(wizard.getWizardComponents(), param);
    wizard.getWizardComponents().addWizardPanel(fieldsPanel);

    wizard.getWizardComponents().getFinishButton().setEnabled(false);
    wizard.getWindowInfo().setWidth(640);
    wizard.getWindowInfo().setHeight(350);
    wizard.getWindowInfo().setTitle(PluginServices.getText(this, "import_fields"));

    wizard.getWizardComponents().setFinishAction(new ImportFieldsAction(wizard, param));

    PluginServices.getMDIManager().addWindow(wizard);
  }
Exemplo n.º 2
0
  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();
      }
    }
  }