001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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                    _command = command;
042                    _type = type;
043                    _currentFolder = currentFolder;
044                    _newFolder = newFolder;
045                    _themeDisplay = themeDisplay;
046                    _request = request;
047            }
048    
049            public String getCommand() {
050                    return _command;
051            }
052    
053            public long getCompanyId() {
054                    return _themeDisplay.getCompanyId();
055            }
056    
057            public String getCurrentFolder() {
058                    return _currentFolder;
059            }
060    
061            public Group getCurrentGroup() throws Exception {
062                    String currentGroupName = getCurrentGroupName();
063    
064                    int pos = currentGroupName.indexOf(" - ");
065    
066                    if (pos == -1) {
067                            throw new NoSuchGroupException();
068                    }
069    
070                    long groupId = GetterUtil.getLong(currentGroupName.substring(0, pos));
071    
072                    Group group = GroupLocalServiceUtil.getGroup(groupId);
073    
074                    if (group.getCompanyId() == getCompanyId()) {
075                            return group;
076                    }
077    
078                    throw new NoSuchGroupException("{groupId=" + groupId + "}");
079            }
080    
081            public String getCurrentGroupName() {
082                    if (_currentFolder.equals("/")) {
083                            return StringPool.BLANK;
084                    }
085    
086                    StringTokenizer st = new StringTokenizer(_currentFolder, "/");
087    
088                    return st.nextToken();
089            }
090    
091            public HttpServletRequest getHttpServletRequest() {
092                    return _request;
093            }
094    
095            public Locale getLocale() {
096                    return _themeDisplay.getLocale();
097            }
098    
099            public String getNewFolder() {
100                    return _newFolder;
101            }
102    
103            public long getPlid() throws Exception {
104                    long plid = _themeDisplay.getPlid();
105    
106                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
107    
108                    Group group = getCurrentGroup();
109    
110                    if (layout.getGroupId() != group.getGroupId()) {
111                            plid = LayoutLocalServiceUtil.getDefaultPlid(group.getGroupId());
112                    }
113    
114                    return plid;
115            }
116    
117            public ThemeDisplay getThemeDisplay() {
118                    return _themeDisplay;
119            }
120    
121            public String getType() {
122                    return _type;
123            }
124    
125            public long getUserId() {
126                    return _themeDisplay.getUserId();
127            }
128    
129            private String _command;
130            private String _currentFolder;
131            private String _newFolder;
132            private HttpServletRequest _request;
133            private ThemeDisplay _themeDisplay;
134            private String _type;
135    
136    }