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
021public enum Rel {
022
023    SELF(RelDefinition.IANA, "self"), 
024    DESCRIBEDBY(RelDefinition.IANA, "describedby"), 
025    UP(RelDefinition.IANA,"up"), 
026    PREVIOUS(RelDefinition.IANA,"previous"),
027    NEXT(RelDefinition.IANA,"next"),
028    HELP(RelDefinition.IANA,"help"), 
029    ICON(RelDefinition.IANA,"icon"),
030
031    // Restful Objects namespace
032    ACTION(RelDefinition.RO_SPEC, "action"), 
033    ACTION_PARAM(RelDefinition.RO_SPEC, "action-param"), 
034    ADD_TO(RelDefinition.RO_SPEC, "add-to"), 
035    ATTACHMENT(RelDefinition.RO_SPEC, "attachment"), 
036    CHOICE(RelDefinition.RO_SPEC, "choice"),
037    CLEAR(RelDefinition.RO_SPEC, "clear"), 
038    COLLECTION(RelDefinition.RO_SPEC, "collection"), 
039    DEFAULT(RelDefinition.RO_SPEC, "default"), 
040    DELETE(RelDefinition.RO_SPEC, "delete"), 
041    DETAILS(RelDefinition.RO_SPEC, "details"), 
042    DOMAIN_TYPE(RelDefinition.RO_SPEC, "domain-type"),
043    DOMAIN_TYPES(RelDefinition.RO_SPEC, "domain-types"), 
044    ELEMENT(RelDefinition.RO_SPEC, "element"), 
045    ELEMENT_TYPE(RelDefinition.RO_SPEC, "element-type"), 
046    INVOKE(RelDefinition.RO_SPEC, "invoke"), 
047    MODIFY(RelDefinition.RO_SPEC, "modify"), 
048    PERSIST(RelDefinition.RO_SPEC, "persist"), 
049    PROPERTY(RelDefinition.RO_SPEC, "property"), 
050    REMOVE_FROM(RelDefinition.RO_SPEC, "remove-from"), 
051    RETURN_TYPE(RelDefinition.RO_SPEC, "return-type"), 
052    SERVICE(RelDefinition.RO_SPEC, "service"), 
053    SERVICES(RelDefinition.RO_SPEC, "services"), 
054    UPDATE(RelDefinition.RO_SPEC, "update"), 
055    USER(RelDefinition.RO_SPEC, "user"), 
056    VALUE(RelDefinition.RO_SPEC, "value"), 
057    VERSION(RelDefinition.RO_SPEC, "version"), 
058    
059
060    // implementation specific
061    CONTRIBUTED_BY(RelDefinition.IMPL, "contributed-by");
062
063    private final RelDefinition relDef;
064    private final String relSuffix;
065
066    private Rel(final RelDefinition relDef, final String name) {
067        this.relDef = relDef;
068        this.relSuffix = name;
069    }
070
071    public String getName() {
072        return relDef.nameOf(relSuffix);
073    }
074
075    /**
076     * For those {@link Rel}s that also take a param
077     */
078    public String andParam(String paramName, String paramValue) {
079        return getName() +
080                (relDef.canAddParams() 
081                 ?";" + paramName + "=" + "\"" + paramValue + "\""
082                 :"");
083    }
084
085    public boolean matches(Rel otherRel) {
086        return this == otherRel;
087    }
088
089    public boolean matches(final String otherRelStr) {
090        final Rel otherRel = Rel.parse(otherRelStr);
091        return matches(otherRel);
092    }
093
094    public static Rel parse(String str) {
095        final int i = str.indexOf(";");
096        if(i != -1) {
097            str = str.substring(0, i);
098        }
099        for (Rel candidate: Rel.values()) {
100            if(candidate.getName().equals(str)) {
101                return candidate;
102            }
103        }
104        return null;
105    }
106}