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