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