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.editor.fckeditor.command;
21  
22  import com.liferay.portal.NoSuchGroupException;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.model.Group;
26  import com.liferay.portal.model.Layout;
27  import com.liferay.portal.service.GroupLocalServiceUtil;
28  import com.liferay.portal.service.LayoutLocalServiceUtil;
29  import com.liferay.portal.theme.ThemeDisplay;
30  
31  import java.util.StringTokenizer;
32  
33  import javax.servlet.http.HttpServletRequest;
34  
35  /**
36   * <a href="CommandArgument.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Ivica Cardic
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class CommandArgument {
43  
44      public CommandArgument(
45          String command, String type, String currentFolder, String newFolder,
46          ThemeDisplay themeDisplay, HttpServletRequest request) {
47  
48          _command = command;
49          _type = type;
50          _currentFolder = currentFolder;
51          _newFolder = newFolder;
52          _themeDisplay = themeDisplay;
53          _request = request;
54      }
55  
56      public String getCommand() {
57          return _command;
58      }
59  
60      public String getType() {
61          return _type;
62      }
63  
64      public String getCurrentFolder() {
65          return _currentFolder;
66      }
67  
68      public String getNewFolder() {
69          return _newFolder;
70      }
71  
72      public ThemeDisplay getThemeDisplay() {
73          return _themeDisplay;
74      }
75  
76      public HttpServletRequest getHttpServletRequest() {
77          return _request;
78      }
79  
80      public long getCompanyId() {
81          return _themeDisplay.getCompanyId();
82      }
83  
84      public Group getCurrentGroup() throws Exception {
85          String currentGroupName = getCurrentGroupName();
86  
87          int pos = currentGroupName.indexOf(" - ");
88  
89          if (pos > 0) {
90              long groupId = GetterUtil.getLong(
91                  currentGroupName.substring(0, pos));
92  
93              Group group = GroupLocalServiceUtil.getGroup(groupId);
94  
95              if (group.getCompanyId() == getCompanyId()) {
96                  return group;
97              }
98          }
99  
100         throw new NoSuchGroupException();
101     }
102 
103     public String getCurrentGroupName() {
104         if (_currentFolder.equals("/")) {
105             return StringPool.BLANK;
106         }
107         else {
108             StringTokenizer st = new StringTokenizer(_currentFolder, "/");
109 
110             return st.nextToken();
111         }
112     }
113 
114     public long getUserId() {
115         return _themeDisplay.getUserId();
116     }
117 
118     public long getPlid() throws Exception {
119         long plid = _themeDisplay.getPlid();
120 
121         Layout layout = LayoutLocalServiceUtil.getLayout(plid);
122 
123         Group group = getCurrentGroup();
124 
125         if (layout.getGroupId() != group.getGroupId()) {
126             plid = LayoutLocalServiceUtil.getDefaultPlid(group.getGroupId());
127         }
128 
129         return plid;
130     }
131 
132     private String _command;
133     private String _type;
134     private String _currentFolder;
135     private String _newFolder;
136     private ThemeDisplay _themeDisplay;
137     private HttpServletRequest _request;
138 
139 }