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.servlet.ImageServletTokenUtil;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.model.Group;
24  import com.liferay.portal.model.Image;
25  import com.liferay.portal.service.ImageLocalServiceUtil;
26  import com.liferay.portal.service.ServiceContext;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portlet.imagegallery.model.IGFolder;
29  import com.liferay.portlet.imagegallery.model.IGFolderConstants;
30  import com.liferay.portlet.imagegallery.model.IGImage;
31  import com.liferay.portlet.imagegallery.model.impl.IGFolderImpl;
32  import com.liferay.portlet.imagegallery.service.IGFolderServiceUtil;
33  import com.liferay.portlet.imagegallery.service.IGImageServiceUtil;
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="ImageCommandReceiver.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Ivica Cardic
48   */
49  public class ImageCommandReceiver extends BaseCommandReceiver {
50  
51      protected String createFolder(CommandArgument arg) {
52          try {
53              Group group = arg.getCurrentGroup();
54  
55              IGFolder folder = _getFolder(
56                  group.getGroupId(), StringPool.SLASH + arg.getCurrentFolder());
57  
58              long parentFolderId = folder.getFolderId();
59              String name = arg.getNewFolder();
60              String description = StringPool.BLANK;
61  
62              ServiceContext serviceContext = new ServiceContext();
63  
64              serviceContext.setAddCommunityPermissions(true);
65              serviceContext.setAddGuestPermissions(true);
66              serviceContext.setPlid(arg.getPlid());
67              serviceContext.setScopeGroupId(group.getGroupId());
68  
69              IGFolderServiceUtil.addFolder(
70                  parentFolderId, name, description, serviceContext);
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              long groupId = group.getGroupId();
86  
87              IGFolder folder = _getFolder(groupId, arg.getCurrentFolder());
88  
89              long folderId = folder.getFolderId();
90              String name = fileName;
91              String description = StringPool.BLANK;
92              String contentType = extension.toLowerCase();
93  
94              ServiceContext serviceContext = new ServiceContext();
95  
96              serviceContext.setAddCommunityPermissions(true);
97              serviceContext.setAddGuestPermissions(true);
98  
99              IGImageServiceUtil.addImage(
100                 groupId, folderId, name, description, file, contentType,
101                 serviceContext);
102         }
103         catch (Exception e) {
104             throw new FCKException(e);
105         }
106 
107         return "0";
108     }
109 
110     protected void getFolders(CommandArgument arg, Document doc, Node root) {
111         try {
112             _getFolders(arg, doc, root);
113         }
114         catch (Exception e) {
115             throw new FCKException(e);
116         }
117     }
118 
119     protected void getFoldersAndFiles(
120         CommandArgument arg, Document doc, Node root) {
121 
122         try {
123             _getFolders(arg, doc, root);
124             _getFiles(arg, doc, root);
125         }
126         catch (Exception e) {
127             throw new FCKException(e);
128         }
129     }
130 
131     private void _getFiles(CommandArgument arg, Document doc, Node root)
132         throws Exception {
133 
134         Element filesEl = doc.createElement("Files");
135 
136         root.appendChild(filesEl);
137 
138         if (Validator.isNull(arg.getCurrentGroupName())) {
139             return;
140         }
141 
142         Group group = arg.getCurrentGroup();
143 
144         IGFolder folder = _getFolder(
145             group.getGroupId(), arg.getCurrentFolder());
146 
147         List<IGImage> images = IGImageServiceUtil.getImages(
148             folder.getGroupId(), folder.getFolderId());
149 
150         for (IGImage image : images) {
151             long largeImageId = image.getLargeImageId();
152 
153             Image portalImage = ImageLocalServiceUtil.getImageOrDefault(
154                 largeImageId);
155 
156             Element fileEl = doc.createElement("File");
157 
158             filesEl.appendChild(fileEl);
159 
160             fileEl.setAttribute("name", image.getNameWithExtension());
161             fileEl.setAttribute("desc", image.getNameWithExtension());
162             fileEl.setAttribute("size", getSize(portalImage.getSize()));
163 
164             StringBundler url = new StringBundler(7);
165 
166             ThemeDisplay themeDisplay = arg.getThemeDisplay();
167 
168             url.append(themeDisplay.getPathImage());
169             url.append("/image_gallery?uuid=");
170             url.append(image.getUuid());
171             url.append("&groupId=");
172             url.append(folder.getGroupId());
173             url.append("&t=");
174             url.append(ImageServletTokenUtil.getToken(largeImageId));
175 
176             fileEl.setAttribute("url", url.toString());
177         }
178     }
179 
180     private IGFolder _getFolder(long groupId, String folderName)
181         throws Exception {
182 
183         IGFolder folder = new IGFolderImpl();
184 
185         folder.setFolderId(IGFolderConstants.DEFAULT_PARENT_FOLDER_ID);
186 
187         if (folderName.equals(StringPool.SLASH)) {
188             return folder;
189         }
190 
191         StringTokenizer st = new StringTokenizer(folderName, StringPool.SLASH);
192 
193         while (st.hasMoreTokens()) {
194             String curFolderName = st.nextToken();
195 
196             List<IGFolder> folders = IGFolderServiceUtil.getFolders(
197                 groupId, folder.getFolderId());
198 
199             for (IGFolder curFolder : folders) {
200                 if (curFolder.getName().equals(curFolderName)) {
201                     folder = curFolder;
202 
203                     break;
204                 }
205             }
206         }
207 
208         return folder;
209     }
210 
211     private void _getFolders(CommandArgument arg, Document doc, Node root)
212         throws Exception {
213 
214         Element foldersEl = doc.createElement("Folders");
215 
216         root.appendChild(foldersEl);
217 
218         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
219             getRootFolders(arg, doc, foldersEl);
220         }
221         else {
222             Group group = arg.getCurrentGroup();
223 
224             IGFolder folder = _getFolder(
225                 group.getGroupId(), arg.getCurrentFolder());
226 
227             List<IGFolder> folders = IGFolderServiceUtil.getFolders(
228                 group.getGroupId(), folder.getFolderId());
229 
230             for (IGFolder curFolder : folders) {
231                 Element folderEl = doc.createElement("Folder");
232 
233                 foldersEl.appendChild(folderEl);
234 
235                 folderEl.setAttribute("name", curFolder.getName());
236             }
237         }
238     }
239 
240 }