Search in sources :

Example 16 with BaseGenericIdEntity

use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.

the class EditorWindowDelegate method setItem.

@SuppressWarnings("unchecked")
public void setItem(Entity item) {
    Datasource ds = getDatasource();
    DataSupplier dataservice = ds.getDataSupplier();
    DatasourceImplementation parentDs = (DatasourceImplementation) ((DatasourceImplementation) ds).getParent();
    DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.NAME);
    if (dynamicAttributesGuiTools.screenContainsDynamicAttributes(ds.getView(), getWrapper().getId())) {
        ds.setLoadDynamicAttributes(true);
    }
    Class<? extends Entity> entityClass = item.getClass();
    Object entityId = item.getId();
    if (parentDs != null) {
        if (!PersistenceHelper.isNew(item) && !parentDs.getItemsToCreate().contains(item) && !parentDs.getItemsToUpdate().contains(item) && parentDs instanceof CollectionDatasource && ((CollectionDatasource) parentDs).containsItem(item.getId()) && !entityStates.isLoadedWithView(item, ds.getView())) {
            item = dataservice.reload(item, ds.getView(), ds.getMetaClass(), ds.getLoadDynamicAttributes());
            if (parentDs instanceof CollectionPropertyDatasourceImpl) {
                ((CollectionPropertyDatasourceImpl) parentDs).replaceItem(item);
            } else {
                ((CollectionDatasource) parentDs).updateItem(item);
            }
        }
        item = EntityCopyUtils.copyCompositions(item);
        handlePreviouslyDeletedCompositionItems(item, parentDs);
    } else if (!PersistenceHelper.isNew(item)) {
        item = dataservice.reload(item, ds.getView(), ds.getMetaClass(), ds.getLoadDynamicAttributes());
    }
    if (item == null) {
        throw new EntityAccessException(entityClass, entityId);
    }
    if (PersistenceHelper.isNew(item) && !ds.getMetaClass().equals(item.getMetaClass())) {
        Entity newItem = ds.getDataSupplier().newInstance(ds.getMetaClass());
        metadata.getTools().copy(item, newItem);
        item = newItem;
    }
    if (ds.getLoadDynamicAttributes() && item instanceof BaseGenericIdEntity) {
        if (PersistenceHelper.isNew(item)) {
            dynamicAttributesGuiTools.initDefaultAttributeValues((BaseGenericIdEntity) item, item.getMetaClass());
        }
        if (item instanceof Categorized) {
            dynamicAttributesGuiTools.listenCategoryChanges(ds);
        }
    }
    ds.setItem(item);
    if (PersistenceHelper.isNew(item)) {
        // make sure that they will be saved on commit.
        for (Datasource datasource : ds.getDsContext().getAll()) {
            if (datasource instanceof NestedDatasource && ((NestedDatasource) datasource).getMaster() == ds) {
                if (datasource.getItem() != null && PersistenceHelper.isNew(datasource.getItem()))
                    ((DatasourceImplementation) datasource).modified(datasource.getItem());
            }
        }
    }
    ((DatasourceImplementation) ds).setModified(false);
    Security security = AppBeans.get(Security.NAME);
    if (!PersistenceHelper.isNew(item) && security.isEntityOpPermitted(ds.getMetaClass(), EntityOp.UPDATE)) {
        readOnly = false;
        LockInfo lockInfo = lockService.lock(getMetaClassForLocking(ds).getName(), item.getId().toString());
        if (lockInfo == null) {
            justLocked = true;
        } else if (!(lockInfo instanceof LockNotSupported)) {
            window.getWindowManager().showNotification(messages.getMainMessage("entityLocked.msg"), String.format(messages.getMainMessage("entityLocked.desc"), lockInfo.getUser().getLogin(), Datatypes.getNN(Date.class).format(lockInfo.getSince(), userSessionSource.getLocale())), Frame.NotificationType.HUMANIZED);
            Action action = window.getAction(Window.Editor.WINDOW_COMMIT);
            if (action != null)
                action.setEnabled(false);
            action = window.getAction(Window.Editor.WINDOW_COMMIT_AND_CLOSE);
            if (action != null)
                action.setEnabled(false);
            readOnly = true;
        }
    }
}
Also used : Categorized(com.haulmont.cuba.core.entity.Categorized) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools) CollectionPropertyDatasourceImpl(com.haulmont.cuba.gui.data.impl.CollectionPropertyDatasourceImpl)

Example 17 with BaseGenericIdEntity

use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.

the class CreateAction method internalOpenEditor.

@SuppressWarnings("unchecked")
protected void internalOpenEditor(CollectionDatasource datasource, Entity newItem, Datasource parentDs, Map<String, Object> params) {
    Window.Editor window = target.getFrame().openEditor(getWindowId(), newItem, getOpenType(), params, parentDs);
    if (editorCloseListener == null) {
        window.addCloseListener(actionId -> {
            // move focus to owner
            target.requestFocus();
            if (Window.COMMIT_ACTION_ID.equals(actionId)) {
                Entity editedItem = window.getItem();
                if (editedItem != null) {
                    if (parentDs == null) {
                        if (editedItem instanceof BaseGenericIdEntity) {
                            BaseGenericIdEntity genericEditedEntity = (BaseGenericIdEntity) editedItem;
                            if (datasource.getLoadDynamicAttributes() && genericEditedEntity.getDynamicAttributes() == null) {
                                DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
                                dynamicAttributesGuiTools.reloadDynamicAttributes(genericEditedEntity);
                            }
                        }
                        if (addFirst && datasource instanceof CollectionDatasource.Ordered)
                            ((CollectionDatasource.Ordered) datasource).includeItemFirst(editedItem);
                        else
                            datasource.includeItem(editedItem);
                    }
                    target.setSelected(editedItem);
                    afterCommit(editedItem);
                    if (afterCommitHandler != null) {
                        afterCommitHandler.handle(editedItem);
                    }
                }
            }
            afterWindowClosed(window);
            if (afterWindowClosedHandler != null) {
                afterWindowClosedHandler.handle(window, actionId);
            }
        });
    } else {
        window.addCloseListener(editorCloseListener);
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)

Example 18 with BaseGenericIdEntity

use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.

the class PersistenceSecurityImpl method applyConstraints.

@SuppressWarnings("unchecked")
protected void applyConstraints(Entity entity, Set<EntityId> handled) {
    MetaClass metaClass = entity.getMetaClass();
    EntityId entityId = new EntityId(referenceToEntitySupport.getReferenceId(entity), metaClass.getName());
    if (handled.contains(entityId)) {
        return;
    }
    handled.add(entityId);
    if (entity instanceof BaseGenericIdEntity) {
        BaseGenericIdEntity baseGenericIdEntity = (BaseGenericIdEntity) entity;
        Multimap<String, Object> filteredData = BaseEntityInternalAccess.getFilteredData(baseGenericIdEntity);
        for (MetaProperty property : metaClass.getProperties()) {
            if (metadataTools.isPersistent(property) && PersistenceHelper.isLoaded(entity, property.getName())) {
                Object value = entity.getValue(property.getName());
                if (value instanceof Collection) {
                    Collection entities = (Collection) value;
                    for (Iterator<Entity> iterator = entities.iterator(); iterator.hasNext(); ) {
                        Entity item = iterator.next();
                        if (filteredData != null && filteredData.containsEntry(property.getName(), referenceToEntitySupport.getReferenceId(item))) {
                            iterator.remove();
                        } else {
                            applyConstraints(item, handled);
                        }
                    }
                } else if (value instanceof Entity) {
                    if (filteredData != null && filteredData.containsEntry(property.getName(), referenceToEntitySupport.getReferenceId((Entity) value))) {
                        baseGenericIdEntity.setValue(property.getName(), null);
                    } else {
                        applyConstraints((Entity) value, handled);
                    }
                }
            }
        }
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 19 with BaseGenericIdEntity

use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.

the class GlobalPersistentAttributesLoadChecker method isLoadedCommonCheck.

protected PropertyLoadedState isLoadedCommonCheck(Object entity, String property) {
    if (entity instanceof BaseGenericIdEntity) {
        BaseGenericIdEntity baseGenericIdEntity = (BaseGenericIdEntity) entity;
        String[] inaccessibleAttributes = BaseEntityInternalAccess.getInaccessibleAttributes(baseGenericIdEntity);
        if (inaccessibleAttributes != null) {
            for (String inaccessibleAttr : inaccessibleAttributes) {
                if (inaccessibleAttr.equals(property))
                    return PropertyLoadedState.NO;
            }
        }
        if (entity instanceof FetchGroupTracker) {
            FetchGroup fetchGroup = ((FetchGroupTracker) entity)._persistence_getFetchGroup();
            if (fetchGroup != null) {
                boolean inFetchGroup = fetchGroup.getAttributeNames().contains(property);
                if (!inFetchGroup) {
                    // definitely not loaded
                    return PropertyLoadedState.NO;
                } else {
                    // requires additional check specific for the tier
                    return PropertyLoadedState.UNKNOWN;
                }
            }
        }
    }
    return PropertyLoadedState.UNKNOWN;
}
Also used : FetchGroupTracker(org.eclipse.persistence.queries.FetchGroupTracker) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) FetchGroup(org.eclipse.persistence.queries.FetchGroup)

Example 20 with BaseGenericIdEntity

use of com.haulmont.cuba.core.entity.BaseGenericIdEntity 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

BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)21 Entity (com.haulmont.cuba.core.entity.Entity)13 MetaProperty (com.haulmont.chile.core.model.MetaProperty)12 MetaClass (com.haulmont.chile.core.model.MetaClass)11 DynamicAttributesGuiTools (com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)3 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)2 Categorized (com.haulmont.cuba.core.entity.Categorized)2 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)2 SecurityState (com.haulmont.cuba.core.entity.SecurityState)2 Element (org.dom4j.Element)2 FetchGroup (org.eclipse.persistence.queries.FetchGroup)2 FetchGroupTracker (org.eclipse.persistence.queries.FetchGroupTracker)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 MetadataObject (com.haulmont.chile.core.model.MetadataObject)1 AbstractInstance (com.haulmont.chile.core.model.impl.AbstractInstance)1 DataService (com.haulmont.cuba.core.app.DataService)1 DynamicAttributes (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributes)1 BaseDbGeneratedIdEntity (com.haulmont.cuba.core.entity.BaseDbGeneratedIdEntity)1 CategoryAttributeValue (com.haulmont.cuba.core.entity.CategoryAttributeValue)1