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.lar;
16  
17  import com.liferay.documentlibrary.service.DLLocalServiceUtil;
18  import com.liferay.portal.PortalException;
19  import com.liferay.portal.SystemException;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.MapUtil;
23  import com.liferay.portal.kernel.util.StringBundler;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.kernel.xml.Document;
27  import com.liferay.portal.kernel.xml.Element;
28  import com.liferay.portal.kernel.xml.SAXReaderUtil;
29  import com.liferay.portal.lar.BasePortletDataHandler;
30  import com.liferay.portal.lar.PortletDataContext;
31  import com.liferay.portal.lar.PortletDataException;
32  import com.liferay.portal.lar.PortletDataHandlerBoolean;
33  import com.liferay.portal.lar.PortletDataHandlerControl;
34  import com.liferay.portal.lar.PortletDataHandlerKeys;
35  import com.liferay.portal.service.ServiceContext;
36  import com.liferay.portal.util.PortletKeys;
37  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
38  import com.liferay.portlet.documentlibrary.NoSuchFileShortcutException;
39  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
40  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
41  import com.liferay.portlet.documentlibrary.model.DLFileRank;
42  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
43  import com.liferay.portlet.documentlibrary.model.DLFolder;
44  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
45  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
46  import com.liferay.portlet.documentlibrary.service.DLFileRankLocalServiceUtil;
47  import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalServiceUtil;
48  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
49  import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryUtil;
50  import com.liferay.portlet.documentlibrary.service.persistence.DLFileRankUtil;
51  import com.liferay.portlet.documentlibrary.service.persistence.DLFileShortcutUtil;
52  import com.liferay.portlet.documentlibrary.service.persistence.DLFolderUtil;
53  import com.liferay.portlet.documentlibrary.util.DLUtil;
54  
55  import java.io.IOException;
56  import java.io.InputStream;
57  
58  import java.util.List;
59  import java.util.Map;
60  import java.util.regex.Pattern;
61  
62  import javax.portlet.PortletPreferences;
63  
64  /**
65   * <a href="DLPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
66   *
67   * @author Bruno Farache
68   * @author Raymond Augé
69   */
70  public class DLPortletDataHandlerImpl extends BasePortletDataHandler {
71  
72      public static void exportFileEntry(
73              PortletDataContext context, Element foldersEl,
74              Element fileEntriesEl, Element fileRanksEl, DLFileEntry fileEntry)
75          throws PortalException, SystemException {
76  
77          if (!context.isWithinDateRange(fileEntry.getModifiedDate())) {
78              return;
79          }
80  
81          exportParentFolder(context, foldersEl, fileEntry.getFolderId());
82  
83          String path = getFileEntryPath(context, fileEntry);
84  
85          if (context.isPathNotProcessed(path)) {
86              Element fileEntryEl = fileEntriesEl.addElement("file-entry");
87  
88              fileEntryEl.addAttribute("path", path);
89  
90              String binPath = getFileEntryBinPath(context, fileEntry);
91  
92              fileEntryEl.addAttribute("bin-path", binPath);
93  
94              fileEntry.setUserUuid(fileEntry.getUserUuid());
95  
96              context.addLocks(
97                  DLFileEntry.class,
98                  DLUtil.getLockId(fileEntry.getFolderId(), fileEntry.getName()));
99  
100             context.addPermissions(
101                 DLFileEntry.class, fileEntry.getFileEntryId());
102 
103             if (context.getBooleanParameter(_NAMESPACE, "categories")) {
104                 context.addTagsCategories(
105                     DLFileEntry.class, fileEntry.getFileEntryId());
106             }
107 
108             if (context.getBooleanParameter(_NAMESPACE, "comments")) {
109                 context.addComments(
110                     DLFileEntry.class, fileEntry.getFileEntryId());
111             }
112 
113             if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
114                 context.addRatingsEntries(
115                     DLFileEntry.class, fileEntry.getFileEntryId());
116             }
117 
118             if (context.getBooleanParameter(_NAMESPACE, "tags")) {
119                 context.addTagsEntries(
120                     DLFileEntry.class, fileEntry.getFileEntryId());
121             }
122 
123             InputStream is = DLLocalServiceUtil.getFileAsStream(
124                 fileEntry.getCompanyId(), fileEntry.getFolderId(),
125                 fileEntry.getName(), fileEntry.getVersion());
126 
127             if (is == null) {
128                 if (_log.isWarnEnabled()) {
129                     _log.warn(
130                         "No file found for file entry " +
131                             fileEntry.getFileEntryId());
132                 }
133 
134                 fileEntryEl.detach();
135 
136                 return;
137             }
138 
139             try {
140                 context.addZipEntry(
141                     getFileEntryBinPath(context, fileEntry), is);
142             }
143             finally {
144                 try {
145                     is.close();
146                 }
147                 catch (IOException ioe) {
148                     _log.error(ioe, ioe);
149                 }
150             }
151 
152             context.addZipEntry(path, fileEntry);
153 
154             if (context.getBooleanParameter(_NAMESPACE, "ranks")) {
155                 List<DLFileRank> fileRanks = DLFileRankUtil.findByF_N(
156                     fileEntry.getFolderId(), fileEntry.getName());
157 
158                 for (DLFileRank fileRank : fileRanks) {
159                     exportFileRank(context, fileRanksEl, fileRank);
160                 }
161             }
162         }
163     }
164 
165     public static void exportFolder(
166             PortletDataContext context, Element foldersEl,
167             Element fileEntriesEl, Element fileShortcutsEl, Element fileRanksEl,
168             DLFolder folder)
169         throws PortalException, SystemException {
170 
171         if (context.isWithinDateRange(folder.getModifiedDate())) {
172             exportParentFolder(context, foldersEl, folder.getParentFolderId());
173 
174             String path = getFolderPath(context, folder);
175 
176             if (context.isPathNotProcessed(path)) {
177                 Element folderEl = foldersEl.addElement("folder");
178 
179                 folderEl.addAttribute("path", path);
180 
181                 folder.setUserUuid(folder.getUserUuid());
182 
183                 context.addPermissions(DLFolder.class, folder.getFolderId());
184 
185                 context.addZipEntry(path, folder);
186             }
187         }
188 
189         List<DLFileEntry> fileEntries = DLFileEntryUtil.findByFolderId(
190         folder.getFolderId());
191 
192         for (DLFileEntry fileEntry : fileEntries) {
193             exportFileEntry(
194                 context, foldersEl, fileEntriesEl, fileRanksEl, fileEntry);
195         }
196 
197         if (context.getBooleanParameter(_NAMESPACE, "shortcuts")) {
198             List<DLFileShortcut> fileShortcuts =
199                 DLFileShortcutUtil.findByFolderId(folder.getFolderId());
200 
201             for (DLFileShortcut fileShortcut : fileShortcuts) {
202                 exportFileShortcut(
203                     context, foldersEl, fileShortcutsEl, fileShortcut);
204             }
205         }
206     }
207 
208     public static void importFileEntry(
209             PortletDataContext context, Map<Long, Long> folderPKs,
210             Map<String, String> fileEntryNames, DLFileEntry fileEntry,
211             String binPath)
212         throws Exception {
213 
214         long userId = context.getUserId(fileEntry.getUserUuid());
215         long groupId = context.getScopeGroupId();
216         long folderId = MapUtil.getLong(
217             folderPKs, fileEntry.getFolderId(), fileEntry.getFolderId());
218 
219         String[] tagsCategories = null;
220         String[] tagsEntries = null;
221 
222         if (context.getBooleanParameter(_NAMESPACE, "categories")) {
223             tagsCategories = context.getTagsCategories(
224                 DLFileEntry.class, fileEntry.getFileEntryId());
225         }
226 
227         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
228             tagsEntries = context.getTagsEntries(
229                 DLFileEntry.class, fileEntry.getFileEntryId());
230         }
231 
232         ServiceContext serviceContext = new ServiceContext();
233 
234         serviceContext.setAddCommunityPermissions(true);
235         serviceContext.setAddGuestPermissions(true);
236         serviceContext.setCreateDate(fileEntry.getCreateDate());
237         serviceContext.setModifiedDate(fileEntry.getModifiedDate());
238         serviceContext.setScopeGroupId(groupId);
239         serviceContext.setTagsCategories(tagsCategories);
240         serviceContext.setTagsEntries(tagsEntries);
241 
242         InputStream is = context.getZipEntryAsInputStream(binPath);
243 
244         if ((folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
245             (folderId == fileEntry.getFolderId())) {
246 
247             String path = getImportFolderPath(context, folderId);
248 
249             DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
250 
251             importFolder(context, folderPKs, folder);
252 
253             folderId = MapUtil.getLong(
254                 folderPKs, fileEntry.getFolderId(), fileEntry.getFolderId());
255         }
256 
257         DLFileEntry existingFileEntry = null;
258 
259         try {
260             DLFolderUtil.findByPrimaryKey(folderId);
261 
262             if (context.getDataStrategy().equals(
263                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
264 
265                 try {
266                     existingFileEntry = DLFileEntryUtil.findByUUID_G(
267                         fileEntry.getUuid(), context.getScopeGroupId());
268 
269                     if (!isDuplicateFileEntry(fileEntry, existingFileEntry)) {
270                         existingFileEntry =
271                             DLFileEntryLocalServiceUtil.updateFileEntry(
272                                 userId, existingFileEntry.getFolderId(),
273                                 folderId, existingFileEntry.getName(),
274                                 fileEntry.getName(), fileEntry.getTitle(),
275                                 fileEntry.getDescription(), null,
276                                 fileEntry.getExtraSettings(), is,
277                                 fileEntry.getSize(), serviceContext);
278                     }
279                 }
280                 catch (NoSuchFileEntryException nsfee) {
281                     existingFileEntry =
282                         DLFileEntryLocalServiceUtil.addFileEntry(
283                             fileEntry.getUuid(), userId, folderId,
284                             fileEntry.getName(), fileEntry.getTitle(),
285                             fileEntry.getDescription(), null,
286                             fileEntry.getExtraSettings(), is,
287                             fileEntry.getSize(), serviceContext);
288                 }
289             }
290             else {
291                 existingFileEntry = DLFileEntryLocalServiceUtil.addFileEntry(
292                     userId, folderId, fileEntry.getName(), fileEntry.getTitle(),
293                     fileEntry.getDescription(), fileEntry.getExtraSettings(),
294                     is, fileEntry.getSize(), serviceContext);
295             }
296 
297             fileEntryNames.put(
298                 fileEntry.getName(), existingFileEntry.getName());
299 
300             String lockKey = DLUtil.getLockId(
301                 fileEntry.getFolderId(), fileEntry.getName());
302 
303             String newLockKey = DLUtil.getLockId(
304                 existingFileEntry.getFolderId(), existingFileEntry.getName());
305 
306             context.importLocks(DLFileEntry.class, lockKey, newLockKey);
307 
308             context.importPermissions(
309                 DLFileEntry.class, fileEntry.getFileEntryId(),
310                 existingFileEntry.getFileEntryId());
311 
312             if (context.getBooleanParameter(_NAMESPACE, "comments")) {
313                 context.importComments(
314                     DLFileEntry.class, fileEntry.getFileEntryId(),
315                     existingFileEntry.getFileEntryId(),
316                     context.getScopeGroupId());
317             }
318 
319             if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
320                 context.importRatingsEntries(
321                     DLFileEntry.class, fileEntry.getFileEntryId(),
322                     existingFileEntry.getFileEntryId());
323             }
324         }
325         catch (NoSuchFolderException nsfe) {
326             _log.error(
327                 "Could not find the parent folder for entry " +
328                     fileEntry.getFileEntryId());
329         }
330     }
331 
332     public static void importFileRank(
333             PortletDataContext context, Map<Long, Long> folderPKs,
334             Map<String, String> fileEntryNames, DLFileRank rank)
335         throws Exception {
336 
337         long userId = context.getUserId(rank.getUserUuid());
338         long folderId = MapUtil.getLong(
339             folderPKs, rank.getFolderId(), rank.getFolderId());
340 
341         String name = fileEntryNames.get(rank.getName());
342 
343         if (name == null) {
344             name = rank.getName();
345         }
346 
347         ServiceContext serviceContext = new ServiceContext();
348 
349         serviceContext.setCreateDate(rank.getCreateDate());
350 
351         if ((folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
352             (folderId == rank.getFolderId())) {
353 
354             String path = getImportFolderPath(context, folderId);
355 
356             DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
357 
358             importFolder(context, folderPKs, folder);
359 
360             folderId = MapUtil.getLong(
361                 folderPKs, rank.getFolderId(), rank.getFolderId());
362         }
363 
364         try {
365             DLFolderUtil.findByPrimaryKey(folderId);
366 
367             DLFileRankLocalServiceUtil.updateFileRank(
368                 context.getScopeGroupId(), context.getCompanyId(), userId,
369                 folderId, name, serviceContext);
370         }
371         catch (NoSuchFolderException nsfe) {
372             _log.error(
373                 "Could not find the folder for rank " + rank.getFileRankId());
374         }
375     }
376 
377     public static void importFolder(
378             PortletDataContext context, Map<Long, Long> folderPKs,
379             DLFolder folder)
380         throws Exception {
381 
382         long userId = context.getUserId(folder.getUserUuid());
383         long groupId = context.getScopeGroupId();
384         long parentFolderId = MapUtil.getLong(
385             folderPKs, folder.getParentFolderId(), folder.getParentFolderId());
386 
387         ServiceContext serviceContext = new ServiceContext();
388 
389         serviceContext.setAddCommunityPermissions(true);
390         serviceContext.setAddGuestPermissions(true);
391         serviceContext.setCreateDate(folder.getCreateDate());
392         serviceContext.setModifiedDate(folder.getModifiedDate());
393 
394         if ((parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
395             (parentFolderId == folder.getParentFolderId())) {
396 
397             String path = getImportFolderPath(context, parentFolderId);
398 
399             DLFolder parentFolder = (DLFolder)context.getZipEntryAsObject(path);
400 
401             importFolder(context, folderPKs, parentFolder);
402 
403             parentFolderId = MapUtil.getLong(
404                 folderPKs, folder.getParentFolderId(),
405                 folder.getParentFolderId());
406         }
407 
408         DLFolder existingFolder = null;
409 
410         try {
411             if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
412                 DLFolderUtil.findByPrimaryKey(parentFolderId);
413             }
414 
415             if (context.getDataStrategy().equals(
416                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
417 
418                 existingFolder = DLFolderUtil.fetchByUUID_G(
419                     folder.getUuid(), context.getScopeGroupId());
420 
421                 if (existingFolder == null) {
422                     String name = getFolderName(
423                         null, context.getCompanyId(), context.getScopeGroupId(),
424                         parentFolderId, folder.getName(), 2);
425 
426                     existingFolder = DLFolderLocalServiceUtil.addFolder(
427                         folder.getUuid(), userId, groupId, parentFolderId,
428                         name, folder.getDescription(), serviceContext);
429                 }
430                 else {
431                     String name = getFolderName(
432                         folder.getUuid(), context.getCompanyId(),
433                         context.getScopeGroupId(), parentFolderId,
434                         folder.getName(), 2);
435 
436                     existingFolder = DLFolderLocalServiceUtil.updateFolder(
437                         existingFolder.getFolderId(), parentFolderId, name,
438                         folder.getDescription(), serviceContext);
439                 }
440             }
441             else {
442                 String name = getFolderName(
443                     null, context.getCompanyId(), context.getScopeGroupId(),
444                     parentFolderId, folder.getName(), 2);
445 
446                 existingFolder = DLFolderLocalServiceUtil.addFolder(
447                     userId, groupId, parentFolderId, name,
448                     folder.getDescription(), serviceContext);
449             }
450 
451             folderPKs.put(folder.getFolderId(), existingFolder.getFolderId());
452 
453             context.importPermissions(
454                 DLFolder.class, folder.getFolderId(),
455                 existingFolder.getFolderId());
456         }
457         catch (NoSuchFolderException nsfe) {
458             _log.error(
459                 "Could not find the parent folder for folder " +
460                     folder.getFolderId());
461         }
462     }
463 
464     public PortletPreferences deleteData(
465             PortletDataContext context, String portletId,
466             PortletPreferences preferences)
467         throws PortletDataException {
468 
469         try {
470             if (!context.addPrimaryKey(
471                     DLPortletDataHandlerImpl.class, "deleteData")) {
472 
473                 DLFolderLocalServiceUtil.deleteFolders(
474                     context.getScopeGroupId());
475             }
476 
477             return null;
478         }
479         catch (Exception e) {
480             throw new PortletDataException(e);
481         }
482     }
483 
484     public String exportData(
485             PortletDataContext context, String portletId,
486             PortletPreferences preferences)
487         throws PortletDataException {
488 
489         try {
490             Document doc = SAXReaderUtil.createDocument();
491 
492             Element root = doc.addElement("documentlibrary-data");
493 
494             root.addAttribute(
495                 "group-id", String.valueOf(context.getScopeGroupId()));
496 
497             Element foldersEl = root.addElement("folders");
498             Element fileEntriesEl = root.addElement("file-entries");
499             Element fileShortcutsEl = root.addElement("file-shortcuts");
500             Element fileRanksEl = root.addElement("file-ranks");
501 
502             List<DLFolder> folders = DLFolderUtil.findByGroupId(
503                 context.getScopeGroupId());
504 
505             for (DLFolder folder : folders) {
506                 exportFolder(
507                     context, foldersEl, fileEntriesEl, fileShortcutsEl,
508                     fileRanksEl, folder);
509             }
510 
511             return doc.formattedString();
512         }
513         catch (Exception e) {
514             throw new PortletDataException(e);
515         }
516     }
517 
518     public PortletDataHandlerControl[] getExportControls() {
519         return new PortletDataHandlerControl[] {
520             _foldersAndDocuments, _shortcuts, _ranks, _categories, _comments,
521             _ratings, _tags
522         };
523     }
524 
525     public PortletDataHandlerControl[] getImportControls() {
526         return new PortletDataHandlerControl[] {
527             _foldersAndDocuments, _shortcuts, _ranks, _categories, _comments,
528             _ratings, _tags
529         };
530     }
531 
532     public PortletPreferences importData(
533             PortletDataContext context, String portletId,
534             PortletPreferences preferences, String data)
535         throws PortletDataException {
536 
537         try {
538             Document doc = SAXReaderUtil.read(data);
539 
540             Element root = doc.getRootElement();
541 
542             List<Element> folderEls = root.element("folders").elements(
543                 "folder");
544 
545             Map<Long, Long> folderPKs =
546                 (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFolder.class);
547 
548             for (Element folderEl : folderEls) {
549                 String path = folderEl.attributeValue("path");
550 
551                 if (!context.isPathNotProcessed(path)) {
552                     continue;
553                 }
554 
555                 DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
556 
557                 importFolder(context, folderPKs, folder);
558             }
559 
560             List<Element> fileEntryEls = root.element("file-entries").elements(
561                 "file-entry");
562 
563             Map<String, String> fileEntryNames =
564                 (Map<String, String>)context.getNewPrimaryKeysMap(
565                     DLFileEntry.class);
566 
567             for (Element fileEntryEl : fileEntryEls) {
568                 String path = fileEntryEl.attributeValue("path");
569 
570                 if (!context.isPathNotProcessed(path)) {
571                     continue;
572                 }
573 
574                 DLFileEntry fileEntry =
575                     (DLFileEntry)context.getZipEntryAsObject(path);
576 
577                 String binPath = fileEntryEl.attributeValue("bin-path");
578 
579                 importFileEntry(
580                     context, folderPKs, fileEntryNames, fileEntry, binPath);
581             }
582 
583             if (context.getBooleanParameter(_NAMESPACE, "shortcuts")) {
584                 List<Element> fileShortcutEls = root.element(
585                     "file-shortcuts").elements("file-shortcut");
586 
587                 for (Element fileShortcutEl : fileShortcutEls) {
588                     String path = fileShortcutEl.attributeValue("path");
589 
590                     if (!context.isPathNotProcessed(path)) {
591                         continue;
592                     }
593 
594                     DLFileShortcut fileShortcut =
595                         (DLFileShortcut)context.getZipEntryAsObject(path);
596 
597                     importFileShortcut(
598                         context, folderPKs, fileEntryNames, fileShortcut);
599                 }
600             }
601 
602             if (context.getBooleanParameter(_NAMESPACE, "ranks")) {
603                 List<Element> fileRankEls = root.element("file-ranks").elements(
604                     "file-rank");
605 
606                 for (Element fileRankEl : fileRankEls) {
607                     String path = fileRankEl.attributeValue("path");
608 
609                     if (!context.isPathNotProcessed(path)) {
610                         continue;
611                     }
612 
613                     DLFileRank fileRank =
614                         (DLFileRank)context.getZipEntryAsObject(path);
615 
616                     importFileRank(
617                         context, folderPKs, fileEntryNames, fileRank);
618                 }
619             }
620 
621             return null;
622         }
623         catch (Exception e) {
624             throw new PortletDataException(e);
625         }
626     }
627 
628     protected static void exportFileRank(
629             PortletDataContext context, Element fileRanksEl,
630             DLFileRank fileRank)
631         throws SystemException {
632 
633         String path = getFileRankPath(context, fileRank);
634 
635         if (!context.isPathNotProcessed(path)) {
636             return;
637         }
638 
639         Element fileRankEl = fileRanksEl.addElement("file-rank");
640 
641         fileRankEl.addAttribute("path", path);
642 
643         fileRank.setUserUuid(fileRank.getUserUuid());
644 
645         context.addZipEntry(path, fileRank);
646     }
647 
648     protected static void exportFileShortcut(
649             PortletDataContext context, Element foldersEl,
650             Element fileShortcutsEl, DLFileShortcut fileShortcut)
651         throws PortalException, SystemException {
652 
653         exportParentFolder(context, foldersEl, fileShortcut.getFolderId());
654 
655         String path = getFileShortcutPath(context, fileShortcut);
656 
657         if (context.isPathNotProcessed(path)) {
658             Element fileShortcutEl = fileShortcutsEl.addElement(
659                 "file-shortcut");
660 
661             fileShortcutEl.addAttribute("path", path);
662 
663             fileShortcut.setUserUuid(fileShortcut.getUserUuid());
664 
665             context.addPermissions(
666                 DLFileShortcut.class, fileShortcut.getFileShortcutId());
667 
668             context.addZipEntry(path, fileShortcut);
669         }
670     }
671 
672     protected static void exportParentFolder(
673             PortletDataContext context, Element foldersEl, long folderId)
674         throws PortalException, SystemException {
675 
676         if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
677             return;
678         }
679 
680         DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
681 
682         exportParentFolder(context, foldersEl, folder.getParentFolderId());
683 
684         String path = getFolderPath(context, folder);
685 
686         if (context.isPathNotProcessed(path)) {
687             Element folderEl = foldersEl.addElement("folder");
688 
689             folderEl.addAttribute("path", path);
690 
691             folder.setUserUuid(folder.getUserUuid());
692 
693             context.addPermissions(DLFolder.class, folder.getFolderId());
694 
695             context.addZipEntry(path, folder);
696         }
697     }
698 
699     protected static String getFileEntryBinPath(
700         PortletDataContext context, DLFileEntry fileEntry) {
701 
702         StringBundler sb = new StringBundler(5);
703 
704         sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
705         sb.append("/bin/");
706         sb.append(fileEntry.getFileEntryId());
707         sb.append(StringPool.SLASH);
708         sb.append(fileEntry.getVersion());
709 
710         return sb.toString();
711     }
712 
713     protected static String getFileEntryPath(
714         PortletDataContext context, DLFileEntry fileEntry) {
715 
716         StringBundler sb = new StringBundler(6);
717 
718         sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
719         sb.append("/file-entries/");
720         sb.append(fileEntry.getFileEntryId());
721         sb.append(StringPool.SLASH);
722         sb.append(fileEntry.getVersion());
723         sb.append(".xml");
724 
725         return sb.toString();
726     }
727 
728     protected static String getFolderName(
729             String uuid, long companyId, long groupId, long parentFolderId,
730             String name, int count)
731         throws SystemException {
732 
733         DLFolder folder = DLFolderUtil.fetchByG_P_N(
734             groupId, parentFolderId, name);
735 
736         if (folder == null) {
737             return name;
738         }
739 
740         if (Validator.isNotNull(uuid) && uuid.equals(folder.getUuid())) {
741             return name;
742         }
743 
744         if (Pattern.matches(".* \\(\\d+\\)", name)) {
745             int pos = name.lastIndexOf(" (");
746 
747             name = name.substring(0, pos);
748         }
749 
750         StringBundler sb = new StringBundler(5);
751 
752         sb.append(name);
753         sb.append(StringPool.SPACE);
754         sb.append(StringPool.OPEN_PARENTHESIS);
755         sb.append(count);
756         sb.append(StringPool.CLOSE_PARENTHESIS);
757 
758         name = sb.toString();
759 
760         return getFolderName(
761             uuid, companyId, groupId, parentFolderId, name, ++count);
762     }
763 
764     protected static String getFolderPath(
765         PortletDataContext context, DLFolder folder) {
766 
767         StringBundler sb = new StringBundler(4);
768 
769         sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
770         sb.append("/folders/");
771         sb.append(folder.getFolderId());
772         sb.append(".xml");
773 
774         return sb.toString();
775     }
776 
777     protected static String getFileRankPath(
778         PortletDataContext context, DLFileRank fileRank) {
779 
780         StringBundler sb = new StringBundler(4);
781 
782         sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
783         sb.append("/ranks/");
784         sb.append(fileRank.getFileRankId());
785         sb.append(".xml");
786 
787         return sb.toString();
788     }
789 
790     protected static String getFileShortcutPath(
791         PortletDataContext context, DLFileShortcut fileShortcut) {
792 
793         StringBundler sb = new StringBundler(4);
794 
795         sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
796         sb.append("/shortcuts/");
797         sb.append(fileShortcut.getFileShortcutId());
798         sb.append(".xml");
799 
800         return sb.toString();
801     }
802 
803     protected static String getImportFolderPath(
804         PortletDataContext context, long folderId) {
805 
806         StringBundler sb = new StringBundler(4);
807 
808         sb.append(context.getSourcePortletPath(PortletKeys.DOCUMENT_LIBRARY));
809         sb.append("/folders/");
810         sb.append(folderId);
811         sb.append(".xml");
812 
813         return sb.toString();
814     }
815 
816     protected static void importFileShortcut(
817             PortletDataContext context, Map<Long, Long> folderPKs,
818             Map<String, String> fileEntryNames, DLFileShortcut fileShortcut)
819         throws Exception {
820 
821         long userId = context.getUserId(fileShortcut.getUserUuid());
822         long folderId = MapUtil.getLong(
823             folderPKs, fileShortcut.getFolderId(), fileShortcut.getFolderId());
824         long toFolderId = MapUtil.getLong(
825             folderPKs, fileShortcut.getToFolderId(),
826             fileShortcut.getToFolderId());
827         String toName = MapUtil.getString(
828             fileEntryNames, fileShortcut.getToName(), fileShortcut.getToName());
829 
830         try {
831             DLFolderUtil.findByPrimaryKey(folderId);
832             DLFolderUtil.findByPrimaryKey(toFolderId);
833 
834             DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
835                 toFolderId, toName);
836 
837             String[] tagsCategories = null;
838             String[] tagsEntries = null;
839 
840             if (context.getBooleanParameter(_NAMESPACE, "categories")) {
841                 tagsCategories = context.getTagsCategories(
842                     DLFileEntry.class, fileEntry.getFileEntryId());
843             }
844 
845             if (context.getBooleanParameter(_NAMESPACE, "tags")) {
846                 tagsEntries = context.getTagsEntries(
847                     DLFileEntry.class, fileEntry.getFileEntryId());
848             }
849 
850             ServiceContext serviceContext = new ServiceContext();
851 
852             serviceContext.setAddCommunityPermissions(true);
853             serviceContext.setAddGuestPermissions(true);
854             serviceContext.setCreateDate(fileShortcut.getCreateDate());
855             serviceContext.setModifiedDate(fileShortcut.getModifiedDate());
856             serviceContext.setScopeGroupId(context.getScopeGroupId());
857             serviceContext.setTagsCategories(tagsCategories);
858             serviceContext.setTagsEntries(tagsEntries);
859 
860             DLFileShortcut existingFileShortcut = null;
861 
862             if (context.getDataStrategy().equals(
863                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
864 
865                 try {
866                     existingFileShortcut = DLFileShortcutUtil.findByUUID_G(
867                         fileShortcut.getUuid(), context.getScopeGroupId());
868 
869                     existingFileShortcut =
870                         DLFileShortcutLocalServiceUtil.updateFileShortcut(
871                             userId, existingFileShortcut.getFileShortcutId(),
872                             folderId, toFolderId, toName, serviceContext);
873                 }
874                 catch (NoSuchFileShortcutException nsfse) {
875                     existingFileShortcut =
876                         DLFileShortcutLocalServiceUtil.addFileShortcut(
877                             fileShortcut.getUuid(), userId, folderId,
878                             toFolderId, toName, serviceContext);
879                 }
880             }
881             else {
882                 existingFileShortcut =
883                     DLFileShortcutLocalServiceUtil.addFileShortcut(
884                         userId, folderId, toFolderId, toName, serviceContext);
885             }
886 
887             context.importPermissions(
888                 DLFileShortcut.class, fileShortcut.getPrimaryKey(),
889                 existingFileShortcut.getPrimaryKey());
890         }
891         catch (NoSuchFolderException nsfe) {
892             _log.error(
893                 "Could not find the folder for shortcut " +
894                     fileShortcut.getFileShortcutId());
895         }
896     }
897 
898     protected static boolean isDuplicateFileEntry(
899         DLFileEntry fileEntry1, DLFileEntry fileEntry2) {
900 
901         try {
902             DLFolder folder1 = fileEntry1.getFolder();
903             DLFolder folder2 = fileEntry2.getFolder();
904 
905             if ((folder1.getUuid().equals(folder2.getUuid())) &&
906                 (fileEntry1.getSize() == fileEntry2.getSize()) &&
907                 (fileEntry1.getVersion() == fileEntry2.getVersion()) &&
908                 (fileEntry1.getVersionUserUuid().equals(
909                     fileEntry2.getVersionUserUuid()))) {
910 
911                 return true;
912             }
913             else {
914                 return false;
915             }
916         }
917         catch (SystemException se) {
918             return false;
919         }
920     }
921 
922     private static final String _NAMESPACE = "document_library";
923 
924     private static final PortletDataHandlerBoolean _foldersAndDocuments =
925         new PortletDataHandlerBoolean(
926             _NAMESPACE, "folders-and-documents", true, true);
927 
928     private static final PortletDataHandlerBoolean _ranks =
929         new PortletDataHandlerBoolean(_NAMESPACE, "ranks");
930 
931     private static final PortletDataHandlerBoolean _shortcuts=
932         new PortletDataHandlerBoolean(_NAMESPACE, "shortcuts");
933 
934     private static final PortletDataHandlerBoolean _categories =
935         new PortletDataHandlerBoolean(_NAMESPACE, "categories");
936 
937     private static final PortletDataHandlerBoolean _comments =
938         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
939 
940     private static final PortletDataHandlerBoolean _ratings =
941         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
942 
943     private static final PortletDataHandlerBoolean _tags =
944         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
945 
946     private static Log _log = LogFactoryUtil.getLog(
947         DLPortletDataHandlerImpl.class);
948 
949 }