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