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