Search in sources :

Example 1 with AbstractInstance

use of com.haulmont.chile.core.model.impl.AbstractInstance in project cuba by cuba-platform.

the class EntityCopyUtils method copyCompositionsBack.

public static void copyCompositionsBack(Entity source, Entity dest) {
    Preconditions.checkNotNullArgument(source, "source is null");
    Preconditions.checkNotNullArgument(dest, "dest is null");
    for (MetaProperty srcProperty : source.getMetaClass().getProperties()) {
        String name = srcProperty.getName();
        MetaProperty dstProperty = dest.getMetaClass().getProperty(name);
        if (dstProperty != null && !dstProperty.isReadOnly()) {
            try {
                Object value = source.getValue(name);
                if (value != null && srcProperty.getRange().getCardinality().isMany() && srcProperty.getType() == MetaProperty.Type.COMPOSITION) {
                    ((AbstractInstance) dest).setValue(name, source.getValue(name), false);
                } else {
                    dest.setValue(name, source.getValue(name));
                }
            } catch (RuntimeException e) {
                Throwable cause = ExceptionUtils.getRootCause(e);
                if (cause == null)
                    cause = e;
                // ignore exception on copy for not loaded fields
                if (!isNotLoadedAttributeException(cause))
                    throw e;
            }
        }
    }
}
Also used : AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 2 with AbstractInstance

use of com.haulmont.chile.core.model.impl.AbstractInstance in project cuba by cuba-platform.

the class PropertyDatasourceImpl method committed.

@Override
public void committed(Set<Entity> entities) {
    Entity parentItem = masterDs.getItem();
    T prevItem = getItem();
    T newItem = null;
    for (Entity entity : entities) {
        if (entity.equals(prevItem)) {
            // noinspection unchecked
            newItem = (T) entity;
            break;
        }
    }
    // If committed set contains previousItem
    if ((parentItem != null) && newItem != null) {
        // Value changed
        boolean isModified = masterDs.isModified();
        AbstractInstance parentInstance = (AbstractInstance) parentItem;
        parentInstance.setValue(metaProperty.getName(), newItem, false);
        detachListener(prevItem);
        attachListener(newItem);
        fireItemChanged(prevItem);
        ((DatasourceImplementation) masterDs).setModified(isModified);
    } else {
        if (parentItem != null) {
            Entity newParentItem = null;
            Entity previousParentItem = null;
            // Find previous and new parent items
            Iterator<Entity> commitIter = entities.iterator();
            while (commitIter.hasNext() && (previousParentItem == null) && (newParentItem == null)) {
                Entity commitItem = commitIter.next();
                if (commitItem.equals(parentItem)) {
                    previousParentItem = parentItem;
                    newParentItem = commitItem;
                }
            }
            if (previousParentItem != null) {
                detachListener(getItem(previousParentItem));
            }
            if (newParentItem != null) {
                attachListener(getItem(newParentItem));
            }
        }
    }
    modified = false;
    clearCommitLists();
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance)

Example 3 with AbstractInstance

use of com.haulmont.chile.core.model.impl.AbstractInstance in project cuba by cuba-platform.

the class RdbmsStore method updateReferences.

protected void updateReferences(Entity entity, Entity refEntity, Set<Entity> visited) {
    if (entity == null || refEntity == null || visited.contains(entity))
        return;
    visited.add(entity);
    MetaClass refEntityMetaClass = refEntity.getMetaClass();
    for (MetaProperty property : entity.getMetaClass().getProperties()) {
        if (!property.getRange().isClass() || !property.getRange().asClass().equals(refEntityMetaClass))
            continue;
        if (PersistenceHelper.isLoaded(entity, property.getName())) {
            if (property.getRange().getCardinality().isMany()) {
                Collection collection = entity.getValue(property.getName());
                if (collection != null) {
                    for (Object obj : collection) {
                        updateReferences((Entity) obj, refEntity, visited);
                    }
                }
            } else {
                Entity value = entity.getValue(property.getName());
                if (value != null) {
                    if (value.getId().equals(refEntity.getId())) {
                        if (entity instanceof AbstractInstance) {
                            if (property.isReadOnly() && metadata.getTools().isNotPersistent(property)) {
                                continue;
                            }
                            ((AbstractInstance) entity).setValue(property.getName(), refEntity, false);
                        }
                    } else {
                        updateReferences(value, refEntity, visited);
                    }
                }
            }
        }
    }
}
Also used : AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 4 with AbstractInstance

use of com.haulmont.chile.core.model.impl.AbstractInstance in project cuba by cuba-platform.

the class DsContextImpl method replaceMasterCopies.

// Replace the reference to master entity with actual entity containing in the master datasource,
// because in case of nested property datasources there may be references to cloned master entities.
protected void replaceMasterCopies(Entity entity, NestedDatasource datasource) {
    Datasource masterDs = datasource.getMaster();
    MetaProperty metaProperty = datasource.getProperty();
    if (masterDs != null && metaProperty != null) {
        MetaProperty inverseProp = metaProperty.getInverse();
        if (inverseProp != null && !inverseProp.getRange().getCardinality().isMany()) {
            MetaClass metaClass = metadata.getExtendedEntities().getEffectiveMetaClass(inverseProp.getDomain());
            if (metaClass.equals(datasource.getMetaClass()) && (PersistenceHelper.isLoaded(entity, inverseProp.getName()) && // replace master only if it's already set
            entity.getValue(inverseProp.getName()) != null)) {
                Object masterItem = null;
                if (masterDs instanceof CollectionDatasource) {
                    Entity value = entity.getValue(inverseProp.getName());
                    if (value != null) {
                        Object id = value.getId();
                        // noinspection unchecked
                        masterItem = ((CollectionDatasource) masterDs).getItem(id);
                    }
                } else {
                    masterItem = masterDs.getItem();
                }
                if (masterItem != null) {
                    // CAUTION need to rework this mechanism in case of two or more nested collection datasources
                    ((AbstractInstance) entity).setValue(inverseProp.getName(), masterItem, false);
                }
            }
        }
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 5 with AbstractInstance

use of com.haulmont.chile.core.model.impl.AbstractInstance in project cuba by cuba-platform.

the class DataServiceController method commit.

@RequestMapping(value = "/api/commit", method = RequestMethod.POST)
public void commit(@RequestParam(value = "s") String sessionId, @RequestHeader(value = "Content-Type") MimeType contentType, @RequestBody String requestContent, HttpServletRequest request, HttpServletResponse response) throws IOException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
    if (!connect(sessionId, response))
        return;
    try {
        Converter converter = conversionFactory.getConverter(contentType);
        CommitRequest commitRequest = converter.parseCommitRequest(requestContent);
        Collection commitInstances = commitRequest.getCommitInstances();
        Set<String> newInstanceIds = commitRequest.getNewInstanceIds();
        assignUuidToNewInstances(commitInstances, newInstanceIds);
        for (Object commitInstance : commitInstances) {
            if (commitInstance instanceof BaseGenericIdEntity && !isNewInstance(newInstanceIds, commitInstance) && !PersistenceHelper.isDetached(commitInstance)) {
                PersistenceHelper.makePatch((BaseGenericIdEntity) commitInstance);
            }
        }
        // send error if the user don't have permissions to commit at least one of the entities
        if (!commitPermitted(commitInstances, newInstanceIds)) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
        Collection removeInstances = commitRequest.getRemoveInstances();
        // send error if the user don't have permissions to remove at least one of the entities
        if (!removePermitted(removeInstances)) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
        CommitContext commitContext = new CommitContext();
        commitContext.setCommitInstances(commitInstances);
        commitContext.setRemoveInstances(removeInstances);
        commitContext.setSoftDeletion(commitRequest.isSoftDeletion());
        List<EntityLoadInfo> newInstances = new ArrayList<>(newInstanceIds.size());
        for (String str : newInstanceIds) {
            newInstances.add(entityLoadInfoBuilder.parse(str));
        }
        for (Entity entity : commitContext.getCommitInstances()) {
            MetaClass metaClass = metadata.getSession().getClassNN(entity.getClass());
            for (MetaProperty property : metaClass.getProperties()) {
                if (property.getRange().isClass() && !metadata.getTools().isEmbedded(property) && !property.getRange().getCardinality().isMany() && !property.isReadOnly() && PersistenceHelper.isLoaded(entity, property.getName())) {
                    Entity refEntity = entity.getValue(property.getName());
                    if (refEntity == null || refEntity.getId() == null)
                        continue;
                    if (entityLoadInfoBuilder.contains(newInstances, refEntity)) {
                        // reference to a new entity
                        Entity e = getEntityById(commitContext.getCommitInstances(), refEntity.getId());
                        ((AbstractInstance) entity).setValue(property.getName(), e, false);
                    } else if (BaseGenericIdEntity.class.isAssignableFrom(refEntity.getMetaClass().getJavaClass())) {
                        // reference to an existing entity
                        Object refEntityId = refEntity.getId();
                        refEntity = metadata.create(refEntity.getMetaClass());
                        ((BaseGenericIdEntity) refEntity).setId(refEntityId);
                        PersistenceHelper.makeDetached((BaseGenericIdEntity) refEntity);
                        ((AbstractInstance) entity).setValue(property.getName(), refEntity, false);
                    }
                }
            }
        }
        Set<Entity> result = dataService.commit(commitContext);
        String converted = converter.process(result);
        writeResponse(response, converted, converter.getMimeType());
    } catch (RowLevelSecurityException e) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN, "The operation with entity " + e.getEntity() + " is denied");
    } catch (Throwable e) {
        sendError(request, response, e);
    } finally {
        authentication.end();
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

AbstractInstance (com.haulmont.chile.core.model.impl.AbstractInstance)5 MetaProperty (com.haulmont.chile.core.model.MetaProperty)4 MetaClass (com.haulmont.chile.core.model.MetaClass)3 Entity (com.haulmont.cuba.core.entity.Entity)3 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)1