/** * 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; }
/** * 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 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); }
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; } }