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