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.HttpUtil;
28  import com.liferay.portal.kernel.util.StringMaker;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Group;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
34  import com.liferay.portlet.documentlibrary.model.DLFolder;
35  import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
36  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
37  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
38  import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
39  
40  import java.io.File;
41  
42  import java.util.List;
43  import java.util.StringTokenizer;
44  
45  import org.w3c.dom.Document;
46  import org.w3c.dom.Element;
47  import org.w3c.dom.Node;
48  
49  /**
50   * <a href="DocumentCommandReceiver.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Ivica Cardic
53   *
54   */
55  public class DocumentCommandReceiver extends BaseCommandReceiver {
56  
57      protected String createFolder(CommandArgument arg) {
58          try {
59              Group group = arg.getCurrentGroup();
60  
61              DLFolder folder = _getFolder(
62                  group.getGroupId(), StringPool.SLASH + arg.getCurrentFolder());
63  
64              long plid = arg.getPlid();
65              long parentFolderId = folder.getFolderId();
66              String name = arg.getNewFolder();
67              String description = StringPool.BLANK;
68              boolean addCommunityPermissions = true;
69              boolean addGuestPermissions = true;
70  
71              DLFolderServiceUtil.addFolder(
72                  plid, parentFolderId, name, description,
73                  addCommunityPermissions, addGuestPermissions);
74          }
75          catch (Exception e) {
76              throw new FCKException(e);
77          }
78  
79          return "0";
80      }
81  
82      protected String fileUpload(
83          CommandArgument arg, String fileName, File file, String extension) {
84  
85          try {
86              Group group = arg.getCurrentGroup();
87  
88              DLFolder folder = _getFolder(
89                  group.getGroupId(), arg.getCurrentFolder());
90  
91              long folderId = folder.getFolderId();
92              String name = fileName;
93              String title = fileName;
94              String description = StringPool.BLANK;
95              String[] tagsEntries = null;
96              String extraSettings = StringPool.BLANK;
97              boolean addCommunityPermissions = true;
98              boolean addGuestPermissions = true;
99  
100             DLFileEntryServiceUtil.addFileEntry(
101                 folderId, name, title, description, tagsEntries, extraSettings,
102                 file, addCommunityPermissions, addGuestPermissions);
103         }
104         catch (Exception e) {
105             throw new FCKException(e);
106         }
107 
108         return "0";
109     }
110 
111     protected void getFolders(CommandArgument arg, Document doc, Node root) {
112         try {
113             _getFolders(arg, doc, root);
114         }
115         catch (Exception e) {
116             throw new FCKException(e);
117         }
118     }
119 
120     protected void getFoldersAndFiles(
121         CommandArgument arg, Document doc, Node root) {
122 
123         try {
124             _getFolders(arg, doc, root);
125             _getFiles(arg, doc, root);
126         }
127         catch (Exception e) {
128             throw new FCKException(e);
129         }
130     }
131 
132     private void _getFiles(CommandArgument arg, Document doc, Node root)
133         throws Exception {
134 
135         Element filesEl = doc.createElement("Files");
136 
137         root.appendChild(filesEl);
138 
139         if (Validator.isNull(arg.getCurrentGroupName())) {
140             return;
141         }
142 
143         Group group = arg.getCurrentGroup();
144 
145         DLFolder folder = _getFolder(
146             group.getGroupId(), arg.getCurrentFolder());
147 
148         List<DLFileEntry> fileEntries = DLFileEntryServiceUtil.getFileEntries(
149             folder.getFolderId());
150 
151         for (DLFileEntry fileEntry : fileEntries) {
152             Element fileEl = doc.createElement("File");
153 
154             filesEl.appendChild(fileEl);
155 
156             fileEl.setAttribute("name", fileEntry.getTitleWithExtension());
157             fileEl.setAttribute("desc", fileEntry.getTitleWithExtension());
158             fileEl.setAttribute("size", getSize(fileEntry.getSize()));
159 
160             StringMaker url = new StringMaker();
161 
162             ThemeDisplay themeDisplay = arg.getThemeDisplay();
163 
164             url.append(themeDisplay.getPathMain());
165             url.append("/document_library/get_file?folderId=");
166             url.append(fileEntry.getFolderId());
167             url.append("&name=");
168             url.append(HttpUtil.encodeURL(fileEntry.getName()));
169 
170             fileEl.setAttribute("url", url.toString());
171         }
172     }
173 
174     private DLFolder _getFolder(long groupId, String folderName)
175         throws Exception {
176 
177         DLFolder folder = new DLFolderImpl();
178 
179         folder.setFolderId(DLFolderImpl.DEFAULT_PARENT_FOLDER_ID);
180 
181         if (folderName.equals(StringPool.SLASH)) {
182             return folder;
183         }
184 
185         StringTokenizer st = new StringTokenizer(folderName, StringPool.SLASH);
186 
187         while (st.hasMoreTokens()) {
188             String curFolderName = st.nextToken();
189 
190             List<DLFolder> folders = DLFolderLocalServiceUtil.getFolders(
191                 groupId, folder.getFolderId());
192 
193             for (DLFolder curFolder : folders) {
194                 if (curFolder.getName().equals(curFolderName)) {
195                     folder = curFolder;
196 
197                     break;
198                 }
199             }
200         }
201 
202         return folder;
203     }
204 
205     private void _getFolders(CommandArgument arg, Document doc, Node root)
206         throws Exception {
207 
208         Element foldersEl = doc.createElement("Folders");
209 
210         root.appendChild(foldersEl);
211 
212         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
213             getRootFolders(arg, doc, foldersEl);
214         }
215         else {
216             Group group = arg.getCurrentGroup();
217 
218             DLFolder folder = _getFolder(
219                 group.getGroupId(), arg.getCurrentFolder());
220 
221             List<DLFolder> folders = DLFolderLocalServiceUtil.getFolders(
222                 group.getGroupId(), folder.getFolderId());
223 
224             for (DLFolder curFolder : folders) {
225                 Element folderEl = doc.createElement("Folder");
226 
227                 foldersEl.appendChild(folderEl);
228 
229                 folderEl.setAttribute("name", curFolder.getName());
230             }
231         }
232     }
233 
234 }