1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.wsrp4j.util;
18  
19  import javax.portlet.PortletMode;
20  
21  public class Modes implements java.io.Serializable {
22      private java.lang.String _value_;
23  
24      private static java.util.HashMap _table_ = new java.util.HashMap();
25  
26      // Constructor
27      protected Modes(java.lang.String value) {
28          _value_ = value;
29          _table_.put(_value_, this);
30      }
31  
32      // define the modes we can currently handle
33      public static final java.lang.String _view = "wsrp:view";
34  
35      public static final java.lang.String _edit = "wsrp:edit";
36  
37      public static final java.lang.String _help = "wsrp:help";
38  
39      public static final java.lang.String _preview = "wsrp:preview";
40  
41      public static final Modes view = new Modes(_view);
42  
43      public static final Modes edit = new Modes(_edit);
44  
45      public static final Modes help = new Modes(_help);
46  
47      public static final Modes preview = new Modes(_preview);
48  
49      public java.lang.String getValue() {
50          return _value_;
51      }
52  
53      /**
54       * Returns the WSRP mode build from a string representation
55       * If a not supported Mode is requested, null is returned
56       * @param value <code>String</string> representation of the WSRP mode
57       * @return The WSRP <code>Mode</code> represented by the passed string
58       */
59      public static Modes fromValue(java.lang.String value) {
60          return (Modes) _table_.get(value);
61      }
62  
63      /**
64       * Returns the WSRP mode build from a string representation
65       * If a not supported Mode is requested, null is returned
66       * @param value <code>String</string> representation of the WSRP mode
67       * @return The WSRP <code>Mode</code> represented by the passed string
68       */
69      public static Modes fromString(java.lang.String value) {
70          return fromValue(value);
71      }
72  
73      public boolean equals(java.lang.Object obj) {
74          return (obj == this);
75      }
76  
77      public int hashCode() {
78          return toString().hashCode();
79      }
80  
81      public java.lang.String toString() {
82          return _value_;
83      }
84  
85      public java.lang.Object readResolve() throws java.io.ObjectStreamException {
86          return fromValue(_value_);
87      }
88  
89      /**
90       * This helper method maps portlet modes defined in wsrp to portlet modes
91       * defined in the java portlet standard (JSR-168). If the passed wsrp mode
92       * is null or can not be mapped the view mode is returned. 
93       *
94       * @return The <code>javax.portlet.PortletMode</code> which corresponds to the given wsrp mode.
95       **/
96      public static PortletMode getJsrPortletModeFromWsrpMode(Modes wsrpMode) {
97          if (wsrpMode == null) {
98              return PortletMode.VIEW;
99          }
100         else if (wsrpMode.equals(Modes.edit)) {
101             return PortletMode.EDIT;
102         }
103         else if (wsrpMode.equals(Modes.help)) {
104             return PortletMode.HELP;
105         }
106         else if (wsrpMode.equals(Modes.view)) {
107             return PortletMode.VIEW;
108         }
109 
110         return PortletMode.VIEW;
111     }
112 
113     /**
114      * This helper method maps portlet modes defined in tha java portlet standard (JSR-168)
115      * to modes defined in wsrp. If the passed portlet mode can not be resolved wsrp:view mode
116      * is returned.
117      *
118      * @param portletMode The <code>javax.portlet.PortletMode</code> which should be resolved as
119      *                     as portlet mode defined in wsrp.
120      * @return     
121      **/
122     public static Modes getWsrpModeFromJsrPortletMode(PortletMode portletMode) {
123         if (portletMode == null) {
124             throw new IllegalArgumentException("Portlet mode must not be null.");
125         }
126 
127         if (portletMode.equals(PortletMode.EDIT)) {
128             return Modes.edit;
129         }
130         else if (portletMode.equals(PortletMode.HELP)) {
131             return Modes.help;
132         }
133         else if (portletMode.equals(PortletMode.VIEW)) {
134             return Modes.view;
135         }
136 
137         return Modes.view;
138     }
139 }