1   /**
2    * Copyright (c) 2000-2007 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.GroupLocalServiceUtil;
32  import com.liferay.portal.service.GroupServiceUtil;
33  import com.liferay.portal.service.LayoutLocalServiceUtil;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.util.dao.hibernate.QueryUtil;
36  
37  import java.io.File;
38  
39  import java.util.LinkedHashMap;
40  import java.util.List;
41  
42  import org.w3c.dom.Document;
43  import org.w3c.dom.Element;
44  import org.w3c.dom.Node;
45  
46  /**
47   * <a href="PageCommandReceiver.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Ivica Cardic
50   *
51   */
52  public class PageCommandReceiver extends BaseCommandReceiver {
53  
54      protected String createFolder(CommandArgument arg) {
55          return "0";
56      }
57  
58      protected String fileUpload(
59          CommandArgument arg, String fileName, File file, String extension) {
60  
61          return "0";
62      }
63  
64      protected void getFolders(CommandArgument arg, Node root, Document doc) {
65          try {
66              _getFolders(arg, root, doc);
67          }
68          catch (Exception e) {
69              throw new FCKException(e);
70          }
71      }
72  
73      protected void getFoldersAndFiles(
74          CommandArgument arg, Node root, Document doc) {
75  
76          try {
77              _getFolders(arg, root, doc);
78              _getFiles(arg, root, doc);
79          }
80          catch (Exception e) {
81              throw new FCKException(e);
82          }
83      }
84  
85      private Layout _getLayout(String layoutName, Layout layout)
86          throws Exception {
87  
88          String friendlyURL = layout.getFriendlyURL();
89  
90          if (Validator.isNotNull(friendlyURL)) {
91              if (layoutName.equals(friendlyURL)) {
92                  return layout;
93              }
94          }
95          else {
96              if (layoutName.equals(String.valueOf(layout.getPlid()))) {
97                  return layout;
98              }
99          }
100 
101         List layoutChildren = layout.getChildren();
102 
103         if (layoutChildren.size() == 0) {
104             return null;
105         }
106         else {
107             for (int i = 0; i < layoutChildren.size(); i++) {
108                 Layout layoutChild = (Layout)layoutChildren.get(i);
109 
110                 Layout currentLayout = _getLayout(layoutName, layoutChild);
111 
112                 if (currentLayout != null) {
113                     return currentLayout;
114                 }
115             }
116         }
117 
118         return null;
119     }
120 
121     private String _getLayoutName(Layout layout) {
122         String friendlyURL = layout.getFriendlyURL();
123 
124         if (Validator.isNotNull(friendlyURL)) {
125             return friendlyURL;
126         }
127         else {
128             return String.valueOf(layout.getPlid());
129         }
130     }
131 
132     private String _getLayoutName(String folderName) {
133         String layoutName = folderName.substring(
134             folderName.lastIndexOf('~') + 1, folderName.length() - 1);
135 
136         layoutName = layoutName.replace('>', '/');
137 
138         return layoutName;
139     }
140 
141     private void _getFiles(CommandArgument arg, Node root, Document doc)
142         throws Exception {
143 
144         if (!arg.getCurrentFolder().equals("/")) {
145             Element filesEl = doc.createElement("Files");
146 
147             root.appendChild(filesEl);
148 
149             Group group = GroupServiceUtil.getGroup(
150                 arg.getCompanyId(), arg.getCurrentGroupName());
151 
152             List layouts = LayoutLocalServiceUtil.getLayouts(
153                 group.getGroupId(), false, LayoutImpl.DEFAULT_PARENT_LAYOUT_ID);
154 
155             if (("/" + arg.getCurrentGroupName() + "/").equals(
156                     arg.getCurrentFolder())) {
157 
158                 for (int i = 0; i < layouts.size(); i++) {
159                     Layout layout = (Layout)layouts.get(i);
160 
161                     Element fileEl = doc.createElement("File");
162 
163                     filesEl.appendChild(fileEl);
164 
165                     fileEl.setAttribute("name", _getLayoutName(layout));
166                     fileEl.setAttribute("desc", _getLayoutName(layout));
167                     fileEl.setAttribute("size", "");
168                     fileEl.setAttribute(
169                         "url",
170                         PortalUtil.getLayoutURL(layout,arg.getThemeDisplay()));
171                 }
172             }
173             else {
174                 String layoutName = _getLayoutName(arg.getCurrentFolder());
175 
176                 Layout layout = null;
177 
178                 for (int i = 0; i < layouts.size(); i++) {
179                     layout = _getLayout(layoutName, (Layout)layouts.get(i));
180 
181                     if (layout != null) {
182                         break;
183                     }
184                 }
185 
186                 if (layout != null) {
187                     List layoutChildren = layout.getChildren();
188 
189                     for (int i = 0; i < layoutChildren.size(); i++) {
190                         layout = (Layout)layoutChildren.get(i);
191 
192                         Element fileEl = doc.createElement("File");
193 
194                         filesEl.appendChild(fileEl);
195 
196                         fileEl.setAttribute("name", _getLayoutName(layout));
197                         fileEl.setAttribute("desc", _getLayoutName(layout));
198                         fileEl.setAttribute("size", getSize());
199                         fileEl.setAttribute(
200                             "url",
201                             PortalUtil.getLayoutURL(
202                                 layout, arg.getThemeDisplay()));
203                     }
204                 }
205             }
206         }
207     }
208 
209     private void _getFolders(CommandArgument arg, Node root, Document doc)
210         throws Exception {
211 
212         Element foldersEl = doc.createElement("Folders");
213 
214         root.appendChild(foldersEl);
215 
216         if (arg.getCurrentFolder().equals("/")) {
217             LinkedHashMap groupParams = new LinkedHashMap();
218 
219             groupParams.put("usersGroups", new Long(arg.getUserId()));
220             groupParams.put("layoutSet", Boolean.FALSE);
221 
222             List groups = GroupLocalServiceUtil.search(
223                 arg.getCompanyId(), null, null, groupParams, QueryUtil.ALL_POS,
224                 QueryUtil.ALL_POS);
225 
226             for (int i = 0; i < groups.size(); ++i) {
227                 Group group = (Group)groups.get(i);
228 
229                 Element folderEl = doc.createElement("Folder");
230 
231                 foldersEl.appendChild(folderEl);
232 
233                 folderEl.setAttribute("name", group.getName());
234             }
235         }
236         else {
237             Group group = GroupServiceUtil.getGroup(
238                 arg.getCompanyId(), arg.getCurrentGroupName());
239 
240             List layouts = LayoutLocalServiceUtil.getLayouts(
241                 group.getGroupId(), false, LayoutImpl.DEFAULT_PARENT_LAYOUT_ID);
242 
243             if (("/" + arg.getCurrentGroupName() + "/").equals(
244                     arg.getCurrentFolder())) {
245 
246                 for (int i = 0; i < layouts.size(); i++) {
247                     Layout layout = (Layout)layouts.get(i);
248 
249                     Element folderEl = doc.createElement("Folder");
250 
251                     foldersEl.appendChild(folderEl);
252 
253                     folderEl.setAttribute(
254                         "name", "~" + _getLayoutName(layout).replace('/', '>'));
255                 }
256             }
257             else {
258                 String layoutName = _getLayoutName(arg.getCurrentFolder());
259 
260                 Layout layout = null;
261 
262                 for (int i = 0; i < layouts.size(); i++) {
263                     layout = _getLayout(layoutName, (Layout)layouts.get(i));
264 
265                     if (layout != null) {
266                         break;
267                     }
268                 }
269 
270                 if (layout != null) {
271                     List layoutChildren = layout.getChildren();
272 
273                     for (int i = 0; i < layoutChildren.size(); i++) {
274                         layout = (Layout)layoutChildren.get(i);
275 
276                         Element folderEl = doc.createElement("Folder");
277 
278                         foldersEl.appendChild(folderEl);
279 
280                         folderEl.setAttribute(
281                             "name",
282                             "~" + _getLayoutName(layout).replace('/', '>'));
283                     }
284                 }
285             }
286         }
287     }
288 
289 }