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 Layout _getLayout(String layoutName, Layout layout)
078                    throws Exception {
079    
080                    String friendlyURL = layout.getFriendlyURL();
081    
082                    if (layoutName.equals(friendlyURL)) {
083                            return layout;
084                    }
085    
086                    List<Layout> layoutChildren = layout.getChildren();
087    
088                    if (layoutChildren.size() == 0) {
089                            return null;
090                    }
091                    else {
092                            for (Layout layoutChild : layoutChildren) {
093                                    Layout currentLayout = _getLayout(layoutName, layoutChild);
094    
095                                    if (currentLayout != null) {
096                                            return currentLayout;
097                                    }
098                            }
099                    }
100    
101                    return null;
102            }
103    
104            private String _getLayoutName(Layout layout) {
105                    return layout.getFriendlyURL();
106            }
107    
108            private String _getLayoutName(String folderName) {
109                    String layoutName = folderName.substring(
110                            folderName.lastIndexOf('~') + 1, folderName.length() - 1);
111    
112                    layoutName = layoutName.replace('>', '/');
113    
114                    return layoutName;
115            }
116    
117            private void _getFiles(
118                            CommandArgument commandArgument, Document document, Node rootNode)
119                    throws Exception {
120    
121                    if (commandArgument.getCurrentFolder().equals(StringPool.SLASH)) {
122                            return;
123                    }
124    
125                    Element filesElement = document.createElement("Files");
126    
127                    rootNode.appendChild(filesElement);
128    
129                    Group group = commandArgument.getCurrentGroup();
130    
131                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
132                            group.getGroupId(), false,
133                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
134    
135                    if (("/" + commandArgument.getCurrentGroupName() + "/").equals(
136                                    commandArgument.getCurrentFolder())) {
137    
138                            for (Layout layout : layouts) {
139                                    Element fileElement = document.createElement("File");
140    
141                                    filesElement.appendChild(fileElement);
142    
143                                    fileElement.setAttribute("name", _getLayoutName(layout));
144                                    fileElement.setAttribute("desc", _getLayoutName(layout));
145                                    fileElement.setAttribute("size", StringPool.BLANK);
146                                    fileElement.setAttribute(
147                                            "url",
148                                            PortalUtil.getLayoutURL(
149                                                    layout, commandArgument.getThemeDisplay(), false));
150                            }
151                    }
152                    else {
153                            String layoutName = _getLayoutName(
154                                    commandArgument.getCurrentFolder());
155    
156                            Layout layout = null;
157    
158                            for (int i = 0; i < layouts.size(); i++) {
159                                    layout = _getLayout(layoutName, layouts.get(i));
160    
161                                    if (layout != null) {
162                                            break;
163                                    }
164                            }
165    
166                            if (layout == null) {
167                                    return;
168                            }
169    
170                            List<Layout> layoutChildren = layout.getChildren();
171    
172                            for (int i = 0; i < layoutChildren.size(); i++) {
173                                    layout = layoutChildren.get(i);
174    
175                                    Element fileElement = document.createElement("File");
176    
177                                    filesElement.appendChild(fileElement);
178    
179                                    fileElement.setAttribute("name", _getLayoutName(layout));
180                                    fileElement.setAttribute("desc", _getLayoutName(layout));
181                                    fileElement.setAttribute("size", getSize());
182                                    fileElement.setAttribute(
183                                            "url",
184                                            PortalUtil.getLayoutURL(
185                                                    layout, commandArgument.getThemeDisplay(), false));
186                            }
187                    }
188            }
189    
190            private void _getFolders(
191                            CommandArgument commandArgument, Document document, Node rootNode)
192                    throws Exception {
193    
194                    Element foldersElement = document.createElement("Folders");
195    
196                    rootNode.appendChild(foldersElement);
197    
198                    if (commandArgument.getCurrentFolder().equals(StringPool.SLASH)) {
199                            getRootFolders(commandArgument, document, foldersElement);
200                    }
201                    else {
202                            Group group = commandArgument.getCurrentGroup();
203    
204                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
205                                    group.getGroupId(), false,
206                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
207    
208                            if (("/" + commandArgument.getCurrentGroupName() + "/").equals(
209                                            commandArgument.getCurrentFolder())) {
210    
211                                    for (Layout layout : layouts) {
212                                            Element folderElement = document.createElement("Folder");
213    
214                                            foldersElement.appendChild(folderElement);
215    
216                                            folderElement.setAttribute(
217                                                    "name", "~" + _getLayoutName(layout).replace('/', '>'));
218                                    }
219                            }
220                            else {
221                                    String layoutName = _getLayoutName(
222                                            commandArgument.getCurrentFolder());
223    
224                                    Layout layout = null;
225    
226                                    for (int i = 0; i < layouts.size(); i++) {
227                                            layout = _getLayout(layoutName, layouts.get(i));
228    
229                                            if (layout != null) {
230                                                    break;
231                                            }
232                                    }
233    
234                                    if (layout != null) {
235                                            List<Layout> layoutChildren = layout.getChildren();
236    
237                                            for (int i = 0; i < layoutChildren.size(); i++) {
238                                                    layout = layoutChildren.get(i);
239    
240                                                    Element folderElement = document.createElement(
241                                                            "Folder");
242    
243                                                    foldersElement.appendChild(folderElement);
244    
245                                                    folderElement.setAttribute(
246                                                            "name",
247                                                            "~" + _getLayoutName(layout).replace('/', '>'));
248                                            }
249                                    }
250                            }
251                    }
252            }
253    
254    }