1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.portal.service.ServiceContext;
30  import com.liferay.portlet.imagegallery.model.IGImage;
31  import com.liferay.portlet.imagegallery.service.base.IGImageServiceBaseImpl;
32  import com.liferay.portlet.imagegallery.service.permission.IGFolderPermission;
33  import com.liferay.portlet.imagegallery.service.permission.IGImagePermission;
34  
35  import java.io.File;
36  
37  import java.util.Iterator;
38  import java.util.List;
39  
40  /**
41   * <a href="IGImageServiceImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class IGImageServiceImpl extends IGImageServiceBaseImpl {
46  
47      public IGImage addImage(
48              long folderId, String name, String description, File file,
49              String contentType, ServiceContext serviceContext)
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              serviceContext);
58      }
59  
60      public void deleteImage(long imageId)
61          throws PortalException, SystemException {
62  
63          IGImagePermission.check(
64              getPermissionChecker(), imageId, ActionKeys.DELETE);
65  
66          igImageLocalService.deleteImage(imageId);
67      }
68  
69      public void deleteImageByFolderIdAndNameWithExtension(
70              long folderId, String nameWithExtension)
71          throws PortalException, SystemException {
72  
73          IGImage image =
74              igImageLocalService.getImageByFolderIdAndNameWithExtension(
75                  folderId, nameWithExtension);
76  
77          deleteImage(image.getImageId());
78      }
79  
80      public IGImage getImage(long imageId)
81          throws PortalException, SystemException {
82  
83          IGImagePermission.check(
84              getPermissionChecker(), imageId, ActionKeys.VIEW);
85  
86          return igImageLocalService.getImage(imageId);
87      }
88  
89      public IGImage getImageByFolderIdAndNameWithExtension(
90              long folderId, String nameWithExtension)
91          throws PortalException, SystemException {
92  
93          IGImage image =
94              igImageLocalService.getImageByFolderIdAndNameWithExtension(
95                  folderId, nameWithExtension);
96  
97          IGImagePermission.check(
98              getPermissionChecker(), image, ActionKeys.VIEW);
99  
100         return image;
101     }
102 
103     public IGImage getImageByLargeImageId(long largeImageId)
104         throws PortalException, SystemException {
105 
106         IGImage image = igImageLocalService.getImageByLargeImageId(
107             largeImageId);
108 
109         IGImagePermission.check(
110             getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
111 
112         return image;
113     }
114 
115     public IGImage getImageBySmallImageId(long smallImageId)
116         throws PortalException, SystemException {
117 
118         IGImage image = igImageLocalService.getImageBySmallImageId(
119             smallImageId);
120 
121         IGImagePermission.check(
122             getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
123 
124         return image;
125     }
126 
127     public List<IGImage> getImages(long folderId)
128         throws PortalException, SystemException {
129 
130         List<IGImage> images = igImageLocalService.getImages(folderId);
131 
132         images = ListUtil.copy(images);
133 
134         Iterator<IGImage> itr = images.iterator();
135 
136         while (itr.hasNext()) {
137             IGImage image = itr.next();
138 
139             if (!IGImagePermission.contains(
140                     getPermissionChecker(), image, ActionKeys.VIEW)) {
141 
142                 itr.remove();
143             }
144         }
145 
146         return images;
147     }
148 
149     public IGImage updateImage(
150             long imageId, long folderId, String name, String description,
151             File file, String contentType, ServiceContext serviceContext)
152         throws PortalException, SystemException {
153 
154         IGImagePermission.check(
155             getPermissionChecker(), imageId, ActionKeys.UPDATE);
156 
157         return igImageLocalService.updateImage(
158             getUserId(), imageId, folderId, name, description, file,
159             contentType, serviceContext);
160     }
161 
162 }