001    /**
002     * Copyright (c) 2000-present 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.exportimport.lar;
016    
017    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.EVENT_STAGED_MODEL_EXPORT_FAILED;
018    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.EVENT_STAGED_MODEL_EXPORT_STARTED;
019    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.EVENT_STAGED_MODEL_EXPORT_SUCCEEDED;
020    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.EVENT_STAGED_MODEL_IMPORT_FAILED;
021    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.EVENT_STAGED_MODEL_IMPORT_STARTED;
022    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.EVENT_STAGED_MODEL_IMPORT_SUCCEEDED;
023    
024    import com.liferay.portal.exception.NoSuchModelException;
025    import com.liferay.portal.kernel.comment.CommentManagerUtil;
026    import com.liferay.portal.kernel.comment.DiscussionStagingHandler;
027    import com.liferay.portal.kernel.exception.PortalException;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.ArrayUtil;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.MapUtil;
033    import com.liferay.portal.kernel.util.StringUtil;
034    import com.liferay.portal.kernel.util.TransientValue;
035    import com.liferay.portal.kernel.workflow.WorkflowConstants;
036    import com.liferay.portal.kernel.xml.Element;
037    import com.liferay.portal.model.Group;
038    import com.liferay.portal.model.LocalizedModel;
039    import com.liferay.portal.model.StagedGroupedModel;
040    import com.liferay.portal.model.StagedModel;
041    import com.liferay.portal.model.TrashedModel;
042    import com.liferay.portal.model.WorkflowedModel;
043    import com.liferay.portal.model.adapter.StagedGroup;
044    import com.liferay.portal.service.GroupLocalServiceUtil;
045    import com.liferay.portlet.asset.model.AssetCategory;
046    import com.liferay.portlet.asset.model.AssetTag;
047    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
048    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
049    import com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants;
050    import com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleManagerUtil;
051    import com.liferay.portlet.ratings.model.RatingsEntry;
052    import com.liferay.portlet.ratings.service.RatingsEntryLocalServiceUtil;
053    import com.liferay.portlet.trash.util.TrashUtil;
054    
055    import java.util.ArrayList;
056    import java.util.HashMap;
057    import java.util.HashSet;
058    import java.util.List;
059    import java.util.Map;
060    import java.util.Set;
061    
062    /**
063     * @author Mate Thurzo
064     * @author Daniel Kocsis
065     * @author Zsolt Berentey
066     */
067    public abstract class BaseStagedModelDataHandler<T extends StagedModel>
068            implements StagedModelDataHandler<T> {
069    
070            @Override
071            public abstract void deleteStagedModel(
072                            String uuid, long groupId, String className, String extraData)
073                    throws PortalException;
074    
075            @Override
076            public abstract void deleteStagedModel(T stagedModel)
077                    throws PortalException;
078    
079            @Override
080            public void exportStagedModel(
081                            PortletDataContext portletDataContext, T stagedModel)
082                    throws PortletDataException {
083    
084                    validateExport(portletDataContext, stagedModel);
085    
086                    String path = ExportImportPathUtil.getModelPath(stagedModel);
087    
088                    if (portletDataContext.isPathExportedInScope(path)) {
089                            return;
090                    }
091    
092                    try {
093                            ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
094                                    EVENT_STAGED_MODEL_EXPORT_STARTED, getProcessFlag(),
095                                    PortletDataContextFactoryUtil.clonePortletDataContext(
096                                            portletDataContext),
097                                    new TransientValue<T>(stagedModel));
098    
099                            ManifestSummary manifestSummary =
100                                    portletDataContext.getManifestSummary();
101    
102                            PortletDataHandlerStatusMessageSenderUtil.sendStatusMessage(
103                                    "stagedModel", stagedModel, manifestSummary);
104    
105                            doExportStagedModel(portletDataContext, (T)stagedModel.clone());
106    
107                            exportAssetCategories(portletDataContext, stagedModel);
108                            exportAssetTags(portletDataContext, stagedModel);
109                            exportComments(portletDataContext, stagedModel);
110                            exportRatings(portletDataContext, stagedModel);
111    
112                            if (countStagedModel(portletDataContext, stagedModel)) {
113                                    manifestSummary.incrementModelAdditionCount(
114                                            stagedModel.getStagedModelType());
115                            }
116    
117                            portletDataContext.cleanUpMissingReferences(stagedModel);
118    
119                            ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
120                                    EVENT_STAGED_MODEL_EXPORT_SUCCEEDED, getProcessFlag(),
121                                    PortletDataContextFactoryUtil.clonePortletDataContext(
122                                            portletDataContext),
123                                    new TransientValue<T>(stagedModel));
124                    }
125                    catch (PortletDataException pde) {
126                            ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
127                                    EVENT_STAGED_MODEL_EXPORT_FAILED, getProcessFlag(),
128                                    PortletDataContextFactoryUtil.clonePortletDataContext(
129                                            portletDataContext),
130                                    new TransientValue<T>(stagedModel), pde);
131    
132                            throw pde;
133                    }
134                    catch (Throwable t) {
135                            ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
136                                    EVENT_STAGED_MODEL_EXPORT_FAILED, getProcessFlag(),
137                                    PortletDataContextFactoryUtil.clonePortletDataContext(
138                                            portletDataContext),
139                                    new TransientValue<T>(stagedModel), t);
140    
141                            PortletDataException pde = new PortletDataException(t);
142    
143                            if (t instanceof NoSuchModelException) {
144                                    pde.setStagedModel(stagedModel);
145                                    pde.setType(PortletDataException.MISSING_DEPENDENCY);
146                            }
147    
148                            throw pde;
149                    }
150            }
151    
152            @Override
153            public T fetchMissingReference(String uuid, long groupId) {
154    
155                    // Try to fetch the existing staged model from the importing group
156    
157                    T existingStagedModel = fetchStagedModelByUuidAndGroupId(uuid, groupId);
158    
159                    if ((existingStagedModel != null) &&
160                            !isStagedModelInTrash(existingStagedModel)) {
161    
162                            return existingStagedModel;
163                    }
164    
165                    try {
166    
167                            // Try to fetch the existing staged model from the parent sites
168    
169                            Group originalGroup = GroupLocalServiceUtil.getGroup(groupId);
170    
171                            Group group = originalGroup.getParentGroup();
172    
173                            while (group != null) {
174                                    existingStagedModel = fetchStagedModelByUuidAndGroupId(
175                                            uuid, group.getGroupId());
176    
177                                    if (existingStagedModel != null) {
178                                            break;
179                                    }
180    
181                                    group = group.getParentGroup();
182                            }
183    
184                            if ((existingStagedModel != null) &&
185                                    !isStagedModelInTrash(existingStagedModel)) {
186    
187                                    return existingStagedModel;
188                            }
189    
190                            List<T> existingStagedModels = fetchStagedModelsByUuidAndCompanyId(
191                                    uuid, originalGroup.getCompanyId());
192    
193                            for (T stagedModel : existingStagedModels) {
194                                    try {
195                                            if (stagedModel instanceof StagedGroupedModel) {
196                                                    StagedGroupedModel stagedGroupedModel =
197                                                            (StagedGroupedModel)stagedModel;
198    
199                                                    group = GroupLocalServiceUtil.getGroup(
200                                                            stagedGroupedModel.getGroupId());
201    
202                                                    if (!group.isStagingGroup() &&
203                                                            !isStagedModelInTrash(stagedModel)) {
204    
205                                                            return stagedModel;
206                                                    }
207                                            }
208                                            else if (!isStagedModelInTrash(stagedModel)) {
209                                                    return stagedModel;
210                                            }
211                                    }
212                                    catch (PortalException pe) {
213                                            if (_log.isDebugEnabled()) {
214                                                    _log.debug(pe, pe);
215                                            }
216                                    }
217                            }
218                    }
219                    catch (Exception e) {
220                            if (_log.isDebugEnabled()) {
221                                    _log.debug(e, e);
222                            }
223                            else if (_log.isWarnEnabled()) {
224                                    _log.warn(
225                                            "Unable to fetch missing reference staged model from " +
226                                                    "group " + groupId);
227                            }
228                    }
229    
230                    return null;
231            }
232    
233            @Override
234            public T fetchStagedModelByUuidAndGroupId(String uuid, long groupId) {
235                    return null;
236            }
237    
238            @Override
239            public abstract List<T> fetchStagedModelsByUuidAndCompanyId(
240                    String uuid, long companyId);
241    
242            @Override
243            public abstract String[] getClassNames();
244    
245            @Override
246            public String getDisplayName(T stagedModel) {
247                    return stagedModel.getUuid();
248            }
249    
250            @Override
251            public int[] getExportableStatuses() {
252                    return new int[] {WorkflowConstants.STATUS_APPROVED};
253            }
254    
255            @Override
256            public Map<String, String> getReferenceAttributes(
257                    PortletDataContext portletDataContext, T stagedModel) {
258    
259                    return new HashMap<>();
260            }
261    
262            /**
263             * @deprecated As of 7.0.0, replaced by {@link
264             *             #importMissingReference(PortletDataContext, Element)}
265             */
266            @Deprecated
267            @Override
268            public void importCompanyStagedModel(
269                            PortletDataContext portletDataContext, Element referenceElement)
270                    throws PortletDataException {
271    
272                    importMissingReference(portletDataContext, referenceElement);
273            }
274    
275            /**
276             * @deprecated As of 7.0.0, replaced by {@link
277             *             #importMissingReference(PortletDataContext, String, long,
278             *             long)}
279             */
280            @Deprecated
281            @Override
282            public void importCompanyStagedModel(
283                            PortletDataContext portletDataContext, String uuid, long classPK)
284                    throws PortletDataException {
285    
286                    importMissingReference(
287                            portletDataContext, uuid, portletDataContext.getCompanyGroupId(),
288                            classPK);
289            }
290    
291            @Override
292            public void importMissingReference(
293                            PortletDataContext portletDataContext, Element referenceElement)
294                    throws PortletDataException {
295    
296                    try {
297                            doImportMissingReference(portletDataContext, referenceElement);
298                    }
299                    catch (Exception e) {
300                            if (!StringUtil.equalsIgnoreCase(
301                                            referenceElement.attributeValue("type"),
302                                            PortletDataContext.REFERENCE_TYPE_DEPENDENCY_DISPOSABLE)) {
303    
304                                    throw e;
305                            }
306                    }
307            }
308    
309            @Override
310            public void importMissingReference(
311                            PortletDataContext portletDataContext, String uuid, long groupId,
312                            long classPK)
313                    throws PortletDataException {
314    
315                    try {
316                            doImportMissingReference(
317                                    portletDataContext, uuid, groupId, classPK);
318                    }
319                    catch (PortletDataException pde) {
320                            throw pde;
321                    }
322                    catch (Exception e) {
323                            throw new PortletDataException(e);
324                    }
325            }
326    
327            @Override
328            public void importStagedModel(
329                            PortletDataContext portletDataContext, T stagedModel)
330                    throws PortletDataException {
331    
332                    String path = ExportImportPathUtil.getModelPath(stagedModel);
333    
334                    if (portletDataContext.isPathProcessed(path)) {
335                            return;
336                    }
337    
338                    try {
339                            ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
340                                    EVENT_STAGED_MODEL_IMPORT_STARTED, getProcessFlag(),
341                                    PortletDataContextFactoryUtil.clonePortletDataContext(
342                                            portletDataContext),
343                                    new TransientValue<T>(stagedModel));
344    
345                            ManifestSummary manifestSummary =
346                                    portletDataContext.getManifestSummary();
347    
348                            PortletDataHandlerStatusMessageSenderUtil.sendStatusMessage(
349                                    "stagedModel", stagedModel, manifestSummary);
350    
351                            if (stagedModel instanceof LocalizedModel) {
352                                    LocalizedModel localizedModel = (LocalizedModel)stagedModel;
353    
354                                    localizedModel.prepareLocalizedFieldsForImport();
355                            }
356    
357                            restoreStagedModel(portletDataContext, stagedModel);
358    
359                            importAssetCategories(portletDataContext, stagedModel);
360                            importAssetTags(portletDataContext, stagedModel);
361    
362                            importReferenceStagedModels(portletDataContext, stagedModel);
363    
364                            doImportStagedModel(portletDataContext, stagedModel);
365    
366                            importComments(portletDataContext, stagedModel);
367                            importRatings(portletDataContext, stagedModel);
368    
369                            manifestSummary.incrementModelAdditionCount(
370                                    stagedModel.getStagedModelType());
371    
372                            ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
373                                    EVENT_STAGED_MODEL_IMPORT_SUCCEEDED, getProcessFlag(),
374                                    PortletDataContextFactoryUtil.clonePortletDataContext(
375                                            portletDataContext),
376                                    new TransientValue<T>(stagedModel));
377                    }
378                    catch (PortletDataException pde) {
379                            ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
380                                    EVENT_STAGED_MODEL_IMPORT_FAILED, getProcessFlag(),
381                                    PortletDataContextFactoryUtil.clonePortletDataContext(
382                                            portletDataContext),
383                                    new TransientValue<T>(stagedModel), pde);
384    
385                            throw pde;
386                    }
387                    catch (Throwable t) {
388                            ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
389                                    EVENT_STAGED_MODEL_IMPORT_FAILED, getProcessFlag(),
390                                    PortletDataContextFactoryUtil.clonePortletDataContext(
391                                            portletDataContext),
392                                    new TransientValue<T>(stagedModel), t);
393    
394                            throw new PortletDataException(t);
395                    }
396            }
397    
398            @Override
399            public void restoreStagedModel(
400                            PortletDataContext portletDataContext, T stagedModel)
401                    throws PortletDataException {
402    
403                    try {
404                            if (stagedModel instanceof TrashedModel) {
405                                    doRestoreStagedModel(portletDataContext, stagedModel);
406                            }
407                    }
408                    catch (PortletDataException pde) {
409                            throw pde;
410                    }
411                    catch (Exception e) {
412                            throw new PortletDataException(e);
413                    }
414            }
415    
416            @Override
417            public boolean validateReference(
418                    PortletDataContext portletDataContext, Element referenceElement) {
419    
420                    validateMissingGroupReference(portletDataContext, referenceElement);
421    
422                    String uuid = referenceElement.attributeValue("uuid");
423    
424                    Map<Long, Long> groupIds =
425                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
426                                    Group.class);
427    
428                    long groupId = GetterUtil.getLong(
429                            referenceElement.attributeValue("group-id"));
430    
431                    groupId = MapUtil.getLong(groupIds, groupId);
432    
433                    try {
434                            return validateMissingReference(uuid, groupId);
435                    }
436                    catch (Exception e) {
437                            return false;
438                    }
439            }
440    
441            protected boolean countStagedModel(
442                    PortletDataContext portletDataContext, T stagedModel) {
443    
444                    return !portletDataContext.isStagedModelCounted(stagedModel);
445            }
446    
447            protected abstract void doExportStagedModel(
448                            PortletDataContext portletDataContext, T stagedModel)
449                    throws Exception;
450    
451            protected void doImportMissingReference(
452                            PortletDataContext portletDataContext, Element referenceElement)
453                    throws PortletDataException {
454    
455                    importMissingGroupReference(portletDataContext, referenceElement);
456    
457                    String uuid = referenceElement.attributeValue("uuid");
458    
459                    Map<Long, Long> groupIds =
460                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
461                                    Group.class);
462    
463                    long groupId = GetterUtil.getLong(
464                            referenceElement.attributeValue("group-id"));
465    
466                    groupId = MapUtil.getLong(groupIds, groupId);
467    
468                    long classPK = GetterUtil.getLong(
469                            referenceElement.attributeValue("class-pk"));
470    
471                    importMissingReference(portletDataContext, uuid, groupId, classPK);
472            }
473    
474            protected void doImportMissingReference(
475                            PortletDataContext portletDataContext, String uuid, long groupId,
476                            long classPK)
477                    throws Exception {
478    
479                    throw new UnsupportedOperationException();
480            }
481    
482            protected abstract void doImportStagedModel(
483                            PortletDataContext portletDataContext, T stagedModel)
484                    throws Exception;
485    
486            protected void doRestoreStagedModel(
487                            PortletDataContext portletDataContext, T stagedModel)
488                    throws Exception {
489    
490                    throw new UnsupportedOperationException();
491            }
492    
493            protected void exportAssetCategories(
494                            PortletDataContext portletDataContext, T stagedModel)
495                    throws PortletDataException {
496    
497                    List<AssetCategory> assetCategories =
498                            AssetCategoryLocalServiceUtil.getCategories(
499                                    ExportImportClassedModelUtil.getClassName(stagedModel),
500                                    ExportImportClassedModelUtil.getClassPK(stagedModel));
501    
502                    for (AssetCategory assetCategory : assetCategories) {
503                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
504                                    portletDataContext, stagedModel, assetCategory,
505                                    PortletDataContext.REFERENCE_TYPE_WEAK);
506                    }
507            }
508    
509            protected void exportAssetTags(
510                            PortletDataContext portletDataContext, T stagedModel)
511                    throws PortletDataException {
512    
513                    List<AssetTag> assetTags = AssetTagLocalServiceUtil.getTags(
514                            ExportImportClassedModelUtil.getClassName(stagedModel),
515                            ExportImportClassedModelUtil.getClassPK(stagedModel));
516    
517                    for (AssetTag assetTag : assetTags) {
518                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
519                                    portletDataContext, stagedModel, assetTag,
520                                    PortletDataContext.REFERENCE_TYPE_WEAK);
521                    }
522            }
523    
524            protected void exportComments(
525                            PortletDataContext portletDataContext, T stagedModel)
526                    throws PortletDataException {
527    
528                    if (!MapUtil.getBoolean(
529                                    portletDataContext.getParameterMap(),
530                                    PortletDataHandlerKeys.PORTLET_DATA_ALL) &&
531                            !MapUtil.getBoolean(
532                                    portletDataContext.getParameterMap(),
533                                    PortletDataHandlerKeys.COMMENTS)) {
534    
535                            return;
536                    }
537    
538                    DiscussionStagingHandler discussionStagingHandler =
539                            CommentManagerUtil.getDiscussionStagingHandler();
540    
541                    if (discussionStagingHandler != null) {
542                            discussionStagingHandler.exportReferenceDiscussions(
543                                    portletDataContext, stagedModel);
544                    }
545            }
546    
547            protected void exportRatings(
548                            PortletDataContext portletDataContext, T stagedModel)
549                    throws PortletDataException {
550    
551                    if (!MapUtil.getBoolean(
552                                    portletDataContext.getParameterMap(),
553                                    PortletDataHandlerKeys.PORTLET_DATA_ALL) &&
554                            !MapUtil.getBoolean(
555                                    portletDataContext.getParameterMap(),
556                                    PortletDataHandlerKeys.RATINGS)) {
557    
558                            return;
559                    }
560    
561                    List<RatingsEntry> ratingsEntries =
562                            RatingsEntryLocalServiceUtil.getEntries(
563                                    ExportImportClassedModelUtil.getClassName(stagedModel),
564                                    ExportImportClassedModelUtil.getClassPK(stagedModel));
565    
566                    if (ratingsEntries.isEmpty()) {
567                            return;
568                    }
569    
570                    for (RatingsEntry ratingsEntry : ratingsEntries) {
571                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
572                                    portletDataContext, stagedModel, ratingsEntry,
573                                    PortletDataContext.REFERENCE_TYPE_WEAK);
574                    }
575            }
576    
577            protected int getProcessFlag() {
578    
579                    // Ordered by precedence
580    
581                    if (ExportImportThreadLocal.isLayoutStagingInProcess()) {
582                            return ExportImportLifecycleConstants.
583                                    PROCESS_FLAG_LAYOUT_STAGING_IN_PROCESS;
584                    }
585                    else if (ExportImportThreadLocal.isPortletStagingInProcess()) {
586                            return ExportImportLifecycleConstants.
587                                    PROCESS_FLAG_PORTLET_STAGING_IN_PROCESS;
588                    }
589                    else if (ExportImportThreadLocal.isLayoutExportInProcess()) {
590                            return ExportImportLifecycleConstants.
591                                    PROCESS_FLAG_LAYOUT_EXPORT_IN_PROCESS;
592                    }
593                    else if (ExportImportThreadLocal.isLayoutImportInProcess()) {
594                            return ExportImportLifecycleConstants.
595                                    PROCESS_FLAG_LAYOUT_IMPORT_IN_PROCESS;
596                    }
597                    else if (ExportImportThreadLocal.isPortletExportInProcess()) {
598                            return ExportImportLifecycleConstants.
599                                    PROCESS_FLAG_PORTLET_EXPORT_IN_PROCESS;
600                    }
601                    else if (ExportImportThreadLocal.isPortletImportInProcess()) {
602                            return ExportImportLifecycleConstants.
603                                    PROCESS_FLAG_PORTLET_IMPORT_IN_PROCESS;
604                    }
605    
606                    return 0;
607            }
608    
609            protected void importAssetCategories(
610                            PortletDataContext portletDataContext, T stagedModel)
611                    throws PortletDataException {
612    
613                    List<Element> referenceElements =
614                            portletDataContext.getReferenceElements(
615                                    stagedModel, AssetCategory.class);
616    
617                    List<Long> assetCategoryIds = new ArrayList<>(referenceElements.size());
618    
619                    for (Element referenceElement : referenceElements) {
620                            long classPK = GetterUtil.getLong(
621                                    referenceElement.attributeValue("class-pk"));
622    
623                            StagedModelDataHandlerUtil.importReferenceStagedModel(
624                                    portletDataContext, stagedModel, AssetCategory.class, classPK);
625    
626                            assetCategoryIds.add(classPK);
627                    }
628    
629                    Map<Long, Long> assetCategoryIdsMap =
630                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
631                                    AssetCategory.class);
632    
633                    long[] importedAssetCategoryIds = new long[assetCategoryIds.size()];
634    
635                    for (int i = 0; i < assetCategoryIds.size(); i++) {
636                            long categoryId = assetCategoryIds.get(i);
637    
638                            importedAssetCategoryIds[i] = MapUtil.getLong(
639                                    assetCategoryIdsMap, categoryId, categoryId);
640                    }
641    
642                    portletDataContext.addAssetCategories(
643                            ExportImportClassedModelUtil.getClassName(stagedModel),
644                            ExportImportClassedModelUtil.getClassPK(stagedModel),
645                            importedAssetCategoryIds);
646            }
647    
648            protected void importAssetTags(
649                            PortletDataContext portletDataContext, T stagedModel)
650                    throws PortletDataException {
651    
652                    List<Element> referenceElements =
653                            portletDataContext.getReferenceElements(
654                                    stagedModel, AssetTag.class);
655    
656                    List<Long> assetTagIds = new ArrayList<>(referenceElements.size());
657    
658                    for (Element referenceElement : referenceElements) {
659                            long classPK = GetterUtil.getLong(
660                                    referenceElement.attributeValue("class-pk"));
661    
662                            StagedModelDataHandlerUtil.importReferenceStagedModel(
663                                    portletDataContext, stagedModel, AssetTag.class, classPK);
664    
665                            assetTagIds.add(classPK);
666                    }
667    
668                    Map<Long, Long> assetTagIdsMap =
669                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
670                                    AssetTag.class);
671    
672                    Set<String> assetTagNames = new HashSet<>();
673    
674                    for (long assetTagId : assetTagIds) {
675                            long importedStagedAssetTagId = MapUtil.getLong(
676                                    assetTagIdsMap, assetTagId, assetTagId);
677    
678                            AssetTag assetTag = AssetTagLocalServiceUtil.fetchAssetTag(
679                                    importedStagedAssetTagId);
680    
681                            if (assetTag != null) {
682                                    assetTagNames.add(assetTag.getName());
683                            }
684                    }
685    
686                    portletDataContext.addAssetTags(
687                            ExportImportClassedModelUtil.getClassName(stagedModel),
688                            ExportImportClassedModelUtil.getClassPK(stagedModel),
689                            assetTagNames.toArray(new String[assetTagNames.size()]));
690            }
691    
692            protected void importComments(
693                            PortletDataContext portletDataContext, T stagedModel)
694                    throws PortalException {
695    
696                    if (!MapUtil.getBoolean(
697                                    portletDataContext.getParameterMap(),
698                                    PortletDataHandlerKeys.PORTLET_DATA_ALL) &&
699                            !MapUtil.getBoolean(
700                                    portletDataContext.getParameterMap(),
701                                    PortletDataHandlerKeys.COMMENTS)) {
702    
703                            return;
704                    }
705    
706                    DiscussionStagingHandler discussionStagingDataHandler =
707                            CommentManagerUtil.getDiscussionStagingHandler();
708    
709                    if (discussionStagingDataHandler != null) {
710                            discussionStagingDataHandler.importReferenceDiscussions(
711                                    portletDataContext, stagedModel);
712                    }
713            }
714    
715            protected void importMissingGroupReference(
716                            PortletDataContext portletDataContext, Element referenceElement)
717                    throws PortletDataException {
718    
719                    StagedModelDataHandler<StagedGroup> stagedModelDataHandler =
720                            (StagedModelDataHandler<StagedGroup>)
721                                    StagedModelDataHandlerRegistryUtil.getStagedModelDataHandler(
722                                            StagedGroup.class.getName());
723    
724                    stagedModelDataHandler.importMissingReference(
725                            portletDataContext, referenceElement);
726            }
727    
728            protected void importRatings(
729                            PortletDataContext portletDataContext, T stagedModel)
730                    throws PortalException {
731    
732                    if (!MapUtil.getBoolean(
733                                    portletDataContext.getParameterMap(),
734                                    PortletDataHandlerKeys.PORTLET_DATA_ALL) &&
735                            !MapUtil.getBoolean(
736                                    portletDataContext.getParameterMap(),
737                                    PortletDataHandlerKeys.RATINGS)) {
738    
739                            return;
740                    }
741    
742                    StagedModelDataHandlerUtil.importReferenceStagedModels(
743                            portletDataContext, stagedModel, RatingsEntry.class);
744            }
745    
746            protected void importReferenceStagedModels(
747                            PortletDataContext portletDataContext, T stagedModel)
748                    throws PortletDataException {
749    
750                    Element stagedModelElement =
751                            portletDataContext.getImportDataStagedModelElement(stagedModel);
752    
753                    Element referencesElement = stagedModelElement.element("references");
754    
755                    if (referencesElement == null) {
756                            return;
757                    }
758    
759                    DiscussionStagingHandler discussionStagingHandler =
760                            CommentManagerUtil.getDiscussionStagingHandler();
761    
762                    String stagedModelClassName = null;
763    
764                    if (discussionStagingHandler != null) {
765                            stagedModelClassName = discussionStagingHandler.getClassName();
766                    }
767    
768                    List<Element> referenceElements = referencesElement.elements();
769    
770                    for (Element referenceElement : referenceElements) {
771                            String className = referenceElement.attributeValue("class-name");
772    
773                            if (className.equals(AssetCategory.class.getName()) ||
774                                    className.equals(RatingsEntry.class.getName()) ||
775                                    className.equals(stagedModelClassName)) {
776    
777                                    continue;
778                            }
779    
780                            long classPK = GetterUtil.getLong(
781                                    referenceElement.attributeValue("class-pk"));
782    
783                            StagedModelDataHandlerUtil.importReferenceStagedModel(
784                                    portletDataContext, stagedModel, className, classPK);
785                    }
786            }
787    
788            protected boolean isStagedModelInTrash(T stagedModel) {
789                    String className = ExportImportClassedModelUtil.getClassName(
790                            stagedModel);
791                    long classPK = ExportImportClassedModelUtil.getClassPK(stagedModel);
792    
793                    try {
794                            return TrashUtil.isInTrash(className, classPK);
795                    }
796                    catch (PortalException pe) {
797                            if (_log.isDebugEnabled()) {
798                                    _log.debug(pe, pe);
799                            }
800                    }
801    
802                    return false;
803            }
804    
805            protected void validateExport(
806                            PortletDataContext portletDataContext, T stagedModel)
807                    throws PortletDataException {
808    
809                    if (!portletDataContext.isInitialPublication() &&
810                            (stagedModel instanceof WorkflowedModel)) {
811    
812                            WorkflowedModel workflowedModel = (WorkflowedModel)stagedModel;
813    
814                            if (!ArrayUtil.contains(
815                                            getExportableStatuses(), workflowedModel.getStatus())) {
816    
817                                    PortletDataException pde = new PortletDataException(
818                                            PortletDataException.STATUS_UNAVAILABLE);
819    
820                                    pde.setStagedModel(stagedModel);
821    
822                                    throw pde;
823                            }
824                    }
825    
826                    if (stagedModel instanceof TrashedModel) {
827                            TrashedModel trashedModel = (TrashedModel)stagedModel;
828    
829                            if (trashedModel.isInTrash()) {
830                                    PortletDataException pde = new PortletDataException(
831                                            PortletDataException.STATUS_IN_TRASH);
832    
833                                    pde.setStagedModel(stagedModel);
834    
835                                    throw pde;
836                            }
837                    }
838            }
839    
840            protected boolean validateMissingGroupReference(
841                    PortletDataContext portletDataContext, Element referenceElement) {
842    
843                    StagedModelDataHandler<StagedGroup> stagedModelDataHandler =
844                            (StagedModelDataHandler<StagedGroup>)
845                                    StagedModelDataHandlerRegistryUtil.getStagedModelDataHandler(
846                                            StagedGroup.class.getName());
847    
848                    return stagedModelDataHandler.validateReference(
849                            portletDataContext, referenceElement);
850            }
851    
852            protected boolean validateMissingReference(String uuid, long groupId) {
853                    T existingStagedModel = fetchMissingReference(uuid, groupId);
854    
855                    if (existingStagedModel == null) {
856                            return false;
857                    }
858    
859                    return true;
860            }
861    
862            private static final Log _log = LogFactoryUtil.getLog(
863                    BaseStagedModelDataHandler.class);
864    
865    }