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