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;
018
019
020import javax.ws.rs.core.MediaType;
021
022import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
023import org.apache.isis.viewer.restfulobjects.applib.RepresentationType;
024import org.apache.isis.viewer.restfulobjects.applib.RestfulHttpMethod;
025
026
027public final class LinkBuilder {
028
029    public static LinkBuilder newBuilder(final RendererContext resourceContext, final String rel, final RepresentationType representationType, final String hrefFormat, final Object... hrefArgs) {
030        return newBuilder(resourceContext, rel, representationType.getMediaType(), hrefFormat, hrefArgs);
031    }
032
033    public static LinkBuilder newBuilder(final RendererContext resourceContext, final String rel, final MediaType mediaType, final String hrefFormat, final Object... hrefArgs) {
034        return new LinkBuilder(resourceContext, rel, String.format(hrefFormat, hrefArgs), mediaType);
035    }
036
037    private final RendererContext resourceContext;
038    private final JsonRepresentation representation = JsonRepresentation.newMap();
039
040    private final String rel;
041    private final String href;
042    private final MediaType mediaType;
043
044    private RestfulHttpMethod method = RestfulHttpMethod.GET;
045    private String title;
046    private JsonRepresentation arguments;
047    private JsonRepresentation value;
048    private String id;
049
050    protected LinkBuilder(final RendererContext resourceContext, final String rel, final String href, final MediaType mediaType) {
051        this.resourceContext = resourceContext;
052        this.rel = rel;
053        this.href = href;
054        this.mediaType = mediaType;
055    }
056
057    public LinkBuilder withHttpMethod(final RestfulHttpMethod method) {
058        this.method = method;
059        return this;
060    }
061
062    public LinkBuilder withTitle(final String title) {
063        this.title = title;
064        return this;
065    }
066
067    public LinkBuilder withArguments(final JsonRepresentation arguments) {
068        this.arguments = arguments;
069        return this;
070    }
071
072    public LinkBuilder withValue(final JsonRepresentation value) {
073        this.value = value;
074        return this;
075    }
076
077    public JsonRepresentation build() {
078        representation.mapPut("rel", rel);
079        representation.mapPut("href", resourceContext.urlFor(href));
080        representation.mapPut("method", method);
081        representation.mapPut("type", mediaType.toString());
082        representation.mapPut("title", title);
083        representation.mapPut("arguments", arguments);
084        representation.mapPut("value", value);
085        return representation;
086    }
087
088}