1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.wsrp.util;
21  
22  import com.liferay.portal.util.PortalUtil;
23  import com.liferay.portal.util.WebKeys;
24  import com.liferay.portal.wsrp.util.comparator.PortletDescriptionComparator;
25  
26  import java.util.Comparator;
27  
28  import javax.portlet.PortletMode;
29  import javax.portlet.WindowState;
30  
31  import javax.servlet.ServletContext;
32  import javax.servlet.http.HttpServletRequest;
33  
34  import org.apache.wsrp4j.producer.util.ServletAccess;
35  import org.apache.wsrp4j.util.Modes;
36  import org.apache.wsrp4j.util.WindowStates;
37  
38  /**
39   * <a href="WSRPUtil.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Michael Young
42   *
43   */
44  public class WSRPUtil {
45  
46      public static long getCompanyId() {
47          HttpServletRequest req = WSRPUtil.getHttpServletRequest();
48  
49          long companyId = PortalUtil.getCompanyId(req);
50  
51          return companyId;
52      }
53  
54      public static HttpServletRequest getHttpServletRequest() {
55          return ServletAccess.getServletRequest();
56      }
57  
58      public static ServletContext getServletContext() {
59          return (ServletContext)getHttpServletRequest().getAttribute(WebKeys.CTX);
60      }
61  
62      public static PortletMode fromWsrpMode(String wsrpMode) {
63          PortletMode mode = PortletMode.VIEW;
64  
65          if (wsrpMode.equals(Modes._edit)) {
66              mode = PortletMode.EDIT;
67          }
68          else if (wsrpMode.equals(Modes._view)) {
69              mode = PortletMode.VIEW;
70          }
71          else if (wsrpMode.equals(Modes._help)) {
72              mode = PortletMode.HELP;
73          }
74  
75          return mode;
76      }
77  
78      public static WindowState fromWsrpWindowState(String wsrpWindowState) {
79          WindowState windowState = WindowState.NORMAL;
80  
81          if (wsrpWindowState.equals(WindowStates._maximized)) {
82              windowState = WindowState.MAXIMIZED;
83          }
84          else if (wsrpWindowState.equals(WindowStates._minimized)) {
85              windowState = WindowState.MINIMIZED;
86          }
87          else if (wsrpWindowState.equals(WindowStates._normal)) {
88              windowState = WindowState.NORMAL;
89          }
90  
91          return windowState;
92      }
93  
94      public static String toWsrpMode(String mode) {
95          String wsrpMode = Modes._view;
96  
97          if (mode.equals(PortletMode.EDIT.toString())) {
98              wsrpMode = Modes._edit;
99          }
100         else if (mode.equals(PortletMode.VIEW.toString())) {
101             wsrpMode = Modes._view;
102         }
103         else if (mode.equals(PortletMode.HELP.toString())) {
104             wsrpMode = Modes._help;
105         }
106 
107         return wsrpMode;
108     }
109 
110     public static String toWsrpWindowState(String windowState) {
111         String wsrpWindowState = WindowState.NORMAL.toString();
112 
113         if (windowState.equals(WindowState.MAXIMIZED.toString())) {
114             wsrpWindowState = WindowStates._maximized;
115         }
116         else if (windowState.equals(WindowState.MINIMIZED.toString())) {
117             wsrpWindowState = WindowStates._minimized;
118         }
119         else if (windowState.equals(WindowState.NORMAL.toString())) {
120             windowState = WindowStates._normal;
121         }
122 
123         return wsrpWindowState;
124     }
125 
126     public static Comparator getPortletDescriptionComparator() {
127         return _portletDescriptionComparator;
128     }
129 
130     private static final Comparator _portletDescriptionComparator =
131         new PortletDescriptionComparator();
132 
133 }