Search in sources :

Example 41 with DbAttribute

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

the class DefaultValueTransformerFactoryIT method testCreateEncryptor.

@Test
public void testCreateEncryptor() {
    DbAttribute t1_ct = t1.getAttribute("CRYPTO_STRING");
    ValueEncryptor t1 = f.createEncryptor(t1_ct);
    assertNotNull(t1);
    assertTrue(t1 instanceof DefaultValueEncryptor);
    assertSame(Utf8StringConverter.INSTANCE, ((DefaultValueEncryptor) t1).getPreConverter());
    assertSame(Base64StringConverter.INSTANCE, ((DefaultValueEncryptor) t1).getPostConverter());
    DbAttribute t2_cb = t2.getAttribute("CRYPTO_BYTES");
    ValueEncryptor t2 = f.createEncryptor(t2_cb);
    assertNotNull(t2);
    assertTrue(t2 instanceof DefaultValueEncryptor);
    assertSame(BytesToBytesConverter.INSTANCE, ((DefaultValueEncryptor) t2).getPreConverter());
    assertSame(BytesToBytesConverter.INSTANCE, ((DefaultValueEncryptor) t2).getPostConverter());
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 42 with DbAttribute

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

the class DbRelationshipMerger method createTokensForMissingOriginal.

/**
 * @param imported DbRelationship that is in db but not in model
 * @return generated tokens
 */
@Override
Collection<MergerToken> createTokensForMissingOriginal(DbRelationship imported) {
    DbEntity originalDbEntity = getOriginalSourceDbEntity(imported);
    DbEntity targetEntity = getOriginalTargetDbEntity(imported);
    if (targetEntity != null) {
        imported.setTargetEntityName(targetEntity);
    }
    imported.setSourceEntity(originalDbEntity);
    // manipulate the joins to match the DbAttributes in the model
    for (DbJoin join : imported.getJoins()) {
        DbAttribute sourceAttr = findDbAttribute(originalDbEntity, join.getSourceName());
        if (sourceAttr != null) {
            join.setSourceName(sourceAttr.getName());
        }
        DbAttribute targetAttr = findDbAttribute(targetEntity, join.getTargetName());
        if (targetAttr != null) {
            join.setTargetName(targetAttr.getName());
        }
    }
    // Add all relationships. Tokens will decide whether or not to execute
    MergerToken token = getTokenFactory().createDropRelationshipToDb(originalDbEntity, imported);
    return Collections.singleton(token);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DbJoin(org.apache.cayenne.map.DbJoin)

Example 43 with DbAttribute

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

the class EntityMergeSupport method addMissingAttributes.

private boolean addMissingAttributes(ObjEntity entity) {
    boolean changed = false;
    for (DbAttribute da : getAttributesToAdd(entity)) {
        addMissingAttribute(entity, da);
        changed = true;
    }
    return changed;
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 44 with DbAttribute

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

the class EntityMergeSupport method isSameAttributes.

/**
 * @param collection1 first collection to compare
 * @param collection2 second collection to compare
 * @return true if collections have same size and attributes in them have same names
 */
private boolean isSameAttributes(Collection<DbAttribute> collection1, Collection<DbAttribute> collection2) {
    if (collection1.size() != collection2.size()) {
        return false;
    }
    if (collection1.isEmpty()) {
        return true;
    }
    Iterator<DbAttribute> iterator1 = collection1.iterator();
    Iterator<DbAttribute> iterator2 = collection2.iterator();
    for (int i = 0; i < collection1.size(); i++) {
        DbAttribute attr1 = iterator1.next();
        DbAttribute attr2 = iterator2.next();
        // attribute can be null, see DbJoin.getSource() and DbJoin.getTarget()
        if (attr1 == null) {
            if (attr2 != null) {
                return false;
            }
            continue;
        }
        if (attr2 == null) {
            return false;
        }
        // name is unlikely to be null, but we don't want NPE anyway
        if (attr1.getName() == null) {
            if (attr2.getName() != null) {
                return false;
            }
            continue;
        }
        if (!attr1.getName().equals(attr2.getName())) {
            return false;
        }
    }
    return true;
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 45 with DbAttribute

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

the class SetPrimaryKeyToModel method execute.

@Override
public void execute(MergerContext mergerContext) {
    DbEntity e = getEntity();
    for (DbAttribute attr : e.getAttributes()) {
        boolean wasPrimaryKey = attr.isPrimaryKey();
        boolean willBePrimaryKey = primaryKeyNewAttributeNames.contains(attr.getName().toUpperCase());
        if (wasPrimaryKey != willBePrimaryKey) {
            attr.setPrimaryKey(willBePrimaryKey);
            e.dbAttributeChanged(new AttributeEvent(this, attr, e));
            mergerContext.getDelegate().dbAttributeModified(attr);
        }
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) AttributeEvent(org.apache.cayenne.map.event.AttributeEvent)

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