1   /**
2    * Copyright (c) 2000-2009 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.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   */
45  public class CommandArgument {
46  
47      public CommandArgument(
48          String command, String type, String currentFolder, String newFolder,
49          ThemeDisplay themeDisplay, HttpServletRequest request) {
50  
51          _command = command;
52          _type = type;
53          _currentFolder = currentFolder;
54          _newFolder = newFolder;
55          _themeDisplay = themeDisplay;
56          _request = request;
57      }
58  
59      public String getCommand() {
60          return _command;
61      }
62  
63      public String getType() {
64          return _type;
65      }
66  
67      public String getCurrentFolder() {
68          return _currentFolder;
69      }
70  
71      public String getNewFolder() {
72          return _newFolder;
73      }
74  
75      public ThemeDisplay getThemeDisplay() {
76          return _themeDisplay;
77      }
78  
79      public HttpServletRequest getHttpServletRequest() {
80          return _request;
81      }
82  
83      public long getCompanyId() {
84          return _themeDisplay.getCompanyId();
85      }
86  
87      public Group getCurrentGroup() throws Exception {
88          String currentGroupName = getCurrentGroupName();
89  
90          int pos = currentGroupName.indexOf(" - ");
91  
92          if (pos > 0) {
93              long groupId = GetterUtil.getLong(
94                  currentGroupName.substring(0, pos));
95  
96              Group group = GroupLocalServiceUtil.getGroup(groupId);
97  
98              if (group.getCompanyId() == getCompanyId()) {
99                  return group;
100             }
101         }
102 
103         throw new NoSuchGroupException();
104     }
105 
106     public String getCurrentGroupName() {
107         if (_currentFolder.equals("/")) {
108             return StringPool.BLANK;
109         }
110         else {
111             StringTokenizer st = new StringTokenizer(_currentFolder, "/");
112 
113             return st.nextToken();
114         }
115     }
116 
117     public long getUserId() {
118         return _themeDisplay.getUserId();
119     }
120 
121     public long getPlid() throws Exception {
122         long plid = _themeDisplay.getPlid();
123 
124         Layout layout = LayoutLocalServiceUtil.getLayout(plid);
125 
126         Group group = getCurrentGroup();
127 
128         if (layout.getGroupId() != group.getGroupId()) {
129             plid = LayoutLocalServiceUtil.getDefaultPlid(group.getGroupId());
130         }
131 
132         return plid;
133     }
134 
135     private String _command;
136     private String _type;
137     private String _currentFolder;
138     private String _newFolder;
139     private ThemeDisplay _themeDisplay;
140     private HttpServletRequest _request;
141 
142 }