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.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.FileUtil;
30  import com.liferay.portal.kernel.util.ListUtil;
31  import com.liferay.portal.kernel.util.MimeTypesUtil;
32  import com.liferay.portal.model.Image;
33  import com.liferay.portal.security.permission.ActionKeys;
34  import com.liferay.portal.service.ServiceContext;
35  import com.liferay.portlet.imagegallery.model.IGFolder;
36  import com.liferay.portlet.imagegallery.model.IGImage;
37  import com.liferay.portlet.imagegallery.service.base.IGFolderServiceBaseImpl;
38  import com.liferay.portlet.imagegallery.service.permission.IGFolderPermission;
39  
40  import java.io.File;
41  
42  import java.util.Iterator;
43  import java.util.List;
44  
45  /**
46   * <a href="IGFolderServiceImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Jorge Ferrer
50   */
51  public class IGFolderServiceImpl extends IGFolderServiceBaseImpl {
52  
53      public IGFolder addFolder(
54              long parentFolderId, String name, String description,
55              ServiceContext serviceContext)
56          throws PortalException, SystemException {
57  
58          IGFolderPermission.check(
59              getPermissionChecker(), serviceContext.getScopeGroupId(),
60              parentFolderId, ActionKeys.ADD_FOLDER);
61  
62          return igFolderLocalService.addFolder(
63              getUserId(), parentFolderId, name, description, serviceContext);
64      }
65  
66      public IGFolder copyFolder(
67              long sourceFolderId, long parentFolderId, String name,
68              String description, ServiceContext serviceContext)
69          throws PortalException, SystemException {
70  
71          IGFolder srcFolder = getFolder(sourceFolderId);
72  
73          IGFolder destFolder = addFolder(
74              parentFolderId, name, description, serviceContext);
75  
76          copyFolder(srcFolder, destFolder, serviceContext);
77  
78          return destFolder;
79      }
80  
81      public void deleteFolder(long folderId)
82          throws PortalException, SystemException {
83  
84          IGFolderPermission.check(
85              getPermissionChecker(), folderId, ActionKeys.DELETE);
86  
87          igFolderLocalService.deleteFolder(folderId);
88      }
89  
90      public IGFolder getFolder(long folderId)
91          throws PortalException, SystemException {
92  
93          IGFolderPermission.check(
94              getPermissionChecker(), folderId, ActionKeys.VIEW);
95  
96          return igFolderLocalService.getFolder(folderId);
97      }
98  
99      public IGFolder getFolder(long groupId, long parentFolderId, String name)
100         throws PortalException, SystemException {
101 
102         IGFolder folder = igFolderLocalService.getFolder(
103             groupId, parentFolderId, name);
104 
105         IGFolderPermission.check(
106             getPermissionChecker(), folder.getFolderId(), ActionKeys.VIEW);
107 
108         return folder;
109     }
110 
111     public List<IGFolder> getFolders(long groupId, long parentFolderId)
112         throws PortalException, SystemException {
113 
114         List<IGFolder> folders = igFolderLocalService.getFolders(
115             groupId, parentFolderId);
116 
117         folders = ListUtil.copy(folders);
118 
119         Iterator<IGFolder> itr = folders.iterator();
120 
121         while (itr.hasNext()) {
122             IGFolder folder = itr.next();
123 
124             if (!IGFolderPermission.contains(
125                     getPermissionChecker(), folder.getFolderId(),
126                     ActionKeys.VIEW)) {
127 
128                 itr.remove();
129             }
130         }
131 
132         return folders;
133     }
134 
135     public IGFolder updateFolder(
136             long folderId, long parentFolderId, String name, String description,
137             boolean mergeWithParentFolder, ServiceContext serviceContext)
138         throws PortalException, SystemException {
139 
140         IGFolderPermission.check(
141             getPermissionChecker(), folderId, ActionKeys.UPDATE);
142 
143         return igFolderLocalService.updateFolder(
144             folderId, parentFolderId, name, description, mergeWithParentFolder,
145             serviceContext);
146     }
147 
148     protected void copyFolder(
149             IGFolder srcFolder, IGFolder destFolder,
150             ServiceContext serviceContext)
151         throws PortalException, SystemException {
152 
153         List<IGImage> srcImages = igImageService.getImages(
154             srcFolder.getFolderId());
155 
156         for (IGImage srcImage : srcImages) {
157             String name = srcImage.getName();
158             String description = srcImage.getDescription();
159 
160             File file = null;
161 
162             try {
163                 file = FileUtil.createTempFile(srcImage.getImageType());
164 
165                 Image image = imageLocalService.getImage(
166                     srcImage.getLargeImageId());
167 
168                 byte[] bytes = image.getTextObj();
169 
170                 FileUtil.write(file, bytes);
171             }
172             catch (Exception e) {
173                 _log.error(e, e);
174 
175                 continue;
176             }
177 
178             String contentType = MimeTypesUtil.getContentType(
179                 srcImage.getImageType());
180 
181             igImageService.addImage(
182                 destFolder.getFolderId(), name, description, file, contentType,
183                 serviceContext);
184 
185             file.delete();
186         }
187 
188         List<IGFolder> srcSubfolders = getFolders(
189             srcFolder.getGroupId(), srcFolder.getFolderId());
190 
191         for (IGFolder srcSubfolder : srcSubfolders) {
192             String name = srcSubfolder.getName();
193             String description = srcSubfolder.getDescription();
194 
195             serviceContext.setScopeGroupId(srcFolder.getGroupId());
196 
197             IGFolder destSubfolder = addFolder(
198                 destFolder.getFolderId(), name, description, serviceContext);
199 
200             copyFolder(srcSubfolder, destSubfolder, serviceContext);
201         }
202     }
203 
204     private static Log _log = LogFactoryUtil.getLog(IGFolderServiceImpl.class);
205 
206 }