1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.editor.fckeditor.command;
24  
25  import com.liferay.portal.NoSuchGroupException;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.service.GroupLocalServiceUtil;
31  import com.liferay.portal.service.LayoutLocalServiceUtil;
32  import com.liferay.portal.theme.ThemeDisplay;
33  
34  import java.util.StringTokenizer;
35  
36  import javax.servlet.http.HttpServletRequest;
37  
38  /**
39   * <a href="CommandArgument.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Ivica Cardic
42   * @author Brian Wing Shun Chan
43   */
44  public class CommandArgument {
45  
46      public CommandArgument(
47          String command, String type, String currentFolder, String newFolder,
48          ThemeDisplay themeDisplay, HttpServletRequest request) {
49  
50          _command = command;
51          _type = type;
52          _currentFolder = currentFolder;
53          _newFolder = newFolder;
54          _themeDisplay = themeDisplay;
55          _request = request;
56      }
57  
58      public String getCommand() {
59          return _command;
60      }
61  
62      public String getType() {
63          return _type;
64      }
65  
66      public String getCurrentFolder() {
67          return _currentFolder;
68      }
69  
70      public String getNewFolder() {
71          return _newFolder;
72      }
73  
74      public ThemeDisplay getThemeDisplay() {
75          return _themeDisplay;
76      }
77  
78      public HttpServletRequest getHttpServletRequest() {
79          return _request;
80      }
81  
82      public long getCompanyId() {
83          return _themeDisplay.getCompanyId();
84      }
85  
86      public Group getCurrentGroup() throws Exception {
87          String currentGroupName = getCurrentGroupName();
88  
89          int pos = currentGroupName.indexOf(" - ");
90  
91          if (pos > 0) {
92              long groupId = GetterUtil.getLong(
93                  currentGroupName.substring(0, pos));
94  
95              Group group = GroupLocalServiceUtil.getGroup(groupId);
96  
97              if (group.getCompanyId() == getCompanyId()) {
98                  return group;
99              }
100         }
101 
102         throw new NoSuchGroupException();
103     }
104 
105     public String getCurrentGroupName() {
106         if (_currentFolder.equals("/")) {
107             return StringPool.BLANK;
108         }
109         else {
110             StringTokenizer st = new StringTokenizer(_currentFolder, "/");
111 
112             return st.nextToken();
113         }
114     }
115 
116     public long getUserId() {
117         return _themeDisplay.getUserId();
118     }
119 
120     public long getPlid() throws Exception {
121         long plid = _themeDisplay.getPlid();
122 
123         Layout layout = LayoutLocalServiceUtil.getLayout(plid);
124 
125         Group group = getCurrentGroup();
126 
127         if (layout.getGroupId() != group.getGroupId()) {
128             plid = LayoutLocalServiceUtil.getDefaultPlid(group.getGroupId());
129         }
130 
131         return plid;
132     }
133 
134     private String _command;
135     private String _type;
136     private String _currentFolder;
137     private String _newFolder;
138     private ThemeDisplay _themeDisplay;
139     private HttpServletRequest _request;
140 
141 }