1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.StringPool;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.model.LayoutConstants;
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  public class PageCommandReceiver extends BaseCommandReceiver {
48  
49      protected String createFolder(CommandArgument arg) {
50          return "0";
51      }
52  
53      protected String fileUpload(
54          CommandArgument arg, String fileName, File file, String extension) {
55  
56          return "0";
57      }
58  
59      protected void getFolders(CommandArgument arg, Document doc, Node root) {
60          try {
61              _getFolders(arg, doc, root);
62          }
63          catch (Exception e) {
64              throw new FCKException(e);
65          }
66      }
67  
68      protected void getFoldersAndFiles(
69          CommandArgument arg, Document doc, Node root) {
70  
71          try {
72              _getFolders(arg, doc, root);
73              _getFiles(arg, doc, root);
74          }
75          catch (Exception e) {
76              throw new FCKException(e);
77          }
78      }
79  
80      private Layout _getLayout(String layoutName, Layout layout)
81          throws Exception {
82  
83          String friendlyURL = layout.getFriendlyURL();
84  
85          if (layoutName.equals(friendlyURL)) {
86              return layout;
87          }
88  
89          List<Layout> layoutChildren = layout.getChildren();
90  
91          if (layoutChildren.size() == 0) {
92              return null;
93          }
94          else {
95              for (Layout layoutChild : layoutChildren) {
96                  Layout currentLayout = _getLayout(layoutName, layoutChild);
97  
98                  if (currentLayout != null) {
99                      return currentLayout;
100                 }
101             }
102         }
103 
104         return null;
105     }
106 
107     private String _getLayoutName(Layout layout) {
108         return layout.getFriendlyURL();
109     }
110 
111     private String _getLayoutName(String folderName) {
112         String layoutName = folderName.substring(
113             folderName.lastIndexOf('~') + 1, folderName.length() - 1);
114 
115         layoutName = layoutName.replace('>', '/');
116 
117         return layoutName;
118     }
119 
120     private void _getFiles(CommandArgument arg, Document doc, Node root)
121         throws Exception {
122 
123         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
124             return;
125         }
126 
127         Element filesEl = doc.createElement("Files");
128 
129         root.appendChild(filesEl);
130 
131         Group group = arg.getCurrentGroup();
132 
133         List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
134             group.getGroupId(), false,
135             LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
136 
137         if (("/" + arg.getCurrentGroupName() + "/").equals(
138                 arg.getCurrentFolder())) {
139 
140             for (Layout layout : layouts) {
141                 Element fileEl = doc.createElement("File");
142 
143                 filesEl.appendChild(fileEl);
144 
145                 fileEl.setAttribute("name", _getLayoutName(layout));
146                 fileEl.setAttribute("desc", _getLayoutName(layout));
147                 fileEl.setAttribute("size", StringPool.BLANK);
148                 fileEl.setAttribute(
149                     "url",
150                     PortalUtil.getLayoutURL(
151                         layout,arg.getThemeDisplay(), false));
152             }
153         }
154         else {
155             String layoutName = _getLayoutName(arg.getCurrentFolder());
156 
157             Layout layout = null;
158 
159             for (int i = 0; i < layouts.size(); i++) {
160                 layout = _getLayout(layoutName, layouts.get(i));
161 
162                 if (layout != null) {
163                     break;
164                 }
165             }
166 
167             if (layout == null) {
168                 return;
169             }
170 
171             List<Layout> layoutChildren = layout.getChildren();
172 
173             for (int i = 0; i < layoutChildren.size(); i++) {
174                 layout = layoutChildren.get(i);
175 
176                 Element fileEl = doc.createElement("File");
177 
178                 filesEl.appendChild(fileEl);
179 
180                 fileEl.setAttribute("name", _getLayoutName(layout));
181                 fileEl.setAttribute("desc", _getLayoutName(layout));
182                 fileEl.setAttribute("size", getSize());
183                 fileEl.setAttribute(
184                     "url",
185                     PortalUtil.getLayoutURL(
186                         layout, arg.getThemeDisplay(), false));
187             }
188         }
189     }
190 
191     private void _getFolders(CommandArgument arg, Document doc, Node root)
192         throws Exception {
193 
194         Element foldersEl = doc.createElement("Folders");
195 
196         root.appendChild(foldersEl);
197 
198         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
199             getRootFolders(arg, doc, foldersEl);
200         }
201         else {
202             Group group = arg.getCurrentGroup();
203 
204             List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
205                 group.getGroupId(), false,
206                 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
207 
208             if (("/" + arg.getCurrentGroupName() + "/").equals(
209                     arg.getCurrentFolder())) {
210 
211                 for (Layout layout : layouts) {
212                     Element folderEl = doc.createElement("Folder");
213 
214                     foldersEl.appendChild(folderEl);
215 
216                     folderEl.setAttribute(
217                         "name", "~" + _getLayoutName(layout).replace('/', '>'));
218                 }
219             }
220             else {
221                 String layoutName = _getLayoutName(arg.getCurrentFolder());
222 
223                 Layout layout = null;
224 
225                 for (int i = 0; i < layouts.size(); i++) {
226                     layout = _getLayout(layoutName, layouts.get(i));
227 
228                     if (layout != null) {
229                         break;
230                     }
231                 }
232 
233                 if (layout != null) {
234                     List<Layout> layoutChildren = layout.getChildren();
235 
236                     for (int i = 0; i < layoutChildren.size(); i++) {
237                         layout = layoutChildren.get(i);
238 
239                         Element folderEl = doc.createElement("Folder");
240 
241                         foldersEl.appendChild(folderEl);
242 
243                         folderEl.setAttribute(
244                             "name",
245                             "~" + _getLayoutName(layout).replace('/', '>'));
246                     }
247                 }
248             }
249         }
250     }
251 
252 }