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.util.ListUtil;
28  import com.liferay.portal.security.permission.ActionKeys;
29  import com.liferay.portlet.imagegallery.model.IGImage;
30  import com.liferay.portlet.imagegallery.service.base.IGImageServiceBaseImpl;
31  import com.liferay.portlet.imagegallery.service.permission.IGFolderPermission;
32  import com.liferay.portlet.imagegallery.service.permission.IGImagePermission;
33  
34  import java.io.File;
35  
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /**
40   * <a href="IGImageServiceImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class IGImageServiceImpl extends IGImageServiceBaseImpl {
46  
47      public IGImage addImage(
48              long folderId, String name, String description, File file,
49              String contentType, String[] tagsEntries,
50              boolean addCommunityPermissions, boolean addGuestPermissions)
51          throws PortalException, SystemException {
52  
53          IGFolderPermission.check(
54              getPermissionChecker(), folderId, ActionKeys.ADD_IMAGE);
55  
56          return igImageLocalService.addImage(
57              getUserId(), folderId, name, description, file, contentType,
58              tagsEntries, addCommunityPermissions, addGuestPermissions);
59      }
60  
61      public IGImage addImage(
62              long folderId, String name, String description, File file,
63              String contentType, String[] tagsEntries,
64              String[] communityPermissions, String[] guestPermissions)
65          throws PortalException, SystemException {
66  
67          IGFolderPermission.check(
68              getPermissionChecker(), folderId, ActionKeys.ADD_IMAGE);
69  
70          return igImageLocalService.addImage(
71              getUserId(), folderId, name, description, file, contentType,
72              tagsEntries, communityPermissions, guestPermissions);
73      }
74  
75      public void deleteImage(long imageId)
76          throws PortalException, SystemException {
77  
78          IGImagePermission.check(
79              getPermissionChecker(), imageId, ActionKeys.DELETE);
80  
81          igImageLocalService.deleteImage(imageId);
82      }
83  
84      public void deleteImageByFolderIdAndNameWithExtension(
85              long folderId, String nameWithExtension)
86          throws PortalException, SystemException {
87  
88          IGImage image =
89              igImageLocalService.getImageByFolderIdAndNameWithExtension(
90                  folderId, nameWithExtension);
91  
92          deleteImage(image.getImageId());
93      }
94  
95      public IGImage getImage(long imageId)
96          throws PortalException, SystemException {
97  
98          IGImagePermission.check(
99              getPermissionChecker(), imageId, ActionKeys.VIEW);
100 
101         return igImageLocalService.getImage(imageId);
102     }
103 
104     public IGImage getImageByFolderIdAndNameWithExtension(
105             long folderId, String nameWithExtension)
106         throws PortalException, SystemException {
107 
108         IGImage image =
109             igImageLocalService.getImageByFolderIdAndNameWithExtension(
110                 folderId, nameWithExtension);
111 
112         IGImagePermission.check(
113             getPermissionChecker(), image, ActionKeys.VIEW);
114 
115         return image;
116     }
117 
118     public IGImage getImageByLargeImageId(long largeImageId)
119         throws PortalException, SystemException {
120 
121         IGImage image = igImageLocalService.getImageByLargeImageId(
122             largeImageId);
123 
124         IGImagePermission.check(
125             getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
126 
127         return image;
128     }
129 
130     public IGImage getImageBySmallImageId(long smallImageId)
131         throws PortalException, SystemException {
132 
133         IGImage image = igImageLocalService.getImageBySmallImageId(
134             smallImageId);
135 
136         IGImagePermission.check(
137             getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
138 
139         return image;
140     }
141 
142     public List<IGImage> getImages(long folderId)
143         throws PortalException, SystemException {
144 
145         List<IGImage> images = igImageLocalService.getImages(folderId);
146 
147         images = ListUtil.copy(images);
148 
149         Iterator<IGImage> itr = images.iterator();
150 
151         while (itr.hasNext()) {
152             IGImage image = itr.next();
153 
154             if (!IGImagePermission.contains(
155                     getPermissionChecker(), image, ActionKeys.VIEW)) {
156 
157                 itr.remove();
158             }
159         }
160 
161         return images;
162     }
163 
164     public IGImage updateImage(
165             long imageId, long folderId, String name, String description,
166             File file, String contentType, String[] tagsEntries)
167         throws PortalException, SystemException {
168 
169         IGImagePermission.check(
170             getPermissionChecker(), imageId, ActionKeys.UPDATE);
171 
172         return igImageLocalService.updateImage(
173             getUserId(), imageId, folderId, name, description, file,
174             contentType, tagsEntries);
175     }
176 
177 }