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