001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.editor.fckeditor.command;
016    
017    import com.liferay.portal.NoSuchGroupException;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.model.Group;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.service.GroupLocalServiceUtil;
023    import com.liferay.portal.service.LayoutLocalServiceUtil;
024    import com.liferay.portal.theme.ThemeDisplay;
025    
026    import java.util.Locale;
027    import java.util.StringTokenizer;
028    
029    import javax.servlet.http.HttpServletRequest;
030    
031    /**
032     * @author Ivica Cardic
033     * @author Brian Wing Shun Chan
034     */
035    public class CommandArgument {
036    
037            public CommandArgument(
038                    String command, String type, String currentFolder, String newFolder,
039                    ThemeDisplay themeDisplay, HttpServletRequest request) {
040    
041                    this(
042                            command, type, StringPool.BLANK, false, currentFolder, newFolder,
043                            themeDisplay, request);
044            }
045    
046            public CommandArgument(
047                    String command, String type, String sortType, boolean ascending,
048                    String currentFolder, String newFolder, ThemeDisplay themeDisplay,
049                    HttpServletRequest request) {
050    
051                    _command = command;
052                    _type = type;
053                    _sortType = sortType;
054                    _ascending = ascending;
055                    _currentFolder = currentFolder;
056                    _newFolder = newFolder;
057                    _themeDisplay = themeDisplay;
058                    _request = request;
059            }
060    
061            public boolean getAscending() {
062                    return _ascending;
063            }
064    
065            public String getCommand() {
066                    return _command;
067            }
068    
069            public long getCompanyId() {
070                    return _themeDisplay.getCompanyId();
071            }
072    
073            public String getCurrentFolder() {
074                    return _currentFolder;
075            }
076    
077            public Group getCurrentGroup() throws Exception {
078                    String currentGroupName = getCurrentGroupName();
079    
080                    int pos = currentGroupName.indexOf(" - ");
081    
082                    if (pos == -1) {
083                            throw new NoSuchGroupException();
084                    }
085    
086                    long groupId = GetterUtil.getLong(currentGroupName.substring(0, pos));
087    
088                    Group group = GroupLocalServiceUtil.getGroup(groupId);
089    
090                    if (group.getCompanyId() == getCompanyId()) {
091                            return group;
092                    }
093    
094                    throw new NoSuchGroupException("{groupId=" + groupId + "}");
095            }
096    
097            public String getCurrentGroupName() {
098                    if (_currentFolder.equals("/")) {
099                            return StringPool.BLANK;
100                    }
101    
102                    StringTokenizer st = new StringTokenizer(_currentFolder, "/");
103    
104                    return st.nextToken();
105            }
106    
107            public HttpServletRequest getHttpServletRequest() {
108                    return _request;
109            }
110    
111            public Locale getLocale() {
112                    return _themeDisplay.getLocale();
113            }
114    
115            public String getNewFolder() {
116                    return _newFolder;
117            }
118    
119            public long getPlid() throws Exception {
120                    long plid = _themeDisplay.getPlid();
121    
122                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
123    
124                    Group group = getCurrentGroup();
125    
126                    if (layout.getGroupId() != group.getGroupId()) {
127                            plid = LayoutLocalServiceUtil.getDefaultPlid(group.getGroupId());
128                    }
129    
130                    return plid;
131            }
132    
133            public String getSortType() {
134                    return _sortType;
135            }
136    
137            public ThemeDisplay getThemeDisplay() {
138                    return _themeDisplay;
139            }
140    
141            public String getType() {
142                    return _type;
143            }
144    
145            public long getUserId() {
146                    return _themeDisplay.getUserId();
147            }
148    
149            private boolean _ascending;
150            private String _command;
151            private String _currentFolder;
152            private String _newFolder;
153            private HttpServletRequest _request;
154            private String _sortType;
155            private ThemeDisplay _themeDisplay;
156            private String _type;
157    
158    }