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.domainobjects; 018 019import javax.ws.rs.core.MediaType; 020 021import org.apache.isis.core.metamodel.adapter.ObjectAdapter; 022import org.apache.isis.core.metamodel.facets.object.encodeable.EncodableFacet; 023import org.apache.isis.core.metamodel.spec.ObjectSpecification; 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.LinkFollowSpecs; 028import org.apache.isis.viewer.restfulobjects.rendering.RendererContext; 029import org.apache.isis.viewer.restfulobjects.rendering.ReprRendererAbstract; 030import org.apache.isis.viewer.restfulobjects.rendering.ReprRendererException; 031 032public class ScalarValueReprRenderer extends ReprRendererAbstract<ScalarValueReprRenderer, ObjectAdapter> { 033 034 private ObjectSpecification returnType; 035 036 ScalarValueReprRenderer(final RendererContext resourceContext, final LinkFollowSpecs linkFollower, final JsonRepresentation representation) { 037 super(resourceContext, linkFollower, null, representation); // null for representationType (there is none) 038 } 039 040 /** 041 * Fail early... 042 * 043 * <p> 044 * In case I forget in the future that scalar values don't have a representation. 045 */ 046 @Override 047 public MediaType getMediaType() { 048 throw new UnsupportedOperationException("no mediaType defined for scalar values"); 049 } 050 051 @Override 052 public ScalarValueReprRenderer with(final ObjectAdapter objectAdapter) { 053 final EncodableFacet facet = objectAdapter.getSpecification().getFacet(EncodableFacet.class); 054 if (facet == null) { 055 throw ReprRendererException.create("Not an (encodable) value", objectAdapter.titleString()); 056 } 057 final Object value = JsonValueEncoder.asObject(objectAdapter); 058 059 representation.mapPut("value", value); 060 return this; 061 } 062 063 @Override 064 public JsonRepresentation render() { 065 066 addLinkToReturnType(); 067 068 getExtensions(); 069 070 return representation; 071 } 072 073 public ScalarValueReprRenderer withReturnType(final ObjectSpecification returnType) { 074 this.returnType = returnType; 075 return this; 076 } 077 078 private void addLinkToReturnType() { 079 addLink(Rel.RETURN_TYPE, returnType); 080 } 081 082}