private void getHNodesForWorksheet(HTable htable, Set<String> hnodes, RepFactory factory) {
   for (HNode hnode : htable.getHNodes()) {
     hnodes.add(hnode.getJSONArrayRepresentation(factory).toString());
     if (hnode.hasNestedTable()) {
       getHNodesForWorksheet(hnode.getNestedTable(), hnodes, factory);
     }
   }
 }
 private List<String> addHeaders(Worksheet wk, List<String> columnNames, RepFactory factory) {
   HTable headers = wk.getHeaders();
   ArrayList<String> headersList = new ArrayList<String>();
   for (int i = 0; i < columnNames.size(); i++) {
     HNode hNode = null;
     hNode = headers.addHNode(columnNames.get(i), HNodeType.Regular, wk, factory);
     headersList.add(hNode.getId());
   }
   return headersList;
 }
Example #3
0
 /**
  * Returns the HNodePath for this node.
  *
  * @param factory
  * @return the HNodePath for this node.
  */
 public HNodePath getHNodePath(RepFactory factory) {
   HNodePath p1 = new HNodePath(this);
   // get the table that it belongs to
   HTable t = factory.getHTable(hTableId);
   HNode parentNode = t.getParentHNode();
   if (parentNode != null) {
     HNodePath p2 = parentNode.getHNodePath(factory);
     p1 = HNodePath.concatenate(p2, p1);
   }
   return p1;
 }
Example #4
0
 private void addEmptyRow(Table nestedTable, HNode hNode) {
   HTable headersNestedTable = hNode.getNestedTable();
   Row emptyRow = nestedTable.addRow(getFactory());
   for (HNode nestedHNode : headersNestedTable.getHNodes()) {
     if (nestedHNode.hasNestedTable()) {
       addEmptyRow(emptyRow.getNode(nestedHNode.getId()).getNestedTable(), nestedHNode);
     } else {
       emptyRow.setValue(nestedHNode.getId(), "", getFactory());
     }
   }
 }
Example #5
0
 private void addListElement(Object listValue, HTable headers, Table dataTable)
     throws JSONException {
   if (listValue instanceof JSONObject) {
     Row row = dataTable.addRow(getFactory());
     JSONObject o = (JSONObject) listValue;
     @SuppressWarnings("unchecked")
     Iterator<String> it = o.sortedKeys();
     while (it.hasNext()) {
       String key = it.next();
       addObjectElement(key, o.get(key), headers, row);
     }
   } else if (isPrimitiveValue(listValue)) {
     HNode hNode = addHNode(headers, "values");
     String hNodeId = hNode.getId();
     Row row = dataTable.addRow(getFactory());
     // TODO, conserve the types of the primitive types.
     String value = "";
     if (listValue instanceof String || listValue instanceof Boolean) {
       value = (String) listValue;
     } else if (listValue instanceof Double) {
       value = Double.toString((Double) listValue);
     } else if (listValue instanceof Integer) {
       value = Integer.toString((Integer) listValue);
     } else if (listValue instanceof Long) {
       value = Long.toString((Long) listValue);
     } else {
       // Pedro 2012/09/14
       logger.error("Unexpected value in JSON array:" + listValue.toString());
     }
     row.setValue(hNodeId, value, getFactory());
   } else if (listValue instanceof JSONArray) {
     HNode hNode = addHNode(headers, "nested array");
     String hNodeId = hNode.getId();
     Row row = dataTable.addRow(getFactory());
     HTable nestedHTable = addNestedHTable(hNode, "nested array values", row);
     Table nestedTable = row.getNode(hNodeId).getNestedTable();
     JSONArray a = (JSONArray) listValue;
     for (int i = 0; i < a.length(); i++) {
       addListElement(a.get(i), nestedHTable, nestedTable);
     }
   } else {
     logger.error("Cannot handle whatever case is not covered by the if statements. Sorry.");
   }
 }
Example #6
0
  private HTable addNestedHTable(HNode hNode, String key, Row row) {
    HTable ht = hNode.getNestedTable();
    if (ht == null) {
      ht = hNode.addNestedTable(createNestedTableName(key), getWorksheet(), getFactory());

      // Check for all the nodes that have value and nested tables
      Collection<Node> nodes = new ArrayList<Node>();
      getWorksheet().getDataTable().collectNodes(hNode.getHNodePath(getFactory()), nodes);
      for (Node node : nodes) {
        if (node.getBelongsToRow() == row) break;

        // Add an empty row for each nested table that does not have any row
        if (node.getNestedTable().getNumRows() == 0) {
          addEmptyRow(node.getNestedTable(), hNode);
        }
      }
    }
    return ht;
  }
Example #7
0
  public JSONArray getJSONArrayRepresentation(RepFactory f) throws JSONException {
    JSONArray arr = new JSONArray();
    Stack<HNode> st = new Stack<HNode>();
    st.push(this);
    HTable t = f.getHTable(hTableId);
    HNode parentNode = t.getParentHNode();
    while (parentNode != null) {
      st.push(parentNode);
      t = f.getHTable(parentNode.getHTableId());
      parentNode = t.getParentHNode();
    }

    while (!st.isEmpty()) {
      HNode node = st.pop();
      JSONObject obj = new JSONObject();
      obj.put("columnName", node.getColumnName());
      arr.put(obj);
    }
    return arr;
  }
Example #8
0
  private void addObjectElement(String key, Object value, HTable headers, Row row)
      throws JSONException {
    HNode hNode = addHNode(headers, key);

    String hNodeId = hNode.getId();

    if (value instanceof String) {
      if (((String) value).isEmpty() && hNode.hasNestedTable()) {
        addEmptyRow(row.getNode(hNodeId).getNestedTable(), hNode);
      }
      row.setValue(hNodeId, (String) value, getFactory());
    } else if (value instanceof Integer) {
      row.setValue(hNodeId, value.toString(), getFactory());
    } else if (value instanceof Double) {
      row.setValue(hNodeId, value.toString(), getFactory());
    } else if (value instanceof Long) {
      row.setValue(hNodeId, value.toString(), getFactory());
    } else if (value instanceof Boolean) {
      row.setValue(hNodeId, value.toString(), getFactory());
    } else if (value instanceof JSONObject) {
      HTable nestedHTable = addNestedHTable(hNode, key, row);
      Table nestedTable = row.getNode(hNodeId).getNestedTable();
      addKeysAndValues((JSONObject) value, nestedHTable, nestedTable);
    } else if (value instanceof JSONArray) {
      HTable nestedHTable = addNestedHTable(hNode, key, row);
      Table nestedTable = row.getNode(hNodeId).getNestedTable();
      JSONArray a = (JSONArray) value;
      for (int i = 0; i < a.length(); i++) {
        addListElement(a.get(i), nestedHTable, nestedTable);
      }
    } else if (value == JSONObject.NULL) {
      // Ignore
    } else {
      throw new Error("Cannot handle " + key + ": " + value + " yet.");
    }
  }
  @Override
  public void generateJson(String prefix, PrintWriter pw, VWorkspace vWorkspace) {
    Workspace workspace = vWorkspace.getWorkspace();
    alignment = AlignmentManager.Instance().getAlignment(workspace.getId(), worksheetId);
    SemanticTypes types = worksheet.getSemanticTypes();
    Map<String, ColumnNode> hNodeIdTocolumnNodeMap = createColumnNodeMap();
    Map<String, SemanticTypeNode> hNodeIdToDomainNodeMap = createDomainNodeMap();

    JSONStringer jsonStr = new JSONStringer();
    try {
      JSONWriter writer = jsonStr.object();
      writer.key("worksheetId").value(worksheetId).key("updateType").value("SemanticTypesUpdate");

      writer.key(JsonKeys.Types.name());
      writer.array();
      // Iterate through all the columns
      for (HNodePath path : worksheet.getHeaders().getAllPaths()) {
        HNode node = path.getLeaf();
        String nodeId = node.getId();

        writer.object();

        // Check if a semantic type exists for the HNode
        SemanticType type = types.getSemanticTypeForHNodeId(nodeId);
        if (type != null && type.getConfidenceLevel() != SemanticType.ConfidenceLevel.Low) {
          writer
              .key(JsonKeys.HNodeId.name())
              .value(type.getHNodeId())
              .key(JsonKeys.SemanticTypesArray.name())
              .array();

          ColumnNode alignmentColumnNode = hNodeIdTocolumnNodeMap.get(type.getHNodeId());
          SemanticTypeNode domainNode = hNodeIdToDomainNodeMap.get(type.getHNodeId());

          if (alignmentColumnNode == null || domainNode == null) {
            logger.error(
                "Column node or domain node not found in alignment."
                    + " (This should not happen conceptually!):"
                    + type);
            continue;
          }

          // Add the primary semantic type
          writer
              .object()
              .key(JsonKeys.Origin.name())
              .value(type.getOrigin().name())
              .key(JsonKeys.ConfidenceLevel.name())
              .value(type.getConfidenceLevel().name())
              .key(JsonKeys.isPrimary.name())
              .value(true);

          // Add the RDF literal type to show in the text box
          String rdfLiteralType =
              alignmentColumnNode.getRdfLiteralType() == null
                  ? ""
                  : alignmentColumnNode.getRdfLiteralType().getDisplayName();
          String language =
              alignmentColumnNode.getLanguage() == null ? "" : alignmentColumnNode.getLanguage();
          writer.key(JsonKeys.rdfLiteralType.name()).value(rdfLiteralType);
          writer.key(JsonKeys.language.name()).value(language);

          //					String domainDisplayLabel = (domainNode.getLabel().getPrefix() != null &&
          // (!domainNode.getLabel().getPrefix().equals(""))) ?
          //							(domainNode.getLabel().getPrefix() + ":" + domainNode.getLocalId()) :
          // domainNode.getLocalId();
          if (!type.isClass()) {
            writer
                .key(JsonKeys.FullType.name())
                .value(type.getType().getUri())
                .key(JsonKeys.DisplayLabel.name())
                .value(type.getType().getDisplayName())
                .key(JsonKeys.DisplayRDFSLabel.name())
                .value(type.getType().getRdfsLabel())
                .key(JsonKeys.DisplayRDFSComment.name())
                .value(type.getType().getRdfsComment())
                .key(JsonKeys.DomainId.name())
                .value(domainNode.getId())
                .key(JsonKeys.DomainUri.name())
                .value(domainNode.getUri())
                .key(JsonKeys.DisplayDomainLabel.name())
                .value(domainNode.getDisplayId())
                .key(JsonKeys.DomainRDFSLabel.name())
                .value(domainNode.getRdfsLabel())
                .key(JsonKeys.DomainRDFSComment.name())
                .value(domainNode.getRdfsComment());
          } else {
            writer
                .key(JsonKeys.FullType.name())
                .value(domainNode.getId())
                .key(JsonKeys.DisplayLabel.name())
                .value(domainNode.getDisplayId())
                .key(JsonKeys.DisplayRDFSLabel.name())
                .value(domainNode.getRdfsLabel())
                .key(JsonKeys.DisplayRDFSComment.name())
                .value(domainNode.getRdfsComment())
                .key(JsonKeys.DomainId.name())
                .value("")
                .key(JsonKeys.DomainUri.name())
                .value("")
                .key(JsonKeys.DisplayDomainLabel.name())
                .value("")
                .key(JsonKeys.DomainRDFSLabel.name())
                .value("")
                .key(JsonKeys.DomainRDFSComment.name())
                .value("");
          }

          // Mark the special properties
          writer
              .key(JsonKeys.isMetaProperty.name())
              .value(isMetaProperty(type.getType(), alignmentColumnNode));

          writer.endObject();

          // Iterate through the synonym semantic types
          SynonymSemanticTypes synTypes = types.getSynonymTypesForHNodeId(nodeId);

          if (synTypes != null) {
            for (SemanticType synType : synTypes.getSynonyms()) {
              writer
                  .object()
                  .key(JsonKeys.HNodeId.name())
                  .value(synType.getHNodeId())
                  .key(JsonKeys.FullType.name())
                  .value(synType.getType().getUri())
                  .key(JsonKeys.Origin.name())
                  .value(synType.getOrigin().name())
                  .key(JsonKeys.ConfidenceLevel.name())
                  .value(synType.getConfidenceLevel().name())
                  .key(JsonKeys.DisplayLabel.name())
                  .value(synType.getType().getDisplayName())
                  .key(JsonKeys.DisplayRDFSLabel.name())
                  .value(synType.getType().getRdfsLabel())
                  .key(JsonKeys.DisplayRDFSComment.name())
                  .value(synType.getType().getRdfsComment())
                  .key(JsonKeys.isPrimary.name())
                  .value(false);
              if (!synType.isClass()) {
                writer
                    .key(JsonKeys.DomainUri.name())
                    .value(synType.getDomain().getUri())
                    .key(JsonKeys.DomainId.name())
                    .value("")
                    .key(JsonKeys.DisplayDomainLabel.name())
                    .value(synType.getDomain().getDisplayName())
                    .key(JsonKeys.DomainRDFSLabel.name())
                    .value(synType.getDomain().getRdfsLabel())
                    .key(JsonKeys.DomainRDFSComment.name())
                    .value(synType.getDomain().getRdfsComment());
              } else {
                writer
                    .key(JsonKeys.DomainId.name())
                    .value("")
                    .key(JsonKeys.DomainUri.name())
                    .value("")
                    .key(JsonKeys.DisplayDomainLabel.name())
                    .value("")
                    .key(JsonKeys.DomainRDFSLabel.name())
                    .value("")
                    .key(JsonKeys.DomainRDFSComment.name())
                    .value("");
              }
              writer.endObject();
            }
          }
          writer.endArray();
        } else {
          writer.key(JsonKeys.HNodeId.name()).value(nodeId);
          writer.key(JsonKeys.SemanticTypesArray.name()).array().endArray();
        }

        writer.endObject();
      }
      writer.endArray();
      writer.endObject();

      pw.print(writer.toString());
    } catch (JSONException e) {
      logger.error("Error occured while writing to JSON!", e);
    }
  }
 private boolean isSamehTableId(String hNodeId1, String hNodeId2, Workspace workspace) {
   HNode hNode1 = workspace.getFactory().getHNode(hNodeId1);
   HNode hNode2 = workspace.getFactory().getHNode(hNodeId2);
   if (hNode1 == null || hNode2 == null) return false;
   return hNode1.getHTableId().equals(hNode2.getHTableId());
 }
Example #11
0
 public int compareTo(HNode other) {
   return columnName.compareTo(other.getColumnName());
 }