1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.editor.fckeditor.receiver.impl;
24  
25  import com.liferay.portal.editor.fckeditor.command.CommandArgument;
26  import com.liferay.portal.editor.fckeditor.exception.FCKException;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.model.impl.LayoutImpl;
31  import com.liferay.portal.service.LayoutLocalServiceUtil;
32  import com.liferay.portal.util.PortalUtil;
33  
34  import java.io.File;
35  
36  import java.util.List;
37  
38  import org.w3c.dom.Document;
39  import org.w3c.dom.Element;
40  import org.w3c.dom.Node;
41  
42  /**
43   * <a href="PageCommandReceiver.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Ivica Cardic
46   *
47   */
48  public class PageCommandReceiver extends BaseCommandReceiver {
49  
50      protected String createFolder(CommandArgument arg) {
51          return "0";
52      }
53  
54      protected String fileUpload(
55          CommandArgument arg, String fileName, File file, String extension) {
56  
57          return "0";
58      }
59  
60      protected void getFolders(CommandArgument arg, Document doc, Node root) {
61          try {
62              _getFolders(arg, doc, root);
63          }
64          catch (Exception e) {
65              throw new FCKException(e);
66          }
67      }
68  
69      protected void getFoldersAndFiles(
70          CommandArgument arg, Document doc, Node root) {
71  
72          try {
73              _getFolders(arg, doc, root);
74              _getFiles(arg, doc, root);
75          }
76          catch (Exception e) {
77              throw new FCKException(e);
78          }
79      }
80  
81      private Layout _getLayout(String layoutName, Layout layout)
82          throws Exception {
83  
84          String friendlyURL = layout.getFriendlyURL();
85  
86          if (Validator.isNotNull(friendlyURL)) {
87              if (layoutName.equals(friendlyURL)) {
88                  return layout;
89              }
90          }
91          else {
92              if (layoutName.equals(String.valueOf(layout.getPlid()))) {
93                  return layout;
94              }
95          }
96  
97          List layoutChildren = layout.getChildren();
98  
99          if (layoutChildren.size() == 0) {
100             return null;
101         }
102         else {
103             for (int i = 0; i < layoutChildren.size(); i++) {
104                 Layout layoutChild = (Layout)layoutChildren.get(i);
105 
106                 Layout currentLayout = _getLayout(layoutName, layoutChild);
107 
108                 if (currentLayout != null) {
109                     return currentLayout;
110                 }
111             }
112         }
113 
114         return null;
115     }
116 
117     private String _getLayoutName(Layout layout) {
118         String friendlyURL = layout.getFriendlyURL();
119 
120         if (Validator.isNotNull(friendlyURL)) {
121             return friendlyURL;
122         }
123         else {
124             return String.valueOf(layout.getPlid());
125         }
126     }
127 
128     private String _getLayoutName(String folderName) {
129         String layoutName = folderName.substring(
130             folderName.lastIndexOf('~') + 1, folderName.length() - 1);
131 
132         layoutName = layoutName.replace('>', '/');
133 
134         return layoutName;
135     }
136 
137     private void _getFiles(CommandArgument arg, Document doc, Node root)
138         throws Exception {
139 
140         if (!arg.getCurrentFolder().equals("/")) {
141             Element filesEl = doc.createElement("Files");
142 
143             root.appendChild(filesEl);
144 
145             Group group = arg.getCurrentGroup();
146 
147             List layouts = LayoutLocalServiceUtil.getLayouts(
148                 group.getGroupId(), false, LayoutImpl.DEFAULT_PARENT_LAYOUT_ID);
149 
150             if (("/" + arg.getCurrentGroupName() + "/").equals(
151                     arg.getCurrentFolder())) {
152 
153                 for (int i = 0; i < layouts.size(); i++) {
154                     Layout layout = (Layout)layouts.get(i);
155 
156                     Element fileEl = doc.createElement("File");
157 
158                     filesEl.appendChild(fileEl);
159 
160                     fileEl.setAttribute("name", _getLayoutName(layout));
161                     fileEl.setAttribute("desc", _getLayoutName(layout));
162                     fileEl.setAttribute("size", "");
163                     fileEl.setAttribute(
164                         "url",
165                         PortalUtil.getLayoutURL(layout,arg.getThemeDisplay()));
166                 }
167             }
168             else {
169                 String layoutName = _getLayoutName(arg.getCurrentFolder());
170 
171                 Layout layout = null;
172 
173                 for (int i = 0; i < layouts.size(); i++) {
174                     layout = _getLayout(layoutName, (Layout)layouts.get(i));
175 
176                     if (layout != null) {
177                         break;
178                     }
179                 }
180 
181                 if (layout != null) {
182                     List layoutChildren = layout.getChildren();
183 
184                     for (int i = 0; i < layoutChildren.size(); i++) {
185                         layout = (Layout)layoutChildren.get(i);
186 
187                         Element fileEl = doc.createElement("File");
188 
189                         filesEl.appendChild(fileEl);
190 
191                         fileEl.setAttribute("name", _getLayoutName(layout));
192                         fileEl.setAttribute("desc", _getLayoutName(layout));
193                         fileEl.setAttribute("size", getSize());
194                         fileEl.setAttribute(
195                             "url",
196                             PortalUtil.getLayoutURL(
197                                 layout, arg.getThemeDisplay()));
198                     }
199                 }
200             }
201         }
202     }
203 
204     private void _getFolders(CommandArgument arg, Document doc, Node root)
205         throws Exception {
206 
207         Element foldersEl = doc.createElement("Folders");
208 
209         root.appendChild(foldersEl);
210 
211         if (arg.getCurrentFolder().equals("/")) {
212             getRootFolders(arg, doc, foldersEl);
213         }
214         else {
215             Group group = arg.getCurrentGroup();
216 
217             List layouts = LayoutLocalServiceUtil.getLayouts(
218                 group.getGroupId(), false, LayoutImpl.DEFAULT_PARENT_LAYOUT_ID);
219 
220             if (("/" + arg.getCurrentGroupName() + "/").equals(
221                     arg.getCurrentFolder())) {
222 
223                 for (int i = 0; i < layouts.size(); i++) {
224                     Layout layout = (Layout)layouts.get(i);
225 
226                     Element folderEl = doc.createElement("Folder");
227 
228                     foldersEl.appendChild(folderEl);
229 
230                     folderEl.setAttribute(
231                         "name", "~" + _getLayoutName(layout).replace('/', '>'));
232                 }
233             }
234             else {
235                 String layoutName = _getLayoutName(arg.getCurrentFolder());
236 
237                 Layout layout = null;
238 
239                 for (int i = 0; i < layouts.size(); i++) {
240                     layout = _getLayout(layoutName, (Layout)layouts.get(i));
241 
242                     if (layout != null) {
243                         break;
244                     }
245                 }
246 
247                 if (layout != null) {
248                     List layoutChildren = layout.getChildren();
249 
250                     for (int i = 0; i < layoutChildren.size(); i++) {
251                         layout = (Layout)layoutChildren.get(i);
252 
253                         Element folderEl = doc.createElement("Folder");
254 
255                         foldersEl.appendChild(folderEl);
256 
257                         folderEl.setAttribute(
258                             "name",
259                             "~" + _getLayoutName(layout).replace('/', '>'));
260                     }
261                 }
262             }
263         }
264     }
265 
266 }