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.commons.internal.base._Strings;
022import org.apache.isis.core.metamodel.spec.ObjectSpecification;
023import org.apache.isis.core.metamodel.spec.feature.ObjectFeature;
024import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
025import org.apache.isis.viewer.restfulobjects.applib.RepresentationType;
026import org.apache.isis.viewer.restfulobjects.rendering.IResourceContext;
027import org.apache.isis.viewer.restfulobjects.rendering.LinkFollowSpecs;
028import org.apache.isis.viewer.restfulobjects.rendering.ReprRendererAbstract;
029
030public abstract class AbstractTypeFeatureReprRenderer<R extends ReprRendererAbstract<R, ParentSpecAndFeature<T>>, T extends ObjectFeature> extends ReprRendererAbstract<R, ParentSpecAndFeature<T>> {
031
032    protected ObjectSpecification objectSpecification;
033    protected T objectFeature;
034
035    public AbstractTypeFeatureReprRenderer(final IResourceContext resourceContext, final LinkFollowSpecs linkFollower, final RepresentationType representationType, final JsonRepresentation representation) {
036        super(resourceContext, linkFollower, representationType, representation);
037    }
038
039    public ObjectSpecification getParentSpecification() {
040        return objectSpecification;
041    }
042
043    public T getObjectFeature() {
044        return objectFeature;
045    }
046
047    @Override
048    public R with(final ParentSpecAndFeature<T> specAndFeature) {
049        objectSpecification = specAndFeature.getParentSpec();
050        objectFeature = specAndFeature.getObjectFeature();
051
052        return cast(this);
053    }
054
055    @Override
056    public JsonRepresentation render() {
057
058        addLinkSelfIfRequired();
059        addLinkUpToParent();
060
061        addPropertiesSpecificToFeature();
062
063        addLinksSpecificToFeature();
064        putExtensionsSpecificToFeature();
065
066        return representation;
067    }
068
069    /**
070     * Optional hook method.
071     */
072    protected void addPropertiesSpecificToFeature() {
073    }
074
075    /**
076     * Mandatory hook method.
077     */
078    protected abstract void addLinkSelfIfRequired();
079
080    /**
081     * Mandatory hook method.
082     */
083    protected abstract void addLinkUpToParent();
084
085    /**
086     * Optional hook method.
087     */
088    protected void addLinksSpecificToFeature() {
089    }
090
091    /**
092     * Mandatory hook method.
093     */
094    protected abstract void putExtensionsSpecificToFeature();
095
096    protected void putExtensionsName() {
097        final String friendlyName = getObjectFeature().getName();
098        getExtensions().mapPut("friendlyName", friendlyName);
099    }
100
101    protected void putExtensionsDescriptionIfAvailable() {
102        final String description = getObjectFeature().getDescription();
103        if (!_Strings.isNullOrEmpty(description)) {
104            getExtensions().mapPut("description", description);
105        }
106    }
107
108}