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