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