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