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