Esempio n. 1
0
  /**
   * Create an OEntityKey instance for the specified entity id
   *
   * @param edmDataServices edmDataServices
   * @param entity Entity set name
   * @param id Id
   * @return An OEntityKey instance
   * @throws Exception Error creating key
   */
  public static OEntityKey createEntityKey(
      EdmDataServices edmDataServices, String entitySetName, String id) throws Exception {
    // Lookup type of entity key (simple keys only)
    String keyType = null;
    EdmEntitySet entitySet = edmDataServices.getEdmEntitySet(entitySetName);
    if (entitySet != null) {
      EdmEntityType entityType = entitySet.getType();
      List<String> keys = entityType.getKeys();
      if (keys.size() == 1) {
        EdmProperty prop = entityType.findDeclaredProperty(keys.get(0));
        if (prop != null && prop.getType() != null) {
          keyType = prop.getType().getFullyQualifiedTypeName();
        }
      }
    }
    assert (keyType != null) : "Should not be possible to get this far and find no key type";

    // Create an entity key
    OEntityKey key = null;
    try {
      if (keyType.equals("Edm.Int64")) {
        key = OEntityKey.parse(id);
      } else if (keyType.equals("Edm.Int32")) {
        key = OEntityKey.parse(id);
      } else if (keyType.equals("Edm.DateTime")) {
        key = OEntityKey.parse(id);
      } else if (keyType.equals("Edm.Time")) {
        key = OEntityKey.parse(id);
      } else if (keyType.equals("Edm.String")) {
        key = OEntityKey.parse(id);
      }
    } catch (Exception e) {
      logger.warn(
          "Entity key type "
              + keyType
              + " is not supported by CommandHelper, trying OEntityKey.parse");
    }
    // could not parse the key, have one last attempt with OEntityKey create
    if (key == null) {
      try {
        if (keyType.equals("Edm.Int64")) {
          key = OEntityKey.create(Long.parseLong(id));
        } else if (keyType.equals("Edm.Int32")) {
          key = OEntityKey.create(Integer.parseInt(id));
        } else {
          key = OEntityKey.create(id);
        }
      } catch (Exception e) {
        logger.error("OEntityKey.parse failed to parse id [" + id + "]");
      }
    }
    if (key == null) throw new Exception("Entity key type " + id + " is not supported.");
    return key;
  }