@Override
 public Boolean getBoolean(String attributeName) {
   if (fetch.hasField(attributeName)) {
     return decoratedEntity.getBoolean(attributeName);
   } else {
     return entityManager
         .getReference(getEntityMetaData(), getIdValue())
         .getBoolean(attributeName);
   }
 }
  public Collection<EntityMetaData> loadEMD(RepositoryCollection emdFormattedRepos) {
    // extract entity metadata
    Map<String, DefaultEntityMetaData> entities =
        new LinkedHashMap<String, DefaultEntityMetaData>();
    for (Entity e : emdFormattedRepos.getRepositoryByEntityName("entities")) {
      DefaultEntityMetaData emd = new DefaultEntityMetaData(e.getString("name"));
      if (e.getBoolean("abstract")) emd.setAbstract(true);
    }

    // extract extends relations
    for (Entity e : emdFormattedRepos.getRepositoryByEntityName("entities")) {
      if (e.get("extends") != null) {
        DefaultEntityMetaData emd = entities.get(e.get("name"));
        emd.setExtends(entities.get(e.get("extends")));
      }
    }

    Collection<EntityMetaData> result = new ArrayList<EntityMetaData>();
    result.addAll(entities.values());
    return result;
  }
 private void createEntityValuesResponseRec(
     Entity entity,
     Iterable<AttributeMetaData> attrs,
     Fetch fetch,
     Map<String, Object> responseData) {
   responseData.put(
       "_href",
       Href.concatEntityHref(BASE_URI, entity.getEntityMetaData().getName(), entity.getIdValue()));
   for (AttributeMetaData attr : attrs) // TODO performance use fetch instead of attrs
   {
     String attrName = attr.getName();
     if (fetch == null || fetch.hasField(attr)) {
       FieldTypeEnum dataType = attr.getDataType().getEnumType();
       switch (dataType) {
         case BOOL:
           responseData.put(attrName, entity.getBoolean(attrName));
           break;
         case CATEGORICAL:
         case XREF:
         case FILE:
           Entity refEntity = entity.getEntity(attrName);
           Map<String, Object> refEntityResponse;
           if (refEntity != null) {
             Fetch refAttrFetch = fetch != null ? fetch.getFetch(attr) : null;
             refEntityResponse = createEntityResponse(refEntity, refAttrFetch, false);
           } else {
             refEntityResponse = null;
           }
           responseData.put(attrName, refEntityResponse);
           break;
         case CATEGORICAL_MREF:
         case MREF:
           Iterable<Entity> refEntities = entity.getEntities(attrName);
           List<Map<String, Object>> refEntityResponses;
           if (refEntities != null) {
             refEntityResponses = new ArrayList<Map<String, Object>>();
             Fetch refAttrFetch = fetch != null ? fetch.getFetch(attrName) : null;
             for (Entity refEntitiesEntity : refEntities) {
               refEntityResponses.add(
                   createEntityResponse(refEntitiesEntity, refAttrFetch, false));
             }
           } else {
             refEntityResponses = null;
           }
           responseData.put(attrName, refEntityResponses);
           break;
         case COMPOUND:
           throw new RuntimeException("Invalid data type [" + dataType + "]");
         case DATE:
           Date dateValue = entity.getDate(attrName);
           String dateValueStr = dateValue != null ? getDateFormat().format(dateValue) : null;
           responseData.put(attrName, dateValueStr);
           break;
         case DATE_TIME:
           Date dateTimeValue = entity.getDate(attrName);
           String dateTimeValueStr =
               dateTimeValue != null ? getDateTimeFormat().format(dateTimeValue) : null;
           responseData.put(attrName, dateTimeValueStr);
           break;
         case DECIMAL:
           responseData.put(attrName, entity.getDouble(attrName));
           break;
         case EMAIL:
         case ENUM:
         case HTML:
         case HYPERLINK:
         case SCRIPT:
         case STRING:
         case TEXT:
           responseData.put(attrName, entity.getString(attrName));
           break;
         case INT:
           responseData.put(attrName, entity.getInt(attrName));
           break;
         case LONG:
           responseData.put(attrName, entity.getLong(attrName));
           break;
         default:
           throw new RuntimeException("Unknown data type [" + dataType + "]");
       }
     }
   }
 }
  @Override
  public Map<String, DefaultEntityMetaData> getEntityMetaData(RepositoryCollection source) {
    // TODO: this task is actually a 'merge' instead of 'import'
    // so we need to consider both new metadata as existing ...

    Map<String, DefaultEntityMetaData> entities =
        new LinkedHashMap<String, DefaultEntityMetaData>();

    // load attributes first (because entities are optional).
    for (Entity a : source.getRepositoryByEntityName("attributes")) {
      int i = 1;
      String entityName = a.getString("entity");

      // required
      if (entityName == null) throw new IllegalArgumentException("attributes.entity is missing");
      if (a.get("name") == null) throw new IllegalArgumentException("attributes.name is missing");

      // create entity if not yet defined
      if (entities.get(entityName) == null)
        entities.put(entityName, new DefaultEntityMetaData(entityName));
      DefaultEntityMetaData md = entities.get(entityName);

      DefaultAttributeMetaData am = new DefaultAttributeMetaData(a.getString("name"));

      if (a.get("dataType") != null) {
        FieldType t = MolgenisFieldTypes.getType(a.getString("dataType"));
        if (t == null)
          throw new IllegalArgumentException(
              "attributes.type error on line " + i + ": " + a.getString("dataType") + " unknown");
        am.setDataType(t);
      }
      if (a.get("nillable") != null) am.setNillable(a.getBoolean("nillable"));
      if (a.get("auto") != null) am.setAuto(a.getBoolean("auto"));
      if (a.get("idAttribute") != null) am.setIdAttribute(a.getBoolean("idAttribute"));

      md.addAttributeMetaData(am);
    }

    // load all entities (optional)
    if (source.getRepositoryByEntityName("entities") != null) {
      int i = 1;
      for (Entity e : source.getRepositoryByEntityName("entities")) {
        i++;
        String entityName = e.getString("name");

        // required
        if (entityName == null)
          throw new IllegalArgumentException("entity.name is missing on line " + i);

        if (entities.get(entityName) == null)
          entities.put(entityName, new DefaultEntityMetaData(entityName));
        DefaultEntityMetaData md = entities.get(entityName);

        if (e.get("description") != null) md.setDescription(e.getString("description"));
      }
    }

    // re-iterate to map the mrefs/xref refEntity (or give error if not found)
    // TODO: consider also those in existing db
    int i = 1;
    for (Entity a : source.getRepositoryByEntityName("attributes")) {
      i++;
      if (a.get("refEntity") != null) {
        DefaultEntityMetaData em = entities.get(a.getString("entity"));
        DefaultAttributeMetaData am =
            (DefaultAttributeMetaData) em.getAttribute(a.getString("name"));

        if (entities.get(a.getString("refEntity")) == null) {
          throw new IllegalArgumentException(
              "attributes.refEntity error on line "
                  + i
                  + ": "
                  + a.getString("refEntity")
                  + " unknown");
        }

        am.setRefEntity(entities.get(a.getString("refEntity")));
      }
    }

    return entities;
  }