Search in sources :

Example 11 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class CayenneContext method newObject.

/**
 * Creates and registers a new Persistent object instance.
 */
@Override
public <T> T newObject(Class<T> persistentClass) {
    if (persistentClass == null) {
        throw new NullPointerException("Persistent class can't be null.");
    }
    ObjEntity entity = getEntityResolver().getObjEntity(persistentClass);
    if (entity == null) {
        throw new CayenneRuntimeException("No entity mapped for class: %s", persistentClass);
    }
    ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entity.getName());
    @SuppressWarnings("unchecked") T object = (T) descriptor.createObject();
    descriptor.injectValueHolders(object);
    registerNewObject((Persistent) object, entity.getName(), descriptor);
    return object;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor)

Example 12 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class ClientReturnDiffFilter method isClientArc.

private boolean isClientArc(Object id, Object targetId, Object arcId) {
    ObjectId oid = (ObjectId) id;
    ObjEntity entity = resolver.getObjEntity(oid.getEntityName());
    if (!entity.isClientAllowed()) {
        return false;
    }
    if (entity.getRelationship(arcId.toString()).isRuntime()) {
        return false;
    }
    ObjectId targetOid = (ObjectId) targetId;
    ObjEntity targetEntity = resolver.getObjEntity(targetOid.getEntityName());
    if (!targetEntity.isClientAllowed()) {
        return false;
    }
    return true;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjectId(org.apache.cayenne.ObjectId)

Example 13 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class Cayenne method buildId.

static ObjectId buildId(ObjectContext context, Class<?> dataObjectClass, Object pk) {
    if (pk == null) {
        throw new IllegalArgumentException("Null PK");
    }
    if (dataObjectClass == null) {
        throw new IllegalArgumentException("Null DataObject class.");
    }
    ObjEntity entity = context.getEntityResolver().getObjEntity(dataObjectClass);
    if (entity == null) {
        throw new CayenneRuntimeException("Unmapped DataObject Class: %s", dataObjectClass.getName());
    }
    Collection<String> pkAttributes = entity.getPrimaryKeyNames();
    if (pkAttributes.size() != 1) {
        throw new CayenneRuntimeException("PK contains %d columns, expected 1.", pkAttributes.size());
    }
    String attr = pkAttributes.iterator().next();
    return ObjectId.of(entity.getName(), attr, pk);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity)

Example 14 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class Cayenne method buildId.

static ObjectId buildId(ObjectContext context, String objEntityName, Object pk) {
    if (pk == null) {
        throw new IllegalArgumentException("Null PK");
    }
    if (objEntityName == null) {
        throw new IllegalArgumentException("Null ObjEntity name.");
    }
    ObjEntity entity = context.getEntityResolver().getObjEntity(objEntityName);
    if (entity == null) {
        throw new CayenneRuntimeException("Non-existent ObjEntity: %s", objEntityName);
    }
    Collection<String> pkAttributes = entity.getPrimaryKeyNames();
    if (pkAttributes.size() != 1) {
        throw new CayenneRuntimeException("PK contains %d columns, expected 1.", pkAttributes.size());
    }
    String attr = pkAttributes.iterator().next();
    return ObjectId.of(objEntityName, attr, pk);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity)

Example 15 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class ASTDbIdPath method evaluateEntityNode.

@Override
protected CayenneMapEntry evaluateEntityNode(Entity entity) {
    int lastDot = path.lastIndexOf('.');
    String objPath = null;
    String id = path;
    if (lastDot > -1) {
        objPath = path.substring(0, lastDot);
        id = path.substring(lastDot + 1);
    }
    if (!(entity instanceof ObjEntity)) {
        throw new CayenneRuntimeException("Unable to evaluate DBID path for DbEntity");
    }
    ObjEntity objEntity = (ObjEntity) entity;
    if (objPath != null) {
        CayenneMapEntry entry = new ASTObjPath(objPath).evaluateEntityNode(objEntity);
        if (!(entry instanceof ObjRelationship)) {
            throw new CayenneRuntimeException("Unable to evaluate DBID path %s, relationship expected", path);
        }
        objEntity = ((ObjRelationship) entry).getTargetEntity();
    }
    DbAttribute pk = objEntity.getDbEntity().getAttribute(id);
    if (pk == null || !pk.isPrimaryKey()) {
        throw new CayenneRuntimeException("Unable to find PK %s for entity %s", id, objEntity.getName());
    }
    return pk;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) CayenneMapEntry(org.apache.cayenne.util.CayenneMapEntry) ObjRelationship(org.apache.cayenne.map.ObjRelationship) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbAttribute(org.apache.cayenne.map.DbAttribute)

Aggregations

ObjEntity (org.apache.cayenne.map.ObjEntity)294 Test (org.junit.Test)110 DbEntity (org.apache.cayenne.map.DbEntity)72 ObjAttribute (org.apache.cayenne.map.ObjAttribute)68 ObjRelationship (org.apache.cayenne.map.ObjRelationship)62 DataMap (org.apache.cayenne.map.DataMap)57 DbAttribute (org.apache.cayenne.map.DbAttribute)37 DbRelationship (org.apache.cayenne.map.DbRelationship)29 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)27 ObjectId (org.apache.cayenne.ObjectId)26 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)22 ArrayList (java.util.ArrayList)19 Embeddable (org.apache.cayenne.map.Embeddable)18 EntityResolver (org.apache.cayenne.map.EntityResolver)17 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)16 Expression (org.apache.cayenne.exp.Expression)15 Persistent (org.apache.cayenne.Persistent)12 EntityEvent (org.apache.cayenne.map.event.EntityEvent)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 Entity (org.apache.cayenne.map.Entity)11