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