@Override public boolean isModified() { for (Datasource datasource : datasourceMap.values()) { if (datasource.isModified()) { return true; } } for (DsContext childDsContext : children) { for (Datasource datasource : childDsContext.getAll()) { if (datasource.isModified()) { return true; } } } return false; }
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 addDsContextToResume(DsContext dsContext, List<CollectionDatasource.Suspendable> list) { for (Datasource datasource : dsContext.getAll()) { if (datasource instanceof CollectionDatasource.Suspendable) { addDatasourceToResume(list, datasource); } } for (DsContext childContext : dsContext.getChildren()) { addDsContextToResume(childContext, list); } }
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); } }
@Override public boolean commit() { Map<DataSupplier, Collection<Datasource<Entity>>> commitData = collectCommitData(); boolean committed = false; if (!commitData.isEmpty()) { DataSupplier dataservice = getDataSupplier(); Set<DataSupplier> suppliers = commitData.keySet(); if (suppliers.size() == 1 && Objects.equals(suppliers.iterator().next(), dataservice)) { CommitContext context = createCommitContext(dataservice, commitData); fireBeforeCommit(context); Set<Entity> committedEntities = dataservice.commit(context); fireAfterCommit(context, committedEntities); notifyAllDsCommited(dataservice, committedEntities); committed = true; } else { throw new UnsupportedOperationException(); } } for (DsContext childDsContext : children) { boolean c = commitToParent(childDsContext.getAll()); committed = committed || c; } boolean c = commitToParent(datasourceMap.values()); committed = committed || c; return committed; }
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(); } } }
protected void addParentsToNested() { if (parentDs == null || getDsContext() == parentDs.getDsContext()) { return; } // Iterate through all datasources in the same DsContext for (Datasource sibling : getDsContext().getAll()) { // If the datasource is a property datasource of the Child if (sibling instanceof NestedDatasource && ((NestedDatasource) sibling).getMaster().equals(this) && !metadata.getTools().isEmbeddable(sibling.getMetaClass())) { // Look for corresponding property datasource in the Parent's DsContext for (Datasource siblingOfParent : parentDs.getDsContext().getAll()) { if (siblingOfParent instanceof NestedDatasource && ((NestedDatasource) siblingOfParent).getProperty().equals(((NestedDatasource) sibling).getProperty()) && ((NestedDatasource) siblingOfParent).getMaster() == parentDs) { // If such corresponding datasource found, set it as a parent for our property datasource ((DatasourceImplementation) sibling).setParent(siblingOfParent); } } } } }
/** * 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 Datasource getPropertyDatasource() { if (pickerField.getDatasource() == null || pickerField.getMetaPropertyPath() == null || pickerField.getMetaPropertyPath().getMetaProperty().getType() != MetaProperty.Type.COMPOSITION || pickerField.getDatasource().getDsContext() == null) return null; for (Datasource datasource : pickerField.getDatasource().getDsContext().getAll()) { if (datasource instanceof NestedDatasource && ((NestedDatasource) datasource).getMaster() == pickerField.getDatasource() && ((NestedDatasource) datasource).getProperty() == pickerField.getMetaPropertyPath().getMetaProperty()) return datasource; } return null; }
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()); } } }
Datasource propertyDatasource = getPropertyDatasource(); if (propertyDatasource != null) { for (Datasource datasource : propertyDatasource.getDsContext().getAll()) { if (datasource instanceof NestedDatasource && ((NestedDatasource) datasource).getMaster() == propertyDatasource
for (Datasource datasource : ds.getDsContext().getAll()) { if (datasource instanceof NestedDatasource && ((NestedDatasource) datasource).getMaster() == ds) { if (datasource.getItem() != null && PersistenceHelper.isNew(datasource.getItem()))