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.applib;
020
021
022import java.util.Collections;
023import java.util.Map;
024
025import javax.ws.rs.core.MediaType;
026
027import org.apache.isis.applib.util.Enums;
028import org.apache.isis.viewer.restfulobjects.applib.domainobjects.ActionResultRepresentation;
029import org.apache.isis.viewer.restfulobjects.applib.domainobjects.DomainObjectRepresentation;
030import org.apache.isis.viewer.restfulobjects.applib.domainobjects.ListRepresentation;
031import org.apache.isis.viewer.restfulobjects.applib.domainobjects.ObjectActionRepresentation;
032import org.apache.isis.viewer.restfulobjects.applib.domainobjects.ObjectCollectionRepresentation;
033import org.apache.isis.viewer.restfulobjects.applib.domainobjects.ObjectPropertyRepresentation;
034import org.apache.isis.viewer.restfulobjects.applib.domaintypes.ActionDescriptionRepresentation;
035import org.apache.isis.viewer.restfulobjects.applib.domaintypes.ActionParameterDescriptionRepresentation;
036import org.apache.isis.viewer.restfulobjects.applib.domaintypes.CollectionDescriptionRepresentation;
037import org.apache.isis.viewer.restfulobjects.applib.domaintypes.DomainTypeRepresentation;
038import org.apache.isis.viewer.restfulobjects.applib.domaintypes.PropertyDescriptionRepresentation;
039import org.apache.isis.viewer.restfulobjects.applib.domaintypes.TypeActionResultRepresentation;
040import org.apache.isis.viewer.restfulobjects.applib.domaintypes.TypeListRepresentation;
041import org.apache.isis.viewer.restfulobjects.applib.errors.ErrorRepresentation;
042import org.apache.isis.viewer.restfulobjects.applib.homepage.HomePageRepresentation;
043import org.apache.isis.viewer.restfulobjects.applib.user.UserRepresentation;
044import org.apache.isis.viewer.restfulobjects.applib.util.Parser;
045import org.apache.isis.viewer.restfulobjects.applib.version.VersionRepresentation;
046
047import com.google.common.collect.Maps;
048
049public enum RepresentationType {
050
051    HOME_PAGE(RestfulMediaType.APPLICATION_JSON_HOME_PAGE, HomePageRepresentation.class), 
052    USER(RestfulMediaType.APPLICATION_JSON_USER, UserRepresentation.class), 
053    VERSION(RestfulMediaType.APPLICATION_JSON_VERSION, VersionRepresentation.class), 
054    LIST(RestfulMediaType.APPLICATION_JSON_LIST, ListRepresentation.class), 
055    DOMAIN_OBJECT(RestfulMediaType.APPLICATION_JSON_OBJECT, DomainObjectRepresentation.class), 
056    OBJECT_PROPERTY(RestfulMediaType.APPLICATION_JSON_OBJECT_PROPERTY, ObjectPropertyRepresentation.class), 
057    OBJECT_COLLECTION(RestfulMediaType.APPLICATION_JSON_OBJECT_COLLECTION, ObjectCollectionRepresentation.class), 
058    OBJECT_ACTION(RestfulMediaType.APPLICATION_JSON_OBJECT_ACTION, ObjectActionRepresentation.class), 
059    ACTION_RESULT(RestfulMediaType.APPLICATION_JSON_ACTION_RESULT, ActionResultRepresentation.class), 
060    TYPE_LIST(RestfulMediaType.APPLICATION_JSON_TYPE_LIST, TypeListRepresentation.class), 
061    DOMAIN_TYPE(RestfulMediaType.APPLICATION_JSON_DOMAIN_TYPE, DomainTypeRepresentation.class), 
062    PROPERTY_DESCRIPTION(RestfulMediaType.APPLICATION_JSON_PROPERTY_DESCRIPTION, PropertyDescriptionRepresentation.class), 
063    COLLECTION_DESCRIPTION(RestfulMediaType.APPLICATION_JSON_COLLECTION_DESCRIPTION, CollectionDescriptionRepresentation.class), 
064    ACTION_DESCRIPTION(RestfulMediaType.APPLICATION_JSON_ACTION_DESCRIPTION, ActionDescriptionRepresentation.class), 
065    ACTION_PARAMETER_DESCRIPTION(RestfulMediaType.APPLICATION_JSON_ACTION_PARAMETER_DESCRIPTION, ActionParameterDescriptionRepresentation.class), 
066    TYPE_ACTION_RESULT(RestfulMediaType.APPLICATION_JSON_TYPE_ACTION_RESULT, TypeActionResultRepresentation.class), 
067    ERROR(RestfulMediaType.APPLICATION_JSON_ERROR, ErrorRepresentation.class), 
068    GENERIC(MediaType.APPLICATION_JSON, JsonRepresentation.class);
069
070    private final String name;
071    private final MediaType mediaType;
072    private final Class<? extends JsonRepresentation> representationClass;
073
074    private RepresentationType(final String mediaTypeStr, final Class<? extends JsonRepresentation> representationClass) {
075        this(MediaType.valueOf(mediaTypeStr), representationClass);
076    }
077
078    private RepresentationType(final MediaType mediaType, final Class<? extends JsonRepresentation> representationClass) {
079        this.representationClass = representationClass;
080        this.name = Enums.enumToCamelCase(this);
081        this.mediaType = mediaType;
082    }
083
084    public String getName() {
085        return name;
086    }
087
088    public final MediaType getMediaType() {
089        return mediaType;
090    }
091
092    /**
093     * Clones the (immutable) {@link #getMediaType() media type}, adding in one additional
094     * parameter value.
095     */
096    public MediaType getMediaType(String parameter, String paramValue) {
097        return getMediaType(Collections.singletonMap(parameter, paramValue));
098    }
099
100    /**
101     * Clones the (immutable) {@link #getMediaType() media type}, adding all provided
102     * parameters.
103     */
104    public MediaType getMediaType(Map<String, String> mediaTypeParams) {
105        Map<String, String> parameters = Maps.newHashMap(mediaType.getParameters());
106        parameters.putAll(mediaTypeParams);
107        return new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters);
108    }
109
110    public String getMediaTypeProfile() {
111        return getMediaType().getParameters().get("profile");
112    }
113
114    public Class<? extends JsonRepresentation> getRepresentationClass() {
115        return representationClass;
116    }
117
118    public static RepresentationType lookup(final String name) {
119        for (final RepresentationType representationType : values()) {
120            if (representationType.getName().equals(name)) {
121                return representationType;
122            }
123        }
124        return RepresentationType.GENERIC;
125    }
126
127    public static RepresentationType lookup(final MediaType mediaType) {
128        if(mediaType != null) {
129            for (final RepresentationType representationType : values()) {
130                final MediaType candidate = representationType.getMediaType();
131                if(!candidate.getType().equals(mediaType.getType())) {
132                    continue;
133                }
134                if(!candidate.getSubtype().equals(mediaType.getSubtype())) {
135                    continue;
136                }
137                String candidateProfile = candidate.getParameters().get("profile");
138                String mediaTypeProfile = mediaType.getParameters().get("profile");
139                if(candidateProfile == null || candidateProfile.equals(mediaTypeProfile)) {
140                    return representationType;
141                }
142            }
143        }
144        return RepresentationType.GENERIC;
145    }
146
147    public static Parser<RepresentationType> parser() {
148        return new Parser<RepresentationType>() {
149            @Override
150            public RepresentationType valueOf(final String str) {
151                return RepresentationType.lookup(str);
152            }
153
154            @Override
155            public String asString(final RepresentationType t) {
156                return t.getName();
157            }
158        };
159    }
160
161
162
163}