1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.documentlibrary.lar;
16  
17  import com.liferay.documentlibrary.service.DLLocalServiceUtil;
18  import com.liferay.portal.kernel.exception.PortalException;
19  import com.liferay.portal.kernel.exception.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  import com.liferay.portlet.documentlibrary.util.DLUtil;
53  
54  import java.io.IOException;
55  import java.io.InputStream;
56  
57  import java.util.List;
58  import java.util.Map;
59  import java.util.regex.Pattern;
60  
61  import javax.portlet.PortletPreferences;
62  
63  /**
64   * <a href="DLPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
65   *
66   * @author Bruno Farache
67   * @author Raymond Augé
68   */
69  public class DLPortletDataHandlerImpl extends BasePortletDataHandler {
70  
71      public static void exportFileEntry(
72              PortletDataContext context, Element foldersEl,
73              Element fileEntriesEl, Element fileRanksEl, DLFileEntry fileEntry)
74          throws PortalException, SystemException {
75  
76          if (!context.isWithinDateRange(fileEntry.getModifiedDate())) {
77              return;
78          }
79  
80          if (foldersEl != null) {
81              exportParentFolder(context, foldersEl, fileEntry.getFolderId());
82          }
83  
84          String path = getFileEntryPath(context, fileEntry);
85  
86          if (context.isPathNotProcessed(path)) {
87              Element fileEntryEl = fileEntriesEl.addElement("file-entry");
88  
89              fileEntryEl.addAttribute("path", path);
90  
91              String binPath = getFileEntryBinPath(context, fileEntry);
92  
93              fileEntryEl.addAttribute("bin-path", binPath);
94  
95              fileEntry.setUserUuid(fileEntry.getUserUuid());
96  
97              if (context.getBooleanParameter(_NAMESPACE, "categories")) {
98                  context.addAssetCategories(
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.addAssetTags(
114                     DLFileEntry.class, fileEntry.getFileEntryId());
115             }
116 
117             long repositoryId = getRepositoryId(
118                 fileEntry.getGroupId(), fileEntry.getFolderId());
119 
120             InputStream is = DLLocalServiceUtil.getFileAsStream(
121                 fileEntry.getCompanyId(), repositoryId, fileEntry.getName(),
122                 fileEntry.getVersion());
123 
124             if (is == null) {
125                 if (_log.isWarnEnabled()) {
126                     _log.warn(
127                         "No file found for file entry " +
128                             fileEntry.getFileEntryId());
129                 }
130 
131                 fileEntryEl.detach();
132 
133                 return;
134             }
135 
136             try {
137                 context.addZipEntry(
138                     getFileEntryBinPath(context, fileEntry), is);
139             }
140             finally {
141                 try {
142                     is.close();
143                 }
144                 catch (IOException ioe) {
145                     _log.error(ioe, ioe);
146                 }
147             }
148 
149             context.addZipEntry(path, fileEntry);
150 
151             if (context.getBooleanParameter(_NAMESPACE, "ranks")) {
152                 List<DLFileRank> fileRanks = DLFileRankUtil.findByF_N(
153                     fileEntry.getFolderId(), fileEntry.getName());
154 
155                 for (DLFileRank fileRank : fileRanks) {
156                     exportFileRank(context, fileRanksEl, fileRank);
157                 }
158             }
159         }
160     }
161 
162     public static void exportFolder(
163             PortletDataContext context, Element foldersEl,
164             Element fileEntriesEl, Element fileShortcutsEl, Element fileRanksEl,
165             DLFolder folder)
166         throws PortalException, SystemException {
167 
168         if (context.isWithinDateRange(folder.getModifiedDate())) {
169             exportParentFolder(context, foldersEl, folder.getParentFolderId());
170 
171             String path = getFolderPath(context, folder);
172 
173             if (context.isPathNotProcessed(path)) {
174                 Element folderEl = foldersEl.addElement("folder");
175 
176                 folderEl.addAttribute("path", path);
177 
178                 folder.setUserUuid(folder.getUserUuid());
179 
180                 context.addZipEntry(path, folder);
181             }
182         }
183 
184         List<DLFileEntry> fileEntries = DLFileEntryUtil.findByG_F(
185             folder.getGroupId(), folder.getFolderId());
186 
187         for (DLFileEntry fileEntry : fileEntries) {
188             exportFileEntry(
189                 context, foldersEl, fileEntriesEl, fileRanksEl, fileEntry);
190         }
191 
192         if (context.getBooleanParameter(_NAMESPACE, "shortcuts")) {
193             List<DLFileShortcut> fileShortcuts = DLFileShortcutUtil.findByG_F(
194                 folder.getGroupId(), folder.getFolderId());
195 
196             for (DLFileShortcut fileShortcut : fileShortcuts) {
197                 exportFileShortcut(
198                     context, foldersEl, fileShortcutsEl, fileShortcut);
199             }
200         }
201     }
202 
203     public static void importFileEntry(
204             PortletDataContext context, Map<Long, Long> folderPKs,
205             Map<String, String> fileEntryNames, DLFileEntry fileEntry,
206             String binPath)
207         throws Exception {
208 
209         long userId = context.getUserId(fileEntry.getUserUuid());
210         long groupId = context.getGroupId();
211         long folderId = MapUtil.getLong(
212             folderPKs, fileEntry.getFolderId(), fileEntry.getFolderId());
213 
214         long[] assetCategoryIds = null;
215         String[] assetTagNames = null;
216 
217         if (context.getBooleanParameter(_NAMESPACE, "categories")) {
218             assetCategoryIds = context.getAssetCategoryIds(
219                 DLFileEntry.class, fileEntry.getFileEntryId());
220         }
221 
222         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
223             assetTagNames = context.getAssetTagNames(
224                 DLFileEntry.class, fileEntry.getFileEntryId());
225         }
226 
227         ServiceContext serviceContext = new ServiceContext();
228 
229         serviceContext.setAddCommunityPermissions(true);
230         serviceContext.setAddGuestPermissions(true);
231         serviceContext.setAssetCategoryIds(assetCategoryIds);
232         serviceContext.setAssetTagNames(assetTagNames);
233         serviceContext.setScopeGroupId(groupId);
234 
235         InputStream is = context.getZipEntryAsInputStream(binPath);
236 
237         if ((folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
238             (folderId == fileEntry.getFolderId())) {
239 
240             String path = getImportFolderPath(context, folderId);
241 
242             DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
243 
244             importFolder(context, folderPKs, folder);
245 
246             folderId = MapUtil.getLong(
247                 folderPKs, fileEntry.getFolderId(), fileEntry.getFolderId());
248         }
249 
250         DLFileEntry existingFileEntry = null;
251 
252         try {
253             if ((folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
254                 (folderId > 0)) {
255 
256                 DLFolderUtil.findByPrimaryKey(folderId);
257             }
258 
259             if (context.getDataStrategy().equals(
260                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
261 
262                 try {
263                     existingFileEntry = DLFileEntryUtil.findByUUID_G(
264                         fileEntry.getUuid(), groupId);
265 
266                     if (!isDuplicateFileEntry(fileEntry, existingFileEntry)) {
267                         existingFileEntry =
268                             DLFileEntryLocalServiceUtil.updateFileEntry(
269                                 userId, groupId,
270                                 existingFileEntry.getFolderId(), folderId,
271                                 existingFileEntry.getName(),
272                                 fileEntry.getTitle(), fileEntry.getTitle(),
273                                 fileEntry.getDescription(), null, true,
274                                 fileEntry.getExtraSettings(), is,
275                                 fileEntry.getSize(), serviceContext);
276                     }
277                 }
278                 catch (NoSuchFileEntryException nsfee) {
279                     existingFileEntry =
280                         DLFileEntryLocalServiceUtil.addFileEntry(
281                             fileEntry.getUuid(), userId, groupId, folderId,
282                             fileEntry.getName(), fileEntry.getTitle(),
283                             fileEntry.getDescription(), null,
284                             fileEntry.getExtraSettings(), is,
285                             fileEntry.getSize(), serviceContext);
286                 }
287             }
288             else {
289                 existingFileEntry = DLFileEntryLocalServiceUtil.addFileEntry(
290                     null, userId, groupId, folderId, fileEntry.getName(),
291                     fileEntry.getTitle(), fileEntry.getDescription(), null,
292                     fileEntry.getExtraSettings(), is, fileEntry.getSize(),
293                     serviceContext);
294             }
295 
296             fileEntryNames.put(
297                 fileEntry.getName(), existingFileEntry.getName());
298 
299             if (context.getBooleanParameter(_NAMESPACE, "comments")) {
300                 context.importComments(
301                     DLFileEntry.class, fileEntry.getFileEntryId(),
302                     existingFileEntry.getFileEntryId(), groupId);
303             }
304 
305             if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
306                 context.importRatingsEntries(
307                     DLFileEntry.class, fileEntry.getFileEntryId(),
308                     existingFileEntry.getFileEntryId());
309             }
310         }
311         catch (NoSuchFolderException nsfe) {
312             _log.error(
313                 "Could not find the parent folder for entry " +
314                     fileEntry.getFileEntryId());
315         }
316     }
317 
318     public static void importFileRank(
319             PortletDataContext context, Map<Long, Long> folderPKs,
320             Map<String, String> fileEntryNames, DLFileRank rank)
321         throws Exception {
322 
323         long userId = context.getUserId(rank.getUserUuid());
324         long folderId = MapUtil.getLong(
325             folderPKs, rank.getFolderId(), rank.getFolderId());
326 
327         String name = fileEntryNames.get(rank.getName());
328 
329         if (name == null) {
330             name = rank.getName();
331         }
332 
333         if ((folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
334             (folderId == rank.getFolderId())) {
335 
336             String path = getImportFolderPath(context, folderId);
337 
338             DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
339 
340             importFolder(context, folderPKs, folder);
341 
342             folderId = MapUtil.getLong(
343                 folderPKs, rank.getFolderId(), rank.getFolderId());
344         }
345 
346         try {
347             DLFolderUtil.findByPrimaryKey(folderId);
348 
349             DLFileRankLocalServiceUtil.updateFileRank(
350                 context.getGroupId(), context.getCompanyId(), userId, folderId,
351                 name);
352         }
353         catch (NoSuchFolderException nsfe) {
354             _log.error(
355                 "Could not find the folder for rank " + rank.getFileRankId());
356         }
357     }
358 
359     public static void importFolder(
360             PortletDataContext context, Map<Long, Long> folderPKs,
361             DLFolder folder)
362         throws Exception {
363 
364         long userId = context.getUserId(folder.getUserUuid());
365         long groupId = context.getGroupId();
366         long parentFolderId = MapUtil.getLong(
367             folderPKs, folder.getParentFolderId(), folder.getParentFolderId());
368 
369         ServiceContext serviceContext = new ServiceContext();
370 
371         serviceContext.setAddCommunityPermissions(true);
372         serviceContext.setAddGuestPermissions(true);
373 
374         if ((parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
375             (parentFolderId == folder.getParentFolderId())) {
376 
377             String path = getImportFolderPath(context, parentFolderId);
378 
379             DLFolder parentFolder = (DLFolder)context.getZipEntryAsObject(path);
380 
381             importFolder(context, folderPKs, parentFolder);
382 
383             parentFolderId = MapUtil.getLong(
384                 folderPKs, folder.getParentFolderId(),
385                 folder.getParentFolderId());
386         }
387 
388         DLFolder existingFolder = null;
389 
390         try {
391             if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
392                 DLFolderUtil.findByPrimaryKey(parentFolderId);
393             }
394 
395             if (context.getDataStrategy().equals(
396                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
397 
398                 existingFolder = DLFolderUtil.fetchByUUID_G(
399                     folder.getUuid(), groupId);
400 
401                 if (existingFolder == null) {
402                     String name = getFolderName(
403                         context.getCompanyId(), groupId, parentFolderId,
404                         folder.getName(), 2);
405 
406                     existingFolder = DLFolderLocalServiceUtil.addFolder(
407                         folder.getUuid(), userId, groupId, parentFolderId,
408                         name, folder.getDescription(), serviceContext);
409                 }
410                 else {
411                     existingFolder = DLFolderLocalServiceUtil.updateFolder(
412                         existingFolder.getFolderId(), parentFolderId,
413                         folder.getName(), folder.getDescription(),
414                         serviceContext);
415                 }
416             }
417             else {
418                 String name = getFolderName(
419                     context.getCompanyId(), groupId, parentFolderId,
420                     folder.getName(), 2);
421 
422                 existingFolder = DLFolderLocalServiceUtil.addFolder(
423                     null, userId, groupId, parentFolderId, name,
424                     folder.getDescription(), serviceContext);
425             }
426 
427             folderPKs.put(folder.getFolderId(), existingFolder.getFolderId());
428         }
429         catch (NoSuchFolderException nsfe) {
430             _log.error(
431                 "Could not find the parent folder for folder " +
432                     folder.getFolderId());
433         }
434     }
435 
436     public PortletPreferences deleteData(
437             PortletDataContext context, String portletId,
438             PortletPreferences preferences)
439         throws PortletDataException {
440 
441         try {
442             if (!context.addPrimaryKey(
443                     DLPortletDataHandlerImpl.class, "deleteData")) {
444 
445                 DLFolderLocalServiceUtil.deleteFolders(context.getGroupId());
446             }
447 
448             return null;
449         }
450         catch (Exception e) {
451             throw new PortletDataException(e);
452         }
453     }
454 
455     public String exportData(
456             PortletDataContext context, String portletId,
457             PortletPreferences preferences)
458         throws PortletDataException {
459 
460         try {
461             Document doc = SAXReaderUtil.createDocument();
462 
463             Element root = doc.addElement("documentlibrary-data");
464 
465             root.addAttribute("group-id", String.valueOf(context.getGroupId()));
466 
467             Element foldersEl = root.addElement("folders");
468             Element fileEntriesEl = root.addElement("file-entries");
469             Element fileShortcutsEl = root.addElement("file-shortcuts");
470             Element fileRanksEl = root.addElement("file-ranks");
471 
472             List<DLFolder> folders = DLFolderUtil.findByGroupId(
473                 context.getGroupId());
474 
475             for (DLFolder folder : folders) {
476                 exportFolder(
477                     context, foldersEl, fileEntriesEl, fileShortcutsEl,
478                     fileRanksEl, folder);
479             }
480 
481             List<DLFileEntry> fileEntries = DLFileEntryUtil.findByG_F(
482                 context.getGroupId(),
483                 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
484 
485             for (DLFileEntry fileEntry : fileEntries) {
486                 exportFileEntry(
487                     context, foldersEl, fileEntriesEl, fileRanksEl, fileEntry);
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.addZipEntry(path, fileShortcut);
645         }
646     }
647 
648     protected static void exportParentFolder(
649             PortletDataContext context, Element foldersEl, long folderId)
650         throws PortalException, SystemException {
651 
652         if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
653             return;
654         }
655 
656         DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
657 
658         exportParentFolder(context, foldersEl, folder.getParentFolderId());
659 
660         String path = getFolderPath(context, folder);
661 
662         if (context.isPathNotProcessed(path)) {
663             Element folderEl = foldersEl.addElement("folder");
664 
665             folderEl.addAttribute("path", path);
666 
667             folder.setUserUuid(folder.getUserUuid());
668 
669             context.addZipEntry(path, folder);
670         }
671     }
672 
673     protected static String getFileEntryBinPath(
674         PortletDataContext context, DLFileEntry fileEntry) {
675 
676         StringBundler sb = new StringBundler(7);
677 
678         sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
679         sb.append("/bin/");
680         sb.append(fileEntry.getFileEntryId());
681         sb.append(StringPool.SLASH);
682         sb.append(fileEntry.getVersion());
683         sb.append(StringPool.SLASH);
684         sb.append(fileEntry.getTitle());
685 
686         return sb.toString();
687     }
688 
689     protected static String getFileEntryPath(
690         PortletDataContext context, DLFileEntry fileEntry) {
691 
692         StringBundler sb = new StringBundler(6);
693 
694         sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
695         sb.append("/file-entries/");
696         sb.append(fileEntry.getFileEntryId());
697         sb.append(StringPool.SLASH);
698         sb.append(fileEntry.getVersion());
699         sb.append(".xml");
700 
701         return sb.toString();
702     }
703 
704     protected static String getFolderName(
705             long companyId, long groupId, long parentFolderId, String name,
706             int count)
707         throws SystemException {
708 
709         DLFolder folder = DLFolderUtil.fetchByG_P_N(
710             groupId, parentFolderId, name);
711 
712         if (folder == null) {
713             return name;
714         }
715 
716         if (Pattern.matches(".* \\(\\d+\\)", name)) {
717             int pos = name.lastIndexOf(" (");
718 
719             name = name.substring(0, pos);
720         }
721 
722         StringBundler sb = new StringBundler(5);
723 
724         sb.append(name);
725         sb.append(StringPool.SPACE);
726         sb.append(StringPool.OPEN_PARENTHESIS);
727         sb.append(count);
728         sb.append(StringPool.CLOSE_PARENTHESIS);
729 
730         name = sb.toString();
731 
732         return getFolderName(companyId, groupId, parentFolderId, name, ++count);
733     }
734 
735     protected static String getFolderPath(
736         PortletDataContext context, DLFolder folder) {
737 
738         StringBundler sb = new StringBundler(4);
739 
740         sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
741         sb.append("/folders/");
742         sb.append(folder.getFolderId());
743         sb.append(".xml");
744 
745         return sb.toString();
746     }
747 
748     protected static String getFileRankPath(
749         PortletDataContext context, DLFileRank fileRank) {
750 
751         StringBundler sb = new StringBundler(4);
752 
753         sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
754         sb.append("/ranks/");
755         sb.append(fileRank.getFileRankId());
756         sb.append(".xml");
757 
758         return sb.toString();
759     }
760 
761     protected static String getFileShortcutPath(
762         PortletDataContext context, DLFileShortcut fileShortcut) {
763 
764         StringBundler sb = new StringBundler(4);
765 
766         sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
767         sb.append("/shortcuts/");
768         sb.append(fileShortcut.getFileShortcutId());
769         sb.append(".xml");
770 
771         return sb.toString();
772     }
773 
774     protected static String getImportFolderPath(
775         PortletDataContext context, long folderId) {
776 
777         StringBundler sb = new StringBundler(4);
778 
779         sb.append(context.getSourcePortletPath(PortletKeys.DOCUMENT_LIBRARY));
780         sb.append("/folders/");
781         sb.append(folderId);
782         sb.append(".xml");
783 
784         return sb.toString();
785     }
786 
787     protected static long getRepositoryId(long groupId, long folderId) {
788         if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
789             return groupId;
790         }
791         else {
792             return folderId;
793         }
794     }
795 
796     protected static void importFileShortcut(
797             PortletDataContext context, Map<Long, Long> folderPKs,
798             Map<String, String> fileEntryNames, DLFileShortcut fileShortcut)
799         throws Exception {
800 
801         long userId = context.getUserId(fileShortcut.getUserUuid());
802         long folderId = MapUtil.getLong(
803             folderPKs, fileShortcut.getFolderId(), fileShortcut.getFolderId());
804         long toFolderId = MapUtil.getLong(
805             folderPKs, fileShortcut.getToFolderId(),
806             fileShortcut.getToFolderId());
807         String toName = MapUtil.getString(
808             fileEntryNames, fileShortcut.getToName(), fileShortcut.getToName());
809 
810         try {
811             DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
812             DLFolderUtil.findByPrimaryKey(toFolderId);
813 
814             long groupId = folder.getGroupId();
815 
816             DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
817                 groupId, toFolderId, toName);
818 
819             long[] assetCategoryIds = null;
820             String[] assetTagNames = null;
821 
822             if (context.getBooleanParameter(_NAMESPACE, "categories")) {
823                 assetCategoryIds = context.getAssetCategoryIds(
824                     DLFileEntry.class, fileEntry.getFileEntryId());
825             }
826 
827             if (context.getBooleanParameter(_NAMESPACE, "tags")) {
828                 assetTagNames = context.getAssetTagNames(
829                     DLFileEntry.class, fileEntry.getFileEntryId());
830             }
831 
832             ServiceContext serviceContext = new ServiceContext();
833 
834             serviceContext.setAddCommunityPermissions(true);
835             serviceContext.setAddGuestPermissions(true);
836             serviceContext.setAssetCategoryIds(assetCategoryIds);
837             serviceContext.setAssetTagNames(assetTagNames);
838             serviceContext.setScopeGroupId(context.getGroupId());
839 
840             if (context.getDataStrategy().equals(
841                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
842 
843                 try {
844                     DLFileShortcut existingFileShortcut =
845                         DLFileShortcutUtil.findByUUID_G(
846                             fileShortcut.getUuid(), context.getGroupId());
847 
848                     DLFileShortcutLocalServiceUtil.updateFileShortcut(
849                         userId, existingFileShortcut.getFileShortcutId(),
850                         folderId, toFolderId, toName, serviceContext);
851                 }
852                 catch (NoSuchFileShortcutException nsfse) {
853                     DLFileShortcutLocalServiceUtil.addFileShortcut(
854                         fileShortcut.getUuid(), userId, groupId, folderId,
855                         toFolderId, toName, serviceContext);
856                 }
857             }
858             else {
859                 DLFileShortcutLocalServiceUtil.addFileShortcut(
860                     null, userId, groupId, folderId, toFolderId, toName,
861                     serviceContext);
862             }
863         }
864         catch (NoSuchFolderException nsfe) {
865             _log.error(
866                 "Could not find the folder for shortcut " +
867                     fileShortcut.getFileShortcutId());
868         }
869     }
870 
871     protected static boolean isDuplicateFileEntry(
872         DLFileEntry fileEntry1, DLFileEntry fileEntry2) {
873 
874         try {
875             DLFolder folder1 = fileEntry1.getFolder();
876             DLFolder folder2 = fileEntry2.getFolder();
877 
878             if ((folder1.getUuid().equals(folder2.getUuid())) &&
879                 (fileEntry1.getSize() == fileEntry2.getSize()) &&
880                 (DLUtil.compareVersions(
881                     fileEntry1.getVersion(), fileEntry2.getVersion()) == 0) &&
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 }