1   /**
2    * Copyright (c) 2000-2008 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.portlet.imagegallery.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.FileUtil;
28  import com.liferay.portal.model.Image;
29  import com.liferay.portal.security.permission.ActionKeys;
30  import com.liferay.portal.util.ContentTypeUtil;
31  import com.liferay.portlet.imagegallery.model.IGFolder;
32  import com.liferay.portlet.imagegallery.model.IGImage;
33  import com.liferay.portlet.imagegallery.service.base.IGFolderServiceBaseImpl;
34  import com.liferay.portlet.imagegallery.service.permission.IGFolderPermission;
35  
36  import java.io.File;
37  
38  import java.rmi.RemoteException;
39  
40  import java.util.Iterator;
41  import java.util.List;
42  
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  
46  /**
47   * <a href="IGFolderServiceImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class IGFolderServiceImpl extends IGFolderServiceBaseImpl {
53  
54      public IGFolder addFolder(
55              long plid, long parentFolderId, String name, String description,
56              boolean addCommunityPermissions, boolean addGuestPermissions)
57          throws PortalException, SystemException {
58  
59          IGFolderPermission.check(
60              getPermissionChecker(), plid, parentFolderId,
61              ActionKeys.ADD_FOLDER);
62  
63          return igFolderLocalService.addFolder(
64              getUserId(), plid, parentFolderId, name, description,
65              addCommunityPermissions, addGuestPermissions);
66      }
67  
68      public IGFolder addFolder(
69              long plid, long parentFolderId, String name, String description,
70              String[] communityPermissions, String[] guestPermissions)
71          throws PortalException, SystemException {
72  
73          IGFolderPermission.check(
74              getPermissionChecker(), plid, parentFolderId,
75              ActionKeys.ADD_FOLDER);
76  
77          return igFolderLocalService.addFolder(
78              getUserId(), plid, parentFolderId, name, description,
79              communityPermissions, guestPermissions);
80      }
81  
82      public IGFolder copyFolder(
83              long plid, long sourceFolderId, long parentFolderId, String name,
84              String description, boolean addCommunityPermissions,
85              boolean addGuestPermissions)
86          throws PortalException, RemoteException, SystemException {
87  
88          IGFolder srcFolder = getFolder(sourceFolderId);
89  
90          IGFolder destFolder = addFolder(
91              plid, parentFolderId, name, description, addCommunityPermissions,
92              addGuestPermissions);
93  
94          copyFolder(
95              srcFolder, destFolder, addCommunityPermissions,
96              addGuestPermissions);
97  
98          return destFolder;
99      }
100 
101     public void deleteFolder(long folderId)
102         throws PortalException, SystemException {
103 
104         IGFolderPermission.check(
105             getPermissionChecker(), folderId, ActionKeys.DELETE);
106 
107         igFolderLocalService.deleteFolder(folderId);
108     }
109 
110     public IGFolder getFolder(long folderId)
111         throws PortalException, SystemException {
112 
113         IGFolderPermission.check(
114             getPermissionChecker(), folderId, ActionKeys.VIEW);
115 
116         return igFolderLocalService.getFolder(folderId);
117     }
118 
119     public IGFolder getFolder(long groupId, long parentFolderId, String name)
120         throws PortalException, SystemException {
121 
122         IGFolder folder = igFolderLocalService.getFolder(
123             groupId, parentFolderId, name);
124 
125         IGFolderPermission.check(
126             getPermissionChecker(), folder.getFolderId(), ActionKeys.VIEW);
127 
128         return folder;
129     }
130 
131     public List<IGFolder> getFolders(long groupId, long parentFolderId)
132         throws PortalException, SystemException {
133 
134         List<IGFolder> folders = igFolderLocalService.getFolders(
135             groupId, parentFolderId);
136 
137         Iterator<IGFolder> itr = folders.iterator();
138 
139         while (itr.hasNext()) {
140             IGFolder folder = itr.next();
141 
142             if (!IGFolderPermission.contains(
143                     getPermissionChecker(), folder.getFolderId(),
144                     ActionKeys.VIEW)) {
145 
146                 itr.remove();
147             }
148         }
149 
150         return folders;
151     }
152 
153     public IGFolder updateFolder(
154             long folderId, long parentFolderId, String name, String description,
155             boolean mergeWithParentFolder)
156         throws PortalException, SystemException {
157 
158         IGFolderPermission.check(
159             getPermissionChecker(), folderId, ActionKeys.UPDATE);
160 
161         return igFolderLocalService.updateFolder(
162             folderId, parentFolderId, name, description, mergeWithParentFolder);
163     }
164 
165     protected void copyFolder(
166             IGFolder srcFolder, IGFolder destFolder,
167             boolean addCommunityPermissions, boolean addGuestPermissions)
168         throws PortalException, RemoteException, SystemException {
169 
170         List<IGImage> srcImages = igImageService.getImages(
171             srcFolder.getFolderId());
172 
173         for (IGImage srcImage : srcImages) {
174             String name = srcImage.getName();
175             String description = srcImage.getDescription();
176 
177             File file = null;
178 
179             try {
180                 file = FileUtil.createTempFile(srcImage.getImageType());
181 
182                 Image image = imageLocalService.getImage(
183                     srcImage.getLargeImageId());
184 
185                 byte[] bytes = image.getTextObj();
186 
187                 FileUtil.write(file, bytes);
188             }
189             catch (Exception e) {
190                 _log.error(e, e);
191 
192                 continue;
193             }
194 
195             String contentType = ContentTypeUtil.getContentType(
196                 srcImage.getImageType());
197             String[] tagsEntries = null;
198 
199             igImageService.addImage(
200                 destFolder.getFolderId(), name, description, file, contentType,
201                 tagsEntries, addCommunityPermissions, addGuestPermissions);
202 
203             file.delete();
204         }
205 
206         long destPlid = layoutLocalService.getDefaultPlid(
207             destFolder.getGroupId());
208 
209         List<IGFolder> srcSubfolders = getFolders(
210             srcFolder.getGroupId(), srcFolder.getFolderId());
211 
212         for (IGFolder srcSubfolder : srcSubfolders) {
213             String name = srcSubfolder.getName();
214             String description = srcSubfolder.getDescription();
215 
216             IGFolder destSubfolder = addFolder(
217                 destPlid, destFolder.getFolderId(), name, description,
218                 addCommunityPermissions, addGuestPermissions);
219 
220             copyFolder(
221                 srcSubfolder, destSubfolder, addCommunityPermissions,
222                 addGuestPermissions);
223         }
224     }
225 
226     private static Log _log = LogFactory.getLog(IGFolderServiceImpl.class);
227 
228 }