Search in sources :

Example 66 with ObjAttribute

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

the class EntityMergeSupport method getRidOfAttributesThatAreNowSrcAttributesForRelationships.

private boolean getRidOfAttributesThatAreNowSrcAttributesForRelationships(ObjEntity entity) {
    boolean changed = false;
    for (DbAttribute da : getMeaningfulFKs(entity)) {
        ObjAttribute oa = entity.getAttributeForDbAttribute(da);
        while (oa != null) {
            String attrName = oa.getName();
            entity.removeAttribute(attrName);
            changed = true;
            oa = entity.getAttributeForDbAttribute(da);
        }
    }
    return changed;
}
Also used : ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 67 with ObjAttribute

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

the class ValueForNullIT method test.

@Test
public void test() throws Exception {
    DbEntity dbEntity = map.getDbEntity("PAINTING");
    assertNotNull(dbEntity);
    ObjEntity objEntity = map.getObjEntity("Painting");
    assertNotNull(objEntity);
    // insert some rows before adding "not null" column
    final int nrows = 10;
    for (int i = 0; i < nrows; i++) {
        DataObject o = (DataObject) context.newObject("Painting");
        o.writeProperty("paintingTitle", "ptitle" + i);
    }
    context.commitChanges();
    // create and add new column to model and db
    DbAttribute column = new DbAttribute("NEWCOL2", Types.VARCHAR, dbEntity);
    column.setMandatory(false);
    column.setMaxLength(10);
    dbEntity.addAttribute(column);
    assertTrue(dbEntity.getAttributes().contains(column));
    assertEquals(column, dbEntity.getAttribute(column.getName()));
    assertTokensAndExecute(1, 0);
    // need obj attr to be able to query
    ObjAttribute objAttr = new ObjAttribute("newcol2");
    objAttr.setDbAttributePath(column.getName());
    objEntity.addAttribute(objAttr);
    // check that is was merged
    assertTokensAndExecute(0, 0);
    // set not null
    column.setMandatory(true);
    // merge to db
    assertTokensAndExecute(2, 0);
    // check that is was merged
    assertTokensAndExecute(0, 0);
    // check values for null
    Expression qual = ExpressionFactory.matchExp(objAttr.getName(), DEFAULT_VALUE_STRING);
    SelectQuery query = new SelectQuery("Painting", qual);
    @SuppressWarnings("unchecked") List<Persistent> rows = context.performQuery(query);
    assertEquals(nrows, rows.size());
    // clean up
    dbEntity.removeAttribute(column.getName());
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ObjEntity(org.apache.cayenne.map.ObjEntity) DataObject(org.apache.cayenne.DataObject) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) Expression(org.apache.cayenne.exp.Expression) DbAttribute(org.apache.cayenne.map.DbAttribute) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Example 68 with ObjAttribute

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

the class SQLTemplateAction method configureRowDescriptorBuilder.

/**
 * @since 3.0
 */
protected RowDescriptorBuilder configureRowDescriptorBuilder(SQLStatement compiled, ResultSet resultSet) throws SQLException {
    RowDescriptorBuilder builder = new RowDescriptorBuilder().setResultSet(resultSet).validateDuplicateColumnNames();
    // SQLTemplate #result columns take precedence over other ways to determine the type
    if (compiled.getResultColumns().length > 0) {
        builder.setColumns(compiled.getResultColumns());
    }
    ObjEntity entity = queryMetadata.getObjEntity();
    if (entity != null) {
        // TODO: andrus 2008/03/28 support flattened attributes with aliases...
        for (ObjAttribute attribute : entity.getAttributes()) {
            String column = attribute.getDbAttributePath();
            if (column == null || column.indexOf('.') > 0) {
                continue;
            }
            builder.overrideColumnType(column, attribute.getType());
        }
    }
    // (BigDecimal vs Long vs. Integer)
    if (dbEntity != null) {
        for (DbAttribute attribute : dbEntity.getAttributes()) {
            if (!builder.isOverriden(attribute.getName()) && TypesMapping.isNumeric(attribute.getType())) {
                builder.overrideColumnType(attribute.getName(), TypesMapping.getJavaBySqlType(attribute.getType()));
            }
        }
    }
    switch(query.getColumnNamesCapitalization()) {
        case LOWER:
            builder.useLowercaseColumnNames();
            break;
        case UPPER:
            builder.useUppercaseColumnNames();
            break;
    }
    return builder;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 69 with ObjAttribute

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

the class EJBQLSelectColumnsTranslator method visitPath.

@Override
public boolean visitPath(EJBQLExpression expression, int finishedChildIndex) {
    EJBQLPathTranslator pathTranslator = new EJBQLPathTranslator(context) {

        @Override
        protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
            throw new EJBQLException("Can't use multi-column paths in column clause");
        }

        @Override
        protected void processTerminatingRelationship(ObjRelationship relationship) {
            Map<String, String> xfields = null;
            if (context.isAppendingResultColumns()) {
                xfields = context.nextEntityResult().getFields();
            }
            final Map<String, String> fields = xfields;
            Collection<DbAttribute> dbAttr = ((ObjEntity) relationship.getTargetEntity()).getDbEntity().getAttributes();
            DbRelationship dbRelationship = relationship.getDbRelationships().get(0);
            DbEntity table = (DbEntity) dbRelationship.getTargetEntity();
            Iterator<DbAttribute> it = dbAttr.iterator();
            if (dbAttr.size() > 0) {
                resolveJoin();
            }
            String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
            boolean first = true;
            while (it.hasNext()) {
                context.append(!first ? ", " : " ");
                DbAttribute dbAttribute = it.next();
                appendColumn(TypesMapping.getJavaBySqlType(dbAttribute.getType()), alias, dbAttribute, fields != null ? fields.get(dbAttribute.getName()) : "");
                first = false;
            }
        }

        @Override
        protected void processTerminatingAttribute(ObjAttribute attribute) {
            DbEntity table = currentEntity.getDbEntity();
            String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
            if (attribute.isFlattened()) {
                Iterator<?> dbPathIterator = attribute.getDbPathIterator();
                EJBQLTableId lhsId = new EJBQLTableId(idPath);
                while (dbPathIterator.hasNext()) {
                    Object pathPart = dbPathIterator.next();
                    // later when appending table
                    if (pathPart == null) {
                        throw new CayenneRuntimeException("ObjAttribute has no component: %s", attribute.getName());
                    } else if (pathPart instanceof DbAttribute) {
                        DbAttribute dbAttribute = (DbAttribute) pathPart;
                        appendColumn(attribute.getType(), context.getTableAlias(lhsId.getEntityId(), context.getQuotingStrategy().quotedFullyQualifiedName((DbEntity) dbAttribute.getEntity())), dbAttribute, context.isAppendingResultColumns() ? context.nextColumnAlias() : "");
                    }
                }
            } else {
                DbAttribute dbAttribute = attribute.getDbAttribute();
                appendColumn(attribute.getType(), alias, dbAttribute, context.isAppendingResultColumns() ? context.nextColumnAlias() : "");
            }
        }
    };
    expression.visit(pathTranslator);
    return false;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship)

Example 70 with ObjAttribute

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

the class LockingUpdateController method updateAction.

public void updateAction() {
    int defaultLockType = dataMap.getDefaultLockType();
    boolean on = defaultLockType == ObjEntity.LOCK_TYPE_OPTIMISTIC;
    boolean updateEntities = view.getEntities().isSelected();
    boolean updateAttributes = view.getAttributes().isSelected();
    boolean updateRelationships = view.getRelationships().isSelected();
    ProjectController parent = (ProjectController) getParent();
    for (ObjEntity entity : dataMap.getObjEntities()) {
        if (updateEntities && defaultLockType != entity.getDeclaredLockType()) {
            entity.setDeclaredLockType(defaultLockType);
            parent.fireObjEntityEvent(new EntityEvent(this, entity));
        }
        if (updateAttributes) {
            for (ObjAttribute a : entity.getAttributes()) {
                if (a.isUsedForLocking() != on) {
                    a.setUsedForLocking(on);
                    parent.fireObjAttributeEvent(new AttributeEvent(this, a, entity));
                }
            }
        }
        if (updateRelationships) {
            for (ObjRelationship r : entity.getRelationships()) {
                if (r.isUsedForLocking() != on) {
                    r.setUsedForLocking(on);
                    parent.fireObjRelationshipEvent(new RelationshipEvent(this, r, entity));
                }
            }
        }
    }
    if (view != null) {
        view.dispose();
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) EntityEvent(org.apache.cayenne.map.event.EntityEvent) ProjectController(org.apache.cayenne.modeler.ProjectController) AttributeEvent(org.apache.cayenne.map.event.AttributeEvent) RelationshipEvent(org.apache.cayenne.map.event.RelationshipEvent)

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