001/**
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.isis.viewer.restfulobjects.rendering.domaintypes;
018
019import org.apache.isis.core.metamodel.spec.ObjectSpecification;
020import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
021import org.apache.isis.viewer.restfulobjects.applib.LinkRepresentation;
022import org.apache.isis.viewer.restfulobjects.applib.Rel;
023import org.apache.isis.viewer.restfulobjects.applib.RepresentationType;
024import org.apache.isis.viewer.restfulobjects.rendering.LinkFollowSpecs;
025import org.apache.isis.viewer.restfulobjects.rendering.RendererContext;
026import org.apache.isis.viewer.restfulobjects.rendering.ReprRendererAbstract;
027
028public class TypeActionResultReprRenderer extends ReprRendererAbstract<TypeActionResultReprRenderer, ObjectSpecification> {
029
030    private ObjectSpecification objectSpecification;
031    private LinkRepresentation selfLink;
032    private Object value;
033
034    public TypeActionResultReprRenderer(final RendererContext resourceContext, final LinkFollowSpecs linkFollower, final JsonRepresentation representation) {
035        super(resourceContext, linkFollower, RepresentationType.TYPE_ACTION_RESULT, representation);
036    }
037
038    @Override
039    public TypeActionResultReprRenderer with(final ObjectSpecification objectSpecification) {
040        this.objectSpecification = objectSpecification;
041        return cast(this);
042    }
043
044    public TypeActionResultReprRenderer withValue(final Object value) {
045        this.value = value;
046        return this;
047    }
048
049    public TypeActionResultReprRenderer withSelf(final JsonRepresentation link) {
050        return withLink(Rel.SELF, link);
051    }
052
053    @Override
054    public JsonRepresentation render() {
055        if (includesSelf && selfLink != null) {
056            getLinks().arrayAdd(selfLink);
057        }
058
059        if (value != null) {
060            representation.mapPut("value", value);
061        }
062        getExtensions();
063
064        return representation;
065    }
066
067    protected void putExtensionsIfService() {
068        getExtensions().mapPut("isService", objectSpecification.isService());
069    }
070
071}