001
014
015 package com.liferay.portlet.documentlibrary.lar;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
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.repository.model.FileEntry;
026 import com.liferay.portal.kernel.repository.model.FileVersion;
027 import com.liferay.portal.kernel.repository.model.Folder;
028 import com.liferay.portal.kernel.search.Indexer;
029 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
030 import com.liferay.portal.kernel.util.FileUtil;
031 import com.liferay.portal.kernel.util.GetterUtil;
032 import com.liferay.portal.kernel.util.MapUtil;
033 import com.liferay.portal.kernel.util.StringBundler;
034 import com.liferay.portal.kernel.util.StringPool;
035 import com.liferay.portal.kernel.util.StringUtil;
036 import com.liferay.portal.kernel.util.Validator;
037 import com.liferay.portal.kernel.workflow.WorkflowConstants;
038 import com.liferay.portal.kernel.xml.Document;
039 import com.liferay.portal.kernel.xml.Element;
040 import com.liferay.portal.kernel.xml.SAXReaderUtil;
041 import com.liferay.portal.model.Group;
042 import com.liferay.portal.model.Repository;
043 import com.liferay.portal.model.RepositoryEntry;
044 import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
045 import com.liferay.portal.service.GroupLocalServiceUtil;
046 import com.liferay.portal.service.RepositoryEntryLocalServiceUtil;
047 import com.liferay.portal.service.RepositoryLocalServiceUtil;
048 import com.liferay.portal.service.ServiceContext;
049 import com.liferay.portal.service.persistence.RepositoryEntryUtil;
050 import com.liferay.portal.service.persistence.RepositoryUtil;
051 import com.liferay.portal.util.PortletKeys;
052 import com.liferay.portal.util.PropsValues;
053 import com.liferay.portlet.documentlibrary.DuplicateFileException;
054 import com.liferay.portlet.documentlibrary.NoSuchFileException;
055 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
056 import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
057 import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
058 import com.liferay.portlet.documentlibrary.model.DLFileRank;
059 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
060 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
061 import com.liferay.portlet.documentlibrary.model.DLFolder;
062 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
063 import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
064 import com.liferay.portlet.documentlibrary.service.DLFileEntryMetadataLocalServiceUtil;
065 import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
066 import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeServiceUtil;
067 import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
068 import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
069 import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryTypeUtil;
070 import com.liferay.portlet.documentlibrary.service.persistence.DLFileRankUtil;
071 import com.liferay.portlet.documentlibrary.service.persistence.DLFileShortcutUtil;
072 import com.liferay.portlet.documentlibrary.util.DLProcessorRegistryUtil;
073 import com.liferay.portlet.documentlibrary.util.DLProcessorThreadLocal;
074 import com.liferay.portlet.documentlibrary.util.DLUtil;
075 import com.liferay.portlet.dynamicdatamapping.lar.DDMPortletDataHandlerImpl;
076 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
077 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
078 import com.liferay.portlet.dynamicdatamapping.storage.Fields;
079 import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
080 import com.liferay.util.PwdGenerator;
081
082 import java.io.IOException;
083 import java.io.InputStream;
084
085 import java.util.ArrayList;
086 import java.util.List;
087 import java.util.Map;
088
089 import javax.portlet.PortletPreferences;
090
091
096 public class DLPortletDataHandlerImpl extends BasePortletDataHandler {
097
098 public static void exportFileEntry(
099 PortletDataContext portletDataContext,
100 Element fileEntryTypesElement, Element foldersElement,
101 Element fileEntriesElement, Element fileRanksElement,
102 Element repositoriesElement, Element repositoryEntriesElement,
103 FileEntry fileEntry, boolean checkDateRange)
104 throws Exception {
105
106 if (checkDateRange &&
107 !portletDataContext.isWithinDateRange(
108 fileEntry.getModifiedDate())) {
109
110 return;
111 }
112
113 if (!fileEntry.isDefaultRepository()) {
114 Repository repository = RepositoryUtil.findByPrimaryKey(
115 fileEntry.getRepositoryId());
116
117 exportRepository(
118 portletDataContext, repositoriesElement,
119 repositoryEntriesElement, repository);
120
121 return;
122 }
123
124 FileVersion fileVersion = fileEntry.getFileVersion();
125
126 if (fileVersion.getStatus() != WorkflowConstants.STATUS_APPROVED) {
127 return;
128 }
129
130 String path = getFileEntryPath(portletDataContext, fileEntry);
131
132 if (!portletDataContext.isPathNotProcessed(path)) {
133 return;
134 }
135
136 Element fileEntryElement = fileEntriesElement.addElement("file-entry");
137
138 if (foldersElement != null) {
139 exportParentFolder(
140 portletDataContext, fileEntryTypesElement, foldersElement,
141 repositoriesElement, repositoryEntriesElement,
142 fileEntry.getFolderId());
143 }
144
145 if (!portletDataContext.isPerformDirectBinaryImport()) {
146 InputStream is = null;
147
148 try {
149 is = FileEntryUtil.getContentStream(fileEntry);
150 }
151 catch (NoSuchFileException nsfe) {
152 }
153
154 if (is == null) {
155 if (_log.isWarnEnabled()) {
156 _log.warn(
157 "No file found for file entry " +
158 fileEntry.getFileEntryId());
159 }
160
161 fileEntryElement.detach();
162
163 return;
164 }
165
166 try {
167 String binPath = getFileEntryBinPath(
168 portletDataContext, fileEntry);
169
170 portletDataContext.addZipEntry(binPath, is);
171
172 fileEntryElement.addAttribute("bin-path", binPath);
173 }
174 finally {
175 try {
176 is.close();
177 }
178 catch (IOException ioe) {
179 _log.error(ioe, ioe);
180 }
181 }
182 }
183
184 if (portletDataContext.getBooleanParameter(_NAMESPACE, "ranks")) {
185 List<DLFileRank> fileRanks = DLFileRankUtil.findByFileEntryId(
186 fileEntry.getFileEntryId());
187
188 for (DLFileRank fileRank : fileRanks) {
189 exportFileRank(portletDataContext, fileRanksElement, fileRank);
190 }
191 }
192
193 if (portletDataContext.getBooleanParameter(
194 _NAMESPACE, "previews-and-thumbnails")) {
195
196 DLProcessorRegistryUtil.exportGeneratedFiles(
197 portletDataContext, fileEntry, fileEntryElement);
198 }
199
200 exportMetaData(
201 portletDataContext, fileEntryTypesElement, fileEntryElement,
202 fileEntry);
203
204 portletDataContext.addClassedModel(
205 fileEntryElement, path, fileEntry, _NAMESPACE);
206 }
207
208 public static String getFileEntryPath(
209 PortletDataContext portletDataContext, FileEntry fileEntry) {
210
211 StringBundler sb = new StringBundler(6);
212
213 sb.append(
214 portletDataContext.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
215 sb.append("/file-entries/");
216 sb.append(fileEntry.getFileEntryId());
217 sb.append(StringPool.SLASH);
218 sb.append(fileEntry.getVersion());
219 sb.append(".xml");
220
221 return sb.toString();
222 }
223
224 public static PortletDataHandlerControl[] getMetadataControls() {
225 return _metadataControls;
226 }
227
228 public static String getRepositoryEntryPath(
229 PortletDataContext portletDataContext, long repositoryEntryId) {
230
231 StringBundler sb = new StringBundler(4);
232
233 sb.append(
234 portletDataContext.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
235 sb.append("/repository-entries/");
236 sb.append(repositoryEntryId);
237 sb.append(".xml");
238
239 return sb.toString();
240 }
241
242 public static void importFileEntry(
243 PortletDataContext portletDataContext, Element fileEntryElement)
244 throws Exception {
245
246 String path = fileEntryElement.attributeValue("path");
247
248 if (!portletDataContext.isPathNotProcessed(path)) {
249 return;
250 }
251
252 boolean dlProcessorEnabled = DLProcessorThreadLocal.isEnabled();
253
254 try {
255 DLProcessorThreadLocal.setEnabled(false);
256
257 importFileEntry(portletDataContext, fileEntryElement, path);
258 }
259 finally {
260 DLProcessorThreadLocal.setEnabled(dlProcessorEnabled);
261 }
262 }
263
264 public static void importFileEntry(
265 PortletDataContext portletDataContext, Element fileEntryElement,
266 String path)
267 throws Exception {
268
269 FileEntry fileEntry = (FileEntry)portletDataContext.getZipEntryAsObject(
270 path);
271
272 long userId = portletDataContext.getUserId(fileEntry.getUserUuid());
273
274 Map<Long, Long> folderIds =
275 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
276 DLFolder.class);
277
278 long folderId = MapUtil.getLong(
279 folderIds, fileEntry.getFolderId(), fileEntry.getFolderId());
280
281 long[] assetCategoryIds = null;
282 String[] assetTagNames = null;
283
284 if (portletDataContext.getBooleanParameter(_NAMESPACE, "categories")) {
285 assetCategoryIds = portletDataContext.getAssetCategoryIds(
286 DLFileEntry.class, fileEntry.getFileEntryId());
287 }
288
289 if (portletDataContext.getBooleanParameter(_NAMESPACE, "tags")) {
290 assetTagNames = portletDataContext.getAssetTagNames(
291 DLFileEntry.class, fileEntry.getFileEntryId());
292 }
293
294 ServiceContext serviceContext = portletDataContext.createServiceContext(
295 fileEntryElement, fileEntry, _NAMESPACE);
296
297 serviceContext.setAttribute(
298 "sourceFileName", "A." + fileEntry.getExtension());
299 serviceContext.setUserId(userId);
300
301 String binPath = fileEntryElement.attributeValue("bin-path");
302
303 InputStream is = null;
304
305 if (Validator.isNull(binPath) &&
306 portletDataContext.isPerformDirectBinaryImport()) {
307
308 try {
309 is = FileEntryUtil.getContentStream(fileEntry);
310 }
311 catch (NoSuchFileException nsfe) {
312 }
313 }
314 else {
315 is = portletDataContext.getZipEntryAsInputStream(binPath);
316 }
317
318 if (is == null) {
319 if (_log.isWarnEnabled()) {
320 _log.warn(
321 "No file found for file entry " +
322 fileEntry.getFileEntryId());
323 }
324
325 return;
326 }
327
328 if ((folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
329 (folderId == fileEntry.getFolderId())) {
330
331 String folderPath = getImportFolderPath(
332 portletDataContext, folderId);
333
334 Folder folder = (Folder)portletDataContext.getZipEntryAsObject(
335 folderPath);
336
337 Document document = fileEntryElement.getDocument();
338
339 Element rootElement = document.getRootElement();
340
341 Element folderElement = (Element)rootElement.selectSingleNode(
342 "
343
344 importFolder(portletDataContext, folderPath, folderElement, folder);
345
346 folderId = MapUtil.getLong(
347 folderIds, fileEntry.getFolderId(), fileEntry.getFolderId());
348 }
349
350 importMetaData(portletDataContext, fileEntryElement, serviceContext);
351
352 FileEntry importedFileEntry = null;
353
354 String titleWithExtension = DLUtil.getTitleWithExtension(fileEntry);
355 String extension = fileEntry.getExtension();
356
357 String dotExtension = StringPool.PERIOD + extension;
358
359 if (portletDataContext.isDataStrategyMirror()) {
360 FileEntry existingFileEntry = FileEntryUtil.fetchByUUID_R(
361 fileEntry.getUuid(), portletDataContext.getScopeGroupId());
362
363 FileVersion fileVersion = fileEntry.getFileVersion();
364
365 if (existingFileEntry == null) {
366 String fileEntryTitle = fileEntry.getTitle();
367
368 FileEntry existingTitleFileEntry = FileEntryUtil.fetchByR_F_T(
369 portletDataContext.getScopeGroupId(), folderId,
370 fileEntryTitle);
371
372 if (existingTitleFileEntry != null) {
373 if ((fileEntry.getGroupId() ==
374 portletDataContext.getSourceGroupId()) &&
375 portletDataContext.
376 isDataStrategyMirrorWithOverwriting()) {
377
378 DLAppLocalServiceUtil.deleteFileEntry(
379 existingTitleFileEntry.getFileEntryId());
380 }
381 else {
382 boolean titleHasExtension = false;
383
384 if (fileEntryTitle.endsWith(dotExtension)) {
385 fileEntryTitle = FileUtil.stripExtension(
386 fileEntryTitle);
387
388 titleHasExtension = true;
389 }
390
391 for (int i = 1;; i++) {
392 fileEntryTitle += StringPool.SPACE + i;
393
394 titleWithExtension = fileEntryTitle + dotExtension;
395
396 existingTitleFileEntry = FileEntryUtil.fetchByR_F_T(
397 portletDataContext.getScopeGroupId(), folderId,
398 titleWithExtension);
399
400 if (existingTitleFileEntry == null) {
401 if (titleHasExtension) {
402 fileEntryTitle += dotExtension;
403 }
404
405 break;
406 }
407 }
408 }
409 }
410
411 serviceContext.setAttribute(
412 "fileVersionUuid", fileVersion.getUuid());
413 serviceContext.setUuid(fileEntry.getUuid());
414
415 importedFileEntry = DLAppLocalServiceUtil.addFileEntry(
416 userId, portletDataContext.getScopeGroupId(), folderId,
417 titleWithExtension, fileEntry.getMimeType(), fileEntryTitle,
418 fileEntry.getDescription(), null, is, fileEntry.getSize(),
419 serviceContext);
420 }
421 else {
422 FileVersion latestExistingFileVersion =
423 existingFileEntry.getLatestFileVersion();
424
425 boolean indexEnabled = serviceContext.isIndexingEnabled();
426
427 try {
428 serviceContext.setIndexingEnabled(false);
429
430 if (!fileVersion.getUuid().equals(
431 latestExistingFileVersion.getUuid())) {
432
433 DLFileVersion alreadyExistingFileVersion =
434 DLFileVersionLocalServiceUtil.
435 getFileVersionByUuidAndGroupId(
436 fileVersion.getUuid(),
437 existingFileEntry.getGroupId());
438
439 if (alreadyExistingFileVersion != null) {
440 serviceContext.setAttribute(
441 "existingDLFileVersionId",
442 alreadyExistingFileVersion.getFileVersionId());
443 }
444
445 serviceContext.setUuid(fileVersion.getUuid());
446
447 importedFileEntry =
448 DLAppLocalServiceUtil.updateFileEntry(
449 userId, existingFileEntry.getFileEntryId(),
450 fileEntry.getTitle(), fileEntry.getMimeType(),
451 fileEntry.getTitle(),
452 fileEntry.getDescription(), null, false, is,
453 fileEntry.getSize(), serviceContext);
454 }
455 else {
456 DLAppLocalServiceUtil.updateAsset(
457 userId, existingFileEntry,
458 latestExistingFileVersion, assetCategoryIds,
459 assetTagNames, null);
460
461 importedFileEntry = existingFileEntry;
462 }
463
464 if (importedFileEntry.getFolderId() != folderId) {
465 importedFileEntry = DLAppLocalServiceUtil.moveFileEntry(
466 userId, importedFileEntry.getFileEntryId(),
467 folderId, serviceContext);
468 }
469
470 if (importedFileEntry instanceof LiferayFileEntry) {
471 LiferayFileEntry liferayFileEntry =
472 (LiferayFileEntry)importedFileEntry;
473
474 Indexer indexer = IndexerRegistryUtil.getIndexer(
475 DLFileEntry.class);
476
477 indexer.reindex(liferayFileEntry.getModel());
478 }
479 }
480 finally {
481 serviceContext.setIndexingEnabled(indexEnabled);
482 }
483 }
484 }
485 else {
486 try {
487 importedFileEntry = DLAppLocalServiceUtil.addFileEntry(
488 userId, portletDataContext.getScopeGroupId(), folderId,
489 titleWithExtension, fileEntry.getMimeType(),
490 fileEntry.getTitle(), fileEntry.getDescription(), null, is,
491 fileEntry.getSize(), serviceContext);
492 }
493 catch (DuplicateFileException dfe) {
494 String title = fileEntry.getTitle();
495
496 String[] titleParts = title.split("\\.", 2);
497
498 title = titleParts[0] + PwdGenerator.getPassword();
499
500 if (titleParts.length > 1) {
501 title += StringPool.PERIOD + titleParts[1];
502 }
503
504 if (!title.endsWith(dotExtension)) {
505 title += dotExtension;
506 }
507
508 importedFileEntry = DLAppLocalServiceUtil.addFileEntry(
509 userId, portletDataContext.getScopeGroupId(), folderId,
510 title, fileEntry.getMimeType(), title,
511 fileEntry.getDescription(), null, is, fileEntry.getSize(),
512 serviceContext);
513 }
514 }
515
516 if (portletDataContext.getBooleanParameter(
517 _NAMESPACE, "previews-and-thumbnails")) {
518
519 DLProcessorRegistryUtil.importGeneratedFiles(
520 portletDataContext, fileEntry, importedFileEntry,
521 fileEntryElement);
522 }
523
524 Map<String, String> fileEntryTitles =
525 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
526 DLFileEntry.class.getName() + ".title");
527
528 fileEntryTitles.put(fileEntry.getTitle(), importedFileEntry.getTitle());
529
530 portletDataContext.importClassedModel(
531 fileEntry, importedFileEntry, _NAMESPACE);
532 }
533
534 public static void importFileRank(
535 PortletDataContext portletDataContext, Element fileRankElement)
536 throws Exception {
537
538 String path = fileRankElement.attributeValue("path");
539
540 if (!portletDataContext.isPathNotProcessed(path)) {
541 return;
542 }
543
544 DLFileRank fileRank =
545 (DLFileRank)portletDataContext.getZipEntryAsObject(path);
546
547 String fileEntryUuid = fileRankElement.attributeValue(
548 "file-entry-uuid");
549
550 importFileRank(portletDataContext, fileRank, fileEntryUuid);
551 }
552
553 public static void importFolder(
554 PortletDataContext portletDataContext, Element folderElement)
555 throws Exception {
556
557 String path = folderElement.attributeValue("path");
558
559 if (!portletDataContext.isPathNotProcessed(path)) {
560 return;
561 }
562
563 Folder folder = (Folder)portletDataContext.getZipEntryAsObject(path);
564
565 importFolder(portletDataContext, path, folderElement, folder);
566 }
567
568 public static void importRepository(
569 PortletDataContext portletDataContext, Element repositoryElement)
570 throws Exception {
571
572 String path = repositoryElement.attributeValue("path");
573
574 if (!portletDataContext.isPathNotProcessed(path)) {
575 return;
576 }
577
578 Repository repository =
579 (Repository)portletDataContext.getZipEntryAsObject(
580 repositoryElement, path);
581
582 long userId = portletDataContext.getUserId(repository.getUserUuid());
583
584 ServiceContext serviceContext = portletDataContext.createServiceContext(
585 repositoryElement, repository, _NAMESPACE);
586
587 long importedRepositoryId = 0;
588
589 try {
590 if (portletDataContext.isDataStrategyMirror()) {
591 Repository existingRepository = RepositoryUtil.fetchByUUID_G(
592 repository.getUuid(), portletDataContext.getScopeGroupId());
593
594 if (existingRepository == null) {
595 serviceContext.setUuid(repository.getUuid());
596
597 importedRepositoryId =
598 RepositoryLocalServiceUtil.addRepository(
599 userId, portletDataContext.getScopeGroupId(),
600 repository.getClassNameId(),
601 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
602 repository.getName(), repository.getDescription(),
603 repository.getPortletId(),
604 repository.getTypeSettingsProperties(),
605 serviceContext);
606 }
607 else {
608 RepositoryLocalServiceUtil.updateRepository(
609 existingRepository.getRepositoryId(),
610 repository.getName(), repository.getDescription());
611
612 importedRepositoryId = existingRepository.getRepositoryId();
613 }
614 }
615 else {
616 importedRepositoryId = RepositoryLocalServiceUtil.addRepository(
617 userId, portletDataContext.getScopeGroupId(),
618 repository.getClassNameId(),
619 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
620 repository.getName(), repository.getDescription(),
621 repository.getPortletId(),
622 repository.getTypeSettingsProperties(), serviceContext);
623 }
624 }
625 catch (Exception e) {
626 if (_log.isWarnEnabled()) {
627 _log.warn(
628 "Unable to connect to repository {name=" +
629 repository.getName() + ", typeSettings=" +
630 repository.getTypeSettingsProperties() + "}",
631 e);
632 }
633 }
634
635 Repository importedRepository =
636 RepositoryLocalServiceUtil.getRepository(importedRepositoryId);
637
638 portletDataContext.importClassedModel(
639 repository, importedRepository, _NAMESPACE);
640 }
641
642 public static void importRepositoryEntry(
643 PortletDataContext portletDataContext,
644 Element repositoryEntryElement)
645 throws Exception {
646
647 String path = repositoryEntryElement.attributeValue("path");
648
649 if (!portletDataContext.isPathNotProcessed(path)) {
650 return;
651 }
652
653 RepositoryEntry repositoryEntry =
654 (RepositoryEntry)portletDataContext.getZipEntryAsObject(path);
655
656 Map<Long, Long> repositoryIds =
657 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
658 Repository.class);
659
660 long repositoryId = MapUtil.getLong(
661 repositoryIds, repositoryEntry.getRepositoryId(),
662 repositoryEntry.getRepositoryId());
663
664 ServiceContext serviceContext = portletDataContext.createServiceContext(
665 repositoryEntryElement, repositoryEntry, _NAMESPACE);
666
667 RepositoryEntry importedRepositoryEntry = null;
668
669 if (portletDataContext.isDataStrategyMirror()) {
670 RepositoryEntry existingRepositoryEntry =
671 RepositoryEntryUtil.fetchByUUID_G(
672 repositoryEntry.getUuid(),
673 portletDataContext.getScopeGroupId());
674
675 if (existingRepositoryEntry == null) {
676 serviceContext.setUuid(repositoryEntry.getUuid());
677
678 importedRepositoryEntry =
679 RepositoryEntryLocalServiceUtil.addRepositoryEntry(
680 portletDataContext.getScopeGroupId(), repositoryId,
681 repositoryEntry.getMappedId(), serviceContext);
682 }
683 else {
684 importedRepositoryEntry =
685 RepositoryEntryLocalServiceUtil.updateRepositoryEntry(
686 existingRepositoryEntry.getRepositoryEntryId(),
687 repositoryEntry.getMappedId());
688 }
689 }
690 else {
691 importedRepositoryEntry =
692 RepositoryEntryLocalServiceUtil.addRepositoryEntry(
693 portletDataContext.getScopeGroupId(), repositoryId,
694 repositoryEntry.getMappedId(), serviceContext);
695 }
696
697 Map<Long, Long> repositoryEntryIds =
698 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
699 RepositoryEntry.class);
700
701 repositoryEntryIds.put(
702 repositoryEntry.getRepositoryEntryId(),
703 importedRepositoryEntry.getRepositoryEntryId());
704
705 portletDataContext.importClassedModel(
706 repositoryEntry, importedRepositoryEntry, _NAMESPACE);
707 }
708
709 @Override
710 public String[] getDataPortletPreferences() {
711 return new String[] {"rootFolderId"};
712 }
713
714 @Override
715 public PortletDataHandlerControl[] getExportControls() {
716 return new PortletDataHandlerControl[] {
717 _foldersAndDocuments, _shortcuts, _previewsAndThumbnails, _ranks
718 };
719 }
720
721 @Override
722 public PortletDataHandlerControl[] getExportMetadataControls() {
723 return new PortletDataHandlerControl[] {
724 new PortletDataHandlerBoolean(
725 _NAMESPACE, "folders-and-documents", true, _metadataControls)
726 };
727 }
728
729 @Override
730 public PortletDataHandlerControl[] getImportControls() {
731 return new PortletDataHandlerControl[] {
732 _foldersAndDocuments, _shortcuts, _previewsAndThumbnails, _ranks
733 };
734 }
735
736 @Override
737 public PortletDataHandlerControl[] getImportMetadataControls() {
738 return new PortletDataHandlerControl[] {
739 new PortletDataHandlerBoolean(
740 _NAMESPACE, "folders-and-documents", true, _metadataControls)
741 };
742 }
743
744 @Override
745 public boolean isAlwaysExportable() {
746 return _ALWAYS_EXPORTABLE;
747 }
748
749 @Override
750 public boolean isDataLocalized() {
751 return _DATA_LOCALIZED;
752 }
753
754 @Override
755 public boolean isPublishToLiveByDefault() {
756 return PropsValues.DL_PUBLISH_TO_LIVE_BY_DEFAULT;
757 }
758
759 protected static void exportFileEntryType(
760 PortletDataContext portletDataContext,
761 Element fileEntryTypesElement, DLFileEntryType dlFileEntryType)
762 throws Exception {
763
764 String path = getFileEntryTypePath(portletDataContext, dlFileEntryType);
765
766 if (!portletDataContext.isPathNotProcessed(path)) {
767 return;
768 }
769
770 Element fileEntryTypeElement = fileEntryTypesElement.addElement(
771 "file-entry-type");
772
773 List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
774
775 String[] ddmStructureUuids = new String[ddmStructures.size()];
776
777 for (int i = 0; i < ddmStructures.size(); i++) {
778 DDMStructure ddmStructure = ddmStructures.get(i);
779
780 ddmStructureUuids[i] = ddmStructure.getUuid();
781
782 DDMPortletDataHandlerImpl.exportStructure(
783 portletDataContext, fileEntryTypeElement, ddmStructure);
784 }
785
786 fileEntryTypeElement.addAttribute(
787 "structureUuids", StringUtil.merge(ddmStructureUuids));
788
789 portletDataContext.addClassedModel(
790 fileEntryTypeElement, path, dlFileEntryType, _NAMESPACE);
791 }
792
793 protected static void exportFileRank(
794 PortletDataContext portletDataContext, Element fileRanksElement,
795 DLFileRank fileRank)
796 throws Exception {
797
798 String path = getFileRankPath(portletDataContext, fileRank);
799
800 if (!portletDataContext.isPathNotProcessed(path)) {
801 return;
802 }
803
804 Element fileRankElement = fileRanksElement.addElement("file-rank");
805
806 FileEntry fileEntry = FileEntryUtil.fetchByPrimaryKey(
807 fileRank.getFileEntryId());
808
809 String fileEntryUuid = fileEntry.getUuid();
810
811 fileRankElement.addAttribute("file-entry-uuid", fileEntryUuid);
812
813 portletDataContext.addClassedModel(
814 fileRankElement, path, fileRank, _NAMESPACE);
815 }
816
817 protected static void exportFileShortcut(
818 PortletDataContext portletDataContext,
819 Element fileEntryTypesElement, Element foldersElement,
820 Element fileShortcutsElement, Element repositoriesElement,
821 Element repositoryEntriesElement, DLFileShortcut fileShortcut)
822 throws Exception {
823
824 if (!portletDataContext.isWithinDateRange(
825 fileShortcut.getModifiedDate())) {
826
827 return;
828 }
829
830 exportParentFolder(
831 portletDataContext, fileEntryTypesElement, foldersElement,
832 repositoriesElement, repositoryEntriesElement,
833 fileShortcut.getFolderId());
834
835 String path = getFileShortcutPath(portletDataContext, fileShortcut);
836
837 if (portletDataContext.isPathNotProcessed(path)) {
838 Element fileShortcutElement = fileShortcutsElement.addElement(
839 "file-shortcut");
840
841 FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
842 fileShortcut.getToFileEntryId());
843
844 String fileEntryUuid = fileEntry.getUuid();
845
846 fileShortcutElement.addAttribute("file-entry-uuid", fileEntryUuid);
847
848 portletDataContext.addClassedModel(
849 fileShortcutElement, path, fileShortcut, _NAMESPACE);
850 }
851 }
852
853 protected static void exportFolder(
854 PortletDataContext portletDataContext,
855 Element fileEntryTypesElement, Element foldersElement,
856 Element fileEntriesElement, Element fileShortcutsElement,
857 Element fileRanksElement, Element repositoriesElement,
858 Element repositoryEntriesElement, Folder folder, boolean recurse)
859 throws Exception {
860
861 if (!portletDataContext.isWithinDateRange(folder.getLastPostDate())) {
862 return;
863 }
864
865 if (folder.isMountPoint()) {
866 Repository repository = RepositoryUtil.findByPrimaryKey(
867 folder.getRepositoryId());
868
869 exportRepository(
870 portletDataContext, repositoriesElement,
871 repositoryEntriesElement, repository);
872
873 return;
874 }
875 else if (!folder.isDefaultRepository()) {
876
877
878
879
880 return;
881 }
882
883 exportParentFolder(
884 portletDataContext, fileEntryTypesElement, foldersElement,
885 repositoriesElement, repositoryEntriesElement,
886 folder.getParentFolderId());
887
888 String path = getFolderPath(portletDataContext, folder);
889
890 if (!portletDataContext.isPathNotProcessed(path)) {
891 return;
892 }
893
894 Element folderElement = foldersElement.addElement("folder");
895
896 exportFolderFileEntryTypes(
897 portletDataContext, folder, fileEntryTypesElement, folderElement);
898
899 portletDataContext.addClassedModel(
900 folderElement, path, folder, _NAMESPACE);
901
902 if (recurse) {
903 List<Folder> folders = FolderUtil.findByR_P(
904 folder.getRepositoryId(), folder.getFolderId());
905
906 for (Folder curFolder : folders) {
907 exportFolder(
908 portletDataContext, fileEntryTypesElement, foldersElement,
909 fileEntriesElement, fileShortcutsElement, fileRanksElement,
910 repositoriesElement, repositoryEntriesElement, curFolder,
911 recurse);
912 }
913 }
914
915 List<FileEntry> fileEntries = FileEntryUtil.findByR_F(
916 folder.getRepositoryId(), folder.getFolderId());
917
918 for (FileEntry fileEntry : fileEntries) {
919 exportFileEntry(
920 portletDataContext, fileEntryTypesElement, foldersElement,
921 fileEntriesElement, fileRanksElement, repositoriesElement,
922 repositoryEntriesElement, fileEntry, true);
923 }
924
925 if (portletDataContext.getBooleanParameter(_NAMESPACE, "shortcuts")) {
926 List<DLFileShortcut> fileShortcuts = DLFileShortcutUtil.findByG_F(
927 folder.getRepositoryId(), folder.getFolderId());
928
929 for (DLFileShortcut fileShortcut : fileShortcuts) {
930 exportFileShortcut(
931 portletDataContext, fileEntryTypesElement, foldersElement,
932 fileShortcutsElement, repositoriesElement,
933 repositoryEntriesElement, fileShortcut);
934 }
935 }
936 }
937
938 protected static void exportFolderFileEntryTypes(
939 PortletDataContext portletDataContext, Folder folder,
940 Element fileEntryTypesElement, Element folderElement)
941 throws Exception {
942
943 List<DLFileEntryType> dlFileEntryTypes =
944 DLFileEntryTypeLocalServiceUtil.getFolderFileEntryTypes(
945 new long[] {portletDataContext.getScopeGroupId()},
946 folder.getFolderId(), false);
947
948 String[] fileEntryTypeUuids = new String[dlFileEntryTypes.size()];
949
950 long defaultFileEntryTypeId =
951 DLFileEntryTypeLocalServiceUtil.getDefaultFileEntryTypeId(
952 folder.getFolderId());
953
954 String defaultFileEntryTypeUuid = StringPool.BLANK;
955
956 for (int i = 0; i < dlFileEntryTypes.size(); i++) {
957 DLFileEntryType dlFileEntryType = dlFileEntryTypes.get(i);
958
959 if (!isFileEntryTypeExportable(dlFileEntryType)) {
960 continue;
961 }
962
963 if (dlFileEntryType.getFileEntryTypeId() == 0) {
964 fileEntryTypeUuids[i] = "@basic_document@";
965 }
966 else {
967 fileEntryTypeUuids[i] = dlFileEntryType.getUuid();
968 }
969
970 if (defaultFileEntryTypeId ==
971 dlFileEntryType.getFileEntryTypeId()) {
972
973 defaultFileEntryTypeUuid = dlFileEntryType.getUuid();
974 }
975
976 exportFileEntryType(
977 portletDataContext, fileEntryTypesElement, dlFileEntryType);
978 }
979
980 folderElement.addAttribute(
981 "fileEntryTypeUuids", StringUtil.merge(fileEntryTypeUuids));
982 folderElement.addAttribute(
983 "defaultFileEntryTypeUuid", defaultFileEntryTypeUuid);
984 }
985
986 protected static void exportMetaData(
987 PortletDataContext portletDataContext,
988 Element fileEntryTypesElement, Element fileEntryElement,
989 FileEntry fileEntry)
990 throws Exception {
991
992 if (!(fileEntry instanceof LiferayFileEntry)) {
993 return;
994 }
995
996 LiferayFileEntry liferayFileEntry = (LiferayFileEntry)fileEntry;
997
998 DLFileEntry dlFileEntry = liferayFileEntry.getDLFileEntry();
999
1000 long fileEntryTypeId = dlFileEntry.getFileEntryTypeId();
1001
1002 DLFileEntryType dlFileEntryType =
1003 DLFileEntryTypeLocalServiceUtil.fetchFileEntryType(fileEntryTypeId);
1004
1005 if (dlFileEntryType == null) {
1006 return;
1007 }
1008
1009 fileEntryElement.addAttribute(
1010 "fileEntryTypeUuid", dlFileEntryType.getUuid());
1011
1012 if (!isFileEntryTypeExportable(dlFileEntryType)) {
1013 return;
1014 }
1015
1016 exportFileEntryType(
1017 portletDataContext, fileEntryTypesElement, dlFileEntryType);
1018
1019 fileEntryElement.addAttribute(
1020 "fileEntryTypeUuid", dlFileEntryType.getUuid());
1021
1022 List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
1023
1024 for (DDMStructure ddmStructure : ddmStructures) {
1025 Element structureFields = fileEntryElement.addElement(
1026 "structure-fields");
1027
1028 String path = getFileEntryFileEntryTypeStructureFieldsPath(
1029 portletDataContext, fileEntry, dlFileEntryType.getUuid(),
1030 ddmStructure.getStructureId());
1031
1032 structureFields.addAttribute("path", path);
1033
1034 structureFields.addAttribute(
1035 "structureUuid", ddmStructure.getUuid());
1036
1037 FileVersion fileVersion = fileEntry.getFileVersion();
1038
1039 DLFileEntryMetadata dlFileEntryMetadata =
1040 DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadata(
1041 ddmStructure.getStructureId(),
1042 fileVersion.getFileVersionId());
1043
1044 Fields fields = StorageEngineUtil.getFields(
1045 dlFileEntryMetadata.getDDMStorageId());
1046
1047 portletDataContext.addZipEntry(path, fields);
1048 }
1049 }
1050
1051 protected static void exportParentFolder(
1052 PortletDataContext portletDataContext,
1053 Element fileEntryTypesElement, Element foldersElement,
1054 Element repositoriesElement, Element repositoryEntriesElement,
1055 long folderId)
1056 throws Exception {
1057
1058 if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1059 return;
1060 }
1061
1062 Folder folder = DLAppLocalServiceUtil.getFolder(folderId);
1063
1064 if (folder.isMountPoint()) {
1065 Repository repository = RepositoryUtil.findByPrimaryKey(
1066 folder.getRepositoryId());
1067
1068 exportRepository(
1069 portletDataContext, repositoriesElement,
1070 repositoryEntriesElement, repository);
1071
1072 return;
1073 }
1074 else if (!folder.isDefaultRepository()) {
1075
1076
1077 return;
1078 }
1079
1080 exportParentFolder(
1081 portletDataContext, fileEntryTypesElement, foldersElement,
1082 repositoriesElement, repositoryEntriesElement,
1083 folder.getParentFolderId());
1084
1085 String path = getFolderPath(portletDataContext, folder);
1086
1087 if (portletDataContext.isPathNotProcessed(path)) {
1088 Element folderElement = foldersElement.addElement("folder");
1089
1090 exportFolderFileEntryTypes(
1091 portletDataContext, folder, fileEntryTypesElement,
1092 folderElement);
1093
1094 portletDataContext.addClassedModel(
1095 folderElement, path, folder, _NAMESPACE);
1096 }
1097 }
1098
1099 protected static void exportRepository(
1100 PortletDataContext portletDataContext, Element repositoriesElement,
1101 Element repositoryEntriesElement, Repository repository)
1102 throws Exception {
1103
1104 if (!portletDataContext.isWithinDateRange(
1105 repository.getModifiedDate())) {
1106
1107 return;
1108 }
1109
1110 String path = getRepositoryPath(portletDataContext, repository);
1111
1112 if (!portletDataContext.isPathNotProcessed(path)) {
1113 return;
1114 }
1115
1116 Element repositoryElement = repositoriesElement.addElement(
1117 "repository");
1118
1119 portletDataContext.addClassedModel(
1120 repositoryElement, path, repository, _NAMESPACE);
1121
1122 List<RepositoryEntry> repositoryEntries =
1123 RepositoryEntryUtil.findByRepositoryId(
1124 repository.getRepositoryId());
1125
1126 for (RepositoryEntry repositoryEntry : repositoryEntries) {
1127 exportRepositoryEntry(
1128 portletDataContext, repositoryEntriesElement, repositoryEntry);
1129 }
1130 }
1131
1132 protected static void exportRepositoryEntry(
1133 PortletDataContext portletDataContext,
1134 Element repositoryEntriesElement, RepositoryEntry repositoryEntry)
1135 throws Exception {
1136
1137 String path = getRepositoryEntryPath(
1138 portletDataContext, repositoryEntry);
1139
1140 if (!portletDataContext.isPathNotProcessed(path)) {
1141 return;
1142 }
1143
1144 Element repositoryEntryElement = repositoryEntriesElement.addElement(
1145 "repository-entry");
1146
1147 portletDataContext.addClassedModel(
1148 repositoryEntryElement, path, repositoryEntry, _NAMESPACE);
1149 }
1150
1151 protected static String getFileEntryBinPath(
1152 PortletDataContext portletDataContext, FileEntry fileEntry) {
1153
1154 StringBundler sb = new StringBundler(5);
1155
1156 sb.append(
1157 portletDataContext.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
1158 sb.append("/bin/");
1159 sb.append(fileEntry.getFileEntryId());
1160 sb.append(StringPool.SLASH);
1161 sb.append(fileEntry.getVersion());
1162
1163 return sb.toString();
1164 }
1165
1166 protected static String getFileEntryFileEntryTypeStructureFieldsPath(
1167 PortletDataContext portletDataContext, FileEntry fileEntry,
1168 String fileEntryTypeUuid, long structureId) {
1169
1170 StringBundler sb = new StringBundler(4);
1171
1172 String fileEntryPath = getFileEntryPath(portletDataContext, fileEntry);
1173
1174 sb.append(StringUtil.replace(fileEntryPath, ".xml", StringPool.BLANK));
1175 sb.append("/file-entry-type/");
1176 sb.append(fileEntryTypeUuid);
1177 sb.append("/structure-fields/");
1178 sb.append(structureId);
1179 sb.append(".xml");
1180
1181 return sb.toString();
1182 }
1183
1184
1190 protected static String getFileEntryTypeName(
1191 String uuid, long groupId, String name, int count)
1192 throws Exception {
1193
1194 DLFileEntryType dlFileEntryType = DLFileEntryTypeUtil.fetchByG_N(
1195 groupId, name);
1196
1197 if (dlFileEntryType == null) {
1198 return name;
1199 }
1200
1201 if (Validator.isNotNull(uuid) &&
1202 uuid.equals(dlFileEntryType.getUuid())) {
1203
1204 return name;
1205 }
1206
1207 name = StringUtil.appendParentheticalSuffix(name, count);
1208
1209 return getFileEntryTypeName(uuid, groupId, name, ++count);
1210 }
1211
1212 protected static String getFileEntryTypePath(
1213 PortletDataContext portletDataContext,
1214 DLFileEntryType dlFileEntryType) {
1215
1216 StringBundler sb = new StringBundler(4);
1217
1218 sb.append(
1219 portletDataContext.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
1220 sb.append("/entry-types/");
1221 sb.append(dlFileEntryType.getFileEntryTypeId());
1222 sb.append(".xml");
1223
1224 return sb.toString();
1225 }
1226
1227 protected static String getFileRankPath(
1228 PortletDataContext portletDataContext, DLFileRank fileRank) {
1229
1230 StringBundler sb = new StringBundler(4);
1231
1232 sb.append(
1233 portletDataContext.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
1234 sb.append("/ranks/");
1235 sb.append(fileRank.getFileRankId());
1236 sb.append(".xml");
1237
1238 return sb.toString();
1239 }
1240
1241 protected static String getFileShortcutPath(
1242 PortletDataContext portletDataContext, DLFileShortcut fileShortcut) {
1243
1244 StringBundler sb = new StringBundler(4);
1245
1246 sb.append(
1247 portletDataContext.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
1248 sb.append("/shortcuts/");
1249 sb.append(fileShortcut.getFileShortcutId());
1250 sb.append(".xml");
1251
1252 return sb.toString();
1253 }
1254
1255
1261 protected static String getFolderName(
1262 String uuid, long groupId, long parentFolderId, String name,
1263 int count)
1264 throws Exception {
1265
1266 Folder folder = FolderUtil.fetchByR_P_N(groupId, parentFolderId, name);
1267
1268 if (folder == null) {
1269 return name;
1270 }
1271
1272 if (Validator.isNotNull(uuid) && uuid.equals(folder.getUuid())) {
1273 return name;
1274 }
1275
1276 name = StringUtil.appendParentheticalSuffix(name, count);
1277
1278 return getFolderName(uuid, groupId, parentFolderId, name, ++count);
1279 }
1280
1281 protected static String getFolderPath(
1282 PortletDataContext portletDataContext, Folder folder) {
1283
1284 StringBundler sb = new StringBundler(4);
1285
1286 sb.append(
1287 portletDataContext.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
1288 sb.append("/folders/");
1289 sb.append(folder.getFolderId());
1290 sb.append(".xml");
1291
1292 return sb.toString();
1293 }
1294
1295 protected static String getImportFolderPath(
1296 PortletDataContext portletDataContext, long folderId) {
1297
1298 StringBundler sb = new StringBundler(4);
1299
1300 sb.append(
1301 portletDataContext.getSourcePortletPath(
1302 PortletKeys.DOCUMENT_LIBRARY));
1303 sb.append("/folders/");
1304 sb.append(folderId);
1305 sb.append(".xml");
1306
1307 return sb.toString();
1308 }
1309
1310 protected static String getRepositoryEntryPath(
1311 PortletDataContext portletDataContext,
1312 RepositoryEntry repositoryEntry) {
1313
1314 StringBundler sb = new StringBundler(4);
1315
1316 sb.append(
1317 portletDataContext.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
1318 sb.append("/repository-entries/");
1319 sb.append(repositoryEntry.getRepositoryEntryId());
1320 sb.append(".xml");
1321
1322 return sb.toString();
1323 }
1324
1325 protected static String getRepositoryPath(
1326 PortletDataContext portletDataContext, Repository repository) {
1327
1328 StringBundler sb = new StringBundler(4);
1329
1330 sb.append(
1331 portletDataContext.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
1332 sb.append("/repositories/");
1333 sb.append(repository.getRepositoryId());
1334 sb.append(".xml");
1335
1336 return sb.toString();
1337 }
1338
1339 protected static void importFileEntryType(
1340 PortletDataContext portletDataContext, Element fileEntryTypeElement)
1341 throws Exception {
1342
1343 String path = fileEntryTypeElement.attributeValue("path");
1344
1345 if (!portletDataContext.isPathNotProcessed(path)) {
1346 return;
1347 }
1348
1349 DLFileEntryType dlFileEntryType =
1350 (DLFileEntryType)portletDataContext.getZipEntryAsObject(path);
1351
1352 long userId = portletDataContext.getUserId(
1353 dlFileEntryType.getUserUuid());
1354
1355 String name = getFileEntryTypeName(
1356 dlFileEntryType.getUuid(), portletDataContext.getScopeGroupId(),
1357 dlFileEntryType.getName(), 2);
1358
1359 List<Element> structureElements = fileEntryTypeElement.elements(
1360 "structure");
1361
1362 for (Element structureElement : structureElements) {
1363 DDMPortletDataHandlerImpl.importStructure(
1364 portletDataContext, structureElement);
1365 }
1366
1367 Map<Long, Long> ddmStructureIds =
1368 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
1369 DDMStructure.class);
1370
1371 List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
1372
1373 long[] ddmStructureIdsArray = new long[ddmStructures.size()];
1374
1375 for (int i = 0; i < ddmStructures.size(); i++) {
1376 DDMStructure ddmStructure = ddmStructures.get(i);
1377
1378 ddmStructureIdsArray[i] = MapUtil.getLong(
1379 ddmStructureIds, ddmStructure.getStructureId());
1380 }
1381
1382 ServiceContext serviceContext = portletDataContext.createServiceContext(
1383 path, dlFileEntryType, _NAMESPACE);
1384
1385 DLFileEntryType importedDLFileEntryType = null;
1386
1387 if (portletDataContext.isDataStrategyMirror()) {
1388 DLFileEntryType existingDLFileEntryType =
1389 DLFileEntryTypeUtil.fetchByUUID_G(
1390 dlFileEntryType.getUuid(),
1391 portletDataContext.getScopeGroupId());
1392
1393 if (existingDLFileEntryType == null) {
1394 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
1395 portletDataContext.getCompanyId());
1396
1397 existingDLFileEntryType = DLFileEntryTypeUtil.fetchByUUID_G(
1398 dlFileEntryType.getUuid(), companyGroup.getGroupId());
1399 }
1400
1401 if (existingDLFileEntryType == null) {
1402 serviceContext.setUuid(dlFileEntryType.getUuid());
1403
1404 importedDLFileEntryType =
1405 DLFileEntryTypeLocalServiceUtil.addFileEntryType(
1406 userId, portletDataContext.getScopeGroupId(), name,
1407 dlFileEntryType.getDescription(), ddmStructureIdsArray,
1408 serviceContext);
1409 }
1410 else {
1411 if (!isFileEntryTypeGlobal(
1412 portletDataContext.getCompanyId(),
1413 existingDLFileEntryType)) {
1414
1415 DLFileEntryTypeLocalServiceUtil.updateFileEntryType(
1416 userId, existingDLFileEntryType.getFileEntryTypeId(),
1417 name, dlFileEntryType.getDescription(),
1418 ddmStructureIdsArray, serviceContext);
1419 }
1420
1421 importedDLFileEntryType = existingDLFileEntryType;
1422 }
1423 }
1424 else {
1425 importedDLFileEntryType =
1426 DLFileEntryTypeLocalServiceUtil.addFileEntryType(
1427 userId, portletDataContext.getScopeGroupId(), name,
1428 dlFileEntryType.getDescription(), ddmStructureIdsArray,
1429 serviceContext);
1430 }
1431
1432 if (!isFileEntryTypeGlobal(
1433 portletDataContext.getCompanyId(), importedDLFileEntryType)) {
1434
1435 portletDataContext.importClassedModel(
1436 dlFileEntryType, importedDLFileEntryType, _NAMESPACE);
1437
1438 String importedDLFileEntryDDMStructureKey =
1439 DLUtil.getDDMStructureKey(importedDLFileEntryType);
1440
1441 List<DDMStructure> importedDDMStructures =
1442 importedDLFileEntryType.getDDMStructures();
1443
1444 for (DDMStructure importedDDMStructure : importedDDMStructures) {
1445 String ddmStructureKey = importedDDMStructure.getStructureKey();
1446
1447 if (!DLUtil.isAutoGeneratedDLFileEntryTypeDDMStructureKey(
1448 ddmStructureKey)) {
1449
1450 continue;
1451 }
1452
1453 if (ddmStructureKey.equals(
1454 importedDLFileEntryDDMStructureKey)) {
1455
1456 continue;
1457 }
1458
1459 importedDDMStructure.setStructureKey(
1460 importedDLFileEntryDDMStructureKey);
1461
1462 DDMStructureLocalServiceUtil.updateDDMStructure(
1463 importedDDMStructure);
1464 }
1465 }
1466 }
1467
1468 protected static void importFileRank(
1469 PortletDataContext portletDataContext, DLFileRank fileRank,
1470 String fileEntryUuid)
1471 throws Exception {
1472
1473 long userId = portletDataContext.getUserId(fileRank.getUserUuid());
1474
1475 long groupId = portletDataContext.getScopeGroupId();
1476
1477 FileEntry fileEntry = FileEntryUtil.fetchByUUID_R(
1478 fileEntryUuid, groupId);
1479
1480 if (fileEntry == null) {
1481 if (_log.isWarnEnabled()) {
1482 _log.warn(
1483 "Unable to retrieve file " + fileEntryUuid +
1484 " to import file rank");
1485 }
1486
1487 return;
1488 }
1489
1490 long fileEntryId = fileEntry.getFileEntryId();
1491
1492 ServiceContext serviceContext = new ServiceContext();
1493
1494 serviceContext.setCreateDate(fileRank.getCreateDate());
1495
1496 DLAppLocalServiceUtil.updateFileRank(
1497 portletDataContext.getScopeGroupId(),
1498 portletDataContext.getCompanyId(), userId, fileEntryId,
1499 serviceContext);
1500 }
1501
1502 protected static void importFileShortcut(
1503 PortletDataContext portletDataContext, Element fileShortcutElement)
1504 throws Exception {
1505
1506 String path = fileShortcutElement.attributeValue("path");
1507
1508 if (!portletDataContext.isPathNotProcessed(path)) {
1509 return;
1510 }
1511
1512 DLFileShortcut fileShortcut =
1513 (DLFileShortcut)portletDataContext.getZipEntryAsObject(path);
1514
1515 importFileShortcut(
1516 portletDataContext, fileShortcutElement, fileShortcut);
1517 }
1518
1519 protected static void importFileShortcut(
1520 PortletDataContext portletDataContext, Element fileShortcutElement,
1521 DLFileShortcut fileShortcut)
1522 throws Exception {
1523
1524 long userId = portletDataContext.getUserId(fileShortcut.getUserUuid());
1525
1526 Map<Long, Long> folderIds =
1527 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
1528 DLFolder.class);
1529
1530 long folderId = MapUtil.getLong(
1531 folderIds, fileShortcut.getFolderId(), fileShortcut.getFolderId());
1532
1533 long groupId = portletDataContext.getScopeGroupId();
1534
1535 if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1536 Folder folder = FolderUtil.findByPrimaryKey(folderId);
1537
1538 groupId = folder.getRepositoryId();
1539 }
1540
1541 String fileEntryUuid = fileShortcutElement.attributeValue(
1542 "file-entry-uuid");
1543
1544 FileEntry fileEntry = FileEntryUtil.fetchByUUID_R(
1545 fileEntryUuid, groupId);
1546
1547 if (fileEntry == null) {
1548 if (_log.isWarnEnabled()) {
1549 _log.warn(
1550 "Unable to fetch file entry {uuid=" + fileEntryUuid +
1551 ", groupId=" + groupId + "}");
1552 }
1553
1554 return;
1555 }
1556
1557 long fileEntryId = fileEntry.getFileEntryId();
1558
1559 ServiceContext serviceContext = portletDataContext.createServiceContext(
1560 fileShortcutElement, fileShortcut, _NAMESPACE);
1561
1562 DLFileShortcut importedFileShortcut = null;
1563
1564 if (portletDataContext.isDataStrategyMirror()) {
1565 DLFileShortcut existingFileShortcut =
1566 DLFileShortcutUtil.fetchByUUID_G(
1567 fileShortcut.getUuid(),
1568 portletDataContext.getScopeGroupId());
1569
1570 if (existingFileShortcut == null) {
1571 serviceContext.setUuid(fileShortcut.getUuid());
1572
1573 importedFileShortcut = DLAppLocalServiceUtil.addFileShortcut(
1574 userId, groupId, folderId, fileEntryId, serviceContext);
1575 }
1576 else {
1577 importedFileShortcut = DLAppLocalServiceUtil.updateFileShortcut(
1578 userId, existingFileShortcut.getFileShortcutId(), folderId,
1579 fileEntryId, serviceContext);
1580 }
1581 }
1582 else {
1583 importedFileShortcut = DLAppLocalServiceUtil.addFileShortcut(
1584 userId, groupId, folderId, fileEntryId, serviceContext);
1585 }
1586
1587 portletDataContext.importClassedModel(
1588 fileShortcut, importedFileShortcut, _NAMESPACE);
1589 }
1590
1591 protected static void importFolder(
1592 PortletDataContext portletDataContext, String folderPath,
1593 Element folderElement, Folder folder)
1594 throws Exception {
1595
1596 long userId = portletDataContext.getUserId(folder.getUserUuid());
1597
1598 Map<Long, Long> folderIds =
1599 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
1600 DLFolder.class);
1601
1602 long parentFolderId = MapUtil.getLong(
1603 folderIds, folder.getParentFolderId(), folder.getParentFolderId());
1604
1605 ServiceContext serviceContext = portletDataContext.createServiceContext(
1606 folderPath, folder, _NAMESPACE);
1607
1608 if ((parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
1609 (parentFolderId == folder.getParentFolderId())) {
1610
1611 String path = getImportFolderPath(
1612 portletDataContext, parentFolderId);
1613
1614 Folder parentFolder =
1615 (Folder)portletDataContext.getZipEntryAsObject(path);
1616
1617 importFolder(portletDataContext, path, folderElement, parentFolder);
1618
1619 parentFolderId = MapUtil.getLong(
1620 folderIds, folder.getParentFolderId(),
1621 folder.getParentFolderId());
1622 }
1623
1624 Folder importedFolder = null;
1625
1626 if (portletDataContext.isDataStrategyMirror()) {
1627 Folder existingFolder = FolderUtil.fetchByUUID_R(
1628 folder.getUuid(), portletDataContext.getScopeGroupId());
1629
1630 if (existingFolder == null) {
1631 String name = getFolderName(
1632 null, portletDataContext.getScopeGroupId(), parentFolderId,
1633 folder.getName(), 2);
1634
1635 serviceContext.setUuid(folder.getUuid());
1636
1637 importedFolder = DLAppLocalServiceUtil.addFolder(
1638 userId, portletDataContext.getScopeGroupId(),
1639 parentFolderId, name, folder.getDescription(),
1640 serviceContext);
1641 }
1642 else {
1643 String name = getFolderName(
1644 folder.getUuid(), portletDataContext.getScopeGroupId(),
1645 parentFolderId, folder.getName(), 2);
1646
1647 importedFolder = DLAppLocalServiceUtil.updateFolder(
1648 existingFolder.getFolderId(), parentFolderId, name,
1649 folder.getDescription(), serviceContext);
1650 }
1651 }
1652 else {
1653 String name = getFolderName(
1654 null, portletDataContext.getScopeGroupId(), parentFolderId,
1655 folder.getName(), 2);
1656
1657 importedFolder = DLAppLocalServiceUtil.addFolder(
1658 userId, portletDataContext.getScopeGroupId(), parentFolderId,
1659 name, folder.getDescription(), serviceContext);
1660 }
1661
1662 folderIds.put(folder.getFolderId(), importedFolder.getFolderId());
1663
1664 importFolderFileEntryTypes(
1665 portletDataContext, folderElement, importedFolder, serviceContext);
1666
1667 portletDataContext.importClassedModel(
1668 folder, importedFolder, _NAMESPACE);
1669 }
1670
1671 protected static void importFolderFileEntryTypes(
1672 PortletDataContext portletDataContext, Element folderElement,
1673 Folder folder, ServiceContext serviceContext)
1674 throws Exception {
1675
1676 if (folderElement == null) {
1677 return;
1678 }
1679
1680 String[] fileEntryTypeUuids = StringUtil.split(
1681 folderElement.attributeValue("fileEntryTypeUuids"));
1682
1683 List<Long> fileEntryTypeIds = new ArrayList<Long>();
1684
1685 String defaultFileEntryTypeUuid = GetterUtil.getString(
1686 folderElement.attributeValue("defaultFileEntryTypeUuid"));
1687
1688 long defaultFileEntryTypeId = 0;
1689
1690 for (String fileEntryTypeUuid : fileEntryTypeUuids) {
1691 DLFileEntryType dlFileEntryType = DLFileEntryTypeUtil.fetchByUUID_G(
1692 fileEntryTypeUuid, portletDataContext.getScopeGroupId());
1693
1694 if (dlFileEntryType == null) {
1695 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
1696 portletDataContext.getCompanyId());
1697
1698 dlFileEntryType = DLFileEntryTypeUtil.fetchByUUID_G(
1699 fileEntryTypeUuid, companyGroup.getGroupId());
1700 }
1701
1702 if (dlFileEntryType == null) {
1703 dlFileEntryType = DLFileEntryTypeUtil.fetchByUUID_G(
1704 fileEntryTypeUuid, 0);
1705 }
1706
1707 if ((dlFileEntryType == null) &&
1708 fileEntryTypeUuid.equals("@basic_document@")) {
1709
1710 dlFileEntryType =
1711 DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryType(0);
1712 }
1713
1714 if (dlFileEntryType == null) {
1715 continue;
1716 }
1717
1718 fileEntryTypeIds.add(dlFileEntryType.getFileEntryTypeId());
1719
1720 if (defaultFileEntryTypeUuid.equals(dlFileEntryType.getUuid())) {
1721 defaultFileEntryTypeId = dlFileEntryType.getFileEntryTypeId();
1722 }
1723 }
1724
1725 if (!fileEntryTypeIds.isEmpty()) {
1726 DLFolder dlFolder = (DLFolder)folder.getModel();
1727
1728 if (Validator.isNotNull(defaultFileEntryTypeId)) {
1729 dlFolder.setDefaultFileEntryTypeId(defaultFileEntryTypeId);
1730
1731 dlFolder.setOverrideFileEntryTypes(true);
1732
1733 DLFolderLocalServiceUtil.updateDLFolder(dlFolder);
1734 }
1735
1736 DLFileEntryTypeLocalServiceUtil.updateFolderFileEntryTypes(
1737 dlFolder, fileEntryTypeIds, defaultFileEntryTypeId,
1738 serviceContext);
1739 }
1740 }
1741
1742 protected static void importMetaData(
1743 PortletDataContext portletDataContext, Element fileEntryElement,
1744 ServiceContext serviceContext)
1745 throws Exception {
1746
1747 String fileEntryTypeUuid = fileEntryElement.attributeValue(
1748 "fileEntryTypeUuid");
1749
1750 if (Validator.isNull(fileEntryTypeUuid)) {
1751 return;
1752 }
1753
1754 DLFileEntryType dlFileEntryType = DLFileEntryTypeUtil.fetchByUUID_G(
1755 fileEntryTypeUuid, portletDataContext.getScopeGroupId());
1756
1757 if (dlFileEntryType == null) {
1758 Group group = GroupLocalServiceUtil.getCompanyGroup(
1759 portletDataContext.getCompanyId());
1760
1761 dlFileEntryType = DLFileEntryTypeUtil.fetchByUUID_G(
1762 fileEntryTypeUuid, group.getGroupId());
1763
1764 if (dlFileEntryType == null) {
1765 serviceContext.setAttribute("fileEntryTypeId", -1);
1766
1767 return;
1768 }
1769 }
1770
1771 serviceContext.setAttribute(
1772 "fileEntryTypeId", dlFileEntryType.getFileEntryTypeId());
1773
1774 List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
1775
1776 for (DDMStructure ddmStructure : ddmStructures) {
1777 Element structureFieldsElement =
1778 (Element)fileEntryElement.selectSingleNode(
1779 "structure-fields[@structureUuid='".concat(
1780 ddmStructure.getUuid()).concat("']"));
1781
1782 if (structureFieldsElement == null) {
1783 continue;
1784 }
1785
1786 String path = structureFieldsElement.attributeValue("path");
1787
1788 Fields fields = (Fields)portletDataContext.getZipEntryAsObject(
1789 path);
1790
1791 serviceContext.setAttribute(
1792 Fields.class.getName() + ddmStructure.getStructureId(), fields);
1793 }
1794 }
1795
1796 protected static boolean isFileEntryTypeExportable(
1797 DLFileEntryType dlFileEntryType) {
1798
1799 if (dlFileEntryType.getFileEntryTypeId() == 0) {
1800 return false;
1801 }
1802
1803 return true;
1804 }
1805
1806 protected static boolean isFileEntryTypeGlobal(
1807 long companyId, DLFileEntryType dlFileEntryType)
1808 throws PortalException, SystemException {
1809
1810 Group group = GroupLocalServiceUtil.getCompanyGroup(companyId);
1811
1812 if (dlFileEntryType.getGroupId() == group.getGroupId()) {
1813 return true;
1814 }
1815
1816 return false;
1817 }
1818
1819 @Override
1820 protected PortletPreferences doDeleteData(
1821 PortletDataContext portletDataContext, String portletId,
1822 PortletPreferences portletPreferences)
1823 throws Exception {
1824
1825 if (!portletDataContext.addPrimaryKey(
1826 DLPortletDataHandlerImpl.class, "deleteData")) {
1827
1828 DLAppLocalServiceUtil.deleteAll(
1829 portletDataContext.getScopeGroupId());
1830 }
1831
1832 return null;
1833 }
1834
1835 @Override
1836 protected String doExportData(
1837 PortletDataContext portletDataContext, String portletId,
1838 PortletPreferences portletPreferences)
1839 throws Exception {
1840
1841 portletDataContext.addPermissions(
1842 "com.liferay.portlet.documentlibrary",
1843 portletDataContext.getScopeGroupId());
1844
1845 Document document = SAXReaderUtil.createDocument();
1846
1847 Element rootElement = document.addElement("documentlibrary-data");
1848
1849 rootElement.addAttribute(
1850 "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
1851
1852 long rootFolderId = GetterUtil.getLong(
1853 portletPreferences.getValue("rootFolderId", null));
1854
1855 if (rootFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1856 rootElement.addAttribute(
1857 "root-folder-id", String.valueOf(rootFolderId));
1858 }
1859
1860 Element fileEntryTypesElement = rootElement.addElement(
1861 "file-entry-types");
1862 Element foldersElement = rootElement.addElement("folders");
1863 Element fileEntriesElement = rootElement.addElement("file-entries");
1864 Element fileShortcutsElement = rootElement.addElement("file-shortcuts");
1865 Element fileRanksElement = rootElement.addElement("file-ranks");
1866 Element repositoriesElement = rootElement.addElement("repositories");
1867 Element repositoryEntriesElement = rootElement.addElement(
1868 "repository-entries");
1869
1870 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
1871 portletDataContext.getCompanyId());
1872
1873 List<DLFileEntryType> dlFileEntryTypes =
1874 DLFileEntryTypeServiceUtil.getFileEntryTypes(
1875 new long[] {
1876 portletDataContext.getScopeGroupId(),
1877 companyGroup.getGroupId()
1878 });
1879
1880 for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
1881 if (!isFileEntryTypeExportable(dlFileEntryType)) {
1882 continue;
1883 }
1884
1885 exportFileEntryType(
1886 portletDataContext, fileEntryTypesElement, dlFileEntryType);
1887 }
1888
1889 List<Folder> folders = FolderUtil.findByRepositoryId(
1890 portletDataContext.getScopeGroupId());
1891
1892 for (Folder folder : folders) {
1893 exportFolder(
1894 portletDataContext, fileEntryTypesElement, foldersElement,
1895 fileEntriesElement, fileShortcutsElement, fileRanksElement,
1896 repositoriesElement, repositoryEntriesElement, folder, false);
1897 }
1898
1899 List<FileEntry> fileEntries = FileEntryUtil.findByR_F(
1900 portletDataContext.getScopeGroupId(),
1901 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
1902
1903 for (FileEntry fileEntry : fileEntries) {
1904 exportFileEntry(
1905 portletDataContext, fileEntryTypesElement, foldersElement,
1906 fileEntriesElement, fileRanksElement, repositoriesElement,
1907 repositoryEntriesElement, fileEntry, true);
1908 }
1909
1910 if (portletDataContext.getBooleanParameter(_NAMESPACE, "shortcuts")) {
1911 List<DLFileShortcut> fileShortcuts = DLFileShortcutUtil.findByG_F(
1912 portletDataContext.getScopeGroupId(),
1913 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
1914
1915 for (DLFileShortcut fileShortcut : fileShortcuts) {
1916 exportFileShortcut(
1917 portletDataContext, fileEntryTypesElement, foldersElement,
1918 fileShortcutsElement, repositoriesElement,
1919 repositoryEntriesElement, fileShortcut);
1920 }
1921 }
1922
1923 return document.formattedString();
1924 }
1925
1926 @Override
1927 protected PortletPreferences doImportData(
1928 PortletDataContext portletDataContext, String portletId,
1929 PortletPreferences portletPreferences, String data)
1930 throws Exception {
1931
1932 portletDataContext.importPermissions(
1933 "com.liferay.portlet.documentlibrary",
1934 portletDataContext.getSourceGroupId(),
1935 portletDataContext.getScopeGroupId());
1936
1937 Document document = SAXReaderUtil.read(data);
1938
1939 Element rootElement = document.getRootElement();
1940
1941 Element repositoriesElement = rootElement.element("repositories");
1942
1943 if (repositoriesElement != null) {
1944 List<Element> repositoryElements = repositoriesElement.elements(
1945 "repository");
1946
1947 for (Element repositoryElement : repositoryElements) {
1948 importRepository(portletDataContext, repositoryElement);
1949 }
1950 }
1951
1952 Element repositoryEntriesElement = rootElement.element(
1953 "repository-entries");
1954
1955 List<Element> repositoryEntryElements =
1956 repositoryEntriesElement.elements("repository-entry");
1957
1958 for (Element repositoryEntryElement : repositoryEntryElements) {
1959 importRepositoryEntry(portletDataContext, repositoryEntryElement);
1960 }
1961
1962 Element fileEntryTypesElement = rootElement.element("file-entry-types");
1963
1964 List<Element> fileEntryTypeElements = fileEntryTypesElement.elements(
1965 "file-entry-type");
1966
1967 for (Element fileEntryTypeElement : fileEntryTypeElements) {
1968 importFileEntryType(portletDataContext, fileEntryTypeElement);
1969 }
1970
1971 Element foldersElement = rootElement.element("folders");
1972
1973 List<Element> folderElements = foldersElement.elements("folder");
1974
1975 for (Element folderElement : folderElements) {
1976 importFolder(portletDataContext, folderElement);
1977 }
1978
1979 Element fileEntriesElement = rootElement.element("file-entries");
1980
1981 List<Element> fileEntryElements = fileEntriesElement.elements(
1982 "file-entry");
1983
1984 for (Element fileEntryElement : fileEntryElements) {
1985 importFileEntry(portletDataContext, fileEntryElement);
1986 }
1987
1988 if (portletDataContext.getBooleanParameter(_NAMESPACE, "shortcuts")) {
1989 List<Element> fileShortcutElements = rootElement.element(
1990 "file-shortcuts").elements("file-shortcut");
1991
1992 for (Element fileShortcutElement : fileShortcutElements) {
1993 importFileShortcut(portletDataContext, fileShortcutElement);
1994 }
1995 }
1996
1997 if (portletDataContext.getBooleanParameter(_NAMESPACE, "ranks")) {
1998 Element fileRanksElement = rootElement.element("file-ranks");
1999
2000 List<Element> fileRankElements = fileRanksElement.elements(
2001 "file-rank");
2002
2003 for (Element fileRankElement : fileRankElements) {
2004 importFileRank(portletDataContext, fileRankElement);
2005 }
2006 }
2007
2008 long rootFolderId = GetterUtil.getLong(
2009 rootElement.attributeValue("root-folder-id"));
2010
2011 if (rootFolderId > 0) {
2012 Map<Long, Long> folderIds =
2013 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
2014 DLFolder.class);
2015
2016 rootFolderId = MapUtil.getLong(
2017 folderIds, rootFolderId, rootFolderId);
2018
2019 portletPreferences.setValue(
2020 "rootFolderId", String.valueOf(rootFolderId));
2021 }
2022
2023 return portletPreferences;
2024 }
2025
2026 private static final boolean _ALWAYS_EXPORTABLE = true;
2027
2028 private static final boolean _DATA_LOCALIZED = true;
2029
2030 private static final String _NAMESPACE = "document_library";
2031
2032 private static Log _log = LogFactoryUtil.getLog(
2033 DLPortletDataHandlerImpl.class);
2034
2035 private static PortletDataHandlerBoolean _foldersAndDocuments =
2036 new PortletDataHandlerBoolean(
2037 _NAMESPACE, "folders-and-documents", true, true);
2038 private static PortletDataHandlerControl[] _metadataControls =
2039 new PortletDataHandlerControl[] {
2040 new PortletDataHandlerBoolean(_NAMESPACE, "categories"),
2041 new PortletDataHandlerBoolean(_NAMESPACE, "comments"),
2042 new PortletDataHandlerBoolean(_NAMESPACE, "ratings"),
2043 new PortletDataHandlerBoolean(_NAMESPACE, "tags")
2044 };
2045 private static PortletDataHandlerBoolean _previewsAndThumbnails =
2046 new PortletDataHandlerBoolean(_NAMESPACE, "previews-and-thumbnails");
2047 private static PortletDataHandlerBoolean _ranks =
2048 new PortletDataHandlerBoolean(_NAMESPACE, "ranks");
2049 private static PortletDataHandlerBoolean _shortcuts=
2050 new PortletDataHandlerBoolean(_NAMESPACE, "shortcuts");
2051
2052 }