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