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