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 > 0) {
067                            long groupId = GetterUtil.getLong(
068                                    currentGroupName.substring(0, pos));
069    
070                            Group group = GroupLocalServiceUtil.getGroup(groupId);
071    
072                            if (group.getCompanyId() == getCompanyId()) {
073                                    return group;
074                            }
075                    }
076    
077                    throw new NoSuchGroupException();
078            }
079    
080            public String getCurrentGroupName() {
081                    if (_currentFolder.equals("/")) {
082                            return StringPool.BLANK;
083                    }
084    
085                    StringTokenizer st = new StringTokenizer(_currentFolder, "/");
086    
087                    return st.nextToken();
088            }
089    
090            public HttpServletRequest getHttpServletRequest() {
091                    return _request;
092            }
093    
094            public Locale getLocale() {
095                    return _themeDisplay.getLocale();
096            }
097    
098            public String getNewFolder() {
099                    return _newFolder;
100            }
101    
102            public long getPlid() throws Exception {
103                    long plid = _themeDisplay.getPlid();
104    
105                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
106    
107                    Group group = getCurrentGroup();
108    
109                    if (layout.getGroupId() != group.getGroupId()) {
110                            plid = LayoutLocalServiceUtil.getDefaultPlid(group.getGroupId());
111                    }
112    
113                    return plid;
114            }
115    
116            public ThemeDisplay getThemeDisplay() {
117                    return _themeDisplay;
118            }
119    
120            public String getType() {
121                    return _type;
122            }
123    
124            public long getUserId() {
125                    return _themeDisplay.getUserId();
126            }
127    
128            private String _command;
129            private String _currentFolder;
130            private String _newFolder;
131            private HttpServletRequest _request;
132            private ThemeDisplay _themeDisplay;
133            private String _type;
134    
135    }