/** * This method is required for multi-level composition, when a user deletes records from nested editors, saves them * and then reopens. When an editor is opened, we reload the item from the database, hence we need to remove * nested items previously deleted by the user. */ protected void handlePreviouslyDeletedCompositionItems(Entity entity, DatasourceImplementation parentDs) { Metadata metadata = getBeanLocator().get(Metadata.NAME); for (MetaProperty property : metadata.getClassNN(entity.getClass()).getProperties()) { if (!PersistenceHelper.isLoaded(entity, property.getName())) return; if (property.getType() == MetaProperty.Type.COMPOSITION) { for (Datasource datasource : parentDs.getDsContext().getAll()) { if (datasource instanceof NestedDatasource && ((NestedDatasource) datasource).getMaster().equals(parentDs)) { Object value = entity.getValue(property.getName()); if (value instanceof Collection) { Collection collection = (Collection) value; //noinspection unchecked collection.removeAll(((DatasourceImplementation) datasource).getItemsToDelete()); } } } } } }
DatasourceImplementation<UserRole> rolesDsImpl = (DatasourceImplementation) rolesDs; CommitContext ctx = new CommitContext(Collections.emptyList(), rolesDsImpl.getItemsToDelete()); dataSupplier.commit(ctx);
protected CommitContext createCommitContext(DataSupplier dataservice, Map<DataSupplier, Collection<Datasource<Entity>>> commitData) { CommitContext context = new CommitContext(); context.setDiscardCommitted(discardCommitted); for (Datasource<Entity> datasource : commitData.get(dataservice)) { if (datasource instanceof DatasourceImplementation) { DatasourceImplementation<Entity> implementation = (DatasourceImplementation) datasource; boolean listenersEnabled = implementation.enableListeners(false); try { for (Entity entity : implementation.getItemsToCreate()) { addToContext(entity, datasource, context.getCommitInstances(), context.getViews()); } for (Entity entity : implementation.getItemsToUpdate()) { addToContext(entity, datasource, context.getCommitInstances(), context.getViews()); } for (Entity entity : implementation.getItemsToDelete()) { addToContext(entity, datasource, context.getRemoveInstances(), context.getViews()); } } finally { implementation.enableListeners(listenersEnabled); } } } repairReferences(context); return context; }