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