001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.imagegallery.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portlet.imagegallery.model.IGImage;
023    import com.liferay.portlet.imagegallery.service.base.IGImageServiceBaseImpl;
024    import com.liferay.portlet.imagegallery.service.permission.IGFolderPermission;
025    import com.liferay.portlet.imagegallery.service.permission.IGImagePermission;
026    
027    import java.io.File;
028    
029    import java.util.Iterator;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Alexander Chow
035     */
036    public class IGImageServiceImpl extends IGImageServiceBaseImpl {
037    
038            public IGImage addImage(
039                            long groupId, long folderId, String name, String description,
040                            File file, String contentType, ServiceContext serviceContext)
041                    throws PortalException, SystemException {
042    
043                    IGFolderPermission.check(
044                            getPermissionChecker(), groupId, folderId, ActionKeys.ADD_IMAGE);
045    
046                    return igImageLocalService.addImage(
047                            getUserId(), groupId, folderId, name, description, file,
048                            contentType, serviceContext);
049            }
050    
051            public void deleteImage(long imageId)
052                    throws PortalException, SystemException {
053    
054                    IGImagePermission.check(
055                            getPermissionChecker(), imageId, ActionKeys.DELETE);
056    
057                    igImageLocalService.deleteImage(imageId);
058            }
059    
060            public void deleteImageByFolderIdAndNameWithExtension(
061                            long groupId, long folderId, String nameWithExtension)
062                    throws PortalException, SystemException {
063    
064                    IGImage image =
065                            igImageLocalService.getImageByFolderIdAndNameWithExtension(
066                                    groupId, folderId, nameWithExtension);
067    
068                    deleteImage(image.getImageId());
069            }
070    
071            public IGImage getImage(long imageId)
072                    throws PortalException, SystemException {
073    
074                    IGImagePermission.check(
075                            getPermissionChecker(), imageId, ActionKeys.VIEW);
076    
077                    return igImageLocalService.getImage(imageId);
078            }
079    
080            public IGImage getImageByFolderIdAndNameWithExtension(
081                            long groupId, long folderId, String nameWithExtension)
082                    throws PortalException, SystemException {
083    
084                    IGImage image =
085                            igImageLocalService.getImageByFolderIdAndNameWithExtension(
086                                    groupId, folderId, nameWithExtension);
087    
088                    IGImagePermission.check(
089                            getPermissionChecker(), image, ActionKeys.VIEW);
090    
091                    return image;
092            }
093    
094            public IGImage getImageByLargeImageId(long largeImageId)
095                    throws PortalException, SystemException {
096    
097                    IGImage image = igImageLocalService.getImageByLargeImageId(
098                            largeImageId);
099    
100                    IGImagePermission.check(
101                            getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
102    
103                    return image;
104            }
105    
106            public IGImage getImageBySmallImageId(long smallImageId)
107                    throws PortalException, SystemException {
108    
109                    IGImage image = igImageLocalService.getImageBySmallImageId(
110                            smallImageId);
111    
112                    IGImagePermission.check(
113                            getPermissionChecker(), image.getImageId(), ActionKeys.VIEW);
114    
115                    return image;
116            }
117    
118            public List<IGImage> getImages(long groupId, long folderId)
119                    throws PortalException, SystemException {
120    
121                    List<IGImage> images = igImageLocalService.getImages(groupId, folderId);
122    
123                    images = ListUtil.copy(images);
124    
125                    Iterator<IGImage> itr = images.iterator();
126    
127                    while (itr.hasNext()) {
128                            IGImage image = itr.next();
129    
130                            if (!IGImagePermission.contains(
131                                            getPermissionChecker(), image, ActionKeys.VIEW)) {
132    
133                                    itr.remove();
134                            }
135                    }
136    
137                    return images;
138            }
139    
140            public IGImage updateImage(
141                            long imageId, long groupId, long folderId, String name,
142                            String description, File file, String contentType,
143                            ServiceContext serviceContext)
144                    throws PortalException, SystemException {
145    
146                    IGImagePermission.check(
147                            getPermissionChecker(), imageId, ActionKeys.UPDATE);
148    
149                    return igImageLocalService.updateImage(
150                            getUserId(), imageId, groupId, folderId, name, description, file,
151                            contentType, serviceContext);
152            }
153    
154    }