1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.DLFolderConstants;
34  import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
35  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
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  public class DocumentCommandReceiver extends BaseCommandReceiver {
53  
54      protected String createFolder(CommandArgument arg) {
55          try {
56              Group group = arg.getCurrentGroup();
57  
58              DLFolder folder = _getFolder(
59                  group.getGroupId(), StringPool.SLASH + arg.getCurrentFolder());
60  
61              long plid = arg.getPlid();
62              long parentFolderId = folder.getFolderId();
63              String name = arg.getNewFolder();
64              String description = StringPool.BLANK;
65              boolean addCommunityPermissions = true;
66              boolean addGuestPermissions = true;
67  
68              DLFolderServiceUtil.addFolder(
69                  plid, parentFolderId, name, description,
70                  addCommunityPermissions, addGuestPermissions);
71          }
72          catch (Exception e) {
73              throw new FCKException(e);
74          }
75  
76          return "0";
77      }
78  
79      protected String fileUpload(
80          CommandArgument arg, String fileName, File file, String extension) {
81  
82          try {
83              Group group = arg.getCurrentGroup();
84  
85              DLFolder folder = _getFolder(
86                  group.getGroupId(), arg.getCurrentFolder());
87  
88              long folderId = folder.getFolderId();
89              String name = fileName;
90              String title = fileName;
91              String description = StringPool.BLANK;
92              String[] tagsEntries = null;
93              String extraSettings = StringPool.BLANK;
94              boolean addCommunityPermissions = true;
95              boolean addGuestPermissions = true;
96  
97              DLFileEntryServiceUtil.addFileEntry(
98                  folderId, name, title, description, tagsEntries, extraSettings,
99                  file, addCommunityPermissions, addGuestPermissions);
100         }
101         catch (Exception e) {
102             throw new FCKException(e);
103         }
104 
105         return "0";
106     }
107 
108     protected void getFolders(CommandArgument arg, Document doc, Node root) {
109         try {
110             _getFolders(arg, doc, root);
111         }
112         catch (Exception e) {
113             throw new FCKException(e);
114         }
115     }
116 
117     protected void getFoldersAndFiles(
118         CommandArgument arg, Document doc, Node root) {
119 
120         try {
121             _getFolders(arg, doc, root);
122             _getFiles(arg, doc, root);
123         }
124         catch (Exception e) {
125             throw new FCKException(e);
126         }
127     }
128 
129     private void _getFiles(CommandArgument arg, Document doc, Node root)
130         throws Exception {
131 
132         Element filesEl = doc.createElement("Files");
133 
134         root.appendChild(filesEl);
135 
136         if (Validator.isNull(arg.getCurrentGroupName())) {
137             return;
138         }
139 
140         Group group = arg.getCurrentGroup();
141 
142         DLFolder folder = _getFolder(
143             group.getGroupId(), arg.getCurrentFolder());
144 
145         List<DLFileEntry> fileEntries = DLFileEntryServiceUtil.getFileEntries(
146             folder.getFolderId());
147 
148         for (DLFileEntry fileEntry : fileEntries) {
149             Element fileEl = doc.createElement("File");
150 
151             filesEl.appendChild(fileEl);
152 
153             fileEl.setAttribute("name", fileEntry.getTitleWithExtension());
154             fileEl.setAttribute("desc", fileEntry.getTitleWithExtension());
155             fileEl.setAttribute("size", getSize(fileEntry.getSize()));
156 
157             StringBuilder url = new StringBuilder();
158 
159             ThemeDisplay themeDisplay = arg.getThemeDisplay();
160 
161             url.append(themeDisplay.getPathMain());
162             url.append("/document_library/get_file?uuid=");
163             url.append(fileEntry.getUuid());
164             url.append("&groupId=");
165             url.append(folder.getGroupId());
166 
167             fileEl.setAttribute("url", url.toString());
168         }
169     }
170 
171     private DLFolder _getFolder(long groupId, String folderName)
172         throws Exception {
173 
174         DLFolder folder = new DLFolderImpl();
175 
176         folder.setFolderId(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
177 
178         if (folderName.equals(StringPool.SLASH)) {
179             return folder;
180         }
181 
182         StringTokenizer st = new StringTokenizer(folderName, StringPool.SLASH);
183 
184         while (st.hasMoreTokens()) {
185             String curFolderName = st.nextToken();
186 
187             List<DLFolder> folders = DLFolderServiceUtil.getFolders(
188                 groupId, folder.getFolderId());
189 
190             for (DLFolder curFolder : folders) {
191                 if (curFolder.getName().equals(curFolderName)) {
192                     folder = curFolder;
193 
194                     break;
195                 }
196             }
197         }
198 
199         return folder;
200     }
201 
202     private void _getFolders(CommandArgument arg, Document doc, Node root)
203         throws Exception {
204 
205         Element foldersEl = doc.createElement("Folders");
206 
207         root.appendChild(foldersEl);
208 
209         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
210             getRootFolders(arg, doc, foldersEl);
211         }
212         else {
213             Group group = arg.getCurrentGroup();
214 
215             DLFolder folder = _getFolder(
216                 group.getGroupId(), arg.getCurrentFolder());
217 
218             List<DLFolder> folders = DLFolderServiceUtil.getFolders(
219                 group.getGroupId(), folder.getFolderId());
220 
221             for (DLFolder curFolder : folders) {
222                 Element folderEl = doc.createElement("Folder");
223 
224                 foldersEl.appendChild(folderEl);
225 
226                 folderEl.setAttribute("name", curFolder.getName());
227             }
228         }
229     }
230 
231 }