Search in sources :

Example 1 with DbAttribute

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

the class EntityIdCoderTest method testSingleLongPk.

@Test
public void testSingleLongPk() {
    DbEntity dbEntity = new DbEntity("X");
    DbAttribute pk = new DbAttribute("ID");
    pk.setType(Types.BIGINT);
    pk.setPrimaryKey(true);
    dbEntity.addAttribute(pk);
    ObjEntity entity = mock(ObjEntity.class);
    when(entity.getName()).thenReturn("x");
    when(entity.getDbEntityName()).thenReturn(dbEntity.getName());
    when(entity.getDbEntity()).thenReturn(dbEntity);
    ObjectId id = new ObjectId("x", "ID", 3L);
    EntityIdCoder coder = new EntityIdCoder(entity);
    assertEquals("x:3", coder.toStringId(id));
    ObjectId parsedId = coder.toObjectId("x:3");
    assertEquals(id, parsedId);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 2 with DbAttribute

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

the class EntityIdCoderTest method testSingleIntPk.

@Test
public void testSingleIntPk() {
    DbEntity dbEntity = new DbEntity("X");
    DbAttribute pk = new DbAttribute("ID");
    pk.setType(Types.INTEGER);
    pk.setPrimaryKey(true);
    dbEntity.addAttribute(pk);
    ObjEntity entity = mock(ObjEntity.class);
    when(entity.getName()).thenReturn("x");
    when(entity.getDbEntityName()).thenReturn(dbEntity.getName());
    when(entity.getDbEntity()).thenReturn(dbEntity);
    ObjectId id = new ObjectId("x", "ID", 3);
    EntityIdCoder coder = new EntityIdCoder(entity);
    assertEquals("x:3", coder.toStringId(id));
    ObjectId parsedId = coder.toObjectId("x:3");
    assertEquals(id, parsedId);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 3 with DbAttribute

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

the class EntityIdCoderTest method testSingleStringPk.

@Test
public void testSingleStringPk() {
    DbEntity dbEntity = new DbEntity("X");
    DbAttribute pk = new DbAttribute("ID");
    pk.setType(Types.VARCHAR);
    pk.setPrimaryKey(true);
    dbEntity.addAttribute(pk);
    ObjEntity entity = mock(ObjEntity.class);
    when(entity.getName()).thenReturn("x");
    when(entity.getDbEntityName()).thenReturn(dbEntity.getName());
    when(entity.getDbEntity()).thenReturn(dbEntity);
    EntityIdCoder coder = new EntityIdCoder(entity);
    ObjectId id = new ObjectId("x", "ID", "AbC");
    assertEquals("x:AbC", coder.toStringId(id));
    ObjectId parsedId = coder.toObjectId("x:AbC");
    assertEquals(id, parsedId);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 4 with DbAttribute

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

the class EntityIdCoderTest method testIdEncoding.

@Test
public void testIdEncoding() {
    DbEntity dbEntity = new DbEntity("X");
    DbAttribute pk = new DbAttribute("ID");
    pk.setType(Types.VARCHAR);
    pk.setPrimaryKey(true);
    dbEntity.addAttribute(pk);
    ObjEntity entity = mock(ObjEntity.class);
    when(entity.getName()).thenReturn("x");
    when(entity.getDbEntityName()).thenReturn(dbEntity.getName());
    when(entity.getDbEntity()).thenReturn(dbEntity);
    EntityIdCoder coder = new EntityIdCoder(entity);
    ObjectId id = new ObjectId("x", "ID", "Ab:C");
    assertEquals("x:Ab%3AC", coder.toStringId(id));
    ObjectId parsedId = coder.toObjectId("x:Ab%3AC");
    assertEquals(id, parsedId);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 5 with DbAttribute

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

the class BaseDataObject method validateForSave.

/**
 * Performs property validation of the object, appending any validation
 * failures to the provided validationResult object. This method is invoked
 * from "validateFor.." before committing a NEW or MODIFIED object to the
 * database. Validation includes checking for null values and value sizes.
 * CayenneDataObject subclasses may override this method, calling super.
 *
 * @since 1.1
 */
protected void validateForSave(ValidationResult validationResult) {
    ObjEntity objEntity = getObjectContext().getEntityResolver().getObjEntity(this);
    if (objEntity == null) {
        throw new CayenneRuntimeException("No ObjEntity mapping found for DataObject %s", getClass().getName());
    }
    // validate mandatory attributes
    Map<String, ValidationFailure> failedDbAttributes = null;
    for (ObjAttribute next : objEntity.getAttributes()) {
        // TODO: andrus, 2/20/2007 - handle embedded attribute
        if (next instanceof EmbeddedAttribute) {
            continue;
        }
        DbAttribute dbAttribute = next.getDbAttribute();
        if (dbAttribute == null) {
            throw new CayenneRuntimeException("ObjAttribute '%s" + "' does not have a corresponding DbAttribute", next.getName());
        }
        // pk may still be generated
        if (dbAttribute.isPrimaryKey()) {
            continue;
        }
        Object value = this.readPropertyDirectly(next.getName());
        if (dbAttribute.isMandatory()) {
            ValidationFailure failure = BeanValidationFailure.validateNotNull(this, next.getName(), value);
            if (failure != null) {
                if (failedDbAttributes == null) {
                    failedDbAttributes = new HashMap<>();
                }
                failedDbAttributes.put(dbAttribute.getName(), failure);
                continue;
            }
        }
        // validate length
        if (value != null && dbAttribute.getMaxLength() > 0) {
            if (value.getClass().isArray()) {
                int len = Array.getLength(value);
                if (len > dbAttribute.getMaxLength()) {
                    String message = "\"" + next.getName() + "\" exceeds maximum allowed length (" + dbAttribute.getMaxLength() + " bytes): " + len;
                    validationResult.addFailure(new BeanValidationFailure(this, next.getName(), message));
                }
            } else if (value instanceof CharSequence) {
                int len = ((CharSequence) value).length();
                if (len > dbAttribute.getMaxLength()) {
                    String message = "\"" + next.getName() + "\" exceeds maximum allowed length (" + dbAttribute.getMaxLength() + " chars): " + len;
                    validationResult.addFailure(new BeanValidationFailure(this, next.getName(), message));
                }
            }
        }
    }
    // validate mandatory relationships
    for (final ObjRelationship relationship : objEntity.getRelationships()) {
        List<DbRelationship> dbRels = relationship.getDbRelationships();
        if (dbRels.isEmpty()) {
            continue;
        }
        // see ObjRelationship.recalculateReadOnlyValue() for more info
        if (relationship.isSourceIndependentFromTargetChange()) {
            continue;
        }
        // if db relationship is not based on a PK and is based on mandatory
        // attributes, see if we have a target object set
        // relationship will be validated only if all db path has mandatory
        // db relationships
        boolean validate = true;
        for (DbRelationship dbRelationship : dbRels) {
            for (DbJoin join : dbRelationship.getJoins()) {
                DbAttribute source = join.getSource();
                if (source.isMandatory()) {
                    // clear attribute failures...
                    if (failedDbAttributes != null && !failedDbAttributes.isEmpty()) {
                        failedDbAttributes.remove(source.getName());
                    }
                } else {
                    // do not validate if the relation is based on
                    // multiple keys with some that can be nullable.
                    validate = false;
                }
            }
        }
        if (validate) {
            Object value = this.readPropertyDirectly(relationship.getName());
            ValidationFailure failure = BeanValidationFailure.validateNotNull(this, relationship.getName(), value);
            if (failure != null) {
                validationResult.addFailure(failure);
            }
        }
    }
    // deal with previously found attribute failures...
    if (failedDbAttributes != null && !failedDbAttributes.isEmpty()) {
        for (ValidationFailure failure : failedDbAttributes.values()) {
            validationResult.addFailure(failure);
        }
    }
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure) DbAttribute(org.apache.cayenne.map.DbAttribute) EmbeddedAttribute(org.apache.cayenne.map.EmbeddedAttribute) ValidationFailure(org.apache.cayenne.validation.ValidationFailure) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure) ObjEntity(org.apache.cayenne.map.ObjEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin)

Aggregations

DbAttribute (org.apache.cayenne.map.DbAttribute)194 DbEntity (org.apache.cayenne.map.DbEntity)109 Test (org.junit.Test)67 ObjEntity (org.apache.cayenne.map.ObjEntity)36 DbRelationship (org.apache.cayenne.map.DbRelationship)35 ObjAttribute (org.apache.cayenne.map.ObjAttribute)32 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)21 DbJoin (org.apache.cayenne.map.DbJoin)18 HashMap (java.util.HashMap)16 ObjRelationship (org.apache.cayenne.map.ObjRelationship)16 ArrayList (java.util.ArrayList)14 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)12 DataMap (org.apache.cayenne.map.DataMap)11 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)10 QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)10 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)10 DeleteBatchQuery (org.apache.cayenne.query.DeleteBatchQuery)10 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)10 ObjectId (org.apache.cayenne.ObjectId)9 Expression (org.apache.cayenne.exp.Expression)8