use of io.crnk.meta.model.MetaElement in project crnk-framework by crnk-project.
the class ResourceMetaFilter method onInitialized.
@Override
public void onInitialized(MetaElement element) {
if (element instanceof MetaResourceBase) {
MetaResourceBase metaResource = (MetaResourceBase) element;
ResourceInformation information = getResourceInformation(metaResource, true);
PreconditionUtil.assertNotNull(information.getResourceType(), metaResource);
for (ResourceField field : information.getRelationshipFields()) {
if (field.getOppositeName() != null) {
String oppositeType = field.getOppositeResourceType();
MetaResource oppositeMeta = (MetaResource) context.getMetaElement(partition.getId(oppositeType)).get();
MetaAttribute attr = metaResource.getAttribute(field.getUnderlyingName());
MetaAttribute oppositeAttr = oppositeMeta.getAttribute(field.getOppositeName());
PreconditionUtil.assertNotNull(attr.getId() + " opposite not found", oppositeAttr);
attr.setOppositeAttribute(oppositeAttr);
}
}
ResourceField idField = information.getIdField();
if (idField != null) {
MetaAttribute idAttr = metaResource.getAttribute(idField.getUnderlyingName());
idAttr.setPrimaryKeyAttribute(true);
if (metaResource.getSuperType() == null || metaResource.getSuperType().getPrimaryKey() == null) {
MetaPrimaryKey primaryKey = new MetaPrimaryKey();
primaryKey.setName(metaResource.getName() + "$primaryKey");
primaryKey.setName(metaResource.getId() + "$primaryKey");
primaryKey.setElements(Arrays.asList(idAttr));
primaryKey.setUnique(true);
primaryKey.setParent(metaResource, true);
metaResource.setPrimaryKey(primaryKey);
partition.addElement(null, primaryKey);
}
}
}
if (element instanceof MetaAttribute && element.getParent() instanceof MetaResourceBase) {
MetaAttribute attr = (MetaAttribute) element;
MetaResourceBase parent = (MetaResourceBase) attr.getParent();
ResourceInformation information = getResourceInformation(parent, true);
ResourceField field = information.findFieldByUnderlyingName(attr.getName());
PreconditionUtil.assertNotNull(attr.getName(), field);
if (field.getResourceFieldType() == ResourceFieldType.RELATIONSHIP) {
String oppositeType = field.getOppositeResourceType();
String oppositeId = partition.getId(oppositeType);
Optional<MetaElement> optOppositeMeta = context.getMetaElement(oppositeId);
if (!optOppositeMeta.isPresent()) {
throw new IllegalStateException("opposite meta element '" + oppositeId + "' for element '" + element.getId() + "' not found");
}
MetaResource oppositeMeta = (MetaResource) optOppositeMeta.get();
if (field.isCollection()) {
boolean isSet = Set.class.isAssignableFrom(field.getType());
String suffix = (isSet ? "$set" : "$list");
Optional<MetaElement> optMetaCollection = context.getMetaElement(oppositeId + suffix);
MetaCollectionType metaCollection;
if (optMetaCollection.isPresent()) {
metaCollection = (MetaCollectionType) optMetaCollection.get();
} else {
metaCollection = isSet ? new MetaSetType() : new MetaListType();
metaCollection.setId(oppositeMeta.getId() + suffix);
metaCollection.setName(oppositeMeta.getName() + suffix);
metaCollection.setImplementationType(field.getGenericType());
metaCollection.setElementType(oppositeMeta);
partition.addElement(null, metaCollection);
}
attr.setType(metaCollection);
} else {
attr.setType(oppositeMeta);
}
} else {
Type implementationType = field.getGenericType();
MetaElement metaType = partition.allocateMetaElement(implementationType).get();
attr.setType(metaType.asType());
}
} else if (element instanceof MetaAttribute && element.getParent() instanceof MetaJsonObject) {
MetaAttribute attr = (MetaAttribute) element;
MetaDataObject parent = attr.getParent();
Type implementationType = PropertyUtils.getPropertyType(parent.getImplementationClass(), attr.getName());
MetaElement metaType = partition.allocateMetaElement(implementationType).get();
attr.setType(metaType.asType());
}
}
use of io.crnk.meta.model.MetaElement in project crnk-framework by crnk-project.
the class MetaDataObjectProvider method create.
@Override
public MetaElement create(Type type) {
Class<?> rawClazz = ClassUtils.getRawType(type);
Class<?> superClazz = rawClazz.getSuperclass();
MetaElement superMeta = null;
if (superClazz != Object.class && superClazz != null) {
superMeta = context.allocate(superClazz);
}
MetaDataObject meta = newDataObject();
meta.setElementType(meta);
meta.setName(rawClazz.getSimpleName());
meta.setImplementationType(type);
meta.setSuperType((MetaDataObject) superMeta);
if (superMeta != null) {
((MetaDataObject) superMeta).addSubType(meta);
}
createAttributes(meta);
return meta;
}
use of io.crnk.meta.model.MetaElement in project crnk-framework by crnk-project.
the class TypedMetaPartitionBase method doAllocateMetaElement.
@Override
protected Optional<MetaElement> doAllocateMetaElement(Type type) {
for (TypedMetaElementFactory factory : factories) {
if (factory.accept(type)) {
MetaElement element = factory.create(type);
addElement(type, element);
return Optional.of(element);
}
}
return Optional.empty();
}
use of io.crnk.meta.model.MetaElement in project crnk-framework by crnk-project.
the class MetaLookup method initialize.
private void initialize(MetaElement element) {
LOGGER.debug("discovering {}", element.getId());
for (MetaFilter filter : filters) {
filter.onInitializing(element);
}
for (MetaElement child : element.getChildren()) {
initialize(child);
}
for (MetaFilter filter : filters) {
filter.onInitialized(element);
}
LOGGER.debug("added {}", element.getId());
}
use of io.crnk.meta.model.MetaElement in project crnk-framework by crnk-project.
the class MetaLookup method discover.
private <T> T discover(Callable<T> callable) {
LOGGER.debug("discovery started");
discovering = true;
try {
T result = callable.call();
while (!initializationQueue.isEmpty()) {
MetaElement element = initializationQueue.pollFirst();
// initialize from roots down to decendants.
if (element.getParent() == null) {
initialize(element);
}
}
return result;
} catch (Exception e) {
throw new IllegalStateException(e);
} finally {
LOGGER.debug("discovery completed");
discovering = false;
}
}
Aggregations