io.katharsis.meta.internal.MetaRelationshipRepository Maven / Gradle / Ivy
package io.katharsis.meta.internal;
import java.util.Collection;
import io.katharsis.core.internal.utils.PropertyUtils;
import io.katharsis.errorhandling.exception.ResourceNotFoundException;
import io.katharsis.meta.MetaLookup;
import io.katharsis.meta.model.MetaElement;
import io.katharsis.queryspec.QuerySpec;
import io.katharsis.repository.RelationshipRepositoryV2;
import io.katharsis.resource.list.ResourceList;
public class MetaRelationshipRepository implements RelationshipRepositoryV2 {
private MetaLookup lookup;
private Class extends MetaElement> sourceResourceClass;
private Class extends MetaElement> targetResourceClass;
public MetaRelationshipRepository(MetaLookup lookup, Class extends MetaElement> sourceClass,
Class extends MetaElement> targetClass) {
this.lookup = lookup;
this.sourceResourceClass = sourceClass;
this.targetResourceClass = targetClass;
}
@Override
public MetaElement findOneTarget(String sourceId, String fieldName, QuerySpec querySpec) {
MetaElement source = lookup.getMetaById().get(sourceId);
if (source == null) {
throw new ResourceNotFoundException(sourceId);
}
Object value = PropertyUtils.getProperty(source, fieldName);
if (!(value instanceof MetaElement) && value != null) {
throw new IllegalStateException("relation " + fieldName + " is not of type MetaElement, got " + value);
}
return (MetaElement) value;
}
@SuppressWarnings("unchecked")
@Override
public ResourceList findManyTargets(String sourceId, String fieldName, QuerySpec querySpec) {
MetaElement source = lookup.getMetaById().get(sourceId);
if (source == null) {
throw new ResourceNotFoundException(sourceId);
}
Object value = PropertyUtils.getProperty(source, fieldName);
if (!(value instanceof Collection)) {
throw new IllegalStateException("relation " + fieldName + " is not a collection, got " + value);
}
return querySpec.apply((Collection) value);
}
@Override
public Class getSourceResourceClass() {
return (Class) sourceResourceClass;
}
@Override
public Class getTargetResourceClass() {
return (Class) targetResourceClass;
}
@Override
public void setRelation(MetaElement source, String targetId, String fieldName) {
throw new UnsupportedOperationException("repository is read-only");
}
@Override
public void setRelations(MetaElement source, Iterable targetIds, String fieldName) {
throw new UnsupportedOperationException("repository is read-only");
}
@Override
public void addRelations(MetaElement source, Iterable targetIds, String fieldName) {
throw new UnsupportedOperationException("repository is read-only");
}
@Override
public void removeRelations(MetaElement source, Iterable targetIds, String fieldName) {
throw new UnsupportedOperationException("repository is read-only");
}
// @SuppressWarnings("unchecked")
// public T findOne(String id, QuerySpec querySpec) {
// MetaElement metaElement = lookup.getMetaById().get(id);
// Class resourceClass = this.getResourceClass();
// if (metaElement != null && resourceClass.isInstance(metaElement)) {
// return (T) metaElement;
// }
// throw new ResourceNotFoundException(id);
// }
//
// @Override
// public ResourceList findAll(QuerySpec querySpec) {
// Collection values = filterByType(lookup.getMetaById().values());
// return querySpec.apply(values);
// }
//
// @SuppressWarnings("unchecked")
// private Collection filterByType(Collection values) {
// Collection results = new ArrayList<>();
// Class resourceClass = this.getResourceClass();
// for (MetaElement element : values) {
// if (resourceClass.isInstance(element)) {
// results.add((T) element);
// }
// }
// return results;
// }
}