1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.documentlibrary.service.impl;
16  
17  import com.liferay.documentlibrary.DuplicateFileException;
18  import com.liferay.documentlibrary.NoSuchDirectoryException;
19  import com.liferay.portal.NoSuchLayoutException;
20  import com.liferay.portal.PortalException;
21  import com.liferay.portal.SystemException;
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.search.Hits;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.LocaleUtil;
27  import com.liferay.portal.kernel.util.PropsKeys;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.model.LayoutConstants;
31  import com.liferay.portal.model.ResourceConstants;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.PortletKeys;
35  import com.liferay.portal.util.PropsUtil;
36  import com.liferay.portal.util.PropsValues;
37  import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
38  import com.liferay.portlet.documentlibrary.FolderNameException;
39  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
40  import com.liferay.portlet.documentlibrary.model.DLFolder;
41  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
42  import com.liferay.portlet.documentlibrary.service.base.DLFolderLocalServiceBaseImpl;
43  import com.liferay.portlet.tags.util.TagsUtil;
44  
45  import java.rmi.RemoteException;
46  
47  import java.util.ArrayList;
48  import java.util.Date;
49  import java.util.List;
50  
51  /**
52   * <a href="DLFolderLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   */
56  public class DLFolderLocalServiceImpl extends DLFolderLocalServiceBaseImpl {
57  
58      public DLFolder addFolder(
59              long userId, long plid, long parentFolderId, String name,
60              String description, boolean addCommunityPermissions,
61              boolean addGuestPermissions)
62          throws PortalException, SystemException {
63  
64          return addFolder(
65              null, userId, plid, parentFolderId, name, description,
66              Boolean.valueOf(addCommunityPermissions),
67              Boolean.valueOf(addGuestPermissions), null, null);
68      }
69  
70      public DLFolder addFolder(
71              String uuid, long userId, long plid, long parentFolderId,
72              String name, String description, boolean addCommunityPermissions,
73              boolean addGuestPermissions)
74          throws PortalException, SystemException {
75  
76          return addFolder(
77              uuid, userId, plid, parentFolderId, name, description,
78              Boolean.valueOf(addCommunityPermissions),
79              Boolean.valueOf(addGuestPermissions), null, null);
80      }
81  
82      public DLFolder addFolder(
83              long userId, long plid, long parentFolderId, String name,
84              String description, String[] communityPermissions,
85              String[] guestPermissions)
86          throws PortalException, SystemException {
87  
88          return addFolder(
89              null, userId, plid, parentFolderId, name, description, null, null,
90              communityPermissions, guestPermissions);
91      }
92  
93      public DLFolder addFolder(
94              String uuid, long userId, long plid, long parentFolderId,
95              String name, String description, Boolean addCommunityPermissions,
96              Boolean addGuestPermissions, String[] communityPermissions,
97              String[] guestPermissions)
98          throws PortalException, SystemException {
99  
100         long groupId = PortalUtil.getScopeGroupId(plid);
101 
102         return addFolderToGroup(
103             uuid, userId, groupId, parentFolderId, name, description,
104             addCommunityPermissions, addGuestPermissions, communityPermissions,
105             guestPermissions);
106     }
107 
108     public DLFolder addFolderToGroup(
109             String uuid, long userId, long groupId, long parentFolderId,
110             String name, String description, Boolean addCommunityPermissions,
111             Boolean addGuestPermissions, String[] communityPermissions,
112             String[] guestPermissions)
113         throws PortalException, SystemException {
114 
115         // Folder
116 
117         User user = userPersistence.findByPrimaryKey(userId);
118         parentFolderId = getParentFolderId(groupId, parentFolderId);
119         Date now = new Date();
120 
121         validate(groupId, parentFolderId, name);
122 
123         long folderId = counterLocalService.increment();
124 
125         DLFolder folder = dlFolderPersistence.create(folderId);
126 
127         folder.setUuid(uuid);
128         folder.setGroupId(groupId);
129         folder.setCompanyId(user.getCompanyId());
130         folder.setUserId(user.getUserId());
131         folder.setCreateDate(now);
132         folder.setModifiedDate(now);
133         folder.setParentFolderId(parentFolderId);
134         folder.setName(name);
135         folder.setDescription(description);
136 
137         dlFolderPersistence.update(folder, false);
138 
139         // Resources
140 
141         if ((addCommunityPermissions != null) &&
142             (addGuestPermissions != null)) {
143 
144             addFolderResources(
145                 folder, addCommunityPermissions.booleanValue(),
146                 addGuestPermissions.booleanValue());
147         }
148         else {
149             addFolderResources(folder, communityPermissions, guestPermissions);
150         }
151 
152         // Layout
153 
154         String[] pathArray = folder.getPathArray();
155 
156         if (PropsValues.DL_LAYOUTS_SYNC_ENABLED &&
157             (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
158 
159             String layoutsSyncPrivateFolder = GetterUtil.getString(
160                 PropsUtil.get(PropsKeys.DL_LAYOUTS_SYNC_PRIVATE_FOLDER));
161             String layoutsSyncPublicFolder = GetterUtil.getString(
162                 PropsUtil.get(PropsKeys.DL_LAYOUTS_SYNC_PUBLIC_FOLDER));
163 
164             if (pathArray[0].equals(layoutsSyncPrivateFolder) ||
165                 pathArray[0].equals(layoutsSyncPublicFolder)) {
166 
167                 boolean privateLayout = true;
168 
169                 if (pathArray[0].equals(layoutsSyncPublicFolder)) {
170                     privateLayout = false;
171                 }
172 
173                 long parentLayoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
174                 String title = StringPool.BLANK;
175                 String layoutDescription = StringPool.BLANK;
176                 String type = LayoutConstants.TYPE_PORTLET;
177                 boolean hidden = false;
178                 String friendlyURL = StringPool.BLANK;
179 
180                 Layout dlFolderLayout = null;
181 
182                 try {
183                     dlFolderLayout = layoutLocalService.getDLFolderLayout(
184                         folder.getParentFolderId());
185 
186                     parentLayoutId = dlFolderLayout.getLayoutId();
187                 }
188                 catch (NoSuchLayoutException nsle) {
189                 }
190 
191                 layoutLocalService.addLayout(
192                     userId, groupId, privateLayout, parentLayoutId, name, title,
193                     layoutDescription, type, hidden, friendlyURL,
194                     folder.getFolderId());
195             }
196         }
197 
198         // Parent folder
199 
200         if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
201             DLFolder parentFolder = dlFolderPersistence.findByPrimaryKey(
202                 parentFolderId);
203 
204             parentFolder.setLastPostDate(now);
205 
206             dlFolderPersistence.update(parentFolder, false);
207         }
208 
209         return folder;
210     }
211 
212     public void addFolderResources(
213             long folderId, boolean addCommunityPermissions,
214             boolean addGuestPermissions)
215         throws PortalException, SystemException {
216 
217         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
218 
219         addFolderResources(
220             folder, addCommunityPermissions, addGuestPermissions);
221     }
222 
223     public void addFolderResources(
224             DLFolder folder, boolean addCommunityPermissions,
225             boolean addGuestPermissions)
226         throws PortalException, SystemException {
227 
228         resourceLocalService.addResources(
229             folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
230             DLFolder.class.getName(), folder.getFolderId(), false,
231             addCommunityPermissions, addGuestPermissions);
232     }
233 
234     public void addFolderResources(
235             long folderId, String[] communityPermissions,
236             String[] guestPermissions)
237         throws PortalException, SystemException {
238 
239         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
240 
241         addFolderResources(folder, communityPermissions, guestPermissions);
242     }
243 
244     public void addFolderResources(
245             DLFolder folder, String[] communityPermissions,
246             String[] guestPermissions)
247         throws PortalException, SystemException {
248 
249         resourceLocalService.addModelResources(
250             folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
251             DLFolder.class.getName(), folder.getFolderId(),
252             communityPermissions, guestPermissions);
253     }
254 
255     public void deleteFolder(long folderId)
256         throws PortalException, SystemException {
257 
258         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
259 
260         deleteFolder(folder);
261     }
262 
263     public void deleteFolder(DLFolder folder)
264         throws PortalException, SystemException {
265 
266         // Folder
267 
268         dlFolderPersistence.remove(folder);
269 
270         // Resources
271 
272         resourceLocalService.deleteResource(
273             folder.getCompanyId(), DLFolder.class.getName(),
274             ResourceConstants.SCOPE_INDIVIDUAL, folder.getFolderId());
275 
276         // WebDAVProps
277 
278         webDAVPropsLocalService.deleteWebDAVProps(
279             DLFolder.class.getName(), folder.getPrimaryKey());
280 
281         // File entries
282 
283         dlFileEntryLocalService.deleteFileEntries(folder.getFolderId());
284 
285         // Folders
286 
287         List<DLFolder> folders = dlFolderPersistence.findByG_P(
288             folder.getGroupId(), folder.getFolderId());
289 
290         for (DLFolder curFolder : folders) {
291             deleteFolder(curFolder);
292         }
293 
294         try {
295             dlService.deleteDirectory(
296                 folder.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
297                 folder.getFolderId(), StringPool.BLANK);
298         }
299         catch (NoSuchDirectoryException nsde) {
300             if (_log.isDebugEnabled()) {
301                 _log.debug(nsde.getMessage());
302             }
303         }
304         catch (RemoteException re) {
305             throw new SystemException(re);
306         }
307     }
308 
309     public void deleteFolders(long groupId)
310         throws PortalException, SystemException {
311 
312         List<DLFolder> folders = dlFolderPersistence.findByG_P(
313             groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
314 
315         for (DLFolder folder : folders) {
316             deleteFolder(folder);
317         }
318     }
319 
320     public DLFolder getFolder(long folderId)
321         throws PortalException, SystemException {
322 
323         return dlFolderPersistence.findByPrimaryKey(folderId);
324     }
325 
326     public DLFolder getFolder(long groupId, long parentFolderId, String name)
327         throws PortalException, SystemException {
328 
329         return dlFolderPersistence.findByG_P_N(groupId, parentFolderId, name);
330     }
331 
332     public List<DLFolder> getFolders(long companyId) throws SystemException {
333         return dlFolderPersistence.findByCompanyId(companyId);
334     }
335 
336     public List<DLFolder> getFolders(long groupId, long parentFolderId)
337         throws SystemException {
338 
339         return dlFolderPersistence.findByG_P(groupId, parentFolderId);
340     }
341 
342     public List<DLFolder> getFolders(
343             long groupId, long parentFolderId, int start, int end)
344         throws SystemException {
345 
346         return dlFolderPersistence.findByG_P(
347             groupId, parentFolderId, start, end);
348     }
349 
350     public int getFoldersCount(long groupId, long parentFolderId)
351         throws SystemException {
352 
353         return dlFolderPersistence.countByG_P(groupId, parentFolderId);
354     }
355 
356     public void getSubfolderIds(
357             List<Long> folderIds, long groupId, long folderId)
358         throws SystemException {
359 
360         List<DLFolder> folders = dlFolderPersistence.findByG_P(
361             groupId, folderId);
362 
363         for (DLFolder folder : folders) {
364             folderIds.add(folder.getFolderId());
365 
366             getSubfolderIds(
367                 folderIds, folder.getGroupId(), folder.getFolderId());
368         }
369     }
370 
371     public void reIndex(String[] ids) throws SystemException {
372         long companyId = GetterUtil.getLong(ids[0]);
373 
374         try {
375             List<DLFolder> folders = getFolders(companyId);
376 
377             for (DLFolder folder : folders) {
378                 String portletId = PortletKeys.DOCUMENT_LIBRARY;
379                 long groupId = folder.getGroupId();
380                 long folderId = folder.getFolderId();
381 
382                 String[] newIds = {
383                     String.valueOf(companyId), portletId,
384                     String.valueOf(groupId), String.valueOf(folderId)
385                 };
386 
387                 dlService.reIndex(newIds);
388             }
389         }
390         catch (SystemException se) {
391             throw se;
392         }
393         catch (Exception e) {
394             throw new SystemException(e);
395         }
396     }
397 
398     public Hits search(
399             long companyId, long groupId, long[] folderIds, String keywords,
400             int start, int end)
401         throws SystemException {
402 
403         return dlLocalService.search(
404             companyId, PortletKeys.DOCUMENT_LIBRARY, groupId, folderIds,
405             keywords, start, end);
406     }
407 
408     public DLFolder updateFolder(
409             long folderId, long parentFolderId, String name,
410             String description)
411         throws PortalException, SystemException {
412 
413         // Folder
414 
415         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
416 
417         parentFolderId = getParentFolderId(folder, parentFolderId);
418 
419         validate(
420             folder.getFolderId(), folder.getGroupId(), parentFolderId, name);
421 
422         folder.setModifiedDate(new Date());
423         folder.setParentFolderId(parentFolderId);
424         folder.setName(name);
425         folder.setDescription(description);
426 
427         dlFolderPersistence.update(folder, false);
428 
429         // Layout
430 
431         if (PropsValues.DL_LAYOUTS_SYNC_ENABLED) {
432             String privateFolder = GetterUtil.getString(PropsUtil.get(
433                 PropsKeys.DL_LAYOUTS_SYNC_PRIVATE_FOLDER));
434 
435             boolean privateLayout = false;
436 
437             String[] path = folder.getPathArray();
438 
439             if (path[0].equals(privateFolder)) {
440                 privateLayout = true;
441             }
442 
443             Layout layout = layoutLocalService.getDLFolderLayout(
444                 folder.getFolderId());
445 
446             layout.setName(folder.getName());
447 
448             layoutLocalService.updateName(
449                 folder.getGroupId(), privateLayout, layout.getLayoutId(),
450                 folder.getName(),
451                 LocaleUtil.toLanguageId(LocaleUtil.getDefault()));
452         }
453 
454         return folder;
455     }
456 
457     protected long getParentFolderId(long groupId, long parentFolderId)
458         throws SystemException {
459 
460         if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
461             DLFolder parentFolder = dlFolderPersistence.fetchByPrimaryKey(
462                 parentFolderId);
463 
464             if ((parentFolder == null) ||
465                 (groupId != parentFolder.getGroupId())) {
466 
467                 parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
468             }
469         }
470 
471         return parentFolderId;
472     }
473 
474     protected long getParentFolderId(DLFolder folder, long parentFolderId)
475         throws SystemException {
476 
477         if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
478             return parentFolderId;
479         }
480 
481         if (folder.getFolderId() == parentFolderId) {
482             return folder.getParentFolderId();
483         }
484         else {
485             DLFolder parentFolder = dlFolderPersistence.fetchByPrimaryKey(
486                 parentFolderId);
487 
488             if ((parentFolder == null) ||
489                 (folder.getGroupId() != parentFolder.getGroupId())) {
490 
491                 return folder.getParentFolderId();
492             }
493 
494             List<Long> subfolderIds = new ArrayList<Long>();
495 
496             getSubfolderIds(
497                 subfolderIds, folder.getGroupId(), folder.getFolderId());
498 
499             if (subfolderIds.contains(parentFolderId)) {
500                 return folder.getParentFolderId();
501             }
502 
503             return parentFolderId;
504         }
505     }
506 
507     protected void validate(long groupId, long parentFolderId, String name)
508         throws PortalException, SystemException {
509 
510         long folderId = 0;
511 
512         validate(folderId, groupId, parentFolderId, name);
513     }
514 
515     protected void validate(
516             long folderId, long groupId, long parentFolderId, String name)
517         throws PortalException, SystemException {
518 
519         if (!TagsUtil.isValidWord(name)) {
520             throw new FolderNameException();
521         }
522 
523         try {
524             dlFileEntryLocalService.getFileEntryByTitle(parentFolderId, name);
525 
526             throw new DuplicateFileException();
527         }
528         catch (NoSuchFileEntryException nsfee) {
529         }
530 
531         DLFolder folder = dlFolderPersistence.fetchByG_P_N(
532             groupId, parentFolderId, name);
533 
534         if ((folder != null) && (folder.getFolderId() != folderId)) {
535             throw new DuplicateFolderNameException();
536         }
537     }
538 
539     private static Log _log = LogFactoryUtil.getLog(
540         DLFolderLocalServiceImpl.class);
541 
542 }