001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.lar;
016    
017    import com.liferay.documentlibrary.DuplicateFileException;
018    import com.liferay.documentlibrary.service.DLLocalServiceUtil;
019    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
020    import com.liferay.portal.kernel.lar.PortletDataContext;
021    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
022    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.search.Indexer;
026    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
027    import com.liferay.portal.kernel.util.MapUtil;
028    import com.liferay.portal.kernel.util.StringBundler;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.workflow.WorkflowConstants;
031    import com.liferay.portal.kernel.xml.Document;
032    import com.liferay.portal.kernel.xml.Element;
033    import com.liferay.portal.kernel.xml.SAXReaderUtil;
034    import com.liferay.portal.service.ServiceContext;
035    import com.liferay.portal.util.PortletKeys;
036    import com.liferay.portal.util.PropsValues;
037    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
038    import com.liferay.portlet.documentlibrary.model.DLFileRank;
039    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
040    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
041    import com.liferay.portlet.documentlibrary.model.DLFolder;
042    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
043    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
044    import com.liferay.portlet.documentlibrary.service.DLFileRankLocalServiceUtil;
045    import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalServiceUtil;
046    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
047    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
048    import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryUtil;
049    import com.liferay.portlet.documentlibrary.service.persistence.DLFileRankUtil;
050    import com.liferay.portlet.documentlibrary.service.persistence.DLFileShortcutUtil;
051    import com.liferay.portlet.documentlibrary.service.persistence.DLFolderUtil;
052    import com.liferay.portlet.documentlibrary.util.DLUtil;
053    import com.liferay.util.PwdGenerator;
054    
055    import java.io.IOException;
056    import java.io.InputStream;
057    
058    import java.util.List;
059    import java.util.Map;
060    import java.util.regex.Pattern;
061    
062    import javax.portlet.PortletPreferences;
063    
064    /**
065     * @author Bruno Farache
066     * @author Raymond Augé
067     */
068    public class DLPortletDataHandlerImpl extends BasePortletDataHandler {
069    
070            public static void exportFileEntry(
071                            PortletDataContext context, Element foldersElement,
072                            Element fileEntriesElement, Element fileRanksElement,
073                            DLFileEntry fileEntry)
074                    throws Exception {
075    
076                    if (!context.isWithinDateRange(fileEntry.getModifiedDate())) {
077                            return;
078                    }
079    
080                    DLFileVersion fileVersion =
081                            DLFileVersionLocalServiceUtil.getFileVersion(
082                                    context.getScopeGroupId(), fileEntry.getFolderId(),
083                                    fileEntry.getName(), fileEntry.getVersion());
084    
085                    if (fileVersion.getStatus() != WorkflowConstants.STATUS_APPROVED) {
086                            return;
087                    }
088    
089                    if (foldersElement != null) {
090                            exportParentFolder(
091                                    context, foldersElement, fileEntry.getFolderId());
092                    }
093    
094                    String path = getFileEntryPath(context, fileEntry);
095    
096                    if (context.isPathNotProcessed(path)) {
097                            Element fileEntryElement = fileEntriesElement.addElement(
098                                    "file-entry");
099    
100                            fileEntryElement.addAttribute("path", path);
101    
102                            String binPath = getFileEntryBinPath(context, fileEntry);
103    
104                            fileEntryElement.addAttribute("bin-path", binPath);
105    
106                            fileEntry.setUserUuid(fileEntry.getUserUuid());
107    
108                            context.addLocks(
109                                    DLFileEntry.class,
110                                    DLUtil.getLockId(
111                                            fileEntry.getGroupId(), fileEntry.getFolderId(),
112                                            fileEntry.getName()));
113    
114                            context.addPermissions(
115                                    DLFileEntry.class, fileEntry.getFileEntryId());
116    
117                            if (context.getBooleanParameter(_NAMESPACE, "categories")) {
118                                    context.addAssetCategories(
119                                            DLFileEntry.class, fileEntry.getFileEntryId());
120                            }
121    
122                            if (context.getBooleanParameter(_NAMESPACE, "comments")) {
123                                    context.addComments(
124                                            DLFileEntry.class, fileEntry.getFileEntryId());
125                            }
126    
127                            if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
128                                    context.addRatingsEntries(
129                                            DLFileEntry.class, fileEntry.getFileEntryId());
130                            }
131    
132                            if (context.getBooleanParameter(_NAMESPACE, "tags")) {
133                                    context.addAssetTags(
134                                            DLFileEntry.class, fileEntry.getFileEntryId());
135                            }
136    
137                            long repositoryId = getRepositoryId(
138                                    fileEntry.getGroupId(), fileEntry.getFolderId());
139    
140                            InputStream is = DLLocalServiceUtil.getFileAsStream(
141                                    fileEntry.getCompanyId(), repositoryId, fileEntry.getName(),
142                                    fileEntry.getVersion());
143    
144                            if (is == null) {
145                                    if (_log.isWarnEnabled()) {
146                                            _log.warn(
147                                                    "No file found for file entry " +
148                                                            fileEntry.getFileEntryId());
149                                    }
150    
151                                    fileEntryElement.detach();
152    
153                                    return;
154                            }
155    
156                            try {
157                                    context.addZipEntry(
158                                            getFileEntryBinPath(context, fileEntry), is);
159                            }
160                            finally {
161                                    try {
162                                            is.close();
163                                    }
164                                    catch (IOException ioe) {
165                                            _log.error(ioe, ioe);
166                                    }
167                            }
168    
169                            context.addZipEntry(path, fileEntry);
170    
171                            if (context.getBooleanParameter(_NAMESPACE, "ranks")) {
172                                    List<DLFileRank> fileRanks = DLFileRankUtil.findByF_N(
173                                            fileEntry.getFolderId(), fileEntry.getName());
174    
175                                    for (DLFileRank fileRank : fileRanks) {
176                                            exportFileRank(context, fileRanksElement, fileRank);
177                                    }
178                            }
179                    }
180            }
181    
182            public static String getFileEntryPath(
183                    PortletDataContext context, DLFileEntry fileEntry) {
184    
185                    StringBundler sb = new StringBundler(6);
186    
187                    sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
188                    sb.append("/file-entries/");
189                    sb.append(fileEntry.getFileEntryId());
190                    sb.append(StringPool.SLASH);
191                    sb.append(fileEntry.getVersion());
192                    sb.append(".xml");
193    
194                    return sb.toString();
195            }
196    
197            public static void importFileEntry(
198                            PortletDataContext context, Element fileEntryElement)
199                    throws Exception {
200    
201                    String path = fileEntryElement.attributeValue("path");
202    
203                    if (!context.isPathNotProcessed(path)) {
204                            return;
205                    }
206    
207                    DLFileEntry fileEntry = (DLFileEntry)context.getZipEntryAsObject(path);
208    
209                    String binPath = fileEntryElement.attributeValue("bin-path");
210    
211                    long userId = context.getUserId(fileEntry.getUserUuid());
212    
213                    Map<Long, Long> folderPKs =
214                            (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFolder.class);
215    
216                    long folderId = MapUtil.getLong(
217                            folderPKs, fileEntry.getFolderId(), fileEntry.getFolderId());
218    
219                    long[] assetCategoryIds = null;
220                    String[] assetTagNames = null;
221    
222                    if (context.getBooleanParameter(_NAMESPACE, "categories")) {
223                            assetCategoryIds = context.getAssetCategoryIds(
224                                    DLFileEntry.class, fileEntry.getFileEntryId());
225                    }
226    
227                    if (context.getBooleanParameter(_NAMESPACE, "tags")) {
228                            assetTagNames = context.getAssetTagNames(
229                                    DLFileEntry.class, fileEntry.getFileEntryId());
230                    }
231    
232                    ServiceContext serviceContext = new ServiceContext();
233    
234                    serviceContext.setAddCommunityPermissions(true);
235                    serviceContext.setAddGuestPermissions(true);
236                    serviceContext.setAssetCategoryIds(assetCategoryIds);
237                    serviceContext.setAssetTagNames(assetTagNames);
238                    serviceContext.setCreateDate(fileEntry.getCreateDate());
239                    serviceContext.setModifiedDate(fileEntry.getModifiedDate());
240                    serviceContext.setScopeGroupId(context.getScopeGroupId());
241    
242                    InputStream is = context.getZipEntryAsInputStream(binPath);
243    
244                    if ((folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
245                            (folderId == fileEntry.getFolderId())) {
246    
247                            String folderPath = getImportFolderPath(context, folderId);
248    
249                            DLFolder folder = (DLFolder)context.getZipEntryAsObject(folderPath);
250    
251                            importFolder(context, folder);
252    
253                            folderId = MapUtil.getLong(
254                                    folderPKs, fileEntry.getFolderId(), fileEntry.getFolderId());
255                    }
256    
257                    DLFileEntry importedFileEntry = null;
258    
259                    String nameWithExtension =
260                            fileEntry.getName().concat(StringPool.PERIOD).concat(
261                                    fileEntry.getExtension());
262    
263                    if (context.isDataStrategyMirror()) {
264                            DLFileEntry existingFileEntry = DLFileEntryUtil.fetchByUUID_G(
265                                    fileEntry.getUuid(), context.getScopeGroupId());
266    
267                            if (existingFileEntry == null) {
268                                    serviceContext.setUuid(fileEntry.getUuid());
269    
270                                    importedFileEntry =
271                                            DLFileEntryLocalServiceUtil.addFileEntry(
272                                                    userId, context.getScopeGroupId(), folderId,
273                                                    nameWithExtension, fileEntry.getTitle(),
274                                                    fileEntry.getDescription(), null,
275                                                    fileEntry.getExtraSettings(), is, fileEntry.getSize(),
276                                                    serviceContext);
277                            }
278                            else if (!isDuplicateFileEntry(fileEntry, existingFileEntry)) {
279                                    importedFileEntry = DLFileEntryLocalServiceUtil.updateFileEntry(
280                                            userId, context.getScopeGroupId(),
281                                            existingFileEntry.getFolderId(),
282                                            existingFileEntry.getName(), fileEntry.getTitle(),
283                                            fileEntry.getTitle(), fileEntry.getDescription(), null,
284                                            true, fileEntry.getExtraSettings(), is, fileEntry.getSize(),
285                                            serviceContext);
286                            }
287                            else {
288                                    DLFileVersion latestFileVersion =
289                                            DLFileVersionLocalServiceUtil.getLatestFileVersion(
290                                                    context.getScopeGroupId(), folderId,
291                                                    existingFileEntry.getName());
292    
293                                    DLFileEntryLocalServiceUtil.updateAsset(
294                                            userId, existingFileEntry, latestFileVersion,
295                                            assetCategoryIds, assetTagNames);
296    
297                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
298                                            DLFileEntry.class);
299    
300                                    indexer.reindex(existingFileEntry);
301    
302                                    importedFileEntry = existingFileEntry;
303                            }
304                    }
305                    else {
306                            String title = fileEntry.getTitle();
307    
308                            try {
309                                    importedFileEntry =
310                                            DLFileEntryLocalServiceUtil.addFileEntry(
311                                                    userId, context.getScopeGroupId(), folderId,
312                                                    nameWithExtension, title, fileEntry.getDescription(),
313                                                    null, fileEntry.getExtraSettings(), is,
314                                                    fileEntry.getSize(), serviceContext);
315                            }
316                            catch (DuplicateFileException dfe) {
317                                    String[] titleParts = title.split("\\.", 2);
318    
319                                    title = titleParts[0] + PwdGenerator.getPassword();
320    
321                                    if (titleParts.length > 1) {
322                                            title += StringPool.PERIOD + titleParts[1];
323                                    }
324    
325                                    importedFileEntry =
326                                            DLFileEntryLocalServiceUtil.addFileEntry(
327                                                    userId, context.getScopeGroupId(), folderId,
328                                                    nameWithExtension, title, fileEntry.getDescription(),
329                                                    null, fileEntry.getExtraSettings(), is,
330                                                    fileEntry.getSize(), serviceContext);
331                            }
332                    }
333    
334                    Map<Long, Long> fileEntryPKs =
335                            (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFileEntry.class);
336    
337                    fileEntryPKs.put(
338                            fileEntry.getFileEntryId(), importedFileEntry.getFileEntryId());
339    
340                    Map<String, String> fileEntryNames =
341                            (Map<String, String>)context.getNewPrimaryKeysMap(
342                                    DLFileEntry.class.getName() + ".name");
343    
344                    fileEntryNames.put(fileEntry.getName(), importedFileEntry.getName());
345    
346                    String lockKey = DLUtil.getLockId(
347                            fileEntry.getGroupId(), fileEntry.getFolderId(),
348                            fileEntry.getName());
349    
350                    String newLockKey = DLUtil.getLockId(
351                            importedFileEntry.getGroupId(), importedFileEntry.getFolderId(),
352                            importedFileEntry.getName());
353    
354                    context.importLocks(DLFileEntry.class, lockKey, newLockKey);
355    
356                    context.importPermissions(
357                            DLFileEntry.class, fileEntry.getFileEntryId(),
358                            importedFileEntry.getFileEntryId());
359    
360                    if (context.getBooleanParameter(_NAMESPACE, "comments")) {
361                            context.importComments(
362                                    DLFileEntry.class, fileEntry.getFileEntryId(),
363                                    importedFileEntry.getFileEntryId(), context.getScopeGroupId());
364                    }
365    
366                    if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
367                            context.importRatingsEntries(
368                                    DLFileEntry.class, fileEntry.getFileEntryId(),
369                                    importedFileEntry.getFileEntryId());
370                    }
371            }
372    
373            public static void importFileRank(
374                            PortletDataContext context, Element fileRankElement)
375                    throws Exception {
376    
377                    String path = fileRankElement.attributeValue("path");
378    
379                    if (!context.isPathNotProcessed(path)) {
380                            return;
381                    }
382    
383                    DLFileRank fileRank =
384                            (DLFileRank)context.getZipEntryAsObject(path);
385    
386                    importFileRank(context, fileRank);
387            }
388    
389            public static void importFolder(
390                            PortletDataContext context, Element folderElement)
391                    throws Exception {
392    
393                    String path = folderElement.attributeValue("path");
394    
395                    if (!context.isPathNotProcessed(path)) {
396                            return;
397                    }
398    
399                    DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
400    
401                    importFolder(context, folder);
402            }
403    
404            public PortletDataHandlerControl[] getExportControls() {
405                    return new PortletDataHandlerControl[] {
406                            _foldersAndDocuments, _shortcuts, _ranks, _categories, _comments,
407                            _ratings, _tags
408                    };
409            }
410    
411            public PortletDataHandlerControl[] getImportControls() {
412                    return new PortletDataHandlerControl[] {
413                            _foldersAndDocuments, _shortcuts, _ranks, _categories, _comments,
414                            _ratings, _tags
415                    };
416            }
417    
418            public boolean isAlwaysExportable() {
419                    return _ALWAYS_EXPORTABLE;
420            }
421    
422            public boolean isPublishToLiveByDefault() {
423                    return PropsValues.DL_PUBLISH_TO_LIVE_BY_DEFAULT;
424            }
425    
426            protected static void exportFileRank(
427                            PortletDataContext context, Element fileRanksElement,
428                            DLFileRank fileRank)
429                    throws Exception {
430    
431                    String path = getFileRankPath(context, fileRank);
432    
433                    if (!context.isPathNotProcessed(path)) {
434                            return;
435                    }
436    
437                    Element fileRankElement = fileRanksElement.addElement("file-rank");
438    
439                    fileRankElement.addAttribute("path", path);
440    
441                    fileRank.setUserUuid(fileRank.getUserUuid());
442    
443                    context.addZipEntry(path, fileRank);
444            }
445    
446            protected static void exportFileShortcut(
447                            PortletDataContext context, Element foldersElement,
448                            Element fileShortcutsElement, DLFileShortcut fileShortcut)
449                    throws Exception {
450    
451                    exportParentFolder(context, foldersElement, fileShortcut.getFolderId());
452    
453                    String path = getFileShortcutPath(context, fileShortcut);
454    
455                    if (context.isPathNotProcessed(path)) {
456                            Element fileShortcutElement = fileShortcutsElement.addElement(
457                                    "file-shortcut");
458    
459                            fileShortcutElement.addAttribute("path", path);
460    
461                            fileShortcut.setUserUuid(fileShortcut.getUserUuid());
462    
463                            context.addPermissions(
464                                    DLFileShortcut.class, fileShortcut.getFileShortcutId());
465    
466                            context.addZipEntry(path, fileShortcut);
467                    }
468            }
469    
470            protected static void exportFolder(
471                            PortletDataContext context, Element foldersElement,
472                            Element fileEntriesElement, Element fileShortcutsElement,
473                            Element fileRanksElement, DLFolder folder)
474                    throws Exception {
475    
476                    if (context.isWithinDateRange(folder.getModifiedDate())) {
477                            exportParentFolder(
478                                    context, foldersElement, folder.getParentFolderId());
479    
480                            String path = getFolderPath(context, folder);
481    
482                            if (context.isPathNotProcessed(path)) {
483                                    Element folderElement = foldersElement.addElement("folder");
484    
485                                    folderElement.addAttribute("path", path);
486    
487                                    folder.setUserUuid(folder.getUserUuid());
488    
489                                    context.addPermissions(DLFolder.class, folder.getFolderId());
490    
491                                    context.addZipEntry(path, folder);
492                            }
493                    }
494    
495                    List<DLFileEntry> fileEntries = DLFileEntryUtil.findByG_F(
496                            folder.getGroupId(), folder.getFolderId());
497    
498                    for (DLFileEntry fileEntry : fileEntries) {
499                            exportFileEntry(
500                                    context, foldersElement, fileEntriesElement, fileRanksElement,
501                                    fileEntry);
502                    }
503    
504                    if (context.getBooleanParameter(_NAMESPACE, "shortcuts")) {
505                            List<DLFileShortcut> fileShortcuts = DLFileShortcutUtil.findByG_F(
506                                    folder.getGroupId(), folder.getFolderId());
507    
508                            for (DLFileShortcut fileShortcut : fileShortcuts) {
509                                    exportFileShortcut(
510                                            context, foldersElement, fileShortcutsElement,
511                                            fileShortcut);
512                            }
513                    }
514            }
515    
516            protected static void exportParentFolder(
517                            PortletDataContext context, Element foldersElement, long folderId)
518                    throws Exception {
519    
520                    if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
521                            return;
522                    }
523    
524                    DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
525    
526                    exportParentFolder(context, foldersElement, folder.getParentFolderId());
527    
528                    String path = getFolderPath(context, folder);
529    
530                    if (context.isPathNotProcessed(path)) {
531                            Element folderElement = foldersElement.addElement("folder");
532    
533                            folderElement.addAttribute("path", path);
534    
535                            folder.setUserUuid(folder.getUserUuid());
536    
537                            context.addPermissions(DLFolder.class, folder.getFolderId());
538    
539                            context.addZipEntry(path, folder);
540                    }
541            }
542    
543            protected static String getFileEntryBinPath(
544                    PortletDataContext context, DLFileEntry fileEntry) {
545    
546                    StringBundler sb = new StringBundler(5);
547    
548                    sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
549                    sb.append("/bin/");
550                    sb.append(fileEntry.getFileEntryId());
551                    sb.append(StringPool.SLASH);
552                    sb.append(fileEntry.getVersion());
553    
554                    return sb.toString();
555            }
556    
557            protected static String getFileRankPath(
558                    PortletDataContext context, DLFileRank fileRank) {
559    
560                    StringBundler sb = new StringBundler(4);
561    
562                    sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
563                    sb.append("/ranks/");
564                    sb.append(fileRank.getFileRankId());
565                    sb.append(".xml");
566    
567                    return sb.toString();
568            }
569    
570            protected static String getFileShortcutPath(
571                    PortletDataContext context, DLFileShortcut fileShortcut) {
572    
573                    StringBundler sb = new StringBundler(4);
574    
575                    sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
576                    sb.append("/shortcuts/");
577                    sb.append(fileShortcut.getFileShortcutId());
578                    sb.append(".xml");
579    
580                    return sb.toString();
581            }
582    
583            protected static String getFolderName(
584                            long companyId, long groupId, long parentFolderId, String name,
585                            int count)
586                    throws Exception {
587    
588                    DLFolder folder = DLFolderUtil.fetchByG_P_N(
589                            groupId, parentFolderId, name);
590    
591                    if (folder == null) {
592                            return name;
593                    }
594    
595                    if (Pattern.matches(".* \\(\\d+\\)", name)) {
596                            int pos = name.lastIndexOf(" (");
597    
598                            name = name.substring(0, pos);
599                    }
600    
601                    StringBundler sb = new StringBundler(5);
602    
603                    sb.append(name);
604                    sb.append(StringPool.SPACE);
605                    sb.append(StringPool.OPEN_PARENTHESIS);
606                    sb.append(count);
607                    sb.append(StringPool.CLOSE_PARENTHESIS);
608    
609                    name = sb.toString();
610    
611                    return getFolderName(companyId, groupId, parentFolderId, name, ++count);
612            }
613    
614            protected static String getFolderPath(
615                    PortletDataContext context, DLFolder folder) {
616    
617                    StringBundler sb = new StringBundler(4);
618    
619                    sb.append(context.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
620                    sb.append("/folders/");
621                    sb.append(folder.getFolderId());
622                    sb.append(".xml");
623    
624                    return sb.toString();
625            }
626    
627            protected static String getImportFolderPath(
628                    PortletDataContext context, long folderId) {
629    
630                    StringBundler sb = new StringBundler(4);
631    
632                    sb.append(context.getSourcePortletPath(PortletKeys.DOCUMENT_LIBRARY));
633                    sb.append("/folders/");
634                    sb.append(folderId);
635                    sb.append(".xml");
636    
637                    return sb.toString();
638            }
639    
640            protected static long getRepositoryId(long groupId, long folderId) {
641                    if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
642                            return groupId;
643                    }
644                    else {
645                            return folderId;
646                    }
647            }
648    
649            protected static void importFileRank(
650                            PortletDataContext context, DLFileRank rank)
651                    throws Exception {
652    
653                    long userId = context.getUserId(rank.getUserUuid());
654    
655                    Map<Long, Long> folderPKs =
656                            (Map<Long, Long>)context.getNewPrimaryKeysMap(
657                                    DLFolder.class);
658    
659                    long folderId = MapUtil.getLong(
660                            folderPKs, rank.getFolderId(), rank.getFolderId());
661    
662                    Map<String, String> fileEntryNames =
663                            (Map<String, String>)context.getNewPrimaryKeysMap(
664                                    DLFileEntry.class.getName() + ".name");
665    
666                    String name = fileEntryNames.get(rank.getName());
667    
668                    if (name == null) {
669                            name = rank.getName();
670                    }
671    
672                    ServiceContext serviceContext = new ServiceContext();
673    
674                    serviceContext.setCreateDate(rank.getCreateDate());
675    
676                    if ((folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
677                            (folderId == rank.getFolderId())) {
678    
679                            String path = getImportFolderPath(context, folderId);
680    
681                            DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
682    
683                            importFolder(context, folder);
684    
685                            folderId = MapUtil.getLong(
686                                    folderPKs, rank.getFolderId(), rank.getFolderId());
687                    }
688    
689                    DLFileRankLocalServiceUtil.updateFileRank(
690                            context.getScopeGroupId(), context.getCompanyId(), userId, folderId,
691                            name, serviceContext);
692            }
693    
694            protected static void importFileShortcut(
695                            PortletDataContext context, DLFileShortcut fileShortcut)
696                    throws Exception {
697    
698                    long userId = context.getUserId(fileShortcut.getUserUuid());
699    
700                    Map<Long, Long> folderPKs =
701                            (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFolder.class);
702    
703                    long folderId = MapUtil.getLong(
704                            folderPKs, fileShortcut.getFolderId(), fileShortcut.getFolderId());
705                    long toFolderId = MapUtil.getLong(
706                            folderPKs, fileShortcut.getToFolderId(),
707                            fileShortcut.getToFolderId());
708    
709                    Map<String, String> fileEntryNames =
710                            (Map<String, String>)context.getNewPrimaryKeysMap(
711                                    DLFileEntry.class.getName() + ".name");
712    
713                    String toName = MapUtil.getString(
714                            fileEntryNames, fileShortcut.getToName(), fileShortcut.getToName());
715    
716                    DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
717    
718                    DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
719                            folder.getGroupId(), toFolderId, toName);
720    
721                    long[] assetCategoryIds = null;
722                    String[] assetTagNames = null;
723    
724                    if (context.getBooleanParameter(_NAMESPACE, "categories")) {
725                            assetCategoryIds = context.getAssetCategoryIds(
726                                    DLFileEntry.class, fileEntry.getFileEntryId());
727                    }
728    
729                    if (context.getBooleanParameter(_NAMESPACE, "tags")) {
730                            assetTagNames = context.getAssetTagNames(
731                                    DLFileEntry.class, fileEntry.getFileEntryId());
732                    }
733    
734                    ServiceContext serviceContext = new ServiceContext();
735    
736                    serviceContext.setAddCommunityPermissions(true);
737                    serviceContext.setAddGuestPermissions(true);
738                    serviceContext.setAssetCategoryIds(assetCategoryIds);
739                    serviceContext.setAssetTagNames(assetTagNames);
740                    serviceContext.setCreateDate(fileShortcut.getCreateDate());
741                    serviceContext.setModifiedDate(fileShortcut.getModifiedDate());
742                    serviceContext.setScopeGroupId(context.getScopeGroupId());
743    
744                    DLFileShortcut importedFileShortcut = null;
745    
746                    if (context.isDataStrategyMirror()) {
747                            DLFileShortcut existingFileShortcut =
748                                    DLFileShortcutUtil.fetchByUUID_G(
749                                            fileShortcut.getUuid(), context.getScopeGroupId());
750    
751                            if (existingFileShortcut == null) {
752                                    serviceContext.setUuid(fileShortcut.getUuid());
753    
754                                    importedFileShortcut =
755                                            DLFileShortcutLocalServiceUtil.addFileShortcut(
756                                                    userId, folder.getGroupId(), folderId, toFolderId,
757                                                    toName, serviceContext);
758                            }
759                            else {
760                                    importedFileShortcut =
761                                            DLFileShortcutLocalServiceUtil.updateFileShortcut(
762                                                    userId, existingFileShortcut.getFileShortcutId(),
763                                                    folderId, toFolderId, toName, serviceContext);
764                            }
765                    }
766                    else {
767                            importedFileShortcut =
768                                    DLFileShortcutLocalServiceUtil.addFileShortcut(
769                                            userId, folder.getGroupId(), folderId, toFolderId, toName,
770                                            serviceContext);
771                    }
772    
773                    context.importPermissions(
774                            DLFileShortcut.class, fileShortcut.getPrimaryKey(),
775                            importedFileShortcut.getPrimaryKey());
776            }
777    
778            protected static void importFileShortcut(
779                            PortletDataContext context, Element fileShortcutElement)
780                    throws Exception {
781    
782                    String path = fileShortcutElement.attributeValue("path");
783    
784                    if (!context.isPathNotProcessed(path)) {
785                            return;
786                    }
787    
788                    DLFileShortcut fileShortcut =
789                            (DLFileShortcut)context.getZipEntryAsObject(path);
790    
791                    importFileShortcut(context, fileShortcut);
792            }
793    
794            protected static void importFolder(
795                            PortletDataContext context, DLFolder folder)
796                    throws Exception {
797    
798                    long userId = context.getUserId(folder.getUserUuid());
799    
800                    Map<Long, Long> folderPKs =
801                            (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFolder.class);
802    
803                    long parentFolderId = MapUtil.getLong(
804                            folderPKs, folder.getParentFolderId(), folder.getParentFolderId());
805    
806                    ServiceContext serviceContext = new ServiceContext();
807    
808                    serviceContext.setAddCommunityPermissions(true);
809                    serviceContext.setAddGuestPermissions(true);
810                    serviceContext.setCreateDate(folder.getCreateDate());
811                    serviceContext.setModifiedDate(folder.getModifiedDate());
812    
813                    if ((parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
814                            (parentFolderId == folder.getParentFolderId())) {
815    
816                            String path = getImportFolderPath(context, parentFolderId);
817    
818                            DLFolder parentFolder = (DLFolder)context.getZipEntryAsObject(path);
819    
820                            importFolder(context, parentFolder);
821    
822                            parentFolderId = MapUtil.getLong(
823                                    folderPKs, folder.getParentFolderId(),
824                                    folder.getParentFolderId());
825                    }
826    
827                    DLFolder importedFolder = null;
828    
829                    if (context.isDataStrategyMirror()) {
830                            DLFolder existingFolder = DLFolderUtil.fetchByUUID_G(
831                                    folder.getUuid(), context.getScopeGroupId());
832    
833                            if (existingFolder == null) {
834                                    String name = getFolderName(
835                                            context.getCompanyId(), context.getScopeGroupId(),
836                                            parentFolderId, folder.getName(), 2);
837    
838                                    serviceContext.setUuid(folder.getUuid());
839    
840                                    importedFolder = DLFolderLocalServiceUtil.addFolder(
841                                            userId, context.getScopeGroupId(), parentFolderId, name,
842                                            folder.getDescription(), serviceContext);
843                            }
844                            else {
845                                    importedFolder = DLFolderLocalServiceUtil.updateFolder(
846                                            existingFolder.getFolderId(), parentFolderId,
847                                            folder.getName(), folder.getDescription(), serviceContext);
848                            }
849                    }
850                    else {
851                            String name = getFolderName(
852                                    context.getCompanyId(), context.getScopeGroupId(),
853                                    parentFolderId, folder.getName(), 2);
854    
855                            importedFolder = DLFolderLocalServiceUtil.addFolder(
856                                    userId, context.getScopeGroupId(), parentFolderId, name,
857                                    folder.getDescription(), serviceContext);
858                    }
859    
860                    folderPKs.put(folder.getFolderId(), importedFolder.getFolderId());
861    
862                    context.importPermissions(
863                            DLFolder.class, folder.getFolderId(), importedFolder.getFolderId());
864            }
865    
866            protected static boolean isDuplicateFileEntry(
867                    DLFileEntry fileEntry1, DLFileEntry fileEntry2) {
868    
869                    try {
870                            DLFolder folder1 = fileEntry1.getFolder();
871                            DLFolder folder2 = fileEntry2.getFolder();
872    
873                            if ((folder1.getUuid().equals(folder2.getUuid())) &&
874                                    (fileEntry1.getSize() == fileEntry2.getSize()) &&
875                                    (DLUtil.compareVersions(
876                                            fileEntry1.getVersion(), fileEntry2.getVersion()) == 0) &&
877                                    (fileEntry1.getVersionUserUuid().equals(
878                                            fileEntry2.getVersionUserUuid()))) {
879    
880                                    return true;
881                            }
882                            else {
883                                    return false;
884                            }
885                    }
886                    catch (Exception e) {
887                            return false;
888                    }
889            }
890    
891            protected PortletPreferences doDeleteData(
892                            PortletDataContext context, String portletId,
893                            PortletPreferences preferences)
894                    throws Exception {
895    
896                    if (!context.addPrimaryKey(
897                                    DLPortletDataHandlerImpl.class, "deleteData")) {
898    
899                            DLFolderLocalServiceUtil.deleteFolders(context.getScopeGroupId());
900    
901                            DLFileEntryLocalServiceUtil.deleteFileEntries(
902                                    context.getScopeGroupId(),
903                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
904                    }
905    
906                    return null;
907            }
908    
909            protected String doExportData(
910                            PortletDataContext context, String portletId,
911                            PortletPreferences preferences)
912                    throws Exception {
913    
914                    context.addPermissions(
915                            "com.liferay.portlet.documentlibrary", context.getScopeGroupId());
916    
917                    Document document = SAXReaderUtil.createDocument();
918    
919                    Element rootElement = document.addElement("documentlibrary-data");
920    
921                    rootElement.addAttribute(
922                            "group-id", String.valueOf(context.getScopeGroupId()));
923    
924                    Element foldersElement = rootElement.addElement("folders");
925                    Element fileEntriesElement = rootElement.addElement("file-entries");
926                    Element fileShortcutsElement = rootElement.addElement("file-shortcuts");
927                    Element fileRanksElement = rootElement.addElement("file-ranks");
928    
929                    List<DLFolder> folders = DLFolderUtil.findByGroupId(
930                            context.getScopeGroupId());
931    
932                    for (DLFolder folder : folders) {
933                            exportFolder(
934                                    context, foldersElement, fileEntriesElement,
935                                    fileShortcutsElement, fileRanksElement, folder);
936                    }
937    
938                    List<DLFileEntry> fileEntries = DLFileEntryUtil.findByG_F(
939                            context.getScopeGroupId(),
940                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
941    
942                    for (DLFileEntry fileEntry : fileEntries) {
943                            exportFileEntry(
944                                    context, foldersElement, fileEntriesElement, fileRanksElement,
945                                    fileEntry);
946                    }
947    
948                    return document.formattedString();
949            }
950    
951            protected PortletPreferences doImportData(
952                            PortletDataContext context, String portletId,
953                            PortletPreferences preferences, String data)
954                    throws Exception {
955    
956                    context.importPermissions(
957                            "com.liferay.portlet.documentlibrary",
958                            context.getSourceGroupId(), context.getScopeGroupId());
959    
960                    Document document = SAXReaderUtil.read(data);
961    
962                    Element rootElement = document.getRootElement();
963    
964                    Element foldersElement = rootElement.element("folders");
965    
966                    List<Element> folderElements = foldersElement.elements("folder");
967    
968                    for (Element folderElement : folderElements) {
969                            importFolder(context, folderElement);
970                    }
971    
972                    Element fileEntriesElement = rootElement.element("file-entries");
973    
974                    List<Element> fileEntryElements = fileEntriesElement.elements(
975                            "file-entry");
976    
977                    for (Element fileEntryElement : fileEntryElements) {
978                            importFileEntry(context, fileEntryElement);
979                    }
980    
981                    if (context.getBooleanParameter(_NAMESPACE, "shortcuts")) {
982                            List<Element> fileShortcutElements = rootElement.element(
983                                    "file-shortcuts").elements("file-shortcut");
984    
985                            for (Element fileShortcutElement : fileShortcutElements) {
986                                    importFileShortcut(context, fileShortcutElement);
987                            }
988                    }
989    
990                    if (context.getBooleanParameter(_NAMESPACE, "ranks")) {
991                            Element fileRanksElement = rootElement.element("file-ranks");
992    
993                            List<Element> fileRankElements = fileRanksElement.elements(
994                                    "file-rank");
995    
996                            for (Element fileRankElement : fileRankElements) {
997                                    importFileRank(context, fileRankElement);
998                            }
999                    }
1000    
1001                    return null;
1002            }
1003    
1004            private static final boolean _ALWAYS_EXPORTABLE = true;
1005    
1006            private static final String _NAMESPACE = "document_library";
1007    
1008            private static Log _log = LogFactoryUtil.getLog(
1009                    DLPortletDataHandlerImpl.class);
1010    
1011            private static PortletDataHandlerBoolean _categories =
1012                    new PortletDataHandlerBoolean(_NAMESPACE, "categories");
1013    
1014            private static PortletDataHandlerBoolean _comments =
1015                    new PortletDataHandlerBoolean(_NAMESPACE, "comments");
1016    
1017            private static PortletDataHandlerBoolean _foldersAndDocuments =
1018                    new PortletDataHandlerBoolean(
1019                            _NAMESPACE, "folders-and-documents", true, true);
1020    
1021            private static PortletDataHandlerBoolean _ranks =
1022                    new PortletDataHandlerBoolean(_NAMESPACE, "ranks");
1023    
1024            private static PortletDataHandlerBoolean _ratings =
1025                    new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
1026    
1027            private static PortletDataHandlerBoolean _shortcuts=
1028                    new PortletDataHandlerBoolean(_NAMESPACE, "shortcuts");
1029    
1030            private static PortletDataHandlerBoolean _tags =
1031                    new PortletDataHandlerBoolean(_NAMESPACE, "tags");
1032    
1033    }