001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019package org.apache.isis.viewer.restfulobjects.rendering.domainobjects;
020
021import java.util.List;
022
023import org.apache.isis.core.metamodel.facets.collections.CollectionFacet;
024import org.apache.isis.core.metamodel.facets.object.encodeable.EncodableFacet;
025import org.apache.isis.core.metamodel.spec.ManagedObject;
026import org.apache.isis.core.metamodel.spec.ObjectSpecification;
027import org.apache.isis.core.metamodel.spec.feature.ObjectAction;
028import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
029import org.apache.isis.viewer.restfulobjects.applib.domainobjects.ActionResultRepresentation;
030
031public class ObjectAndActionInvocation {
032
033    private final ManagedObject objectAdapter;
034    private final ObjectAction action;
035    private final JsonRepresentation arguments;
036    private final List<ManagedObject> argAdapters;
037    private final ManagedObject returnedAdapter;
038    private final ActionResultReprRenderer.SelfLink selfLink;
039
040    public ObjectAndActionInvocation(
041            final ManagedObject objectAdapter,
042            final ObjectAction action,
043            final JsonRepresentation arguments,
044            final List<ManagedObject> argAdapters,
045            final ManagedObject returnedAdapter,
046            final ActionResultReprRenderer.SelfLink selfLink) {
047        
048        this.objectAdapter = objectAdapter;
049        this.action = action;
050        this.arguments = arguments;
051        this.argAdapters = argAdapters;
052        this.returnedAdapter = returnedAdapter;
053        this.selfLink = selfLink;
054    }
055
056    public ManagedObject getObjectAdapter() {
057        return objectAdapter;
058    }
059
060    public ObjectAction getAction() {
061        return action;
062    }
063
064    public JsonRepresentation getArguments() {
065        return arguments;
066    }
067
068    public List<ManagedObject> getArgAdapters() {
069        return argAdapters;
070    }
071
072    public ManagedObject getReturnedAdapter() {
073        return returnedAdapter;
074    }
075
076    public ActionResultReprRenderer.SelfLink getSelfLink() {
077        return selfLink;
078    }
079
080
081    /**
082     * not API
083     */
084    public ActionResultRepresentation.ResultType determineResultType() {
085
086        final ObjectSpecification returnType = this.action.getReturnType();
087
088        if (returnType.getCorrespondingClass() == void.class) {
089            return ActionResultRepresentation.ResultType.VOID;
090        }
091
092        final CollectionFacet collectionFacet = returnType.getFacet(CollectionFacet.class);
093        if (collectionFacet != null) {
094            return ActionResultRepresentation.ResultType.LIST;
095        }
096
097        final EncodableFacet encodableFacet = returnType.getFacet(EncodableFacet.class);
098        if (encodableFacet != null) {
099            return ActionResultRepresentation.ResultType.SCALAR_VALUE;
100        }
101
102        // else
103        return ActionResultRepresentation.ResultType.DOMAIN_OBJECT;
104    }
105
106}