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