1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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 extraSettings = StringPool.BLANK;
89  
90              ServiceContext serviceContext = new ServiceContext();
91  
92              serviceContext.setAddCommunityPermissions(true);
93              serviceContext.setAddGuestPermissions(true);
94  
95              DLFileEntryServiceUtil.addFileEntry(
96                  folderId, name, title, description, extraSettings, file,
97                  serviceContext);
98          }
99          catch (Exception e) {
100             throw new FCKException(e);
101         }
102 
103         return "0";
104     }
105 
106     protected void getFolders(CommandArgument arg, Document doc, Node root) {
107         try {
108             _getFolders(arg, doc, root);
109         }
110         catch (Exception e) {
111             throw new FCKException(e);
112         }
113     }
114 
115     protected void getFoldersAndFiles(
116         CommandArgument arg, Document doc, Node root) {
117 
118         try {
119             _getFolders(arg, doc, root);
120             _getFiles(arg, doc, root);
121         }
122         catch (Exception e) {
123             throw new FCKException(e);
124         }
125     }
126 
127     private void _getFiles(CommandArgument arg, Document doc, Node root)
128         throws Exception {
129 
130         Element filesEl = doc.createElement("Files");
131 
132         root.appendChild(filesEl);
133 
134         if (Validator.isNull(arg.getCurrentGroupName())) {
135             return;
136         }
137 
138         Group group = arg.getCurrentGroup();
139 
140         DLFolder folder = _getFolder(
141             group.getGroupId(), arg.getCurrentFolder());
142 
143         List<DLFileEntry> fileEntries = DLFileEntryServiceUtil.getFileEntries(
144             folder.getFolderId());
145 
146         for (DLFileEntry fileEntry : fileEntries) {
147             Element fileEl = doc.createElement("File");
148 
149             filesEl.appendChild(fileEl);
150 
151             fileEl.setAttribute("name", fileEntry.getTitleWithExtension());
152             fileEl.setAttribute("desc", fileEntry.getTitleWithExtension());
153             fileEl.setAttribute("size", getSize(fileEntry.getSize()));
154 
155             StringBundler url = new StringBundler(5);
156 
157             ThemeDisplay themeDisplay = arg.getThemeDisplay();
158 
159             url.append(themeDisplay.getPathMain());
160             url.append("/document_library/get_file?uuid=");
161             url.append(fileEntry.getUuid());
162             url.append("&groupId=");
163             url.append(folder.getGroupId());
164 
165             fileEl.setAttribute("url", url.toString());
166         }
167     }
168 
169     private DLFolder _getFolder(long groupId, String folderName)
170         throws Exception {
171 
172         DLFolder folder = new DLFolderImpl();
173 
174         folder.setFolderId(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
175         folder.setGroupId(groupId);
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 }