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