io.agrest.runtime.meta.MetadataService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of agrest Show documentation
Show all versions of agrest Show documentation
Cayenne-based Agrest server. Exists for legacy purposes to combine generic server engine with Cayenne backend
package io.agrest.runtime.meta;
import io.agrest.AgException;
import io.agrest.meta.AgEntity;
import io.agrest.meta.LazyAgDataMap;
import io.agrest.meta.AgDataMap;
import io.agrest.meta.Types;
import io.agrest.meta.compiler.AgEntityCompiler;
import io.agrest.runtime.cayenne.ICayennePersister;
import org.apache.cayenne.di.Inject;
import org.apache.cayenne.map.EntityResolver;
import javax.ws.rs.core.Response.Status;
import java.lang.reflect.Type;
import java.util.List;
public class MetadataService implements IMetadataService {
private EntityResolver entityResolver;
private AgDataMap dataMap;
public MetadataService(@Inject List entityCompilers, @Inject ICayennePersister cayenneService) {
this.entityResolver = cayenneService.entityResolver();
this.dataMap = new LazyAgDataMap(entityCompilers);
}
/**
* @since 1.12
*/
@Override
public AgEntity getAgEntity(Class type) {
if (type == null) {
throw new NullPointerException("Null type");
}
AgEntity e = dataMap.getEntity(type);
if (e == null) {
throw new AgException(Status.BAD_REQUEST, "Invalid entity: " + type.getName());
}
return e;
}
@Override
public AgEntity getEntityByType(Type entityType) {
@SuppressWarnings("unchecked")
AgEntity entity = getAgEntity( (Class) Types.getClassForTypeArgument(entityType).orElse(Object.class));
if (entity == null) {
throw new AgException(Status.INTERNAL_SERVER_ERROR,
"EntityUpdate type '" + entityType.getTypeName() + "' is not an entity");
}
return entity;
}
}