1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.wsrp.util;
24  
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portal.util.WebKeys;
27  import com.liferay.portal.wsrp.util.comparator.PortletDescriptionComparator;
28  
29  import java.util.Comparator;
30  
31  import javax.portlet.PortletMode;
32  import javax.portlet.WindowState;
33  
34  import javax.servlet.ServletContext;
35  import javax.servlet.http.HttpServletRequest;
36  
37  import org.apache.wsrp4j.producer.util.ServletAccess;
38  import org.apache.wsrp4j.util.Modes;
39  import org.apache.wsrp4j.util.WindowStates;
40  
41  /**
42   * <a href="WSRPUtil.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Michael Young
45   *
46   */
47  public class WSRPUtil {
48  
49      public static long getCompanyId() {
50          HttpServletRequest req = WSRPUtil.getHttpServletRequest();
51  
52          long companyId = PortalUtil.getCompanyId(req);
53  
54          return companyId;
55      }
56  
57      public static HttpServletRequest getHttpServletRequest() {
58          return ServletAccess.getServletRequest();
59      }
60  
61      public static ServletContext getServletContext() {
62          return (ServletContext)getHttpServletRequest().getAttribute(WebKeys.CTX);
63      }
64  
65      public static PortletMode fromWsrpMode(String wsrpMode) {
66          PortletMode mode = PortletMode.VIEW;
67  
68          if (wsrpMode.equals(Modes._edit)) {
69              mode = PortletMode.EDIT;
70          }
71          else if (wsrpMode.equals(Modes._view)) {
72              mode = PortletMode.VIEW;
73          }
74          else if (wsrpMode.equals(Modes._help)) {
75              mode = PortletMode.HELP;
76          }
77  
78          return mode;
79      }
80  
81      public static WindowState fromWsrpWindowState(String wsrpWindowState) {
82          WindowState windowState = WindowState.NORMAL;
83  
84          if (wsrpWindowState.equals(WindowStates._maximized)) {
85              windowState = WindowState.MAXIMIZED;
86          }
87          else if (wsrpWindowState.equals(WindowStates._minimized)) {
88              windowState = WindowState.MINIMIZED;
89          }
90          else if (wsrpWindowState.equals(WindowStates._normal)) {
91              windowState = WindowState.NORMAL;
92          }
93  
94          return windowState;
95      }
96  
97      public static String toWsrpMode(String mode) {
98          String wsrpMode = Modes._view;
99  
100         if (mode.equals(PortletMode.EDIT.toString())) {
101             wsrpMode = Modes._edit;
102         }
103         else if (mode.equals(PortletMode.VIEW.toString())) {
104             wsrpMode = Modes._view;
105         }
106         else if (mode.equals(PortletMode.HELP.toString())) {
107             wsrpMode = Modes._help;
108         }
109 
110         return wsrpMode;
111     }
112 
113     public static String toWsrpWindowState(String windowState) {
114         String wsrpWindowState = WindowState.NORMAL.toString();
115 
116         if (windowState.equals(WindowState.MAXIMIZED.toString())) {
117             wsrpWindowState = WindowStates._maximized;
118         }
119         else if (windowState.equals(WindowState.MINIMIZED.toString())) {
120             wsrpWindowState = WindowStates._minimized;
121         }
122         else if (windowState.equals(WindowState.NORMAL.toString())) {
123             windowState = WindowStates._normal;
124         }
125 
126         return wsrpWindowState;
127     }
128 
129     public static Comparator getPortletDescriptionComparator() {
130         return _portletDescriptionComparator;
131     }
132 
133     private static final Comparator _portletDescriptionComparator =
134         new PortletDescriptionComparator();
135 
136 }