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