All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.agrest.runtime.meta.MetadataService Maven / Gradle / Ivy

Go to download

Cayenne-based Agrest server. Exists for legacy purposes to combine generic server engine with Cayenne backend

There is a newer version: 4.1
Show newest version
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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy