1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.editor.fckeditor.receiver.impl;
16  
17  import com.liferay.portal.editor.fckeditor.command.CommandArgument;
18  import com.liferay.portal.editor.fckeditor.exception.FCKException;
19  import com.liferay.portal.kernel.util.StringBundler;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.model.Group;
23  import com.liferay.portal.service.ServiceContext;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
26  import com.liferay.portlet.documentlibrary.model.DLFolder;
27  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
28  import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
29  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
30  import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
31  
32  import java.io.File;
33  
34  import java.util.List;
35  import java.util.StringTokenizer;
36  
37  import org.w3c.dom.Document;
38  import org.w3c.dom.Element;
39  import org.w3c.dom.Node;
40  
41  /**
42   * <a href="DocumentCommandReceiver.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Ivica Cardic
45   */
46  public class DocumentCommandReceiver extends BaseCommandReceiver {
47  
48      protected String createFolder(CommandArgument arg) {
49          try {
50              Group group = arg.getCurrentGroup();
51  
52              DLFolder folder = _getFolder(
53                  group.getGroupId(), StringPool.SLASH + arg.getCurrentFolder());
54  
55              long parentFolderId = folder.getFolderId();
56              String name = arg.getNewFolder();
57              String description = StringPool.BLANK;
58  
59              ServiceContext serviceContext = new ServiceContext();
60  
61              serviceContext.setAddCommunityPermissions(true);
62              serviceContext.setAddGuestPermissions(true);
63  
64              DLFolderServiceUtil.addFolder(
65                  group.getGroupId(), parentFolderId, name, description,
66                  serviceContext);
67          }
68          catch (Exception e) {
69              throw new FCKException(e);
70          }
71  
72          return "0";
73      }
74  
75      protected String fileUpload(
76          CommandArgument arg, String fileName, File file, String extension) {
77  
78          try {
79              Group group = arg.getCurrentGroup();
80  
81              DLFolder folder = _getFolder(
82                  group.getGroupId(), arg.getCurrentFolder());
83  
84              long folderId = folder.getFolderId();
85              String name = fileName;
86              String title = fileName;
87              String description = StringPool.BLANK;
88              String versionDescription = StringPool.BLANK;
89              String extraSettings = StringPool.BLANK;
90  
91              ServiceContext serviceContext = new ServiceContext();
92  
93              serviceContext.setAddCommunityPermissions(true);
94              serviceContext.setAddGuestPermissions(true);
95  
96              DLFileEntryServiceUtil.addFileEntry(
97                  group.getGroupId(), folderId, name, title, description,
98                  versionDescription, extraSettings, file, serviceContext);
99          }
100         catch (Exception e) {
101             throw new FCKException(e);
102         }
103 
104         return "0";
105     }
106 
107     protected void getFolders(CommandArgument arg, Document doc, Node root) {
108         try {
109             _getFolders(arg, doc, root);
110         }
111         catch (Exception e) {
112             throw new FCKException(e);
113         }
114     }
115 
116     protected void getFoldersAndFiles(
117         CommandArgument arg, Document doc, Node root) {
118 
119         try {
120             _getFolders(arg, doc, root);
121             _getFiles(arg, doc, root);
122         }
123         catch (Exception e) {
124             throw new FCKException(e);
125         }
126     }
127 
128     private void _getFiles(CommandArgument arg, Document doc, Node root)
129         throws Exception {
130 
131         Element filesEl = doc.createElement("Files");
132 
133         root.appendChild(filesEl);
134 
135         if (Validator.isNull(arg.getCurrentGroupName())) {
136             return;
137         }
138 
139         Group group = arg.getCurrentGroup();
140 
141         DLFolder folder = _getFolder(
142             group.getGroupId(), arg.getCurrentFolder());
143 
144         List<DLFileEntry> fileEntries = DLFileEntryServiceUtil.getFileEntries(
145             folder.getGroupId(), folder.getFolderId());
146 
147         for (DLFileEntry fileEntry : fileEntries) {
148             Element fileEl = doc.createElement("File");
149 
150             filesEl.appendChild(fileEl);
151 
152             fileEl.setAttribute("name", fileEntry.getTitle());
153             fileEl.setAttribute("desc", fileEntry.getTitle());
154             fileEl.setAttribute("size", getSize(fileEntry.getSize()));
155 
156             StringBundler url = new StringBundler(5);
157 
158             ThemeDisplay themeDisplay = arg.getThemeDisplay();
159 
160             url.append(themeDisplay.getPathMain());
161             url.append("/document_library/get_file?uuid=");
162             url.append(fileEntry.getUuid());
163             url.append("&groupId=");
164             url.append(folder.getGroupId());
165 
166             fileEl.setAttribute("url", url.toString());
167         }
168     }
169 
170     private DLFolder _getFolder(long groupId, String folderName)
171         throws Exception {
172 
173         DLFolder folder = new DLFolderImpl();
174 
175         folder.setFolderId(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
176 
177         if (folderName.equals(StringPool.SLASH)) {
178             return folder;
179         }
180 
181         StringTokenizer st = new StringTokenizer(folderName, StringPool.SLASH);
182 
183         while (st.hasMoreTokens()) {
184             String curFolderName = st.nextToken();
185 
186             List<DLFolder> folders = DLFolderServiceUtil.getFolders(
187                 groupId, folder.getFolderId());
188 
189             for (DLFolder curFolder : folders) {
190                 if (curFolder.getName().equals(curFolderName)) {
191                     folder = curFolder;
192 
193                     break;
194                 }
195             }
196         }
197 
198         return folder;
199     }
200 
201     private void _getFolders(CommandArgument arg, Document doc, Node root)
202         throws Exception {
203 
204         Element foldersEl = doc.createElement("Folders");
205 
206         root.appendChild(foldersEl);
207 
208         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
209             getRootFolders(arg, doc, foldersEl);
210         }
211         else {
212             Group group = arg.getCurrentGroup();
213 
214             DLFolder folder = _getFolder(
215                 group.getGroupId(), arg.getCurrentFolder());
216 
217             List<DLFolder> folders = DLFolderServiceUtil.getFolders(
218                 group.getGroupId(), folder.getFolderId());
219 
220             for (DLFolder curFolder : folders) {
221                 Element folderEl = doc.createElement("Folder");
222 
223                 foldersEl.appendChild(folderEl);
224 
225                 folderEl.setAttribute("name", curFolder.getName());
226             }
227         }
228     }
229 
230 }