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.domainobjects;
018
019import java.util.Collection;
020
021import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
022import org.apache.isis.core.metamodel.spec.ObjectSpecification;
023import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
024import org.apache.isis.viewer.restfulobjects.applib.Rel;
025import org.apache.isis.viewer.restfulobjects.applib.RepresentationType;
026import org.apache.isis.viewer.restfulobjects.rendering.LinkFollowSpecs;
027import org.apache.isis.viewer.restfulobjects.rendering.RendererContext;
028import org.apache.isis.viewer.restfulobjects.rendering.ReprRendererAbstract;
029
030public class ListReprRenderer extends ReprRendererAbstract<ListReprRenderer, Collection<ObjectAdapter>> {
031
032    private ObjectAdapterLinkTo linkTo;
033    private Collection<ObjectAdapter> objectAdapters;
034    private ObjectSpecification elementType;
035    private ObjectSpecification returnType;
036    private Rel elementRel;
037
038    public ListReprRenderer(final RendererContext resourceContext, final LinkFollowSpecs linkFollower, final JsonRepresentation representation) {
039        super(resourceContext, linkFollower, RepresentationType.LIST, representation);
040        usingLinkToBuilder(new DomainObjectLinkTo());
041    }
042
043    public ListReprRenderer usingLinkToBuilder(final ObjectAdapterLinkTo objectAdapterLinkToBuilder) {
044        this.linkTo = objectAdapterLinkToBuilder.usingUrlBase(rendererContext);
045        return this;
046    }
047
048    @Override
049    public ListReprRenderer with(final Collection<ObjectAdapter> objectAdapters) {
050        this.objectAdapters = objectAdapters;
051        return this;
052    }
053
054    public ListReprRenderer withElementRel(Rel elementRel) {
055        this.elementRel = elementRel;
056        return this;
057    }
058
059    public ListReprRenderer withReturnType(final ObjectSpecification returnType) {
060        this.returnType = returnType;
061        return this;
062    }
063
064    public ListReprRenderer withElementType(final ObjectSpecification elementType) {
065        this.elementType = elementType;
066        return this;
067    }
068
069    @Override
070    public JsonRepresentation render() {
071        addValue();
072
073        addLinkToReturnType();
074        addLinkToElementType();
075
076        getExtensions();
077
078        return representation;
079    }
080
081    private void addValue() {
082        if (objectAdapters == null) {
083            return;
084        }
085
086        final JsonRepresentation values = JsonRepresentation.newArray();
087
088        for (final ObjectAdapter adapter : objectAdapters) {
089            final ObjectSpecification specification = adapter.getSpecification();
090            if (specification.isHidden()) {
091                continue;
092            }
093            final JsonRepresentation linkToObject = linkTo.with(adapter).builder(elementRel).build();
094            values.arrayAdd(linkToObject);
095
096            final LinkFollowSpecs linkFollower = getLinkFollowSpecs().follow("value");
097            if (linkFollower.matches(linkToObject)) {
098                final DomainObjectReprRenderer renderer = new DomainObjectReprRenderer(getRendererContext(), linkFollower, JsonRepresentation.newMap());
099                final JsonRepresentation domainObject = renderer.with(adapter).render();
100                linkToObject.mapPut("value", domainObject);
101            }
102        }
103        representation.mapPut("value", values);
104    }
105
106
107    protected void addLinkToReturnType() {
108        addLink(Rel.RETURN_TYPE, returnType);
109    }
110
111    protected void addLinkToElementType() {
112        addLink(Rel.ELEMENT_TYPE, elementType);
113    }
114
115}