1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.lar.PortletDataContext;
28  import com.liferay.portal.kernel.lar.PortletDataException;
29  import com.liferay.portal.kernel.lar.PortletDataHandler;
30  import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
31  import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
32  import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
33  import com.liferay.portal.kernel.util.FileUtil;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.util.DocumentUtil;
36  import com.liferay.portal.util.PortletKeys;
37  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
38  import com.liferay.portlet.documentlibrary.NoSuchFileShortcutException;
39  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
40  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
41  import com.liferay.portlet.documentlibrary.model.DLFileRank;
42  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
43  import com.liferay.portlet.documentlibrary.model.DLFolder;
44  import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
45  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
46  import com.liferay.portlet.documentlibrary.service.DLFileRankLocalServiceUtil;
47  import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalServiceUtil;
48  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
49  import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryFinderUtil;
50  import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryUtil;
51  import com.liferay.portlet.documentlibrary.service.persistence.DLFileRankUtil;
52  import com.liferay.portlet.documentlibrary.service.persistence.DLFileShortcutFinderUtil;
53  import com.liferay.portlet.documentlibrary.service.persistence.DLFileShortcutUtil;
54  import com.liferay.portlet.documentlibrary.service.persistence.DLFolderUtil;
55  import com.liferay.util.MapUtil;
56  import com.liferay.util.xml.XMLFormatter;
57  
58  import java.io.IOException;
59  import java.io.InputStream;
60  
61  import java.util.List;
62  import java.util.Map;
63  import java.util.regex.Pattern;
64  
65  import javax.portlet.PortletPreferences;
66  
67  import org.apache.commons.logging.Log;
68  import org.apache.commons.logging.LogFactory;
69  
70  import org.dom4j.Document;
71  import org.dom4j.DocumentHelper;
72  import org.dom4j.Element;
73  
74  /**
75   * <a href="DLPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
76   *
77   * @author Bruno Farache
78   * @author Raymond Augé
79   *
80   */
81  public class DLPortletDataHandlerImpl implements PortletDataHandler {
82  
83      public PortletPreferences deleteData(
84              PortletDataContext context, String portletId,
85              PortletPreferences prefs)
86          throws PortletDataException {
87  
88          try {
89              if (!context.addPrimaryKey(
90                      DLPortletDataHandlerImpl.class, "deleteData")) {
91  
92                  DLFolderLocalServiceUtil.deleteFolders(context.getGroupId());
93              }
94  
95              return null;
96          }
97          catch (Exception e) {
98              throw new PortletDataException(e);
99          }
100     }
101 
102     public String exportData(
103             PortletDataContext context, String portletId,
104             PortletPreferences prefs)
105         throws PortletDataException {
106 
107         try {
108             Document doc = DocumentHelper.createDocument();
109 
110             Element root = doc.addElement("documentlibrary-data");
111 
112             root.addAttribute("group-id", String.valueOf(context.getGroupId()));
113 
114             Element foldersEl = root.addElement("folders");
115             Element fileEntriesEl = root.addElement("file-entries");
116             Element fileShortcutsEl = root.addElement("file-shortcuts");
117             Element fileRanksEl = root.addElement("file-ranks");
118 
119             List<DLFolder> folders = DLFolderUtil.findByGroupId(
120                 context.getGroupId());
121 
122             for (DLFolder folder : folders) {
123                 exportFolder(
124                     context, foldersEl, fileEntriesEl, fileShortcutsEl,
125                     fileRanksEl, folder);
126             }
127 
128             return XMLFormatter.toString(doc);
129         }
130         catch (Exception e) {
131             throw new PortletDataException(e);
132         }
133     }
134 
135     public PortletDataHandlerControl[] getExportControls() {
136         return new PortletDataHandlerControl[] {
137             _foldersAndDocuments, _shortcuts, _ranks, _comments, _ratings, _tags
138         };
139     }
140 
141     public PortletDataHandlerControl[] getImportControls() {
142         return new PortletDataHandlerControl[] {
143             _foldersAndDocuments, _shortcuts, _ranks, _comments, _ratings, _tags
144         };
145     }
146 
147     public PortletPreferences importData(
148             PortletDataContext context, String portletId,
149             PortletPreferences prefs, String data)
150         throws PortletDataException {
151 
152         try {
153             Document doc = DocumentUtil.readDocumentFromXML(data);
154 
155             Element root = doc.getRootElement();
156 
157             List<Element> folderEls = root.element("folders").elements(
158                 "folder");
159 
160             Map<Long, Long> folderPKs = context.getNewPrimaryKeysMap(
161                 DLFolder.class);
162 
163             for (Element folderEl : folderEls) {
164                 String path = folderEl.attributeValue("path");
165 
166                 if (context.isPathNotProcessed(path)) {
167                     DLFolder folder = (DLFolder)context.getZipEntryAsObject(
168                         path);
169 
170                     importFolder(context, folderPKs, folder);
171                 }
172             }
173 
174             List<Element> fileEntryEls = root.element("file-entries").elements(
175                 "file-entry");
176 
177             Map<String, String> fileEntryNames = context.getNewPrimaryKeysMap(
178                 DLFileEntry.class);
179 
180             for (Element fileEntryEl : fileEntryEls) {
181                 String path = fileEntryEl.attributeValue("path");
182                 String binPath = fileEntryEl.attributeValue("bin-path");
183 
184                 if (context.isPathNotProcessed(path)) {
185                     DLFileEntry fileEntry =
186                         (DLFileEntry)context.getZipEntryAsObject(path);
187 
188                     importFileEntry(
189                         context, folderPKs, fileEntryNames, fileEntry, binPath);
190                 }
191             }
192 
193             if (context.getBooleanParameter(_NAMESPACE, "shortcuts")) {
194                 List<Element> fileShortcutEls = root.element(
195                     "file-shortcuts").elements("file-shortcut");
196 
197                 for (Element fileShortcutEl : fileShortcutEls) {
198                     String path = fileShortcutEl.attributeValue("path");
199 
200                     if (context.isPathNotProcessed(path)) {
201                         DLFileShortcut fileShortcut =
202                             (DLFileShortcut)context.getZipEntryAsObject(path);
203 
204                         importFileShortcut(
205                             context, folderPKs, fileEntryNames, fileShortcut);
206                     }
207                 }
208             }
209 
210             if (context.getBooleanParameter(_NAMESPACE, "ranks")) {
211                 List<Element> fileRankEls = root.element("file-ranks").elements(
212                     "file-rank");
213 
214                 for (Element fileRankEl : fileRankEls) {
215                     String path = fileRankEl.attributeValue("path");
216 
217                     if (context.isPathNotProcessed(path)) {
218                         DLFileRank fileRank =
219                             (DLFileRank)context.getZipEntryAsObject(path);
220 
221                         importFileRank(
222                             context, folderPKs, fileEntryNames, fileRank);
223                     }
224                 }
225             }
226 
227             return null;
228         }
229         catch (Exception e) {
230             throw new PortletDataException(e);
231         }
232     }
233 
234     public boolean isPublishToLiveByDefault() {
235         return false;
236     }
237 
238     protected void exportFileEntry(
239             PortletDataContext context, Element foldersEl,
240             Element fileEntriesEl, Element fileRanksEl, DLFileEntry fileEntry)
241         throws PortalException, SystemException {
242 
243         if (!context.isWithinDateRange(fileEntry.getModifiedDate())) {
244             return;
245         }
246 
247         String path = getFileEntryPath(context, fileEntry);
248         String binPath = getFileEntryBinPath(context, fileEntry);
249 
250         Element fileEntryEl = fileEntriesEl.addElement("file-entry");
251 
252         fileEntryEl.addAttribute("path", path);
253         fileEntryEl.addAttribute("bin-path", binPath);
254 
255         if (context.isPathNotProcessed(path)) {
256             fileEntry.setUserUuid(fileEntry.getUserUuid());
257 
258             if (context.getBooleanParameter(_NAMESPACE, "comments")) {
259                 context.addComments(
260                     DLFileEntry.class, fileEntry.getFileEntryId());
261             }
262 
263             if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
264                 context.addRatingsEntries(
265                     DLFileEntry.class, fileEntry.getFileEntryId());
266             }
267 
268             if (context.getBooleanParameter(_NAMESPACE, "tags")) {
269                 context.addTagsEntries(
270                     DLFileEntry.class, fileEntry.getFileEntryId());
271             }
272 
273             InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
274                 fileEntry.getCompanyId(), fileEntry.getUserId(),
275                 fileEntry.getFolderId(), fileEntry.getName());
276 
277             try {
278                 context.addZipEntry(
279                     getFileEntryBinPath(context, fileEntry),
280                     FileUtil.getBytes(is));
281             }
282             catch (IOException ioe) {
283                 throw new SystemException(ioe);
284             }
285 
286             context.addZipEntry(path, fileEntry);
287 
288             if (context.getBooleanParameter(_NAMESPACE, "ranks")) {
289                 List<DLFileRank> fileRanks = DLFileRankUtil.findByF_N(
290                     fileEntry.getFolderId(), fileEntry.getName());
291 
292                 for (DLFileRank fileRank : fileRanks) {
293                     exportFileRank(context, fileRanksEl, fileRank);
294                 }
295             }
296         }
297 
298         exportParentFolder(context, foldersEl, fileEntry.getFolderId());
299     }
300 
301     protected void exportFileRank(
302             PortletDataContext context, Element fileRanksEl,
303             DLFileRank fileRank)
304         throws SystemException {
305 
306         String path = getFileRankPath(context, fileRank);
307 
308         Element fileRankEl = fileRanksEl.addElement("file-rank");
309 
310         fileRankEl.addAttribute("path", path);
311 
312         if (context.isPathNotProcessed(path)) {
313             fileRank.setUserUuid(fileRank.getUserUuid());
314 
315             context.addZipEntry(path, fileRank);
316         }
317     }
318 
319     protected void exportFileShortcut(
320             PortletDataContext context, Element foldersEl,
321             Element fileShortcutsEl, DLFileShortcut fileShortcut)
322         throws PortalException, SystemException {
323 
324         String path = getFileShortcutPath(context, fileShortcut);
325 
326         Element fileShortcutEl = fileShortcutsEl.addElement("file-shortcut");
327 
328         fileShortcutEl.addAttribute("path", path);
329 
330         if (context.isPathNotProcessed(path)) {
331             fileShortcut.setUserUuid(fileShortcut.getUserUuid());
332 
333             context.addZipEntry(path, fileShortcut);
334         }
335 
336         exportParentFolder(context, foldersEl, fileShortcut.getFolderId());
337     }
338 
339     protected void exportFolder(
340             PortletDataContext context, Element foldersEl,
341             Element fileEntriesEl, Element fileShortcutsEl, Element fileRanksEl,
342             DLFolder folder)
343         throws PortalException, SystemException {
344 
345         if (context.isWithinDateRange(folder.getModifiedDate())) {
346             String path = getFolderPath(context, folder);
347 
348             Element folderEl = foldersEl.addElement("folder");
349 
350             folderEl.addAttribute("path", path);
351 
352             if (context.isPathNotProcessed(path)) {
353                 folder.setUserUuid(folder.getUserUuid());
354 
355                 context.addZipEntry(path, folder);
356             }
357 
358             exportParentFolder(context, foldersEl, folder.getParentFolderId());
359         }
360 
361         List<DLFileEntry> fileEntries = DLFileEntryUtil.findByFolderId(
362         folder.getFolderId());
363 
364         for (DLFileEntry fileEntry : fileEntries) {
365             exportFileEntry(
366                 context, foldersEl, fileEntriesEl, fileRanksEl, fileEntry);
367         }
368 
369         if (context.getBooleanParameter(_NAMESPACE, "shortcuts")) {
370             List<DLFileShortcut> fileShortcuts =
371                 DLFileShortcutUtil.findByFolderId(folder.getFolderId());
372 
373             for (DLFileShortcut fileShortcut : fileShortcuts) {
374                 exportFileShortcut(
375                     context, foldersEl, fileShortcutsEl, fileShortcut);
376             }
377         }
378     }
379 
380     protected void exportParentFolder(
381             PortletDataContext context, Element foldersEl, long folderId)
382         throws PortalException, SystemException {
383 
384         if ((!context.hasDateRange()) ||
385             (folderId == DLFolderImpl.DEFAULT_PARENT_FOLDER_ID)) {
386 
387             return;
388         }
389 
390         DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
391 
392         String path = getFolderPath(context, folder);
393 
394         Element folderEl = foldersEl.addElement("folder");
395 
396         folderEl.addAttribute("path", path);
397 
398         if (context.isPathNotProcessed(path)) {
399             folder.setUserUuid(folder.getUserUuid());
400 
401             context.addZipEntry(path, folder);
402         }
403 
404         exportParentFolder(context, foldersEl, folder.getParentFolderId());
405     }
406 
407     protected String getFileEntryBinPath(
408         PortletDataContext context, DLFileEntry fileEntry) {
409 
410         StringBuilder sb = new StringBuilder();
411 
412         sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
413         sb.append("/bin/");
414         sb.append(fileEntry.getFileEntryId());
415         sb.append(StringPool.SLASH);
416         sb.append(fileEntry.getVersion());
417         sb.append(StringPool.SLASH);
418         sb.append(fileEntry.getTitleWithExtension());
419 
420         return sb.toString();
421     }
422 
423     protected String getFileEntryPath(
424         PortletDataContext context, DLFileEntry fileEntry) {
425 
426         StringBuilder sb = new StringBuilder();
427 
428         sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
429         sb.append("/entries/");
430         sb.append(fileEntry.getFileEntryId());
431         sb.append(StringPool.SLASH);
432         sb.append(fileEntry.getVersion());
433         sb.append(".xml");
434 
435         return sb.toString();
436     }
437 
438     protected String getFolderName(
439             long companyId, long groupId, long parentFolderId, String name,
440             int count)
441         throws SystemException {
442 
443         DLFolder folder = DLFolderUtil.fetchByG_P_N(
444             groupId, parentFolderId, name);
445 
446         if (folder == null) {
447             return name;
448         }
449 
450         if (Pattern.matches(".* \\(\\d+\\)", name)) {
451             int pos = name.lastIndexOf(" (");
452 
453             name = name.substring(0, pos);
454         }
455 
456         StringBuilder sb = new StringBuilder();
457 
458         sb.append(name);
459         sb.append(StringPool.SPACE);
460         sb.append(StringPool.OPEN_PARENTHESIS);
461         sb.append(count);
462         sb.append(StringPool.CLOSE_PARENTHESIS);
463 
464         name = sb.toString();
465 
466         return getFolderName(companyId, groupId, parentFolderId, name, ++count);
467     }
468 
469     protected String getFolderPath(
470         PortletDataContext context, DLFolder folder) {
471 
472         return context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY) +
473             "/folders/" + folder.getFolderId() + ".xml";
474     }
475 
476     protected String getFileRankPath(
477         PortletDataContext context, DLFileRank fileRank) {
478 
479         return context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY) +
480             "/ranks/" + fileRank.getFileRankId() + ".xml";
481     }
482 
483     protected String getFileShortcutPath(
484         PortletDataContext context, DLFileShortcut fileShortcut) {
485 
486         return context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY) +
487             "/shortcut/" + fileShortcut.getFileShortcutId() + ".xml";
488     }
489 
490     protected void importFileEntry(
491             PortletDataContext context, Map<Long, Long> folderPKs,
492             Map<String, String> fileEntryNames, DLFileEntry fileEntry,
493             String binPath)
494         throws Exception {
495 
496         long userId = context.getUserId(fileEntry.getUserUuid());
497         long folderId = MapUtil.getLong(
498             folderPKs, fileEntry.getFolderId(), fileEntry.getFolderId());
499 
500         String[] tagsEntries = null;
501 
502         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
503             tagsEntries = context.getTagsEntries(
504                 DLFileEntry.class, fileEntry.getFileEntryId());
505         }
506 
507         boolean addCommunityPermissions = true;
508         boolean addGuestPermissions = true;
509 
510         byte[] bytes = context.getZipEntryAsByteArray(binPath);
511 
512         DLFileEntry existingFileEntry = null;
513 
514         try {
515             DLFolderUtil.findByPrimaryKey(folderId);
516 
517             if (context.getDataStrategy().equals(
518                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
519 
520                 try {
521                     existingFileEntry = DLFileEntryFinderUtil.findByUuid_G(
522                         fileEntry.getUuid(), context.getGroupId());
523 
524                     existingFileEntry =
525                         DLFileEntryLocalServiceUtil.updateFileEntry(
526                             userId, existingFileEntry.getFolderId(), folderId,
527                             existingFileEntry.getName(), fileEntry.getName(),
528                             fileEntry.getTitle(), fileEntry.getDescription(),
529                             tagsEntries, fileEntry.getExtraSettings(), bytes);
530                 }
531                 catch (NoSuchFileEntryException nsfee) {
532                     existingFileEntry =
533                     DLFileEntryLocalServiceUtil.addFileEntry(
534                         fileEntry.getUuid(), userId, folderId,
535                         fileEntry.getName(), fileEntry.getTitle(),
536                         fileEntry.getDescription(), tagsEntries,
537                         fileEntry.getExtraSettings(), bytes,
538                         addCommunityPermissions, addGuestPermissions);
539                 }
540             }
541             else {
542                 existingFileEntry = DLFileEntryLocalServiceUtil.addFileEntry(
543                     userId, folderId, fileEntry.getName(), fileEntry.getTitle(),
544                     fileEntry.getDescription(), tagsEntries,
545                     fileEntry.getExtraSettings(), bytes,
546                     addCommunityPermissions, addGuestPermissions);
547             }
548 
549             fileEntryNames.put(
550                 fileEntry.getName(), existingFileEntry.getName());
551 
552             if (context.getBooleanParameter(_NAMESPACE, "comments")) {
553                 context.importComments(
554                     DLFileEntry.class, fileEntry.getFileEntryId(),
555                     existingFileEntry.getFileEntryId(), context.getGroupId());
556             }
557 
558             if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
559                 context.importRatingsEntries(
560                     DLFileEntry.class, fileEntry.getFileEntryId(),
561                     existingFileEntry.getFileEntryId());
562             }
563         }
564         catch (NoSuchFolderException nsfe) {
565             _log.error(
566                 "Could not find the parent folder for entry " +
567                     fileEntry.getFileEntryId());
568         }
569     }
570 
571     protected void importFileRank(
572             PortletDataContext context, Map<Long, Long> folderPKs,
573             Map<String, String> fileEntryNames, DLFileRank rank)
574         throws Exception {
575 
576         long userId = context.getUserId(rank.getUserUuid());
577         long folderId = MapUtil.getLong(
578             folderPKs, rank.getFolderId(), rank.getFolderId());
579 
580         String name = fileEntryNames.get(rank.getName());
581 
582         if (name == null) {
583             name = rank.getName();
584         }
585 
586         try {
587             DLFolderUtil.findByPrimaryKey(folderId);
588 
589             DLFileRankLocalServiceUtil.updateFileRank(
590                 context.getGroupId(), context.getCompanyId(), userId, folderId,
591                 name);
592         }
593         catch (NoSuchFolderException nsfe) {
594             _log.error(
595                 "Could not find the folder for rank " + rank.getFileRankId());
596         }
597     }
598 
599     protected void importFileShortcut(
600             PortletDataContext context, Map<Long, Long> folderPKs,
601             Map<String, String> fileEntryNames, DLFileShortcut fileShortcut)
602         throws Exception {
603 
604         long userId = context.getUserId(fileShortcut.getUserUuid());
605         long folderId = MapUtil.getLong(
606             folderPKs, fileShortcut.getFolderId(), fileShortcut.getFolderId());
607         long toFolderId = MapUtil.getLong(
608             folderPKs, fileShortcut.getToFolderId(),
609             fileShortcut.getToFolderId());
610         String toName = MapUtil.getString(
611             fileEntryNames, fileShortcut.getToName(), fileShortcut.getToName());
612 
613         boolean addCommunityPermissions = true;
614         boolean addGuestPermissions = true;
615 
616         try {
617             DLFolderUtil.findByPrimaryKey(folderId);
618             DLFolderUtil.findByPrimaryKey(toFolderId);
619 
620             if (context.getDataStrategy().equals(
621                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
622 
623                 try {
624                     DLFileShortcut existingFileShortcut =
625                         DLFileShortcutFinderUtil.findByUuid_G(
626                             fileShortcut.getUuid(), context.getGroupId());
627 
628                     DLFileShortcutLocalServiceUtil.updateFileShortcut(
629                         userId, existingFileShortcut.getFileShortcutId(),
630                         folderId, toFolderId, toName);
631                 }
632                 catch (NoSuchFileShortcutException nsfse) {
633                     DLFileShortcutLocalServiceUtil.addFileShortcut(
634                         fileShortcut.getUuid(), userId, folderId, toFolderId,
635                         toName, addCommunityPermissions, addGuestPermissions);
636                 }
637             }
638             else {
639                 DLFileShortcutLocalServiceUtil.addFileShortcut(
640                     userId, folderId, toFolderId, toName,
641                     addCommunityPermissions, addGuestPermissions);
642             }
643         }
644         catch (NoSuchFolderException nsfe) {
645             _log.error(
646                 "Could not find the folder for shortcut " +
647                     fileShortcut.getFileShortcutId());
648         }
649     }
650 
651     protected void importFolder(
652             PortletDataContext context, Map<Long, Long> folderPKs,
653             DLFolder folder)
654         throws Exception {
655 
656         long userId = context.getUserId(folder.getUserUuid());
657         long plid = context.getPlid();
658         long parentFolderId = MapUtil.getLong(
659             folderPKs, folder.getParentFolderId(), folder.getParentFolderId());
660 
661         boolean addCommunityPermissions = true;
662         boolean addGuestPermissions = true;
663 
664         DLFolder existingFolder = null;
665 
666         try {
667             if (parentFolderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
668                 DLFolderUtil.findByPrimaryKey(parentFolderId);
669             }
670 
671             if (context.getDataStrategy().equals(
672                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
673 
674                 existingFolder = DLFolderUtil.fetchByUUID_G(
675                     folder.getUuid(), context.getGroupId());
676 
677                 if (existingFolder == null) {
678                     String name = getFolderName(
679                         context.getCompanyId(), context.getGroupId(),
680                         parentFolderId, folder.getName(), 2);
681 
682                     existingFolder = DLFolderLocalServiceUtil.addFolder(
683                         folder.getUuid(), userId, plid, parentFolderId,
684                         name, folder.getDescription(), addCommunityPermissions,
685                         addGuestPermissions);
686                 }
687                 else {
688                     existingFolder = DLFolderLocalServiceUtil.updateFolder(
689                         existingFolder.getFolderId(), parentFolderId,
690                         folder.getName(), folder.getDescription());
691                 }
692             }
693             else {
694                 String name = getFolderName(
695                     context.getCompanyId(), context.getGroupId(),
696                     parentFolderId, folder.getName(), 2);
697 
698                 existingFolder = DLFolderLocalServiceUtil.addFolder(
699                     userId, plid, parentFolderId, name, folder.getDescription(),
700                     addCommunityPermissions, addGuestPermissions);
701             }
702 
703             folderPKs.put(folder.getFolderId(), existingFolder.getFolderId());
704         }
705         catch (NoSuchFolderException nsfe) {
706             _log.error(
707                 "Could not find the parent folder for folder " +
708                     folder.getFolderId());
709         }
710     }
711 
712     private static final String _NAMESPACE = "document_library";
713 
714     private static final PortletDataHandlerBoolean _foldersAndDocuments =
715         new PortletDataHandlerBoolean(
716             _NAMESPACE, "folders-and-documents", true, true);
717 
718     private static final PortletDataHandlerBoolean _ranks =
719         new PortletDataHandlerBoolean(_NAMESPACE, "ranks");
720 
721     private static final PortletDataHandlerBoolean _shortcuts=
722         new PortletDataHandlerBoolean(_NAMESPACE, "shortcuts");
723 
724     private static final PortletDataHandlerBoolean _comments =
725         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
726 
727     private static final PortletDataHandlerBoolean _ratings =
728         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
729 
730     private static final PortletDataHandlerBoolean _tags =
731         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
732 
733     private static Log _log = LogFactory.getLog(DLPortletDataHandlerImpl.class);
734 
735 }