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.StringTokenizer;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    /**
031     * @author Ivica Cardic
032     * @author Brian Wing Shun Chan
033     */
034    public class CommandArgument {
035    
036            public CommandArgument(
037                    String command, String type, String currentFolder, String newFolder,
038                    ThemeDisplay themeDisplay, HttpServletRequest request) {
039    
040                    _command = command;
041                    _type = type;
042                    _currentFolder = currentFolder;
043                    _newFolder = newFolder;
044                    _themeDisplay = themeDisplay;
045                    _request = request;
046            }
047    
048            public String getCommand() {
049                    return _command;
050            }
051    
052            public long getCompanyId() {
053                    return _themeDisplay.getCompanyId();
054            }
055    
056            public String getCurrentFolder() {
057                    return _currentFolder;
058            }
059    
060            public Group getCurrentGroup() throws Exception {
061                    String currentGroupName = getCurrentGroupName();
062    
063                    int pos = currentGroupName.indexOf(" - ");
064    
065                    if (pos > 0) {
066                            long groupId = GetterUtil.getLong(
067                                    currentGroupName.substring(0, pos));
068    
069                            Group group = GroupLocalServiceUtil.getGroup(groupId);
070    
071                            if (group.getCompanyId() == getCompanyId()) {
072                                    return group;
073                            }
074                    }
075    
076                    throw new NoSuchGroupException();
077            }
078    
079            public String getCurrentGroupName() {
080                    if (_currentFolder.equals("/")) {
081                            return StringPool.BLANK;
082                    }
083                    else {
084                            StringTokenizer st = new StringTokenizer(_currentFolder, "/");
085    
086                            return st.nextToken();
087                    }
088            }
089    
090            public HttpServletRequest getHttpServletRequest() {
091                    return _request;
092            }
093    
094            public String getNewFolder() {
095                    return _newFolder;
096            }
097    
098            public long getPlid() throws Exception {
099                    long plid = _themeDisplay.getPlid();
100    
101                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
102    
103                    Group group = getCurrentGroup();
104    
105                    if (layout.getGroupId() != group.getGroupId()) {
106                            plid = LayoutLocalServiceUtil.getDefaultPlid(group.getGroupId());
107                    }
108    
109                    return plid;
110            }
111    
112            public ThemeDisplay getThemeDisplay() {
113                    return _themeDisplay;
114            }
115    
116            public String getType() {
117                    return _type;
118            }
119    
120            public long getUserId() {
121                    return _themeDisplay.getUserId();
122            }
123    
124            private String _command;
125            private String _currentFolder;
126            private String _newFolder;
127            private HttpServletRequest _request;
128            private ThemeDisplay _themeDisplay;
129            private String _type;
130    
131    }