Ejemplo n.º 1
0
  public Mapping getMapping(boolean checkForMissingTableAndMapping) {
    String tableName = "";
    if (!Const.isEmpty(m_existingTableNamesCombo.getText().trim())) {
      tableName = m_existingTableNamesCombo.getText().trim();

      if (tableName.indexOf('@') > 0) {
        tableName = tableName.substring(0, tableName.indexOf('@'));
      }
    }

    if (checkForMissingTableAndMapping
        && (Const.isEmpty(m_existingTableNamesCombo.getText().trim())
            || Const.isEmpty(tableName))) {
      MessageDialog.openError(
          m_shell,
          Messages.getString("MappingDialog.Error.Title.MissingTableMappingName"),
          Messages.getString("MappingDialog.Error.Message.MissingTableMappingName"));
      return null;
    }

    // do we have any non-empty rows in the table?
    if (m_fieldsView.nrNonEmpty() == 0) {
      MessageDialog.openError(
          m_shell,
          Messages.getString("MappingDialog.Error.Title.NoFieldsDefined"),
          Messages.getString("MappingDialog.Error.Message.NoFieldsDefined"));
      return null;
    }
    // do we have a key defined in the table?
    Mapping theMapping = new Mapping(tableName, m_existingMappingNamesCombo.getText().trim());
    boolean keyDefined = false;
    boolean moreThanOneKey = false;
    List<String> missingFamilies = new ArrayList<String>();
    List<String> missingColumnNames = new ArrayList<String>();
    List<String> missingTypes = new ArrayList<String>();

    int nrNonEmpty = m_fieldsView.nrNonEmpty();

    // is the mapping a tuple mapping?
    boolean isTupleMapping = false;
    int tupleIdCount = 0;
    if (nrNonEmpty == 5) {
      for (int i = 0; i < nrNonEmpty; i++) {
        if (m_fieldsView.getNonEmpty(i).getText(1).equals("KEY")
            || m_fieldsView.getNonEmpty(i).getText(1).equals("Family")
            || m_fieldsView.getNonEmpty(i).getText(1).equals("Column")
            || m_fieldsView.getNonEmpty(i).getText(1).equals("Value")
            || m_fieldsView.getNonEmpty(i).getText(1).equals("Timestamp")) {
          tupleIdCount++;
        }
      }
    }
    /*    if (nrNonEmpty == 5 && m_fieldsView.getNonEmpty(0).getText(1).equals("KEY") &&
        m_fieldsView.getNonEmpty(1).getText(1).equals("Family") &&
        m_fieldsView.getNonEmpty(2).getText(1).equals("Column") &&
        m_fieldsView.getNonEmpty(3).getText(1).equals("Value") &&
        m_fieldsView.getNonEmpty(4).getText(1).equals("Timestamp")) {
      isTupleMapping = true;
      theMapping.setTupleMapping(true);
    } */
    if (tupleIdCount == 5) {
      isTupleMapping = true;
      theMapping.setTupleMapping(true);
    }

    for (int i = 0; i < nrNonEmpty; i++) {
      TableItem item = m_fieldsView.getNonEmpty(i);
      boolean isKey = false;
      String alias = null;
      if (!Const.isEmpty(item.getText(1))) {
        alias = item.getText(1).trim();
      }
      if (!Const.isEmpty(item.getText(2))) {
        isKey = item.getText(2).trim().equalsIgnoreCase("Y");

        if (isKey && keyDefined) {
          // more than one key, break here
          moreThanOneKey = true;
          break;
        }
        if (isKey) {
          keyDefined = true;
        }
      }
      // String family = null;
      String family = "";
      if (!Const.isEmpty(item.getText(3))) {
        family = item.getText(3);
      } else {
        if (!isKey && !isTupleMapping) {
          missingFamilies.add(item.getText(0));
        }
      }
      // String colName = null;
      String colName = "";
      if (!Const.isEmpty(item.getText(4))) {
        colName = item.getText(4);
      } else {
        if (!isKey && !isTupleMapping) {
          missingColumnNames.add(item.getText(0));
        }
      }
      String type = null;
      if (!Const.isEmpty(item.getText(5))) {
        type = item.getText(5);
      } else {
        missingTypes.add(item.getText(0));
      }
      String indexedVals = null;
      if (!Const.isEmpty(item.getText(6))) {
        indexedVals = item.getText(6);
      } /* else {
          if (!isKey && type != null && type.equalsIgnoreCase("String")) {
            missingIndexedValues.add(item.getText(0));
          }
        }*/

      // only add if we have all data and its all correct
      if (isKey && !moreThanOneKey) {
        if (Const.isEmpty(alias)) {
          // pop up an error dialog - key must have an alias because it does not
          // belong to a column family or have a column name
          MessageDialog.openError(
              m_shell,
              Messages.getString("MappingDialog.Error.Title.NoAliasForKey"),
              Messages.getString("MappingDialog.Error.Message.NoAliasForKey"));
          return null;
        }

        if (Const.isEmpty(type)) {
          // pop up an error dialog - must have a type for the key
          MessageDialog.openError(
              m_shell,
              Messages.getString("MappingDialog.Error.Title.NoTypeForKey"),
              Messages.getString("MappingDialog.Error.Message.NoTypeForKey"));
          return null;
        }

        if (moreThanOneKey) {
          // popup an error and then return
          MessageDialog.openError(
              m_shell,
              Messages.getString("MappingDialog.Error.Title.MoreThanOneKey"),
              Messages.getString("MappingDialog.Error.Message.MoreThanOneKey"));
          return null;
        }

        if (isTupleMapping) {
          theMapping.setKeyName(alias);
          theMapping.setTupleFamilies(family);
        } else {
          theMapping.setKeyName(alias);
        }
        try {
          theMapping.setKeyTypeAsString(type);
        } catch (Exception ex) {

        }
      } else {
        // don't bother adding if there are any errors
        if (missingFamilies.size() == 0
            && missingColumnNames.size() == 0
            && missingTypes.size() == 0) {
          String combinedName = family + HBaseValueMeta.SEPARATOR + colName;
          if (!Const.isEmpty(alias)) {
            combinedName += (HBaseValueMeta.SEPARATOR + alias);
          }
          HBaseValueMeta vm = new HBaseValueMeta(combinedName, 0, -1, -1);
          try {
            vm.setHBaseTypeFromString(type);
          } catch (IllegalArgumentException e) {
            // TODO pop up an error dialog for this one
            return null;
          }
          if (vm.isString() && indexedVals != null && indexedVals.length() > 0) {
            Object[] vals = HBaseValueMeta.stringIndexListToObjects(indexedVals);
            vm.setIndex(vals);
            vm.setStorageType(ValueMetaInterface.STORAGE_TYPE_INDEXED);
          }

          try {
            theMapping.addMappedColumn(vm, isTupleMapping);
          } catch (Exception ex) {
            // popup an error if this family:column is already in the mapping and
            // then return.
            MessageDialog.openError(
                m_shell,
                Messages.getString("MappingDialog.Error.Title.DuplicateColumn"),
                Messages.getString("MappingDialog.Error.Message1.DuplicateColumn")
                    + family
                    + HBaseValueMeta.SEPARATOR
                    + colName
                    + Messages.getString("MappingDialog.Error.Message2.DuplicateColumn"));
            ex.printStackTrace();
            return null;
          }
        }
      }
    }

    // now check for any errors in our Lists
    if (!keyDefined) {
      MessageDialog.openError(
          m_shell,
          Messages.getString("MappingDialog.Error.Title.NoKeyDefined"),
          Messages.getString("MappingDialog.Error.Message.NoKeyDefined"));
      return null;
    }

    if (missingFamilies.size() > 0 || missingColumnNames.size() > 0 || missingTypes.size() > 0) {
      StringBuffer buff = new StringBuffer();
      buff.append(
          Messages.getString("MappingDialog.Error.Message.IssuesPreventingSaving") + ":\n\n");
      if (missingFamilies.size() > 0) {
        buff.append(Messages.getString("MappingDialog.Error.Message.FamilyIssue") + ":\n");
        buff.append(missingFamilies.toString()).append("\n\n");
      }
      if (missingColumnNames.size() > 0) {
        buff.append(Messages.getString("MappingDialog.Error.Message.ColumnIssue") + ":\n");
        buff.append(missingColumnNames.toString()).append("\n\n");
      }
      if (missingTypes.size() > 0) {
        buff.append(Messages.getString("MappingDialog.Error.Message.TypeIssue") + ":\n");
        buff.append(missingTypes.toString()).append("\n\n");
      }

      MessageDialog.openError(
          m_shell,
          Messages.getString("MappingDialog.Error.Title.IssuesPreventingSaving"),
          buff.toString());
      return null;
    }

    return theMapping;
  }