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