001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.File;
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            protected String createFolder(CommandArgument arg) {
040                    return "0";
041            }
042    
043            protected String fileUpload(
044                    CommandArgument arg, String fileName, File file, String extension) {
045    
046                    return "0";
047            }
048    
049            protected void getFolders(CommandArgument arg, Document doc, Node root) {
050                    try {
051                            _getFolders(arg, doc, root);
052                    }
053                    catch (Exception e) {
054                            throw new FCKException(e);
055                    }
056            }
057    
058            protected void getFoldersAndFiles(
059                    CommandArgument arg, Document doc, Node root) {
060    
061                    try {
062                            _getFolders(arg, doc, root);
063                            _getFiles(arg, doc, root);
064                    }
065                    catch (Exception e) {
066                            throw new FCKException(e);
067                    }
068            }
069    
070            private Layout _getLayout(String layoutName, Layout layout)
071                    throws Exception {
072    
073                    String friendlyURL = layout.getFriendlyURL();
074    
075                    if (layoutName.equals(friendlyURL)) {
076                            return layout;
077                    }
078    
079                    List<Layout> layoutChildren = layout.getChildren();
080    
081                    if (layoutChildren.size() == 0) {
082                            return null;
083                    }
084                    else {
085                            for (Layout layoutChild : layoutChildren) {
086                                    Layout currentLayout = _getLayout(layoutName, layoutChild);
087    
088                                    if (currentLayout != null) {
089                                            return currentLayout;
090                                    }
091                            }
092                    }
093    
094                    return null;
095            }
096    
097            private String _getLayoutName(Layout layout) {
098                    return layout.getFriendlyURL();
099            }
100    
101            private String _getLayoutName(String folderName) {
102                    String layoutName = folderName.substring(
103                            folderName.lastIndexOf('~') + 1, folderName.length() - 1);
104    
105                    layoutName = layoutName.replace('>', '/');
106    
107                    return layoutName;
108            }
109    
110            private void _getFiles(CommandArgument arg, Document doc, Node root)
111                    throws Exception {
112    
113                    if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
114                            return;
115                    }
116    
117                    Element filesEl = doc.createElement("Files");
118    
119                    root.appendChild(filesEl);
120    
121                    Group group = arg.getCurrentGroup();
122    
123                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
124                            group.getGroupId(), false,
125                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
126    
127                    if (("/" + arg.getCurrentGroupName() + "/").equals(
128                                    arg.getCurrentFolder())) {
129    
130                            for (Layout layout : layouts) {
131                                    Element fileEl = doc.createElement("File");
132    
133                                    filesEl.appendChild(fileEl);
134    
135                                    fileEl.setAttribute("name", _getLayoutName(layout));
136                                    fileEl.setAttribute("desc", _getLayoutName(layout));
137                                    fileEl.setAttribute("size", StringPool.BLANK);
138                                    fileEl.setAttribute(
139                                            "url",
140                                            PortalUtil.getLayoutURL(
141                                                    layout,arg.getThemeDisplay(), false));
142                            }
143                    }
144                    else {
145                            String layoutName = _getLayoutName(arg.getCurrentFolder());
146    
147                            Layout layout = null;
148    
149                            for (int i = 0; i < layouts.size(); i++) {
150                                    layout = _getLayout(layoutName, layouts.get(i));
151    
152                                    if (layout != null) {
153                                            break;
154                                    }
155                            }
156    
157                            if (layout == null) {
158                                    return;
159                            }
160    
161                            List<Layout> layoutChildren = layout.getChildren();
162    
163                            for (int i = 0; i < layoutChildren.size(); i++) {
164                                    layout = layoutChildren.get(i);
165    
166                                    Element fileEl = doc.createElement("File");
167    
168                                    filesEl.appendChild(fileEl);
169    
170                                    fileEl.setAttribute("name", _getLayoutName(layout));
171                                    fileEl.setAttribute("desc", _getLayoutName(layout));
172                                    fileEl.setAttribute("size", getSize());
173                                    fileEl.setAttribute(
174                                            "url",
175                                            PortalUtil.getLayoutURL(
176                                                    layout, arg.getThemeDisplay(), false));
177                            }
178                    }
179            }
180    
181            private void _getFolders(CommandArgument arg, Document doc, Node root)
182                    throws Exception {
183    
184                    Element foldersEl = doc.createElement("Folders");
185    
186                    root.appendChild(foldersEl);
187    
188                    if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
189                            getRootFolders(arg, doc, foldersEl);
190                    }
191                    else {
192                            Group group = arg.getCurrentGroup();
193    
194                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
195                                    group.getGroupId(), false,
196                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
197    
198                            if (("/" + arg.getCurrentGroupName() + "/").equals(
199                                            arg.getCurrentFolder())) {
200    
201                                    for (Layout layout : layouts) {
202                                            Element folderEl = doc.createElement("Folder");
203    
204                                            foldersEl.appendChild(folderEl);
205    
206                                            folderEl.setAttribute(
207                                                    "name", "~" + _getLayoutName(layout).replace('/', '>'));
208                                    }
209                            }
210                            else {
211                                    String layoutName = _getLayoutName(arg.getCurrentFolder());
212    
213                                    Layout layout = null;
214    
215                                    for (int i = 0; i < layouts.size(); i++) {
216                                            layout = _getLayout(layoutName, layouts.get(i));
217    
218                                            if (layout != null) {
219                                                    break;
220                                            }
221                                    }
222    
223                                    if (layout != null) {
224                                            List<Layout> layoutChildren = layout.getChildren();
225    
226                                            for (int i = 0; i < layoutChildren.size(); i++) {
227                                                    layout = layoutChildren.get(i);
228    
229                                                    Element folderEl = doc.createElement("Folder");
230    
231                                                    foldersEl.appendChild(folderEl);
232    
233                                                    folderEl.setAttribute(
234                                                            "name",
235                                                            "~" + _getLayoutName(layout).replace('/', '>'));
236                                            }
237                                    }
238                            }
239                    }
240            }
241    
242    }