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