This is an automated email from the ASF dual-hosted git repository. ahuber pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/isis.git
commit ad04e45dd83ce39232d41b8488fb22bac8c3ebc3 Author: Andi Huber <[email protected]> AuthorDate: Fri Apr 24 13:33:02 2020 +0200 ISIS-2340: share logic of ObjectAdapterAccessHelper/UpdateHelper (6) --- .../ui/components/object/ObjectFormView.java | 1 - .../model/binding/interaction/ObjectBinding.java | 17 ++++--- .../binding/interaction/PropertyAccessChain.java | 56 ++++++++++++++++++++++ .../resources/DomainObjectResourceServerside.java | 39 +++------------ .../resources/ObjectAdapterAccessHelper.java | 9 ++-- 5 files changed, 76 insertions(+), 46 deletions(-) diff --git a/incubator/viewers/vaadin/ui/src/main/java/org/apache/isis/incubator/viewer/vaadin/ui/components/object/ObjectFormView.java b/incubator/viewers/vaadin/ui/src/main/java/org/apache/isis/incubator/viewer/vaadin/ui/components/object/ObjectFormView.java index 6333a8f..d531fbb 100644 --- a/incubator/viewers/vaadin/ui/src/main/java/org/apache/isis/incubator/viewer/vaadin/ui/components/object/ObjectFormView.java +++ b/incubator/viewers/vaadin/ui/src/main/java/org/apache/isis/incubator/viewer/vaadin/ui/components/object/ObjectFormView.java @@ -185,7 +185,6 @@ public class ObjectFormView extends VerticalLayout { protected void onProperty(HasComponents container, PropertyLayoutData propertyData) { objectInteractor .getPropertyBinding(propertyData.getId(), Where.OBJECT_FORMS) - .left() // if visible .ifPresent(propertyBinding->{ val uiProperty = uiComponentFactory .componentFor(UiComponentFactory.Request.of(propertyBinding)); diff --git a/viewers/common/src/main/java/org/apache/isis/viewer/common/model/binding/interaction/ObjectBinding.java b/viewers/common/src/main/java/org/apache/isis/viewer/common/model/binding/interaction/ObjectBinding.java index 8b1a71d..d15f67d 100644 --- a/viewers/common/src/main/java/org/apache/isis/viewer/common/model/binding/interaction/ObjectBinding.java +++ b/viewers/common/src/main/java/org/apache/isis/viewer/common/model/binding/interaction/ObjectBinding.java @@ -172,16 +172,18 @@ public class ObjectBinding { /** * @param propertyId * @param where - * @return either an editable or readonly PropertyBinding, based on visibility and usability + * @return PropertyAccessChain with either an editable or readonly PropertyBinding, + * based on visibility and usability */ - public _Either<PropertyBinding, InteractionResponse> getPropertyBinding(String propertyId, Where where) { - return getPropertyThatIsVisible(propertyId, where) + public PropertyAccessChain getPropertyBinding(String propertyId, Where where) { + val either = getPropertyThatIsVisible(propertyId, where) .map( property-> checkUsability(property, where).isSuccess() ? PropertyBindingEditable.of(managedObject, property) : PropertyBindingReadonly.of(managedObject, property), UnaryOperator.identity()); + return PropertyAccessChain.of(either); } /** @@ -189,14 +191,15 @@ public class ObjectBinding { * @param propertyId * @param where * @param accessIntent - * @return either an editable or readonly PropertyBinding, based on visibility and accessIntent + * @return PropertyAccessChain with either an editable or readonly PropertyBinding, + * based on visibility and accessIntent */ - public _Either<PropertyBinding, InteractionResponse> getPropertyBinding( + public PropertyAccessChain getPropertyBinding( final String propertyId, final Where where, final AccessIntent accessIntent) { - return getPropertyThatIsVisibleForIntent(propertyId, where, accessIntent) + val either = getPropertyThatIsVisibleForIntent(propertyId, where, accessIntent) .map( property->{ switch(accessIntent) { @@ -209,7 +212,7 @@ public class ObjectBinding { } }, UnaryOperator.identity()); - + return PropertyAccessChain.of(either); } /** diff --git a/viewers/common/src/main/java/org/apache/isis/viewer/common/model/binding/interaction/PropertyAccessChain.java b/viewers/common/src/main/java/org/apache/isis/viewer/common/model/binding/interaction/PropertyAccessChain.java new file mode 100644 index 0000000..633f632 --- /dev/null +++ b/viewers/common/src/main/java/org/apache/isis/viewer/common/model/binding/interaction/PropertyAccessChain.java @@ -0,0 +1,56 @@ +package org.apache.isis.viewer.common.model.binding.interaction; + +import java.util.function.Consumer; +import java.util.function.Function; + +import org.apache.isis.core.commons.internal.base._Either; +import org.apache.isis.core.commons.internal.exceptions._Exceptions; +import org.apache.isis.core.metamodel.spec.ManagedObject; +import org.apache.isis.core.metamodel.spec.feature.OneToOneAssociation; + +import lombok.NonNull; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor(staticName = "of") +public class PropertyAccessChain { + + @NonNull private _Either<PropertyBinding, InteractionResponse> chain; + + public PropertyAccessChain modifyProperty( + @NonNull final Function<OneToOneAssociation, ManagedObject> newProperyValueProvider) { + + chain = chain.leftRemap(propertyBinding->{ + final InteractionResponse iResponse = propertyBinding.modifyProperty(newProperyValueProvider); + if(iResponse.isFailure()) { + _Either.right(iResponse); + } + return _Either.left(propertyBinding); + }); + + return this; + } + + public PropertyAccessChain onFailure(Consumer<InteractionResponse> onFailure) { + chain.right().ifPresent(onFailure); + return this; + } + + public PropertyBinding getBinding() { + return chain.left() + .orElseThrow(_Exceptions::noSuchElement); + } + + public OneToOneAssociation getProperty() { + return chain.left() + .map(PropertyBinding::getProperty) + .orElseThrow(_Exceptions::noSuchElement); + } + + public PropertyAccessChain ifPresent(Consumer<PropertyBinding> propertyBindingConsumer) { + chain.left().ifPresent(propertyBindingConsumer::accept); + return this; + } + + + +} diff --git a/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/DomainObjectResourceServerside.java b/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/DomainObjectResourceServerside.java index 3179d0f..4bd5c3a 100644 --- a/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/DomainObjectResourceServerside.java +++ b/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/DomainObjectResourceServerside.java @@ -47,7 +47,6 @@ import org.apache.isis.applib.layout.grid.Grid; import org.apache.isis.applib.layout.links.Link; import org.apache.isis.applib.services.command.Command; import org.apache.isis.core.commons.internal.base._Bytes; -import org.apache.isis.core.commons.internal.base._Either; import org.apache.isis.core.commons.internal.base._Strings; import org.apache.isis.core.commons.internal.codec._UrlDecoderUtil; import org.apache.isis.core.commons.internal.resources._Resources; @@ -457,24 +456,13 @@ public class DomainObjectResourceServerside extends ResourceAbstract implements propertyId, resourceContext.getWhere(), ObjectBinding.AccessIntent.MUTATE) - .leftRemap(propertyBinding->{ + .modifyProperty(property->{ + val proposedNewValue = new JsonParserHelper(resourceContext, property.getSpecification()) + .parseAsMapWithSingleValue(Util.asStringUtf8(body)); - val iResponse = propertyBinding.modifyProperty(property->{ - - val proposedNewValue = new JsonParserHelper(resourceContext, property.getSpecification()) - .parseAsMapWithSingleValue(Util.asStringUtf8(body)); - - return proposedNewValue; - }); - - if(iResponse.isFailure()) { - _Either.right(iResponse); - } - - return _Either.left(propertyBinding); + return proposedNewValue; }) - .right() - .ifPresent(InteractionFailureHandler::onFailure); + .onFailure(InteractionFailureHandler::onFailure) ; val domainResourceHelper = DomainResourceHelper.ofObjectResource(resourceContext, objectAdapter); @@ -506,21 +494,8 @@ public class DomainObjectResourceServerside extends ResourceAbstract implements propertyId, resourceContext.getWhere(), ObjectBinding.AccessIntent.MUTATE) - .leftRemap(propertyBinding->{ - - val iResponse = propertyBinding.modifyProperty(property->{ - return null; - }); - - if(iResponse.isFailure()) { - _Either.right(iResponse); - } - - return _Either.left(propertyBinding); - }) - .right() - .ifPresent(InteractionFailureHandler::onFailure); - ; + .modifyProperty(property->null) + .onFailure(InteractionFailureHandler::onFailure); val domainResourceHelper = DomainResourceHelper.ofObjectResource(resourceContext, objectAdapter); return domainResourceHelper.propertyDetails( diff --git a/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/ObjectAdapterAccessHelper.java b/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/ObjectAdapterAccessHelper.java index cdfae79..5d7625e 100644 --- a/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/ObjectAdapterAccessHelper.java +++ b/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/ObjectAdapterAccessHelper.java @@ -51,12 +51,9 @@ public class ObjectAdapterAccessHelper { public OneToOneAssociation getPropertyThatIsVisibleForIntent( final String propertyId, final AccessIntent intent) { - val propertyBindingOrFailure = objectInteractor.getPropertyBinding(propertyId, where, intent); - - propertyBindingOrFailure.right() - .ifPresent(InteractionFailureHandler::onFailure); - - return propertyBindingOrFailure.leftIfAny().getProperty(); + return objectInteractor.getPropertyBinding(propertyId, where, intent) + .onFailure(InteractionFailureHandler::onFailure) + .getProperty(); } public OneToManyAssociation getCollectionThatIsVisibleForIntent(
