1
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
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
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
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
263 copyFolder(
264 folder, subfolder, addCommunityPermissions,
265 addGuestPermissions);
266 }
267 }
268 }
269
270 private static Log _log = LogFactory.getLog(DLFolderServiceImpl.class);
271
272 }