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.documentlibrary.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.FileUtil;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.security.permission.ActionKeys;
30  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
31  import com.liferay.portlet.documentlibrary.model.DLFolder;
32  import com.liferay.portlet.documentlibrary.service.base.DLFolderServiceBaseImpl;
33  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
34  
35  import java.io.File;
36  import java.io.InputStream;
37  
38  import java.rmi.RemoteException;
39  
40  import java.util.Iterator;
41  import java.util.List;
42  
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  
46  /**
47   * <a href="DLFolderServiceImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class DLFolderServiceImpl extends DLFolderServiceBaseImpl {
53  
54      public DLFolder addFolder(
55              long plid, long parentFolderId, String name, String description,
56              boolean addCommunityPermissions, boolean addGuestPermissions)
57          throws PortalException, SystemException {
58  
59          DLFolderPermission.check(
60              getPermissionChecker(), plid, parentFolderId,
61              ActionKeys.ADD_FOLDER);
62  
63          return dlFolderLocalService.addFolder(
64              getUserId(), plid, parentFolderId, name, description,
65              addCommunityPermissions, addGuestPermissions);
66      }
67  
68      public DLFolder addFolder(
69              long plid, long parentFolderId, String name, String description,
70              String[] communityPermissions, String[] guestPermissions)
71          throws PortalException, SystemException {
72  
73          DLFolderPermission.check(
74              getPermissionChecker(), plid, parentFolderId,
75              ActionKeys.ADD_FOLDER);
76  
77          return dlFolderLocalService.addFolder(
78              getUserId(), plid, parentFolderId, name, description,
79              communityPermissions, guestPermissions);
80      }
81  
82      public DLFolder copyFolder(
83              long plid, long sourceFolderId, long parentFolderId, String name,
84              String description, boolean addCommunityPermissions,
85              boolean addGuestPermissions)
86          throws PortalException, RemoteException, SystemException {
87  
88          DLFolder srcFolder = getFolder(sourceFolderId);
89  
90          DLFolder destFolder = addFolder(
91              plid, parentFolderId, name, description, addCommunityPermissions,
92              addGuestPermissions);
93  
94          copyFolder(
95              srcFolder, destFolder, addCommunityPermissions,
96              addGuestPermissions);
97  
98          return destFolder;
99      }
100 
101     public void deleteFolder(long folderId)
102         throws PortalException, SystemException {
103 
104         DLFolderPermission.check(
105             getPermissionChecker(), folderId, ActionKeys.DELETE);
106 
107         dlFolderLocalService.deleteFolder(folderId);
108     }
109 
110     public void deleteFolder(long groupId, long parentFolderId, String name)
111         throws PortalException, SystemException {
112 
113         DLFolder folder = getFolder(groupId, parentFolderId, name);
114 
115         deleteFolder(folder.getFolderId());
116     }
117 
118     public DLFolder getFolder(long folderId)
119         throws PortalException, SystemException {
120 
121         DLFolderPermission.check(
122             getPermissionChecker(), folderId, ActionKeys.VIEW);
123 
124         return dlFolderLocalService.getFolder(folderId);
125     }
126 
127     public DLFolder getFolder(long groupId, long parentFolderId, String name)
128         throws PortalException, SystemException {
129 
130         DLFolder folder = dlFolderLocalService.getFolder(
131             groupId, parentFolderId, name);
132 
133         DLFolderPermission.check(
134             getPermissionChecker(), folder, ActionKeys.VIEW);
135 
136         return folder;
137     }
138 
139     public long getFolderId(long groupId, long parentFolderId, String name)
140         throws PortalException, SystemException {
141 
142         DLFolder folder = getFolder(groupId, parentFolderId, name);
143 
144         return folder.getFolderId();
145     }
146 
147     public List<DLFolder> getFolders(long groupId, long parentFolderId)
148         throws PortalException, SystemException {
149 
150         List<DLFolder> folders = dlFolderLocalService.getFolders(
151             groupId, parentFolderId);
152 
153         Iterator<DLFolder> itr = folders.iterator();
154 
155         while (itr.hasNext()) {
156             DLFolder folder = itr.next();
157 
158             if (!DLFolderPermission.contains(
159                     getPermissionChecker(), folder.getFolderId(),
160                     ActionKeys.VIEW)) {
161 
162                 itr.remove();
163             }
164         }
165 
166         return folders;
167     }
168 
169     public void reIndexSearch(long companyId)
170         throws PortalException, SystemException {
171 
172         if (!getPermissionChecker().isOmniadmin()) {
173             throw new PrincipalException();
174         }
175 
176         String[] ids = new String[] {String.valueOf(companyId)};
177 
178         dlFolderLocalService.reIndex(ids);
179     }
180 
181     public DLFolder updateFolder(
182             long folderId, long parentFolderId, String name, String description)
183         throws PortalException, SystemException {
184 
185         DLFolderPermission.check(
186             getPermissionChecker(), folderId, ActionKeys.UPDATE);
187 
188         return dlFolderLocalService.updateFolder(
189             folderId, parentFolderId, name, description);
190     }
191 
192     protected void copyFolder(
193             DLFolder srcFolder, DLFolder destFolder,
194             boolean addCommunityPermissions, boolean addGuestPermissions)
195         throws PortalException, RemoteException, SystemException {
196 
197         List<DLFileEntry> srcFileEntries = dlFileEntryService.getFileEntries(
198             srcFolder.getFolderId());
199 
200         for (DLFileEntry srcFileEntry : srcFileEntries) {
201             String name = srcFileEntry.getName();
202             String title = srcFileEntry.getTitleWithExtension();
203             String description = srcFileEntry.getDescription();
204             String[] tagsEntries = null;
205             String extraSettings = srcFileEntry.getExtraSettings();
206 
207             File file = null;
208 
209             try {
210                 file = FileUtil.createTempFile(FileUtil.getExtension(name));
211 
212                 InputStream is = dlLocalService.getFileAsStream(
213                     srcFolder.getCompanyId(), srcFolder.getFolderId(), name);
214 
215                 FileUtil.write(file, is);
216             }
217             catch (Exception e) {
218                 _log.error(e, e);
219 
220                 continue;
221             }
222 
223             dlFileEntryService.addFileEntry(
224                 destFolder.getFolderId(), name, title, description, tagsEntries,
225                 extraSettings, file, addCommunityPermissions,
226                 addGuestPermissions);
227 
228             file.delete();
229         }
230 
231         long destPlid = layoutLocalService.getDefaultPlid(
232             destFolder.getGroupId());
233 
234         List<DLFolder> srcSubfolders = getFolders(
235             srcFolder.getGroupId(), srcFolder.getFolderId());
236 
237         for (DLFolder srcSubfolder : srcSubfolders) {
238             String name = srcSubfolder.getName();
239             String description = srcSubfolder.getDescription();
240 
241             DLFolder destSubfolder = addFolder(
242                 destPlid, destFolder.getFolderId(), name,
243                 description, addCommunityPermissions, addGuestPermissions);
244 
245             copyFolder(
246                 srcSubfolder, destSubfolder, addCommunityPermissions,
247                 addGuestPermissions);
248         }
249     }
250 
251     private static Log _log = LogFactory.getLog(DLFolderServiceImpl.class);
252 
253 }