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.facets.maxlen.MaxLengthFacet;
020import org.apache.isis.core.metamodel.spec.ObjectSpecification;
021import org.apache.isis.core.metamodel.spec.feature.OneToOneAssociation;
022import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
023import org.apache.isis.viewer.restfulobjects.applib.Rel;
024import org.apache.isis.viewer.restfulobjects.applib.RepresentationType;
025import org.apache.isis.viewer.restfulobjects.rendering.LinkBuilder;
026import org.apache.isis.viewer.restfulobjects.rendering.LinkFollowSpecs;
027import org.apache.isis.viewer.restfulobjects.rendering.RendererContext;
028
029public class PropertyDescriptionReprRenderer extends AbstractTypeMemberReprRenderer<PropertyDescriptionReprRenderer, OneToOneAssociation> {
030
031    public static LinkBuilder newLinkToBuilder(final RendererContext resourceContext, final Rel rel, final ObjectSpecification objectSpecification, final OneToOneAssociation property) {
032        final String domainType = objectSpecification.getSpecId().asString();
033        final String propertyId = property.getId();
034        final String url = "domain-types/" + domainType + "/properties/" + propertyId;
035        return LinkBuilder.newBuilder(resourceContext, rel.getName(), RepresentationType.PROPERTY_DESCRIPTION, url);
036    }
037
038    public PropertyDescriptionReprRenderer(final RendererContext resourceContext, final LinkFollowSpecs linkFollower, final JsonRepresentation representation) {
039        super(resourceContext, linkFollower, RepresentationType.PROPERTY_DESCRIPTION, representation);
040    }
041
042    @Override
043    protected void addLinksSpecificToFeature() {
044        addLinkToReturnTypeIfAny();
045    }
046
047    @Override
048    protected void addPropertiesSpecificToFeature() {
049        representation.mapPut("optional", !getObjectFeature().isMandatory());
050        final MaxLengthFacet maxLength = getObjectFeature().getFacet(MaxLengthFacet.class);
051        if (maxLength != null && !maxLength.isNoop()) {
052            representation.mapPut("maxLength", maxLength.value());
053        }
054    }
055
056    private void addLinkToReturnTypeIfAny() {
057        final ObjectSpecification returnType = getObjectFeature().getSpecification();
058        if (returnType == null) {
059            return;
060        }
061        final LinkBuilder linkBuilder = DomainTypeReprRenderer.newLinkToBuilder(getRendererContext(), Rel.RETURN_TYPE, returnType);
062        getLinks().arrayAdd(linkBuilder.build());
063    }
064
065    @Override
066    protected void putExtensionsSpecificToFeature() {
067        putExtensionsName();
068        putExtensionsDescriptionIfAvailable();
069    }
070
071}