/** * Create default entity fetch that fetches all attributes. * * @param entityMeta * @return default entity fetch or null */ public static Fetch createDefaultEntityFetch(EntityMetaData entityMeta, String languageCode) { boolean hasRefAttr = false; Fetch fetch = new Fetch(); for (AttributeMetaData attr : entityMeta.getAtomicAttributes()) { Fetch subFetch = createDefaultAttributeFetch(attr, languageCode); if (subFetch != null) { hasRefAttr = true; } fetch.field(attr.getName(), subFetch); } return hasRefAttr ? fetch : null; }
@Override public Double getDouble(String attributeName) { if (fetch.hasField(attributeName)) { return decoratedEntity.getDouble(attributeName); } else { return entityManager.getReference(getEntityMetaData(), getIdValue()).getDouble(attributeName); } }
@Override public <E extends Entity> Iterable<E> getEntities(String attributeName, Class<E> clazz) { if (fetch.hasField(attributeName)) { return decoratedEntity.getEntities(attributeName, clazz); } else { return entityManager .getReference(getEntityMetaData(), getIdValue()) .getEntities(attributeName, clazz); } }
private void initUniqueValidation(ValidationResource validationResource) { // get unique attributes List<AttributeMetaData> uniqueAttrs = StreamSupport.stream(getEntityMetaData().getAtomicAttributes().spliterator(), false) .filter(attr -> attr.isUnique() && attr.getExpression() == null) .collect(Collectors.toList()); // get existing values for each attributes if (!uniqueAttrs.isEmpty()) { Map<String, HugeMap<Object, Object>> uniqueAttrsValues = new HashMap<>(); Fetch fetch = new Fetch(); uniqueAttrs.forEach( uniqueAttr -> { uniqueAttrsValues.put(uniqueAttr.getName(), new HugeMap<>()); fetch.field(uniqueAttr.getName()); }); Query q = new QueryImpl().fetch(fetch); decoratedRepository .findAll(q) .forEach( entity -> { uniqueAttrs.forEach( uniqueAttr -> { HugeMap<Object, Object> uniqueAttrValues = uniqueAttrsValues.get(uniqueAttr.getName()); Object attrValue = entity.get(uniqueAttr.getName()); if (attrValue != null) { if (uniqueAttr.getDataType() instanceof XrefField) { attrValue = ((Entity) attrValue).getIdValue(); } uniqueAttrValues.put(attrValue, entity.getIdValue()); } }); }); validationResource.setUniqueAttrsValues(uniqueAttrsValues); } validationResource.setUniqueAttrs(uniqueAttrs); }
/** * Create default fetch for the given attribute. For attributes referencing entities the id and * label value are fetched. Additionally for file entities the URL is fetched. For other * attributes the default fetch is null; * * @param attr * @return default attribute fetch or null */ public static Fetch createDefaultAttributeFetch(AttributeMetaData attr, String languageCode) { Fetch fetch; if (isReferenceType(attr)) { fetch = new Fetch(); EntityMetaData refEntityMeta = attr.getRefEntity(); String idAttrName = refEntityMeta.getIdAttribute().getName(); fetch.field(idAttrName); String labelAttrName = refEntityMeta.getLabelAttribute(languageCode).getName(); if (!labelAttrName.equals(idAttrName)) { fetch.field(labelAttrName); } if (attr.getDataType() == FILE) { fetch.field(FileMetaMetaData.URL); } } else { fetch = null; } return fetch; }
private static void createFetchContentRec( AttributeFilter attrFilter, EntityMetaData entityMeta, Fetch fetch, String languageCode) { if (attrFilter.isIncludeAllAttrs()) { entityMeta .getAtomicAttributes() .forEach( attr -> fetch.field(attr.getName(), createDefaultAttributeFetch(attr, languageCode))); } if (attrFilter.isIncludeIdAttr()) { fetch.field(entityMeta.getIdAttribute().getName()); } if (attrFilter.isIncludeLabelAttr()) { fetch.field(entityMeta.getLabelAttribute(languageCode).getName()); } attrFilter.forEach( entry -> { String attrName = entry.getKey(); AttributeMetaData attr = getAttribute(entityMeta, attrName); createFetchContentRec(attrFilter, entityMeta, attr, fetch, languageCode); }); }
private static void createFetchContentRec( AttributeFilter attrFilter, EntityMetaData entityMeta, AttributeMetaData attr, Fetch fetch, String languageCode) { AttributeType attrType = attr.getDataType(); switch (attrType) { case COMPOUND: { AttributeFilter subAttrFilter = attrFilter != null ? attrFilter.getAttributeFilter(entityMeta, attr) : null; if (subAttrFilter != null && !subAttrFilter.isIncludeAllAttrs()) { // include compound attribute parts defined by filter if (subAttrFilter.isIncludeIdAttr()) { createFetchContentRec( subAttrFilter, entityMeta, entityMeta.getIdAttribute(), fetch, languageCode); } if (subAttrFilter.isIncludeLabelAttr()) { createFetchContentRec( subAttrFilter, entityMeta, entityMeta.getLabelAttribute(languageCode), fetch, languageCode); } subAttrFilter.forEach( entry -> { String attrPartName = entry.getKey(); AttributeMetaData attrPart = attr.getAttributePart(attrPartName); createFetchContentRec(subAttrFilter, entityMeta, attrPart, fetch, languageCode); }); } else { // include all compound attribute parts attr.getAttributeParts() .forEach( attrPart -> { createFetchContentRec( subAttrFilter, entityMeta, attrPart, fetch, languageCode); }); } break; } case CATEGORICAL: case CATEGORICAL_MREF: case FILE: case MREF: case XREF: { AttributeFilter subAttrFilter = attrFilter != null ? attrFilter.getAttributeFilter(entityMeta, attr) : null; Fetch subFetch; if (subAttrFilter != null) { subFetch = convert(subAttrFilter, attr.getRefEntity(), languageCode); } else { subFetch = createDefaultAttributeFetch(attr, languageCode); } fetch.field(attr.getName(), subFetch); break; } // $CASES-OMITTED$ default: fetch.field(attr.getName()); break; } }
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 + "]"); } } } }