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 */
019 package org.apache.isis.viewer.restfulobjects.applib;
020
021 import javax.ws.rs.core.MediaType;
022
023 import org.apache.isis.applib.util.Enums;
024 import org.apache.isis.viewer.restfulobjects.applib.domaintypes.ActionDescriptionRepresentation;
025 import org.apache.isis.viewer.restfulobjects.applib.domaintypes.ActionParameterDescriptionRepresentation;
026 import org.apache.isis.viewer.restfulobjects.applib.domaintypes.CollectionDescriptionRepresentation;
027 import org.apache.isis.viewer.restfulobjects.applib.domaintypes.DomainTypeRepresentation;
028 import org.apache.isis.viewer.restfulobjects.applib.domaintypes.PropertyDescriptionRepresentation;
029 import org.apache.isis.viewer.restfulobjects.applib.domaintypes.TypeActionResultRepresentation;
030 import org.apache.isis.viewer.restfulobjects.applib.domaintypes.TypeListRepresentation;
031 import org.apache.isis.viewer.restfulobjects.applib.errors.ErrorRepresentation;
032 import org.apache.isis.viewer.restfulobjects.applib.homepage.HomePageRepresentation;
033 import org.apache.isis.viewer.restfulobjects.applib.user.UserRepresentation;
034 import org.apache.isis.viewer.restfulobjects.applib.util.Parser;
035 import org.apache.isis.viewer.restfulobjects.applib.version.VersionRepresentation;
036 import org.apache.isis.viewer.restfulobjects.domainobjects.ActionResultRepresentation;
037 import org.apache.isis.viewer.restfulobjects.domainobjects.DomainObjectRepresentation;
038 import org.apache.isis.viewer.restfulobjects.domainobjects.ListRepresentation;
039 import org.apache.isis.viewer.restfulobjects.domainobjects.ObjectActionRepresentation;
040 import org.apache.isis.viewer.restfulobjects.domainobjects.ObjectCollectionRepresentation;
041 import org.apache.isis.viewer.restfulobjects.domainobjects.ObjectPropertyRepresentation;
042 import org.apache.isis.viewer.restfulobjects.domainobjects.ScalarValueRepresentation;
043 import org.apache.isis.viewer.restfulobjects.domainobjects.TransientDomainObjectRepresentation;
044
045 public enum RepresentationType {
046
047 HOME_PAGE(RestfulMediaType.APPLICATION_JSON_HOME_PAGE, HomePageRepresentation.class), USER(RestfulMediaType.APPLICATION_JSON_USER, UserRepresentation.class), VERSION(RestfulMediaType.APPLICATION_JSON_VERSION, VersionRepresentation.class), LIST(RestfulMediaType.APPLICATION_JSON_LIST,
048 ListRepresentation.class), SCALAR_VALUE(RestfulMediaType.APPLICATION_JSON_SCALAR_VALUE, ScalarValueRepresentation.class), DOMAIN_OBJECT(RestfulMediaType.APPLICATION_JSON_DOMAIN_OBJECT, DomainObjectRepresentation.class), TRANSIENT_DOMAIN_OBJECT(
049 RestfulMediaType.APPLICATION_JSON_TRANSIENT_DOMAIN_OBJECT, TransientDomainObjectRepresentation.class), OBJECT_PROPERTY(RestfulMediaType.APPLICATION_JSON_OBJECT_PROPERTY, ObjectPropertyRepresentation.class), OBJECT_COLLECTION(RestfulMediaType.APPLICATION_JSON_OBJECT_COLLECTION,
050 ObjectCollectionRepresentation.class), OBJECT_ACTION(RestfulMediaType.APPLICATION_JSON_OBJECT_ACTION, ObjectActionRepresentation.class), ACTION_RESULT(RestfulMediaType.APPLICATION_JSON_ACTION_RESULT, ActionResultRepresentation.class), TYPE_LIST(
051 RestfulMediaType.APPLICATION_JSON_TYPE_LIST, TypeListRepresentation.class), DOMAIN_TYPE(RestfulMediaType.APPLICATION_JSON_DOMAIN_TYPE, DomainTypeRepresentation.class), PROPERTY_DESCRIPTION(RestfulMediaType.APPLICATION_JSON_PROPERTY_DESCRIPTION, PropertyDescriptionRepresentation.class), COLLECTION_DESCRIPTION(
052 RestfulMediaType.APPLICATION_JSON_COLLECTION_DESCRIPTION, CollectionDescriptionRepresentation.class), ACTION_DESCRIPTION(RestfulMediaType.APPLICATION_JSON_ACTION_DESCRIPTION, ActionDescriptionRepresentation.class), ACTION_PARAMETER_DESCRIPTION(
053 RestfulMediaType.APPLICATION_JSON_ACTION_PARAMETER_DESCRIPTION, ActionParameterDescriptionRepresentation.class), TYPE_ACTION_RESULT(RestfulMediaType.APPLICATION_JSON_TYPE_ACTION_RESULT, TypeActionResultRepresentation.class), ERROR(RestfulMediaType.APPLICATION_JSON_ERROR,
054 ErrorRepresentation.class), GENERIC(MediaType.APPLICATION_JSON, JsonRepresentation.class);
055
056 private final String name;
057 private final MediaType mediaType;
058 private final Class<? extends JsonRepresentation> representationClass;
059
060 private RepresentationType(final String mediaTypeStr, final Class<? extends JsonRepresentation> representationClass) {
061 this.representationClass = representationClass;
062 this.name = Enums.enumToCamelCase(this);
063 this.mediaType = MediaType.valueOf(mediaTypeStr);
064 }
065
066 public String getName() {
067 return name;
068 }
069
070 public final MediaType getMediaType() {
071 return mediaType;
072 }
073
074 public String getMediaTypeWithProfile() {
075 return getMediaType().getParameters().get("profile");
076 }
077
078 public Class<? extends JsonRepresentation> getRepresentationClass() {
079 return representationClass;
080 }
081
082 public static RepresentationType lookup(final String name) {
083 for (final RepresentationType representationType : values()) {
084 if (representationType.getName().equals(name)) {
085 return representationType;
086 }
087 }
088 return RepresentationType.GENERIC;
089 }
090
091 public static RepresentationType lookup(final MediaType mediaType) {
092 for (final RepresentationType representationType : values()) {
093 if (representationType.getMediaType().equals(mediaType)) {
094 return representationType;
095 }
096 }
097 return RepresentationType.GENERIC;
098 }
099
100 public static Parser<RepresentationType> parser() {
101 return new Parser<RepresentationType>() {
102 @Override
103 public RepresentationType valueOf(final String str) {
104 return RepresentationType.lookup(str);
105 }
106
107 @Override
108 public String asString(final RepresentationType t) {
109 return t.getName();
110 }
111 };
112 }
113
114 }