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