protected void handleEditorCommit(Entity editorItem, Entity rowItem, String columnId) { MetaPropertyPath mpp = rowItem.getMetaClass().getPropertyPath(columnId); if (mpp == null) { throw new IllegalStateException(String.format("Unable to find metaproperty %s for class %s", columnId, rowItem.getMetaClass())); } if (mpp.getRange().isClass()) { DatasourceImplementation ds = ((DatasourceImplementation) table.getDatasource()); boolean modifiedInTable = ds.getItemsToUpdate().contains(rowItem); boolean ownerDsModified = ds.isModified(); rowItem.setValueEx(columnId, null); rowItem.setValueEx(columnId, editorItem); // restore modified for owner datasource // remove from items to update if it was not modified before setValue if (!modifiedInTable) { ds.getItemsToUpdate().remove(rowItem); } ds.setModified(ownerDsModified); } else { //noinspection unchecked table.getDatasource().updateItem(editorItem); } }
DatasourceImplementation<UserRole> rolesDsImpl = (DatasourceImplementation) rolesDs; CommitContext ctx = new CommitContext(Collections.emptyList(), rolesDsImpl.getItemsToDelete()); dataSupplier.commit(ctx); List<UserRole> modifiedRoles = new ArrayList<>(rolesDsImpl.getItemsToCreate()); modifiedRoles.addAll(rolesDsImpl.getItemsToUpdate()); rolesDsImpl.committed(Collections.emptySet()); for (UserRole userRole : modifiedRoles) { rolesDsImpl.modified(userRole);
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; }
DataSupplier dataservice = ds.getDataSupplier(); DatasourceImplementation parentDs = (DatasourceImplementation) ((DatasourceImplementation) ds).getParent(); && !parentDs.getItemsToCreate().contains(item) && !parentDs.getItemsToUpdate().contains(item) && parentDs instanceof CollectionDatasource && ((CollectionDatasource) parentDs).containsItem(item.getId()) 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);
public void setModified(boolean modified) { ((DatasourceImplementation) datasource).setModified(modified); }
public void initCopy() { @SuppressWarnings("unchecked") DatasourceImplementation<UserRole> rolesDsImpl = (DatasourceImplementation) rolesDs; for (UserRole item : rolesDs.getItems()) { rolesDsImpl.modified(item); } }
protected void initDatasources(Window window, DsContext dsContext, @SuppressWarnings("unused") Map<String, Object> params) { ((LegacyFrame) window.getFrameOwner()).setDsContext(dsContext); for (Datasource ds : dsContext.getAll()) { if (Datasource.State.NOT_INITIALIZED.equals(ds.getState()) && ds instanceof DatasourceImplementation) { ((DatasourceImplementation) ds).initialized(); } } }
/** * 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()); } } } } } }
protected Map<DataSupplier, Collection<Datasource<Entity>>> collectCommitData() { Collection<Datasource> datasources = new ArrayList<>(); for (DsContext childDsContext : children) { datasources.addAll(childDsContext.getAll()); } datasources.addAll(datasourceMap.values()); Map<DataSupplier, Collection<Datasource<Entity>>> commitDatasources = new HashMap<>(); for (Datasource datasource : datasources) { if (Datasource.CommitMode.DATASTORE == datasource.getCommitMode() && datasource.isAllowCommit() && (datasource.isModified() || !((DatasourceImplementation) datasource).getItemsToCreate().isEmpty())) { DataSupplier dataservice = datasource.getDataSupplier(); Collection<Datasource<Entity>> collection = commitDatasources.get(dataservice); if (collection == null) { collection = new ArrayList<>(); commitDatasources.put(dataservice, collection); } collection.add(datasource); } } return commitDatasources; }
protected void notifyAllDsCommited(DataSupplier dataservice, Set<Entity> committedEntities) { // Notify all datasources in context List<Datasource> datasources = new ArrayList<>(); for (DsContext childDsContext : children) { for (Datasource ds : childDsContext.getAll()) { if (Objects.equals(ds.getDataSupplier(), dataservice) && ds.getCommitMode() == Datasource.CommitMode.DATASTORE) { datasources.add(ds); } } } for (Datasource ds : datasourceMap.values()) { if (Objects.equals(ds.getDataSupplier(), dataservice) && ds.getCommitMode() == Datasource.CommitMode.DATASTORE) { datasources.add(ds); } } List<Datasource> sortedList = new DsTree(datasources).toDsList(); for (Datasource datasource : sortedList) { ((DatasourceImplementation) datasource).committed(committedEntities); } }
((DatasourceImplementation) propertyDatasource).deleted(value);
@Override @SuppressWarnings("unchecked") public void actionPerform(Component component) { Map<String, Object> editorParams = new HashMap<>(); editorParams.put("metaClass", entityMeta.getName()); editorParams.put("autocommit", Boolean.FALSE); MetaProperty inverseProperty = metaProperty.getInverse(); if (inverseProperty != null) { editorParams.put("parentProperty", inverseProperty.getName()); } editorParams.put("parent", item); if (metaProperty.getType() == MetaProperty.Type.COMPOSITION) { editorParams.put("parentDs", entitiesDs); } Editor editor = (Editor) openWindow("entityInspector.edit", OPEN_TYPE, editorParams); editor.addCloseListener(actionId -> { if (COMMIT_ACTION_ID.equals(actionId) && metaProperty.getType() == MetaProperty.Type.ASSOCIATION) { boolean modified = entitiesDs.isModified(); entitiesDs.addItem(editor.getItem()); ((DatasourceImplementation) entitiesDs).setModified(modified); } }); } }
final Entity parentItem = masterDs.getItem(); ((DatasourceImplementation) masterDs).modified(parentItem); } else { deleted(item);
protected CollectionDatasource<Entity<Object>, Object> createOptionsDataSource(MetaClass metaClass) { CollectionDatasource ds = DsBuilder.create() .setMetaClass(metaClass) .setViewName(entityView) .buildCollectionDatasource(); ds.setRefreshOnComponentValueChange(true); ((DatasourceImplementation) ds).initialized(); if (!StringUtils.isBlank(entityWhere)) { QueryTransformer transformer = QueryTransformerFactory.createTransformer( "select e from " + metaClass.getName() + " e"); transformer.addWhere(entityWhere); String q = transformer.getResult(); ds.setQuery(q); } // TODO filter ds // if (WindowParams.DISABLE_AUTO_REFRESH.getBool(datasource.getDsContext().getFrameContext())) { // if (ds instanceof CollectionDatasource.Suspendable) // ((CollectionDatasource.Suspendable) ds).refreshIfNotSuspended(); // else ds.refresh(); // } return ds; }
protected boolean commitToParent(Collection<Datasource> datasources) { List<Datasource> list = new ArrayList<>(); for (Datasource datasource : datasources) { if (Datasource.CommitMode.PARENT.equals(datasource.getCommitMode()) && (datasource.isModified() || !((DatasourceImplementation) datasource).getItemsToCreate().isEmpty())) { list.add(datasource); } } List<Datasource> sortedList = new DsTree(list).toDsList(); for (Datasource datasource : sortedList) { datasource.commit(); } return !list.isEmpty(); }
nonModifiedInTable = !ownerDs.getItemsToUpdate().contains( ((EntityValueSource) getValueSource()).getItem()); ownerDsModified = ownerDs.isModified(); } else if (getCollectionContainerFromOwner() != null) { ownerCollectionCont = ((ContainerDataUnit) owner.getItems()).getContainer(); ownerDs.getItemsToUpdate().remove(getDatasource().getItem()); ownerDs.setModified(ownerDsModified); } else if (ownerCollectionCont != null) { ownerCollectionCont.unmute();
protected void subscribeCreateActionCloseHandler(CreateAction createAction) { createAction.setAfterWindowClosedHandler(((window, closeActionId) -> { if (!COMMIT_ACTION_ID.equals(closeActionId)) { Report newReport = (Report) ((Editor) window).getItem(); if (!PersistenceHelper.isNew(newReport)) { DataSupplier dataSupplier = getDsContext().getDataSupplier(); Report reloadedReport = dataSupplier.reload(newReport, reportDs.getView()); boolean modified = reportDs.isModified(); reportDs.addItem(reloadedReport); ((DatasourceImplementation) reportDs).setModified(modified); } } })); } }
@Override public void modifyItem(T item) { checkNotNullArgument(item, "item is null"); Collection<T> collection = getCollection(); if (collection != null) { for (T t : collection) { if (t.equals(item)) { EntityCopyUtils.copyCompositionsBack(item, t); modified = true; if (cascadeProperty) { final Entity parentItem = masterDs.getItem(); //noinspection unchecked ((DatasourceImplementation) masterDs).modified(parentItem); } else { modified(t); } } } fireCollectionChanged(Operation.UPDATE, Collections.singletonList(item)); } }
protected void loadDsContext(@Nullable Element dsContextElement) { DsContext dsContext = null; if (resultComponent.getFrameOwner() instanceof LegacyFrame) { DsContextLoader dsContextLoader; DsContext parentDsContext = context.getParent().getDsContext(); if (parentDsContext != null){ dsContextLoader = new DsContextLoader(parentDsContext.getDataSupplier()); } else { dsContextLoader = new DsContextLoader(new GenericDataSupplier()); } dsContext = dsContextLoader.loadDatasources(dsContextElement, parentDsContext, getContext().getAliasesMap()); ((ComponentLoaderContext) context).setDsContext(dsContext); } if (dsContext != null) { FrameOwner frameOwner = getContext().getFrame().getFrameOwner(); if (frameOwner instanceof LegacyFrame) { LegacyFrame frame = (LegacyFrame) frameOwner; frame.setDsContext(dsContext); for (Datasource ds : dsContext.getAll()) { if (ds instanceof DatasourceImplementation) { ((DatasourceImplementation) ds).initialized(); } } dsContext.setFrameContext(resultComponent.getContext()); } } }
/** * Called by the framework to set an edited entity after creation of all components and datasources, and after * {@link #init(java.util.Map)}. * <p>Don't override this method in subclasses, use hooks {@link #initNewItem(com.haulmont.cuba.core.entity.Entity)} * and {@link #postInit()} instead.</p> * @param item entity instance */ @SuppressWarnings("unchecked") @Override public void setItem(Entity item) { if (PersistenceHelper.isNew(item)) { DatasourceImplementation parentDs = (DatasourceImplementation) getParentDs(); if (parentDs == null || !parentDs.getItemsToCreate().contains(item)) { initNewItem((T) item); } } setItemInternal(item); postInit(); }