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.core.metamodel.spec.feature.OneToManyAssociation;
021import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
022import org.apache.isis.viewer.restfulobjects.applib.Rel;
023import org.apache.isis.viewer.restfulobjects.applib.RepresentationType;
024import org.apache.isis.viewer.restfulobjects.rendering.LinkBuilder;
025import org.apache.isis.viewer.restfulobjects.rendering.LinkFollowSpecs;
026import org.apache.isis.viewer.restfulobjects.rendering.RendererContext;
027
028public class CollectionDescriptionReprRenderer extends AbstractTypeMemberReprRenderer<CollectionDescriptionReprRenderer, OneToManyAssociation> {
029
030    public static LinkBuilder newLinkToBuilder(final RendererContext resourceContext, final Rel rel, final ObjectSpecification objectSpecification, final OneToManyAssociation collection) {
031        final String domainType = objectSpecification.getSpecId().asString();
032        final String collectionId = collection.getId();
033        final String url = "domain-types/" + domainType + "/collections/" + collectionId;
034        return LinkBuilder.newBuilder(resourceContext, rel.getName(), RepresentationType.COLLECTION_DESCRIPTION, url);
035    }
036
037    public CollectionDescriptionReprRenderer(final RendererContext resourceContext, final LinkFollowSpecs linkFollower, final JsonRepresentation representation) {
038        super(resourceContext, linkFollower, RepresentationType.COLLECTION_DESCRIPTION, representation);
039    }
040
041    @Override
042    protected void addLinksSpecificToFeature() {
043        addLinkToElementTypeIfAny();
044    }
045
046    private void addLinkToElementTypeIfAny() {
047        final ObjectSpecification elementType = getObjectFeature().getSpecification();
048        if (elementType == null) {
049            return;
050        }
051        final LinkBuilder linkBuilder = DomainTypeReprRenderer.newLinkToBuilder(getRendererContext(), Rel.ELEMENT_TYPE, elementType);
052        getLinks().arrayAdd(linkBuilder.build());
053    }
054
055    @Override
056    protected void putExtensionsSpecificToFeature() {
057        putExtensionsName();
058        putExtensionsDescriptionIfAvailable();
059    }
060
061}