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.portlet.imagegallery.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.FileUtil;
30  import com.liferay.portal.kernel.util.ListUtil;
31  import com.liferay.portal.model.Image;
32  import com.liferay.portal.security.permission.ActionKeys;
33  import com.liferay.portal.util.ContentTypeUtil;
34  import com.liferay.portlet.imagegallery.model.IGFolder;
35  import com.liferay.portlet.imagegallery.model.IGImage;
36  import com.liferay.portlet.imagegallery.service.base.IGFolderServiceBaseImpl;
37  import com.liferay.portlet.imagegallery.service.permission.IGFolderPermission;
38  
39  import java.io.File;
40  
41  import java.rmi.RemoteException;
42  
43  import java.util.Iterator;
44  import java.util.List;
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         folders = ListUtil.copy(folders);
138 
139         Iterator<IGFolder> itr = folders.iterator();
140 
141         while (itr.hasNext()) {
142             IGFolder folder = itr.next();
143 
144             if (!IGFolderPermission.contains(
145                     getPermissionChecker(), folder.getFolderId(),
146                     ActionKeys.VIEW)) {
147 
148                 itr.remove();
149             }
150         }
151 
152         return folders;
153     }
154 
155     public IGFolder updateFolder(
156             long folderId, long parentFolderId, String name, String description,
157             boolean mergeWithParentFolder)
158         throws PortalException, SystemException {
159 
160         IGFolderPermission.check(
161             getPermissionChecker(), folderId, ActionKeys.UPDATE);
162 
163         return igFolderLocalService.updateFolder(
164             folderId, parentFolderId, name, description, mergeWithParentFolder);
165     }
166 
167     protected void copyFolder(
168             IGFolder srcFolder, IGFolder destFolder,
169             boolean addCommunityPermissions, boolean addGuestPermissions)
170         throws PortalException, RemoteException, SystemException {
171 
172         List<IGImage> srcImages = igImageService.getImages(
173             srcFolder.getFolderId());
174 
175         for (IGImage srcImage : srcImages) {
176             String name = srcImage.getName();
177             String description = srcImage.getDescription();
178 
179             File file = null;
180 
181             try {
182                 file = FileUtil.createTempFile(srcImage.getImageType());
183 
184                 Image image = imageLocalService.getImage(
185                     srcImage.getLargeImageId());
186 
187                 byte[] bytes = image.getTextObj();
188 
189                 FileUtil.write(file, bytes);
190             }
191             catch (Exception e) {
192                 _log.error(e, e);
193 
194                 continue;
195             }
196 
197             String contentType = ContentTypeUtil.getContentType(
198                 srcImage.getImageType());
199             String[] tagsEntries = null;
200 
201             igImageService.addImage(
202                 destFolder.getFolderId(), name, description, file, contentType,
203                 tagsEntries, addCommunityPermissions, addGuestPermissions);
204 
205             file.delete();
206         }
207 
208         long destPlid = layoutLocalService.getDefaultPlid(
209             destFolder.getGroupId());
210 
211         List<IGFolder> srcSubfolders = getFolders(
212             srcFolder.getGroupId(), srcFolder.getFolderId());
213 
214         for (IGFolder srcSubfolder : srcSubfolders) {
215             String name = srcSubfolder.getName();
216             String description = srcSubfolder.getDescription();
217 
218             IGFolder destSubfolder = addFolder(
219                 destPlid, destFolder.getFolderId(), name, description,
220                 addCommunityPermissions, addGuestPermissions);
221 
222             copyFolder(
223                 srcSubfolder, destSubfolder, addCommunityPermissions,
224                 addGuestPermissions);
225         }
226     }
227 
228     private static Log _log = LogFactoryUtil.getLog(IGFolderServiceImpl.class);
229 
230 }