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