Search in sources :

Example 51 with EntityResolver

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

the class DefaultDbRowOpSorter method sortReflexive.

protected void sortReflexive(List<DbRowOp> sortedDbRows) {
    DataDomain dataDomain = dataDomainProvider.get();
    EntitySorter sorter = dataDomain.getEntitySorter();
    EntityResolver resolver = dataDomain.getEntityResolver();
    DbEntity lastEntity = null;
    int start = 0;
    int idx = 0;
    DbRowOp lastRow = null;
    for (DbRowOp row : sortedDbRows) {
        if (row.getEntity() != lastEntity) {
            start = idx;
            if (lastEntity != null && sorter.isReflexive(lastEntity)) {
                ObjEntity objEntity = resolver.getObjEntity(lastRow.getObject().getObjectId().getEntityName());
                List<DbRowOp> reflexiveSublist = sortedDbRows.subList(start, idx);
                sorter.sortObjectsForEntity(objEntity, reflexiveSublist, lastRow instanceof DeleteDbRowOp);
            }
            lastEntity = row.getEntity();
        }
        lastRow = row;
        idx++;
    }
    // sort last chunk
    if (lastEntity != null && sorter.isReflexive(lastEntity)) {
        ObjEntity objEntity = resolver.getObjEntity(lastRow.getObject().getObjectId().getEntityName());
        List<DbRowOp> reflexiveSublist = sortedDbRows.subList(start, idx);
        sorter.sortObjectsForEntity(objEntity, reflexiveSublist, lastRow instanceof DeleteDbRowOp);
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DataDomain(org.apache.cayenne.access.DataDomain) EntitySorter(org.apache.cayenne.map.EntitySorter) DbEntity(org.apache.cayenne.map.DbEntity) EntityResolver(org.apache.cayenne.map.EntityResolver)

Example 52 with EntityResolver

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

the class ObjEntityTab method initFromModel.

/**
 * Updates the view from the current model state. Invoked when a currently displayed
 * ObjEntity is changed.
 *
 * @param entity current entity
 */
private void initFromModel(final ObjEntity entity) {
    // TODO: this is a hack until we implement a real MVC
    qualifier.getComponent().setBackground(Color.WHITE);
    name.setText(entity.getName());
    superClassName.setText(entity.getSuperClassName());
    className.setText(entity.getClassName());
    readOnly.setSelected(entity.isReadOnly());
    isAbstract.setSelected(entity.isAbstract());
    comment.setText(getComment(entity));
    serverOnly.setSelected(entity.isServerOnly());
    clientClassName.setText(entity.getClientClassName());
    clientSuperClassName.setText(entity.getClientSuperClassName());
    qualifier.setText(new ExpressionConvertor().valueAsString(entity.getDeclaredQualifier()));
    // TODO: fix inheritance - we should allow to select optimistic
    // lock if superclass is not already locked,
    // otherwise we must keep this checked in but not editable.
    optimisticLocking.setSelected(entity.getDeclaredLockType() == ObjEntity.LOCK_TYPE_OPTIMISTIC);
    // init DbEntities
    EntityResolver resolver = mediator.getEntityResolver();
    DataMap map = mediator.getCurrentDataMap();
    DbEntity[] dbEntities = resolver.getDbEntities().toArray(new DbEntity[0]);
    Arrays.sort(dbEntities, Comparators.getDataMapChildrenComparator());
    DefaultComboBoxModel<DbEntity> dbModel = new DefaultComboBoxModel<>(dbEntities);
    dbModel.setSelectedItem(entity.getDbEntity());
    dbEntityCombo.setRenderer(CellRenderers.entityListRendererWithIcons(map));
    dbEntityCombo.setModel(dbModel);
    boolean isUsedInheritance = entity.getSuperEntity() != null;
    dbEntityCombo.setEnabled(!isUsedInheritance);
    // toggle visibility and editability rules
    toggleClientFieldsVisible(map.isClientSupported());
    toggleEnabled(entity.getSuperEntityName() == null, !entity.isServerOnly());
    // do not show this entity or any of the subentities
    List<ObjEntity> objEntities = map.getObjEntities().stream().filter(object -> entity != object && !object.isSubentityOf(entity)).sorted(Comparators.getDataMapChildrenComparator()).collect(Collectors.toList());
    objEntities.add(0, NO_INHERITANCE);
    DefaultComboBoxModel<ObjEntity> superEntityModel = new DefaultComboBoxModel<>(objEntities.toArray(new ObjEntity[0]));
    superEntityModel.setSelectedItem(entity.getSuperEntity());
    superEntityCombo.setRenderer(CellRenderers.entityListRendererWithIcons(map));
    superEntityCombo.setModel(superEntityModel);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) EntityResolver(org.apache.cayenne.map.EntityResolver) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) ExpressionConvertor(org.apache.cayenne.modeler.util.ExpressionConvertor) DataMap(org.apache.cayenne.map.DataMap)

Example 53 with EntityResolver

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

the class AnnotationCommitLogEntityFactory method createDescriptor.

private CommitLogEntity createDescriptor(String entityName) {
    EntityResolver entityResolver = getEntityResolver();
    ClassDescriptor classDescriptor = entityResolver.getClassDescriptor(entityName);
    CommitLog a = classDescriptor.getObjectClass().getAnnotation(CommitLog.class);
    if (a == null) {
        return BLOCKED_ENTITY;
    }
    ObjEntity entity = entityResolver.getObjEntity(entityName);
    return new MutableCommitLogLogEntity(entity).setConfidential(a.confidential()).setIgnoreProperties(a.ignoredProperties()).setIgnoreAttributes(a.ignoreAttributes()).setIgnoreToOneRelationships(a.ignoreToOneRelationships()).setIgnoreToManyRelationships(a.ignoreToManyRelationships());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) EntityResolver(org.apache.cayenne.map.EntityResolver) CommitLog(org.apache.cayenne.commitlog.CommitLog)

Example 54 with EntityResolver

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

the class ObjectIdBatchFault method fetchObjects.

private Map<String, Object> fetchObjects() {
    if (sources == null) {
        return Collections.emptyMap();
    }
    EntityResolver resolver = context.getEntityResolver();
    // reasons
    if (sources.size() == 1) {
        String uuid = sources.get(0).getId();
        String entityName = EntityIdCoder.getEntityName(uuid);
        ObjEntity entity = resolver.getObjEntity(entityName);
        ObjectId id = new EntityIdCoder(entity).toObjectId(uuid);
        Object object = Cayenne.objectForQuery(context, new ObjectIdQuery(id));
        if (object == null) {
            return Collections.emptyMap();
        } else {
            return Collections.singletonMap(uuid, object);
        }
    }
    Map<String, ObjectSelect<DataObject>> queriesByEntity = new HashMap<>();
    Map<String, EntityIdCoder> codersByEntity = new HashMap<>();
    for (ObjectIdBatchSourceItem source : sources) {
        String uuid = source.getId();
        String entityName = EntityIdCoder.getEntityName(uuid);
        EntityIdCoder coder = codersByEntity.get(entityName);
        ObjectSelect<DataObject> query;
        if (coder == null) {
            coder = new EntityIdCoder(resolver.getObjEntity(entityName));
            codersByEntity.put(entityName, coder);
            query = ObjectSelect.query(DataObject.class, entityName);
            queriesByEntity.put(entityName, query);
        } else {
            query = queriesByEntity.get(entityName);
        }
        ObjectId id = coder.toObjectId(uuid);
        Expression idExp = ExpressionFactory.matchAllDbExp(id.getIdSnapshot(), Expression.EQUAL_TO);
        query.or(idExp);
    }
    int capacity = (int) Math.ceil(sources.size() / 0.75d);
    Map<String, Object> results = new HashMap<>(capacity);
    for (ObjectSelect<DataObject> query : queriesByEntity.values()) {
        EntityIdCoder coder = codersByEntity.get(query.getEntityName());
        List<DataObject> objects = query.select(context);
        for (DataObject object : objects) {
            String uuid = coder.toStringId(object.getObjectId());
            results.put(uuid, object);
        }
    }
    return results;
}
Also used : EntityIdCoder(org.apache.cayenne.lifecycle.id.EntityIdCoder) ObjectId(org.apache.cayenne.ObjectId) HashMap(java.util.HashMap) ObjectSelect(org.apache.cayenne.query.ObjectSelect) EntityResolver(org.apache.cayenne.map.EntityResolver) ObjEntity(org.apache.cayenne.map.ObjEntity) DataObject(org.apache.cayenne.DataObject) Expression(org.apache.cayenne.exp.Expression) DataObject(org.apache.cayenne.DataObject) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery)

Example 55 with EntityResolver

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

the class ObjGraphBuilder method isIsolated.

@Override
protected boolean isIsolated(DataChannelDescriptor domain, Entity entity) {
    if (!super.isIsolated(domain, entity)) {
        return false;
    }
    if (((ObjEntity) entity).getSuperEntity() != null) {
        return false;
    }
    // TODO: andrus 05/30/2010 - reindexing all DataMaps every time may be VERY slow on large projects
    EntityResolver resolver = new EntityResolver(domain.getDataMaps());
    EntityInheritanceTree inheritanceTree = resolver.getInheritanceTree(entity.getName());
    return inheritanceTree == null || inheritanceTree.getChildren().isEmpty();
}
Also used : EntityInheritanceTree(org.apache.cayenne.map.EntityInheritanceTree) EntityResolver(org.apache.cayenne.map.EntityResolver)

Aggregations

EntityResolver (org.apache.cayenne.map.EntityResolver)87 Test (org.junit.Test)57 ObjEntity (org.apache.cayenne.map.ObjEntity)17 ObjectId (org.apache.cayenne.ObjectId)14 DataMap (org.apache.cayenne.map.DataMap)14 DbEntity (org.apache.cayenne.map.DbEntity)10 ArrayList (java.util.ArrayList)7 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)7 HashMap (java.util.HashMap)6 QueryResponse (org.apache.cayenne.QueryResponse)6 DataNode (org.apache.cayenne.access.DataNode)6 RowReaderFactory (org.apache.cayenne.access.jdbc.reader.RowReaderFactory)6 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)6 MockEventManager (org.apache.cayenne.event.MockEventManager)5 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)5 GenericResponse (org.apache.cayenne.util.GenericResponse)5 List (java.util.List)4 MappingNamespace (org.apache.cayenne.map.MappingNamespace)4 Artist (org.apache.cayenne.testdo.testmap.Artist)4 Map (java.util.Map)3