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