/** * @return bound {@link CollectionDatasource} instance * * @deprecated use {@link HasValueSource#getValueSource()} instead */ @Deprecated @Override default CollectionDatasource getDatasource() { ValueSource<Collection<V>> valueSource = getValueSource(); return valueSource instanceof LegacyCollectionDsValueSource ? ((LegacyCollectionDsValueSource) valueSource).getDatasource() : null; }
/** * Binds the given {@code datasource} with field. * * @param datasource {@link CollectionDatasource} instance * * @deprecated use {@link HasValueSource#setValueSource(ValueSource)} instead */ @Deprecated default void setDatasource(CollectionDatasource datasource) { setValueSource(datasource == null ? null : new LegacyCollectionDsValueSource<>(datasource)); }
@Override public void setValue(Collection<V> value) { if (metaPropertyPath == null) { setDatasourceValue(value); } else { setPropertyDatasourceValue(value); } }
protected void setPropertyDatasourceValue(Collection<V> value) { if (!canUpdateMasterRefs()) { return; Collection<V> itemValue = getMaster().getItem().getValueEx(metaPropertyPath.toPathString()); Collection<V> oldValue = itemValue == null ? null : new ArrayList<>(itemValue); if (equalCollections(oldValue, value)) { return; updateMasterCollection(metaPropertyPath.getMetaProperty(), value); MetaProperty inverseProperty = getInverseProperty(); if (CollectionUtils.isNotEmpty(value)) { for (V v : value) { if (CollectionUtils.isEmpty(oldValue) || !oldValue.contains(v)) { v.setValue(inverseProperty.getName(), getMaster().getItem()); if (!equalCollections(oldValue, value)) { events.publish(ValueChangeEvent.class, new ValueChangeEvent(this, oldValue, value));
protected void updateMasterCollection(MetaProperty metaProperty, @Nullable Collection<V> newCollection) { if (newCollection == null) { getMaster().getItem().setValue(metaProperty.getName(), null); } else { Collection<V> masterCollection; if (List.class.isAssignableFrom(metaProperty.getJavaType())) { masterCollection = new ArrayList(newCollection); } else { masterCollection = new LinkedHashSet(newCollection); } getMaster().getItem().setValue(metaProperty.getName(), masterCollection); } }
protected void setDatasourceValue(Collection<V> value) { Collection<V> oldValue = new ArrayList<>(datasource.getItems()); if (equalCollections(oldValue, value)) { return; } datasource.mute(); if (CollectionUtils.isNotEmpty(oldValue)) { oldValue.forEach(datasource::excludeItem); } if (CollectionUtils.isNotEmpty(value)) { value.forEach(datasource::includeItem); } datasource.unmute(CollectionDatasource.UnmuteEventsMode.FIRE_REFRESH_EVENT); events.publish(ValueChangeEvent.class, new ValueChangeEvent(this, oldValue, value)); }
protected MetaProperty getInverseProperty() { MetaPropertyPath mpp = getMaster().getMetaClass().getPropertyPath(metaPropertyPath.toPathString()); if (mpp == null) { return null; } return mpp.getMetaProperty().getInverse(); }
protected Class<V> getLookupEntityClass() { Class<V> entityClass; ValueSource<Collection<V>> valueSource = getValueSource(); if (valueSource != null) { if (valueSource instanceof EntityValueSource) { //noinspection unchecked entityClass = ((EntityValueSource) valueSource).getMetaPropertyPath().getRangeJavaClass(); } else { entityClass = ((LegacyCollectionDsValueSource<V>) valueSource).getDatasource().getMetaClass().getJavaClass(); } } else if (getOptions() instanceof EntityOptions) { entityClass = ((EntityOptions<V>) getOptions()).getEntityMetaClass().getJavaClass(); } else { throw new RuntimeException("Unable to determine entity class to open lookup"); } return entityClass; }
protected boolean canUpdateMasterRefs() { MetaPropertyPath mpp = getMaster().getMetaClass().getPropertyPath(metaPropertyPath.toPathString()); if (mpp == null) { return false; } if (!mpp.getMetaProperty().getRange().getCardinality().isMany()) { return false; } MetaProperty inverseProperty = mpp.getMetaProperty().getInverse(); if (inverseProperty == null || inverseProperty.getType() != MetaProperty.Type.ASSOCIATION || inverseProperty.getRange().getCardinality().isMany()) { return false; } ExtendedEntities extendedEntities = metadata.getExtendedEntities(); Class invPropEntityClass = extendedEntities.getEffectiveClass(inverseProperty.getDomain()); Class datasourceEntityClass = datasource.getMetaClass().getJavaClass(); //noinspection unchecked return invPropEntityClass.isAssignableFrom(datasourceEntityClass); }
@SuppressWarnings("unchecked") protected void loadDatasource(TokenList tokenList, Element element) { final String datasourceId = element.attributeValue("datasource"); if (StringUtils.isNotEmpty(datasourceId)) { Datasource datasource = context.getDsContext().get(datasourceId); if (datasource == null) { throw new GuiDevelopmentException(String.format("Datasource '%s' is not defined", datasourceId), context.getFullFrameId()); } if (!(datasource instanceof CollectionDatasource)) { throw new GuiDevelopmentException( String.format("Can't set datasource '%s' for TokenList because it supports only CollectionDatasources", datasourceId), context.getFullFrameId()); } tokenList.setValueSource(new LegacyCollectionDsValueSource((CollectionDatasource) datasource)); } }