1   /**
2    * Copyright (c) 2000-2009 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.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.security.auth.PrincipalException;
32  import com.liferay.portal.security.permission.ActionKeys;
33  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
34  import com.liferay.portlet.documentlibrary.model.DLFolder;
35  import com.liferay.portlet.documentlibrary.service.base.DLFolderServiceBaseImpl;
36  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
37  
38  import java.io.File;
39  import java.io.InputStream;
40  
41  import java.rmi.RemoteException;
42  
43  import java.util.Iterator;
44  import java.util.List;
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         folders = ListUtil.copy(folders);
154 
155         Iterator<DLFolder> itr = folders.iterator();
156 
157         while (itr.hasNext()) {
158             DLFolder folder = itr.next();
159 
160             if (!DLFolderPermission.contains(
161                     getPermissionChecker(), folder.getFolderId(),
162                     ActionKeys.VIEW)) {
163 
164                 itr.remove();
165             }
166         }
167 
168         return folders;
169     }
170 
171     public void reIndexSearch(long companyId)
172         throws PortalException, SystemException {
173 
174         if (!getPermissionChecker().isOmniadmin()) {
175             throw new PrincipalException();
176         }
177 
178         String[] ids = new String[] {String.valueOf(companyId)};
179 
180         dlFolderLocalService.reIndex(ids);
181     }
182 
183     public DLFolder updateFolder(
184             long folderId, long parentFolderId, String name, String description)
185         throws PortalException, SystemException {
186 
187         DLFolderPermission.check(
188             getPermissionChecker(), folderId, ActionKeys.UPDATE);
189 
190         return dlFolderLocalService.updateFolder(
191             folderId, parentFolderId, name, description);
192     }
193 
194     protected void copyFolder(
195             DLFolder srcFolder, DLFolder destFolder,
196             boolean addCommunityPermissions, boolean addGuestPermissions)
197         throws PortalException, RemoteException, SystemException {
198 
199         List<DLFileEntry> srcFileEntries = dlFileEntryService.getFileEntries(
200             srcFolder.getFolderId());
201 
202         for (DLFileEntry srcFileEntry : srcFileEntries) {
203             String name = srcFileEntry.getName();
204             String title = srcFileEntry.getTitleWithExtension();
205             String description = srcFileEntry.getDescription();
206             String[] tagsEntries = null;
207             String extraSettings = srcFileEntry.getExtraSettings();
208 
209             File file = null;
210 
211             try {
212                 file = FileUtil.createTempFile(FileUtil.getExtension(name));
213 
214                 InputStream is = dlLocalService.getFileAsStream(
215                     srcFolder.getCompanyId(), srcFolder.getFolderId(), name);
216 
217                 FileUtil.write(file, is);
218             }
219             catch (Exception e) {
220                 _log.error(e, e);
221 
222                 continue;
223             }
224 
225             dlFileEntryService.addFileEntry(
226                 destFolder.getFolderId(), name, title, description, tagsEntries,
227                 extraSettings, file, addCommunityPermissions,
228                 addGuestPermissions);
229 
230             file.delete();
231         }
232 
233         long destPlid = layoutLocalService.getDefaultPlid(
234             destFolder.getGroupId());
235 
236         List<DLFolder> srcSubfolders = getFolders(
237             srcFolder.getGroupId(), srcFolder.getFolderId());
238 
239         for (DLFolder srcSubfolder : srcSubfolders) {
240             String name = srcSubfolder.getName();
241             String description = srcSubfolder.getDescription();
242 
243             DLFolder destSubfolder = addFolder(
244                 destPlid, destFolder.getFolderId(), name,
245                 description, addCommunityPermissions, addGuestPermissions);
246 
247             copyFolder(
248                 srcSubfolder, destSubfolder, addCommunityPermissions,
249                 addGuestPermissions);
250         }
251     }
252 
253     private static Log _log = LogFactoryUtil.getLog(DLFolderServiceImpl.class);
254 
255 }