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