1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.servlet.ImageServletTokenUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Group;
31  import com.liferay.portal.model.Image;
32  import com.liferay.portal.service.ImageLocalServiceUtil;
33  import com.liferay.portal.service.ServiceContext;
34  import com.liferay.portal.theme.ThemeDisplay;
35  import com.liferay.portlet.imagegallery.model.IGFolder;
36  import com.liferay.portlet.imagegallery.model.IGImage;
37  import com.liferay.portlet.imagegallery.model.impl.IGFolderImpl;
38  import com.liferay.portlet.imagegallery.service.IGFolderServiceUtil;
39  import com.liferay.portlet.imagegallery.service.IGImageServiceUtil;
40  
41  import java.io.File;
42  
43  import java.util.List;
44  import java.util.StringTokenizer;
45  
46  import org.w3c.dom.Document;
47  import org.w3c.dom.Element;
48  import org.w3c.dom.Node;
49  
50  /**
51   * <a href="ImageCommandReceiver.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Ivica Cardic
54   *
55   */
56  public class ImageCommandReceiver extends BaseCommandReceiver {
57  
58      protected String createFolder(CommandArgument arg) {
59          try {
60              Group group = arg.getCurrentGroup();
61  
62              IGFolder folder = _getFolder(
63                  group.getGroupId(), StringPool.SLASH + arg.getCurrentFolder());
64  
65              long parentFolderId = folder.getFolderId();
66              String name = arg.getNewFolder();
67              String description = StringPool.BLANK;
68  
69              ServiceContext serviceContext = new ServiceContext();
70  
71              serviceContext.setAddCommunityPermissions(true);
72              serviceContext.setAddGuestPermissions(true);
73              serviceContext.setPlid(arg.getPlid());
74              serviceContext.setScopeGroupId(group.getGroupId());
75  
76              IGFolderServiceUtil.addFolder(
77                  parentFolderId, name, description, serviceContext);
78          }
79          catch (Exception e) {
80              throw new FCKException(e);
81          }
82  
83          return "0";
84      }
85  
86      protected String fileUpload(
87          CommandArgument arg, String fileName, File file, String extension) {
88  
89          try {
90              Group group = arg.getCurrentGroup();
91  
92              IGFolder folder = _getFolder(
93                  group.getGroupId(), arg.getCurrentFolder());
94  
95              long folderId = folder.getFolderId();
96              String name = fileName;
97              String description = StringPool.BLANK;
98              String contentType = extension.toLowerCase();
99  
100             ServiceContext serviceContext = new ServiceContext();
101 
102             serviceContext.setAddCommunityPermissions(true);
103             serviceContext.setAddGuestPermissions(true);
104 
105             IGImageServiceUtil.addImage(
106                 folderId, name, description, file, contentType, serviceContext);
107         }
108         catch (Exception e) {
109             throw new FCKException(e);
110         }
111 
112         return "0";
113     }
114 
115     protected void getFolders(CommandArgument arg, Document doc, Node root) {
116         try {
117             _getFolders(arg, doc, root);
118         }
119         catch (Exception e) {
120             throw new FCKException(e);
121         }
122     }
123 
124     protected void getFoldersAndFiles(
125         CommandArgument arg, Document doc, Node root) {
126 
127         try {
128             _getFolders(arg, doc, root);
129             _getFiles(arg, doc, root);
130         }
131         catch (Exception e) {
132             throw new FCKException(e);
133         }
134     }
135 
136     private void _getFiles(CommandArgument arg, Document doc, Node root)
137         throws Exception {
138 
139         Element filesEl = doc.createElement("Files");
140 
141         root.appendChild(filesEl);
142 
143         if (Validator.isNull(arg.getCurrentGroupName())) {
144             return;
145         }
146 
147         Group group = arg.getCurrentGroup();
148 
149         IGFolder folder = _getFolder(
150             group.getGroupId(), arg.getCurrentFolder());
151 
152         List<IGImage> images = IGImageServiceUtil.getImages(
153             folder.getFolderId());
154 
155         for (IGImage image : images) {
156             long largeImageId = image.getLargeImageId();
157 
158             Image portalImage = ImageLocalServiceUtil.getImageOrDefault(
159                 largeImageId);
160 
161             Element fileEl = doc.createElement("File");
162 
163             filesEl.appendChild(fileEl);
164 
165             fileEl.setAttribute("name", image.getNameWithExtension());
166             fileEl.setAttribute("desc", image.getNameWithExtension());
167             fileEl.setAttribute("size", getSize(portalImage.getSize()));
168 
169             StringBuilder url = new StringBuilder();
170 
171             ThemeDisplay themeDisplay = arg.getThemeDisplay();
172 
173             url.append(themeDisplay.getPathImage());
174             url.append("/image_gallery?uuid=");
175             url.append(image.getUuid());
176             url.append("&groupId=");
177             url.append(folder.getGroupId());
178             url.append("&t=");
179             url.append(ImageServletTokenUtil.getToken(largeImageId));
180 
181             fileEl.setAttribute("url", url.toString());
182         }
183     }
184 
185     private IGFolder _getFolder(long groupId, String folderName)
186         throws Exception {
187 
188         IGFolder folder = new IGFolderImpl();
189 
190         folder.setFolderId(IGFolderImpl.DEFAULT_PARENT_FOLDER_ID);
191 
192         if (folderName.equals(StringPool.SLASH)) {
193             return folder;
194         }
195 
196         StringTokenizer st = new StringTokenizer(folderName, StringPool.SLASH);
197 
198         while (st.hasMoreTokens()) {
199             String curFolderName = st.nextToken();
200 
201             List<IGFolder> folders = IGFolderServiceUtil.getFolders(
202                 groupId, folder.getFolderId());
203 
204             for (IGFolder curFolder : folders) {
205                 if (curFolder.getName().equals(curFolderName)) {
206                     folder = curFolder;
207 
208                     break;
209                 }
210             }
211         }
212 
213         return folder;
214     }
215 
216     private void _getFolders(CommandArgument arg, Document doc, Node root)
217         throws Exception {
218 
219         Element foldersEl = doc.createElement("Folders");
220 
221         root.appendChild(foldersEl);
222 
223         if (arg.getCurrentFolder().equals(StringPool.SLASH)) {
224             getRootFolders(arg, doc, foldersEl);
225         }
226         else {
227             Group group = arg.getCurrentGroup();
228 
229             IGFolder folder = _getFolder(
230                 group.getGroupId(), arg.getCurrentFolder());
231 
232             List<IGFolder> folders = IGFolderServiceUtil.getFolders(
233                 group.getGroupId(), folder.getFolderId());
234 
235             for (IGFolder curFolder : folders) {
236                 Element folderEl = doc.createElement("Folder");
237 
238                 foldersEl.appendChild(folderEl);
239 
240                 folderEl.setAttribute("name", curFolder.getName());
241             }
242         }
243     }
244 
245 }