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