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