Search in sources :

Example 16 with ObjAttribute

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

the class DefaultValueTransformerFactory method getJavaType.

// TODO: calculating Java type of ObjAttribute may become unneeded per
// CAY-1752, as DbAttribute will have it.
protected String getJavaType(DbAttribute a) {
    DbEntity dbEntity = a.getEntity();
    DataMap dataMap = dbEntity.getDataMap();
    Collection<String> javaTypes = new HashSet<>();
    for (ObjEntity objEntity : dataMap.getMappedEntities(dbEntity)) {
        for (ObjAttribute oa : objEntity.getAttributes()) {
            // TODO: this won't pick up flattened attributes
            if (a.getName().equals(oa.getDbAttributePath())) {
                javaTypes.add(oa.getType());
            }
        }
    }
    if (javaTypes.size() != 1) {
        String javaType = TypesMapping.getJavaBySqlType(a.getType());
        String attributeName = dbEntity.getName() + "." + a.getName();
        String msg = javaTypes.size() > 1 ? "ObjAttributes with different java types" : "No ObjAttributes";
        // Warn user about this problem as there is nothing else we can do
        logger.warn(msg + " bound to DbAttribute '" + attributeName + "', " + javaType + " type will be used.");
        return javaType;
    }
    return javaTypes.iterator().next();
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DataMap(org.apache.cayenne.map.DataMap) HashSet(java.util.HashSet)

Example 17 with ObjAttribute

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

the class EntityMergeSupport method createObjRelationship.

private boolean createObjRelationship(ObjEntity entity, DbRelationship dr, String targetEntityName) {
    ObjRelationship or = new ObjRelationship();
    or.setName(NameBuilder.builder(or, entity).baseName(nameGenerator.relationshipName(dr)).name());
    or.addDbRelationship(dr);
    Map<String, ObjEntity> objEntities = entity.getDataMap().getSubclassesForObjEntity(entity);
    boolean hasFlattingAttributes = false;
    boolean needGeneratedEntity = true;
    if (objEntities.containsKey(targetEntityName)) {
        needGeneratedEntity = false;
    }
    for (ObjEntity subObjEntity : objEntities.values()) {
        for (ObjAttribute objAttribute : subObjEntity.getAttributes()) {
            String path = objAttribute.getDbAttributePath();
            if (path != null) {
                if (path.startsWith(or.getDbRelationshipPath())) {
                    hasFlattingAttributes = true;
                    break;
                }
            }
        }
    }
    if (!hasFlattingAttributes) {
        if (needGeneratedEntity) {
            or.setTargetEntityName(targetEntityName);
            or.setSourceEntity(entity);
        }
        entity.addRelationship(or);
        fireRelationshipAdded(or);
    }
    return needGeneratedEntity;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute)

Example 18 with ObjAttribute

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

the class DropColumnToModel method execute.

@Override
public void execute(MergerContext mergerContext) {
    // remove relationships mapped to column. duplicate List to prevent
    // ConcurrentModificationException
    List<DbRelationship> dbRelationships = new ArrayList<>(getEntity().getRelationships());
    for (DbRelationship dbRelationship : dbRelationships) {
        for (DbJoin join : dbRelationship.getJoins()) {
            if (join.getSource() == getColumn() || join.getTarget() == getColumn()) {
                remove(mergerContext.getDelegate(), dbRelationship, true);
            }
        }
    }
    // remove ObjAttribute mapped to same column
    for (ObjEntity objEntity : getEntity().mappedObjEntities()) {
        ObjAttribute objAttribute = objEntity.getAttributeForDbAttribute(getColumn());
        if (objAttribute != null) {
            objEntity.removeAttribute(objAttribute.getName());
            mergerContext.getDelegate().objAttributeRemoved(objAttribute);
        }
    }
    // remove DbAttribute
    getEntity().removeAttribute(getColumn().getName());
    mergerContext.getDelegate().dbAttributeRemoved(getColumn());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbRelationship(org.apache.cayenne.map.DbRelationship) ArrayList(java.util.ArrayList) DbJoin(org.apache.cayenne.map.DbJoin)

Example 19 with ObjAttribute

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

the class MergerFactoryIT method testAddTableToDb.

@Test
public void testAddTableToDb() throws Exception {
    dropTableIfPresent("NEW_TABLE");
    assertTokensAndExecute(0, 0);
    DbEntity dbEntity = new DbEntity("NEW_TABLE");
    DbAttribute column1 = new DbAttribute("ID", Types.INTEGER, dbEntity);
    column1.setMandatory(true);
    column1.setPrimaryKey(true);
    dbEntity.addAttribute(column1);
    DbAttribute column2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity);
    column2.setMaxLength(10);
    column2.setMandatory(false);
    dbEntity.addAttribute(column2);
    map.addDbEntity(dbEntity);
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
    ObjEntity objEntity = new ObjEntity("NewTable");
    objEntity.setDbEntity(dbEntity);
    ObjAttribute oatr1 = new ObjAttribute("name");
    oatr1.setDbAttributePath(column2.getName());
    oatr1.setType("java.lang.String");
    objEntity.addAttribute(oatr1);
    map.addObjEntity(objEntity);
    for (int i = 0; i < 5; i++) {
        CayenneDataObject dao = (CayenneDataObject) context.newObject(objEntity.getName());
        dao.writeProperty(oatr1.getName(), "test " + i);
    }
    context.commitChanges();
    // clear up
    map.removeObjEntity(objEntity.getName(), true);
    map.removeDbEntity(dbEntity.getName(), true);
    resolver.refreshMappingCache();
    assertNull(map.getObjEntity(objEntity.getName()));
    assertNull(map.getDbEntity(dbEntity.getName()));
    assertFalse(map.getDbEntities().contains(dbEntity));
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 20 with ObjAttribute

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

the class ObjAttributeTableModel method setObjAttributeType.

private void setObjAttributeType(ObjAttributeWrapper attribute, Object value) {
    String oldType = attribute.getType();
    String newType = value != null ? value.toString() : null;
    attribute.setType(newType);
    if (oldType == null || newType == null) {
        return;
    }
    String[] registeredTypes = ModelerUtil.getRegisteredTypeNames();
    Collection<String> registeredTypesList = Arrays.asList(registeredTypes);
    if (registeredTypesList.contains(oldType) == registeredTypesList.contains(newType)) {
        return;
    }
    ObjEntity entity = attribute.getEntity();
    ObjAttribute attributeNew;
    if (registeredTypesList.contains(newType) || !mediator.getEmbeddableNamesInCurrentDataDomain().contains(newType)) {
        attributeNew = new ObjAttribute();
        attributeNew.setDbAttributePath(attribute.getDbAttributePath());
    } else {
        attributeNew = new EmbeddedAttribute();
        attributeNew.setDbAttributePath(null);
    }
    attributeNew.setName(attribute.getName());
    attributeNew.setEntity(entity);
    attributeNew.setParent(attribute.getParent());
    attributeNew.setType(attribute.getType());
    attributeNew.setUsedForLocking(attribute.isUsedForLocking());
    entity.updateAttribute(attributeNew);
    mediator.fireObjEntityEvent(new EntityEvent(this, entity, MapEvent.CHANGE));
    mediator.fireObjEntityDisplayEvent(new EntityDisplayEvent(this, mediator.getCurrentObjEntity(), mediator.getCurrentDataMap(), (DataChannelDescriptor) mediator.getProject().getRootNode()));
    mediator.fireObjAttributeEvent(new AttributeEvent(this, attributeNew, entity, MapEvent.CHANGE));
    mediator.fireObjAttributeDisplayEvent(new AttributeDisplayEvent(this, attributeNew, mediator.getCurrentObjEntity(), mediator.getCurrentDataMap(), (DataChannelDescriptor) mediator.getProject().getRootNode()));
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) AttributeDisplayEvent(org.apache.cayenne.modeler.event.AttributeDisplayEvent) EntityDisplayEvent(org.apache.cayenne.modeler.event.EntityDisplayEvent) ObjAttribute(org.apache.cayenne.map.ObjAttribute) EntityEvent(org.apache.cayenne.map.event.EntityEvent) EmbeddedAttribute(org.apache.cayenne.map.EmbeddedAttribute) AttributeEvent(org.apache.cayenne.map.event.AttributeEvent)

Aggregations

ObjAttribute (org.apache.cayenne.map.ObjAttribute)81 ObjEntity (org.apache.cayenne.map.ObjEntity)57 DbAttribute (org.apache.cayenne.map.DbAttribute)31 ObjRelationship (org.apache.cayenne.map.ObjRelationship)27 DbEntity (org.apache.cayenne.map.DbEntity)26 Test (org.junit.Test)26 DbRelationship (org.apache.cayenne.map.DbRelationship)19 DbJoin (org.apache.cayenne.map.DbJoin)12 DataMap (org.apache.cayenne.map.DataMap)9 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)9 HashMap (java.util.HashMap)8 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)8 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)8 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)8 ArrayList (java.util.ArrayList)7 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)7 HashSet (java.util.HashSet)6 Expression (org.apache.cayenne.exp.Expression)6