001    /**
002     * Copyright (c) 2000-2012 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.receiver.impl;
016    
017    import com.liferay.portal.editor.fckeditor.command.CommandArgument;
018    import com.liferay.portal.editor.fckeditor.exception.FCKException;
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.model.LayoutConstants;
023    import com.liferay.portal.service.LayoutLocalServiceUtil;
024    import com.liferay.portal.util.PortalUtil;
025    
026    import java.io.InputStream;
027    
028    import java.util.List;
029    
030    import org.w3c.dom.Document;
031    import org.w3c.dom.Element;
032    import org.w3c.dom.Node;
033    
034    /**
035     * @author Ivica Cardic
036     */
037    public class PageCommandReceiver extends BaseCommandReceiver {
038    
039            @Override
040            protected String createFolder(CommandArgument commandArgument) {
041                    return "0";
042            }
043    
044            @Override
045            protected String fileUpload(
046                    CommandArgument commandArgument, String fileName,
047                    InputStream inputStream, String extension, long size) {
048    
049                    return "0";
050            }
051    
052            @Override
053            protected void getFolders(
054                    CommandArgument commandArgument, Document document, Node rootNode) {
055    
056                    try {
057                            _getFolders(commandArgument, document, rootNode);
058                    }
059                    catch (Exception e) {
060                            throw new FCKException(e);
061                    }
062            }
063    
064            @Override
065            protected void getFoldersAndFiles(
066                    CommandArgument commandArgument, Document document, Node rootNode) {
067    
068                    try {
069                            _getFolders(commandArgument, document, rootNode);
070                            _getFiles(commandArgument, document, rootNode);
071                    }
072                    catch (Exception e) {
073                            throw new FCKException(e);
074                    }
075            }
076    
077            private void _getFiles(
078                            CommandArgument commandArgument, Document document, Node rootNode)
079                    throws Exception {
080    
081                    if (commandArgument.getCurrentFolder().equals(StringPool.SLASH)) {
082                            return;
083                    }
084    
085                    Element filesElement = document.createElement("Files");
086    
087                    rootNode.appendChild(filesElement);
088    
089                    Group group = commandArgument.getCurrentGroup();
090    
091                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
092                            group.getGroupId(), false,
093                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
094    
095                    if (("/" + commandArgument.getCurrentGroupName() + "/").equals(
096                                    commandArgument.getCurrentFolder())) {
097    
098                            for (Layout layout : layouts) {
099                                    Element fileElement = document.createElement("File");
100    
101                                    filesElement.appendChild(fileElement);
102    
103                                    fileElement.setAttribute("name", _getLayoutName(layout));
104                                    fileElement.setAttribute("desc", _getLayoutName(layout));
105                                    fileElement.setAttribute("size", StringPool.BLANK);
106                                    fileElement.setAttribute(
107                                            "url",
108                                            PortalUtil.getLayoutURL(
109                                                    layout, commandArgument.getThemeDisplay(), false));
110                            }
111                    }
112                    else {
113                            String layoutName = _getLayoutName(
114                                    commandArgument.getCurrentFolder());
115    
116                            Layout layout = null;
117    
118                            for (int i = 0; i < layouts.size(); i++) {
119                                    layout = _getLayout(layoutName, layouts.get(i));
120    
121                                    if (layout != null) {
122                                            break;
123                                    }
124                            }
125    
126                            if (layout == null) {
127                                    return;
128                            }
129    
130                            List<Layout> layoutChildren = layout.getChildren();
131    
132                            for (int i = 0; i < layoutChildren.size(); i++) {
133                                    layout = layoutChildren.get(i);
134    
135                                    Element fileElement = document.createElement("File");
136    
137                                    filesElement.appendChild(fileElement);
138    
139                                    fileElement.setAttribute("name", _getLayoutName(layout));
140                                    fileElement.setAttribute("desc", _getLayoutName(layout));
141                                    fileElement.setAttribute("size", getSize());
142                                    fileElement.setAttribute(
143                                            "url",
144                                            PortalUtil.getLayoutURL(
145                                                    layout, commandArgument.getThemeDisplay(), false));
146                            }
147                    }
148            }
149    
150            private void _getFolders(
151                            CommandArgument commandArgument, Document document, Node rootNode)
152                    throws Exception {
153    
154                    Element foldersElement = document.createElement("Folders");
155    
156                    rootNode.appendChild(foldersElement);
157    
158                    if (commandArgument.getCurrentFolder().equals(StringPool.SLASH)) {
159                            getRootFolders(commandArgument, document, foldersElement);
160                    }
161                    else {
162                            Group group = commandArgument.getCurrentGroup();
163    
164                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
165                                    group.getGroupId(), false,
166                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
167    
168                            if (("/" + commandArgument.getCurrentGroupName() + "/").equals(
169                                            commandArgument.getCurrentFolder())) {
170    
171                                    for (Layout layout : layouts) {
172                                            Element folderElement = document.createElement("Folder");
173    
174                                            foldersElement.appendChild(folderElement);
175    
176                                            folderElement.setAttribute(
177                                                    "name", "~" + _getLayoutName(layout).replace('/', '>'));
178                                    }
179                            }
180                            else {
181                                    String layoutName = _getLayoutName(
182                                            commandArgument.getCurrentFolder());
183    
184                                    Layout layout = null;
185    
186                                    for (int i = 0; i < layouts.size(); i++) {
187                                            layout = _getLayout(layoutName, layouts.get(i));
188    
189                                            if (layout != null) {
190                                                    break;
191                                            }
192                                    }
193    
194                                    if (layout != null) {
195                                            List<Layout> layoutChildren = layout.getChildren();
196    
197                                            for (int i = 0; i < layoutChildren.size(); i++) {
198                                                    layout = layoutChildren.get(i);
199    
200                                                    Element folderElement = document.createElement(
201                                                            "Folder");
202    
203                                                    foldersElement.appendChild(folderElement);
204    
205                                                    folderElement.setAttribute(
206                                                            "name",
207                                                            "~" + _getLayoutName(layout).replace('/', '>'));
208                                            }
209                                    }
210                            }
211                    }
212            }
213    
214            private Layout _getLayout(String layoutName, Layout layout)
215                    throws Exception {
216    
217                    String friendlyURL = layout.getFriendlyURL();
218    
219                    if (layoutName.equals(friendlyURL)) {
220                            return layout;
221                    }
222    
223                    List<Layout> layoutChildren = layout.getChildren();
224    
225                    if (layoutChildren.size() == 0) {
226                            return null;
227                    }
228                    else {
229                            for (Layout layoutChild : layoutChildren) {
230                                    Layout currentLayout = _getLayout(layoutName, layoutChild);
231    
232                                    if (currentLayout != null) {
233                                            return currentLayout;
234                                    }
235                            }
236                    }
237    
238                    return null;
239            }
240    
241            private String _getLayoutName(Layout layout) {
242                    return layout.getFriendlyURL();
243            }
244    
245            private String _getLayoutName(String folderName) {
246                    String layoutName = folderName.substring(
247                            folderName.lastIndexOf('~') + 1, folderName.length() - 1);
248    
249                    layoutName = layoutName.replace('>', '/');
250    
251                    return layoutName;
252            }
253    
254    }