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
049    protected LinkBuilder(final RendererContext resourceContext, final String rel, final String href, final MediaType mediaType) {
050        this.resourceContext = resourceContext;
051        this.rel = rel;
052        this.href = href;
053        this.mediaType = mediaType;
054    }
055
056    public LinkBuilder withHttpMethod(final RestfulHttpMethod method) {
057        this.method = method;
058        return this;
059    }
060
061    public LinkBuilder withTitle(final String title) {
062        this.title = title;
063        return this;
064    }
065
066    public LinkBuilder withArguments(final JsonRepresentation arguments) {
067        this.arguments = arguments;
068        return this;
069    }
070
071    public LinkBuilder withValue(final JsonRepresentation value) {
072        this.value = value;
073        return this;
074    }
075
076    public JsonRepresentation build() {
077        representation.mapPut("rel", rel);
078        representation.mapPut("href", resourceContext.urlFor(href));
079        representation.mapPut("method", method);
080        representation.mapPut("type", mediaType.toString());
081        representation.mapPut("title", title);
082        representation.mapPut("arguments", arguments);
083        representation.mapPut("value", value);
084        return representation;
085    }
086
087}