001: /*
002: * Copyright 2000-2001,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.wsrp4j.util;
018:
019: import javax.portlet.PortletMode;
020:
021: public class Modes implements java.io.Serializable {
022: private java.lang.String _value_;
023:
024: private static java.util.HashMap _table_ = new java.util.HashMap();
025:
026: // Constructor
027: protected Modes(java.lang.String value) {
028: _value_ = value;
029: _table_.put(_value_, this );
030: }
031:
032: // define the modes we can currently handle
033: public static final java.lang.String _view = "wsrp:view";
034:
035: public static final java.lang.String _edit = "wsrp:edit";
036:
037: public static final java.lang.String _help = "wsrp:help";
038:
039: public static final java.lang.String _preview = "wsrp:preview";
040:
041: public static final Modes view = new Modes(_view);
042:
043: public static final Modes edit = new Modes(_edit);
044:
045: public static final Modes help = new Modes(_help);
046:
047: public static final Modes preview = new Modes(_preview);
048:
049: public java.lang.String getValue() {
050: return _value_;
051: }
052:
053: /**
054: * Returns the WSRP mode build from a string representation
055: * If a not supported Mode is requested, null is returned
056: * @param value <code>String</string> representation of the WSRP mode
057: * @return The WSRP <code>Mode</code> represented by the passed string
058: */
059: public static Modes fromValue(java.lang.String value) {
060: return (Modes) _table_.get(value);
061: }
062:
063: /**
064: * Returns the WSRP mode build from a string representation
065: * If a not supported Mode is requested, null is returned
066: * @param value <code>String</string> representation of the WSRP mode
067: * @return The WSRP <code>Mode</code> represented by the passed string
068: */
069: public static Modes fromString(java.lang.String value) {
070: return fromValue(value);
071: }
072:
073: public boolean equals(java.lang.Object obj) {
074: return (obj == this );
075: }
076:
077: public int hashCode() {
078: return toString().hashCode();
079: }
080:
081: public java.lang.String toString() {
082: return _value_;
083: }
084:
085: public java.lang.Object readResolve()
086: throws java.io.ObjectStreamException {
087: return fromValue(_value_);
088: }
089:
090: /**
091: * This helper method maps portlet modes defined in wsrp to portlet modes
092: * defined in the java portlet standard (JSR-168). If the passed wsrp mode
093: * is null or can not be mapped the view mode is returned.
094: *
095: * @return The <code>javax.portlet.PortletMode</code> which corresponds to the given wsrp mode.
096: **/
097: public static PortletMode getJsrPortletModeFromWsrpMode(
098: Modes wsrpMode) {
099: if (wsrpMode == null) {
100: return PortletMode.VIEW;
101: } else if (wsrpMode.equals(Modes.edit)) {
102: return PortletMode.EDIT;
103: } else if (wsrpMode.equals(Modes.help)) {
104: return PortletMode.HELP;
105: } else if (wsrpMode.equals(Modes.view)) {
106: return PortletMode.VIEW;
107: }
108:
109: return PortletMode.VIEW;
110: }
111:
112: /**
113: * This helper method maps portlet modes defined in tha java portlet standard (JSR-168)
114: * to modes defined in wsrp. If the passed portlet mode can not be resolved wsrp:view mode
115: * is returned.
116: *
117: * @param portletMode The <code>javax.portlet.PortletMode</code> which should be resolved as
118: * as portlet mode defined in wsrp.
119: * @return
120: **/
121: public static Modes getWsrpModeFromJsrPortletMode(
122: PortletMode portletMode) {
123: if (portletMode == null) {
124: throw new IllegalArgumentException(
125: "Portlet mode must not be null.");
126: }
127:
128: if (portletMode.equals(PortletMode.EDIT)) {
129: return Modes.edit;
130: } else if (portletMode.equals(PortletMode.HELP)) {
131: return Modes.help;
132: } else if (portletMode.equals(PortletMode.VIEW)) {
133: return Modes.view;
134: }
135:
136: return Modes.view;
137: }
138: }
|