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.DLFolderConstants;
31  import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
32  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
33  import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
34  
35  import java.io.File;
36  
37  import java.util.List;
38  import java.util.StringTokenizer;
39  
40  import org.w3c.dom.Document;
41  import org.w3c.dom.Element;
42  import org.w3c.dom.Node;
43  
44  /**
45   * <a href="DocumentCommandReceiver.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Ivica Cardic
48   *
49   */
50  public class DocumentCommandReceiver extends BaseCommandReceiver {
51  
52      protected String createFolder(CommandArgument arg) {
53          try {
54              Group group = arg.getCurrentGroup();
55  
56              DLFolder folder = _getFolder(
57                  group.getGroupId(), StringPool.SLASH + arg.getCurrentFolder());
58  
59              long plid = arg.getPlid();
60              long parentFolderId = folder.getFolderId();
61              String name = arg.getNewFolder();
62              String description = StringPool.BLANK;
63              boolean addCommunityPermissions = true;
64              boolean addGuestPermissions = true;
65  
66              DLFolderServiceUtil.addFolder(
67                  plid, parentFolderId, name, description,
68                  addCommunityPermissions, addGuestPermissions);
69          }
70          catch (Exception e) {
71              throw new FCKException(e);
72          }
73  
74          return "0";
75      }
76  
77      protected String fileUpload(
78          CommandArgument arg, String fileName, File file, String extension) {
79  
80          try {
81              Group group = arg.getCurrentGroup();
82  
83              DLFolder folder = _getFolder(
84                  group.getGroupId(), arg.getCurrentFolder());
85  
86              long folderId = folder.getFolderId();
87              String name = fileName;
88              String title = fileName;
89              String description = StringPool.BLANK;
90              String[] tagsEntries = null;
91              String extraSettings = StringPool.BLANK;
92              boolean addCommunityPermissions = true;
93              boolean addGuestPermissions = true;
94  
95              DLFileEntryServiceUtil.addFileEntry(
96                  folderId, name, title, description, tagsEntries, extraSettings,
97                  file, addCommunityPermissions, addGuestPermissions);
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             StringBuilder url = new StringBuilder();
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 
176         if (folderName.equals(StringPool.SLASH)) {
177             return folder;
178         }
179 
180         StringTokenizer st = new StringTokenizer(folderName, StringPool.SLASH);
181 
182         while (st.hasMoreTokens()) {
183             String curFolderName = st.nextToken();
184 
185             List<DLFolder> folders = DLFolderServiceUtil.getFolders(
186                 groupId, folder.getFolderId());
187 
188             for (DLFolder curFolder : folders) {
189                 if (curFolder.getName().equals(curFolderName)) {
190                     folder = curFolder;
191 
192                     break;
193                 }
194             }
195         }
196 
197         return folder;
198     }
199 
200     private void _getFolders(CommandArgument arg, Document doc, Node root)
201         throws Exception {
202 
203         Element foldersEl = doc.createElement("Folders");
204 
205         root.appendChild(foldersEl);
206 
207         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
208             getRootFolders(arg, doc, foldersEl);
209         }
210         else {
211             Group group = arg.getCurrentGroup();
212 
213             DLFolder folder = _getFolder(
214                 group.getGroupId(), arg.getCurrentFolder());
215 
216             List<DLFolder> folders = DLFolderServiceUtil.getFolders(
217                 group.getGroupId(), folder.getFolderId());
218 
219             for (DLFolder curFolder : folders) {
220                 Element folderEl = doc.createElement("Folder");
221 
222                 foldersEl.appendChild(folderEl);
223 
224                 folderEl.setAttribute("name", curFolder.getName());
225             }
226         }
227     }
228 
229 }