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