001    /**
002     * Copyright (c) 2000-2013 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.portal.lar;
016    
017    import com.liferay.portal.NoSuchRoleException;
018    import com.liferay.portal.NoSuchTeamException;
019    import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
020    import com.liferay.portal.kernel.dao.orm.Conjunction;
021    import com.liferay.portal.kernel.dao.orm.Criterion;
022    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
023    import com.liferay.portal.kernel.dao.orm.Property;
024    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
025    import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
026    import com.liferay.portal.kernel.exception.PortalException;
027    import com.liferay.portal.kernel.exception.SystemException;
028    import com.liferay.portal.kernel.lar.ExportImportClassedModelUtil;
029    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
030    import com.liferay.portal.kernel.lar.ExportImportThreadLocal;
031    import com.liferay.portal.kernel.lar.ManifestSummary;
032    import com.liferay.portal.kernel.lar.PortletDataContext;
033    import com.liferay.portal.kernel.lar.PortletDataContextListener;
034    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
035    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
036    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
037    import com.liferay.portal.kernel.lar.StagedModelType;
038    import com.liferay.portal.kernel.lar.UserIdStrategy;
039    import com.liferay.portal.kernel.log.Log;
040    import com.liferay.portal.kernel.log.LogFactoryUtil;
041    import com.liferay.portal.kernel.util.ArrayUtil;
042    import com.liferay.portal.kernel.util.DateRange;
043    import com.liferay.portal.kernel.util.GetterUtil;
044    import com.liferay.portal.kernel.util.HtmlUtil;
045    import com.liferay.portal.kernel.util.KeyValuePair;
046    import com.liferay.portal.kernel.util.ListUtil;
047    import com.liferay.portal.kernel.util.MapUtil;
048    import com.liferay.portal.kernel.util.PrimitiveLongList;
049    import com.liferay.portal.kernel.util.StringBundler;
050    import com.liferay.portal.kernel.util.StringPool;
051    import com.liferay.portal.kernel.util.StringUtil;
052    import com.liferay.portal.kernel.util.Validator;
053    import com.liferay.portal.kernel.workflow.WorkflowConstants;
054    import com.liferay.portal.kernel.xml.Attribute;
055    import com.liferay.portal.kernel.xml.Element;
056    import com.liferay.portal.kernel.xml.Node;
057    import com.liferay.portal.kernel.xml.SAXReaderUtil;
058    import com.liferay.portal.kernel.xml.XPath;
059    import com.liferay.portal.kernel.zip.ZipReader;
060    import com.liferay.portal.kernel.zip.ZipWriter;
061    import com.liferay.portal.model.AttachedModel;
062    import com.liferay.portal.model.AuditedModel;
063    import com.liferay.portal.model.ClassedModel;
064    import com.liferay.portal.model.Group;
065    import com.liferay.portal.model.Layout;
066    import com.liferay.portal.model.Lock;
067    import com.liferay.portal.model.OrgLabor;
068    import com.liferay.portal.model.Portlet;
069    import com.liferay.portal.model.PortletModel;
070    import com.liferay.portal.model.ResourceConstants;
071    import com.liferay.portal.model.ResourcedModel;
072    import com.liferay.portal.model.Role;
073    import com.liferay.portal.model.RoleConstants;
074    import com.liferay.portal.model.StagedGroupedModel;
075    import com.liferay.portal.model.StagedModel;
076    import com.liferay.portal.model.Team;
077    import com.liferay.portal.model.impl.LockImpl;
078    import com.liferay.portal.security.permission.ResourceActionsUtil;
079    import com.liferay.portal.service.GroupLocalServiceUtil;
080    import com.liferay.portal.service.LockLocalServiceUtil;
081    import com.liferay.portal.service.ResourceBlockLocalServiceUtil;
082    import com.liferay.portal.service.ResourceBlockPermissionLocalServiceUtil;
083    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
084    import com.liferay.portal.service.RoleLocalServiceUtil;
085    import com.liferay.portal.service.ServiceContext;
086    import com.liferay.portal.service.TeamLocalServiceUtil;
087    import com.liferay.portal.util.PortalUtil;
088    import com.liferay.portal.util.PortletKeys;
089    import com.liferay.portal.util.PropsValues;
090    import com.liferay.portlet.asset.model.AssetCategory;
091    import com.liferay.portlet.asset.model.AssetEntry;
092    import com.liferay.portlet.asset.model.AssetLink;
093    import com.liferay.portlet.asset.model.AssetTag;
094    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
095    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
096    import com.liferay.portlet.asset.service.AssetLinkLocalServiceUtil;
097    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
098    import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
099    import com.liferay.portlet.bookmarks.model.impl.BookmarksEntryImpl;
100    import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
101    import com.liferay.portlet.documentlibrary.lar.xstream.FileEntryConverter;
102    import com.liferay.portlet.documentlibrary.lar.xstream.FileVersionConverter;
103    import com.liferay.portlet.documentlibrary.lar.xstream.FolderConverter;
104    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
105    import com.liferay.portlet.documentlibrary.model.impl.DLFileShortcutImpl;
106    import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
107    import com.liferay.portlet.dynamicdatamapping.storage.Field;
108    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
109    import com.liferay.portlet.expando.model.ExpandoBridge;
110    import com.liferay.portlet.expando.model.ExpandoColumn;
111    import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
112    import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
113    import com.liferay.portlet.journal.model.impl.JournalFeedImpl;
114    import com.liferay.portlet.messageboards.model.MBDiscussion;
115    import com.liferay.portlet.messageboards.model.MBMessage;
116    import com.liferay.portlet.messageboards.model.MBThread;
117    import com.liferay.portlet.messageboards.model.impl.MBBanImpl;
118    import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
119    import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
120    import com.liferay.portlet.messageboards.model.impl.MBThreadFlagImpl;
121    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
122    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
123    import com.liferay.portlet.messageboards.service.persistence.MBDiscussionUtil;
124    import com.liferay.portlet.messageboards.service.persistence.MBMessageUtil;
125    import com.liferay.portlet.polls.model.impl.PollsChoiceImpl;
126    import com.liferay.portlet.polls.model.impl.PollsQuestionImpl;
127    import com.liferay.portlet.polls.model.impl.PollsVoteImpl;
128    import com.liferay.portlet.ratings.model.RatingsEntry;
129    import com.liferay.portlet.ratings.model.impl.RatingsEntryImpl;
130    import com.liferay.portlet.ratings.service.RatingsEntryLocalServiceUtil;
131    import com.liferay.portlet.wiki.model.impl.WikiNodeImpl;
132    import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
133    
134    import com.thoughtworks.xstream.XStream;
135    import com.thoughtworks.xstream.security.NoTypePermission;
136    import com.thoughtworks.xstream.security.PrimitiveTypePermission;
137    
138    import java.io.IOException;
139    import java.io.InputStream;
140    import java.io.Serializable;
141    
142    import java.sql.Time;
143    import java.sql.Timestamp;
144    
145    import java.util.ArrayList;
146    import java.util.Collections;
147    import java.util.Date;
148    import java.util.HashMap;
149    import java.util.HashSet;
150    import java.util.List;
151    import java.util.Locale;
152    import java.util.Map;
153    import java.util.Set;
154    
155    import jodd.bean.BeanUtil;
156    
157    /**
158     * <p>
159     * Holds context information that is used during exporting and importing portlet
160     * data.
161     * </p>
162     *
163     * @author Brian Wing Shun Chan
164     * @author Raymond Aug??
165     * @author Bruno Farache
166     * @author Alexander Chow
167     * @author Mate Thurzo
168     */
169    public class PortletDataContextImpl implements PortletDataContext {
170    
171            public PortletDataContextImpl() {
172                    initXStream();
173            }
174    
175            @Override
176            public void addAssetCategories(Class<?> clazz, long classPK)
177                    throws SystemException {
178    
179                    List<AssetCategory> assetCategories =
180                            AssetCategoryLocalServiceUtil.getCategories(
181                                    clazz.getName(), classPK);
182    
183                    _assetCategoryUuidsMap.put(
184                            getPrimaryKeyString(clazz, classPK),
185                            StringUtil.split(
186                                    ListUtil.toString(
187                                            assetCategories, AssetCategory.UUID_ACCESSOR)));
188                    _assetCategoryIdsMap.put(
189                            getPrimaryKeyString(clazz, classPK),
190                            StringUtil.split(
191                                    ListUtil.toString(
192                                            assetCategories, AssetCategory.CATEGORY_ID_ACCESSOR), 0L));
193            }
194    
195            @Override
196            public void addAssetCategories(
197                    String className, long classPK, long[] assetCategoryIds) {
198    
199                    _assetCategoryIdsMap.put(
200                            getPrimaryKeyString(className, classPK), assetCategoryIds);
201            }
202    
203            public void addAssetLinks(Class<?> clazz, long classPK)
204                    throws SystemException {
205    
206                    AssetEntry assetEntry = AssetEntryLocalServiceUtil.fetchEntry(
207                            clazz.getName(), classPK);
208    
209                    if (assetEntry == null) {
210                            return;
211                    }
212    
213                    List<AssetLink> directAssetLinks =
214                            AssetLinkLocalServiceUtil.getDirectLinks(assetEntry.getEntryId());
215    
216                    if (directAssetLinks.isEmpty()) {
217                            return;
218                    }
219    
220                    Map<Integer, List<AssetLink>> assetLinksMap =
221                            new HashMap<Integer, List<AssetLink>>();
222    
223                    for (AssetLink assetLink : directAssetLinks) {
224                            List<AssetLink> assetLinks = assetLinksMap.get(assetLink.getType());
225    
226                            if (assetLinks == null) {
227                                    assetLinks = new ArrayList<AssetLink>();
228    
229                                    assetLinksMap.put(assetLink.getType(), assetLinks);
230                            }
231    
232                            assetLinks.add(assetLink);
233                    }
234    
235                    for (Map.Entry<Integer, List<AssetLink>> entry :
236                                    assetLinksMap.entrySet()) {
237    
238                            _assetLinksMap.put(
239                                    getPrimaryKeyString(assetEntry.getClassUuid(), entry.getKey()),
240                                    entry.getValue());
241                    }
242            }
243    
244            @Override
245            public void addAssetTags(Class<?> clazz, long classPK)
246                    throws SystemException {
247    
248                    String[] tagNames = AssetTagLocalServiceUtil.getTagNames(
249                            clazz.getName(), classPK);
250    
251                    _assetTagNamesMap.put(getPrimaryKeyString(clazz, classPK), tagNames);
252            }
253    
254            @Override
255            public void addAssetTags(
256                    String className, long classPK, String[] assetTagNames) {
257    
258                    _assetTagNamesMap.put(
259                            getPrimaryKeyString(className, classPK), assetTagNames);
260            }
261    
262            @Override
263            public void addClassedModel(
264                            Element element, String path, ClassedModel classedModel)
265                    throws PortalException, SystemException {
266    
267                    addClassedModel(
268                            element, path, classedModel, classedModel.getModelClass());
269            }
270    
271            @Override
272            public void addClassedModel(
273                            Element element, String path, ClassedModel classedModel,
274                            Class<?> clazz)
275                    throws PortalException, SystemException {
276    
277                    element.addAttribute("path", path);
278    
279                    if (classedModel instanceof AttachedModel) {
280                            AttachedModel attachedModel = (AttachedModel)classedModel;
281    
282                            element.addAttribute("class-name", attachedModel.getClassName());
283                    }
284                    else if (BeanUtil.hasProperty(classedModel, "className")) {
285                            String className = BeanPropertiesUtil.getStringSilent(
286                                    classedModel, "className");
287    
288                            if (className != null) {
289                                    element.addAttribute("class-name", className);
290                            }
291                    }
292    
293                    if (isPathProcessed(path)) {
294                            return;
295                    }
296    
297                    if (classedModel instanceof AuditedModel) {
298                            AuditedModel auditedModel = (AuditedModel)classedModel;
299    
300                            auditedModel.setUserUuid(auditedModel.getUserUuid());
301                    }
302    
303                    if (isResourceMain(classedModel)) {
304                            long classPK = getClassPK(classedModel);
305    
306                            addAssetCategories(clazz, classPK);
307                            addAssetLinks(clazz, classPK);
308                            addAssetTags(clazz, classPK);
309                            addExpando(element, path, classedModel, clazz);
310                            addLocks(clazz, String.valueOf(classPK));
311                            addPermissions(clazz, classPK);
312    
313                            boolean portletDataAll = MapUtil.getBoolean(
314                                    getParameterMap(), PortletDataHandlerKeys.PORTLET_DATA_ALL);
315    
316                            if (portletDataAll ||
317                                    MapUtil.getBoolean(
318                                            getParameterMap(), PortletDataHandlerKeys.COMMENTS)) {
319    
320                                    addComments(clazz, classPK);
321                            }
322    
323                            if (portletDataAll ||
324                                    MapUtil.getBoolean(
325                                            getParameterMap(), PortletDataHandlerKeys.RATINGS)) {
326    
327                                    addRatingsEntries(clazz, classPK);
328                            }
329                    }
330    
331                    _references.add(getReferenceKey(classedModel));
332    
333                    addZipEntry(path, classedModel);
334            }
335    
336            /**
337             * @deprecated As of 6.2.0, replaced by {@link #addClassedModel(Element,
338             *             String, ClassedModel, Class)}
339             */
340            @Override
341            public void addClassedModel(
342                            Element element, String path, ClassedModel classedModel,
343                            Class<?> clazz, String namespace)
344                    throws PortalException, SystemException {
345    
346                    addClassedModel(element, path, classedModel, clazz);
347            }
348    
349            /**
350             * @deprecated As of 6.2.0, replaced by {@link #addClassedModel(Element,
351             *             String, ClassedModel)}
352             */
353            @Override
354            public void addClassedModel(
355                            Element element, String path, ClassedModel classedModel,
356                            String namespace)
357                    throws PortalException, SystemException {
358    
359                    addClassedModel(element, path, classedModel);
360            }
361    
362            @Override
363            public void addComments(Class<?> clazz, long classPK)
364                    throws SystemException {
365    
366                    long classNameId = PortalUtil.getClassNameId(clazz);
367    
368                    MBDiscussion discussion = MBDiscussionUtil.fetchByC_C(
369                            classNameId, classPK);
370    
371                    if (discussion == null) {
372                            return;
373                    }
374    
375                    List<MBMessage> messages = MBMessageLocalServiceUtil.getThreadMessages(
376                            discussion.getThreadId(), WorkflowConstants.STATUS_APPROVED);
377    
378                    if (messages.isEmpty()) {
379                            return;
380                    }
381    
382                    MBMessage firstMessage = messages.get(0);
383    
384                    if ((messages.size() == 1) && firstMessage.isRoot()) {
385                            return;
386                    }
387    
388                    for (MBMessage message : messages) {
389                            addRatingsEntries(MBDiscussion.class, message.getPrimaryKey());
390                    }
391    
392                    _commentsMap.put(getPrimaryKeyString(clazz, classPK), messages);
393            }
394    
395            @Override
396            public void addComments(
397                    String className, long classPK, List<MBMessage> messages) {
398    
399                    _commentsMap.put(getPrimaryKeyString(className, classPK), messages);
400            }
401    
402            /**
403             * @see #isWithinDateRange(Date)
404             */
405            @Override
406            public void addDateRangeCriteria(
407                    DynamicQuery dynamicQuery, String propertyName) {
408    
409                    Criterion criterion = getDateRangeCriteria(propertyName);
410    
411                    if (criterion == null) {
412                            return;
413                    }
414    
415                    dynamicQuery.add(criterion);
416            }
417    
418            @Override
419            public void addDeletionSystemEventStagedModelTypes(
420                    StagedModelType... stagedModelTypes) {
421    
422                    for (StagedModelType stagedModelType : stagedModelTypes) {
423                            _deletionSystemEventModelTypes.add(stagedModelType);
424                    }
425            }
426    
427            @Override
428            public void addExpando(
429                            Element element, String path, ClassedModel classedModel)
430                    throws PortalException, SystemException {
431    
432                    addExpando(element, path, classedModel, classedModel.getModelClass());
433            }
434    
435            @Override
436            public void addLocks(Class<?> clazz, String key)
437                    throws PortalException, SystemException {
438    
439                    if (!_locksMap.containsKey(getPrimaryKeyString(clazz, key)) &&
440                            LockLocalServiceUtil.isLocked(clazz.getName(), key)) {
441    
442                            Lock lock = LockLocalServiceUtil.getLock(clazz.getName(), key);
443    
444                            addLocks(clazz.getName(), key, lock);
445                    }
446            }
447    
448            @Override
449            public void addLocks(String className, String key, Lock lock) {
450                    _locksMap.put(getPrimaryKeyString(className, key), lock);
451            }
452    
453            @Override
454            public void addPermissions(Class<?> clazz, long classPK)
455                    throws PortalException, SystemException {
456    
457                    addPermissions(clazz.getName(), classPK);
458            }
459    
460            @Override
461            public void addPermissions(String resourceName, long resourcePK)
462                    throws PortalException, SystemException {
463    
464                    if (!MapUtil.getBoolean(
465                                    _parameterMap, PortletDataHandlerKeys.PERMISSIONS)) {
466    
467                            return;
468                    }
469    
470                    List<KeyValuePair> permissions = new ArrayList<KeyValuePair>();
471    
472                    List<Role> roles = RoleLocalServiceUtil.getGroupRelatedRoles(_groupId);
473    
474                    PrimitiveLongList roleIds = new PrimitiveLongList(roles.size());
475                    Map<Long, String> roleIdsToNames = new HashMap<Long, String>();
476    
477                    for (Role role : roles) {
478                            String roleName = role.getName();
479    
480                            int roleType = role.getType();
481    
482                            if ((roleType == RoleConstants.TYPE_PROVIDER) && role.isTeam()) {
483                                    Team team = TeamLocalServiceUtil.getTeam(role.getClassPK());
484    
485                                    roleName = PermissionExporter.ROLE_TEAM_PREFIX + team.getName();
486                            }
487    
488                            roleIds.add(role.getRoleId());
489                            roleIdsToNames.put(role.getRoleId(), roleName);
490                    }
491    
492                    List<String> actionIds = ResourceActionsUtil.getModelResourceActions(
493                            resourceName);
494    
495                    Map<Long, Set<String>> roleIdsToActionIds = getActionIds(
496                            _companyId, roleIds.getArray(), resourceName, resourcePK,
497                            actionIds);
498    
499                    for (Map.Entry<Long, String> entry : roleIdsToNames.entrySet()) {
500                            long roleId = entry.getKey();
501                            String name = entry.getValue();
502    
503                            Set<String> availableActionIds = roleIdsToActionIds.get(roleId);
504    
505                            if (availableActionIds == null) {
506                                    availableActionIds = Collections.emptySet();
507                            }
508    
509                            KeyValuePair permission = new KeyValuePair(
510                                    name, StringUtil.merge(availableActionIds));
511    
512                            permissions.add(permission);
513                    }
514    
515                    _permissionsMap.put(
516                            getPrimaryKeyString(resourceName, resourcePK), permissions);
517            }
518    
519            @Override
520            public void addPermissions(
521                    String resourceName, long resourcePK, List<KeyValuePair> permissions) {
522    
523                    _permissionsMap.put(
524                            getPrimaryKeyString(resourceName, resourcePK), permissions);
525            }
526    
527            @Override
528            public void addPortalPermissions() throws PortalException, SystemException {
529                    addPermissions(PortletKeys.PORTAL, getCompanyId());
530            }
531    
532            @Override
533            public void addPortletPermissions(String resourceName)
534                    throws PortalException, SystemException {
535    
536                    long groupId = getGroupId();
537    
538                    Group group = GroupLocalServiceUtil.getGroup(groupId);
539    
540                    if (group.isStagingGroup()) {
541                            if (group.isStagedRemotely()) {
542                                    groupId = group.getLiveGroupId();
543                            }
544                            else {
545                                    return;
546                            }
547                    }
548    
549                    addPermissions(resourceName, groupId);
550            }
551    
552            @Override
553            public boolean addPrimaryKey(Class<?> clazz, String primaryKey) {
554                    boolean value = hasPrimaryKey(clazz, primaryKey);
555    
556                    if (!value) {
557                            _primaryKeys.add(getPrimaryKeyString(clazz, primaryKey));
558                    }
559    
560                    return value;
561            }
562    
563            @Override
564            public void addRatingsEntries(Class<?> clazz, long classPK)
565                    throws SystemException {
566    
567                    List<RatingsEntry> ratingsEntries =
568                            RatingsEntryLocalServiceUtil.getEntries(clazz.getName(), classPK);
569    
570                    if (ratingsEntries.size() == 0) {
571                            return;
572                    }
573    
574                    _ratingsEntriesMap.put(
575                            getPrimaryKeyString(clazz, classPK), ratingsEntries);
576            }
577    
578            @Override
579            public void addRatingsEntries(
580                    String className, long classPK, List<RatingsEntry> ratingsEntries) {
581    
582                    _ratingsEntriesMap.put(
583                            getPrimaryKeyString(className, classPK), ratingsEntries);
584            }
585    
586            /**
587             * @deprecated As of 7.0.0, with no direct replacement
588             */
589            @Deprecated
590            @Override
591            public Element addReferenceElement(
592                    ClassedModel referrerClassedModel, Element element,
593                    ClassedModel classedModel, Class<?> clazz, String referenceType,
594                    boolean missing) {
595    
596                    return addReferenceElement(
597                            referrerClassedModel, element, classedModel, clazz.getName(),
598                            StringPool.BLANK, referenceType, missing);
599            }
600    
601            @Override
602            public Element addReferenceElement(
603                    ClassedModel referrerClassedModel, Element element,
604                    ClassedModel classedModel, String referenceType, boolean missing) {
605    
606                    return addReferenceElement(
607                            referrerClassedModel, element, classedModel,
608                            ExportImportClassedModelUtil.getClassName(classedModel),
609                            StringPool.BLANK, referenceType, missing);
610            }
611    
612            @Override
613            public Element addReferenceElement(
614                    ClassedModel referrerClassedModel, Element element,
615                    ClassedModel classedModel, String binPath, String referenceType,
616                    boolean missing) {
617    
618                    return addReferenceElement(
619                            referrerClassedModel, element, classedModel,
620                            ExportImportClassedModelUtil.getClassName(classedModel), binPath,
621                            referenceType, missing);
622            }
623    
624            @Override
625            public Element addReferenceElement(
626                    ClassedModel referrerClassedModel, Element element,
627                    ClassedModel classedModel, String className, String binPath,
628                    String referenceType, boolean missing) {
629    
630                    Element referenceElement = doAddReferenceElement(
631                            referrerClassedModel, element, classedModel, className, binPath,
632                            referenceType, false);
633    
634                    String referenceKey = getReferenceKey(classedModel);
635    
636                    if (missing) {
637                            if (_references.contains(referenceKey)) {
638                                    return referenceElement;
639                            }
640    
641                            referenceElement.addAttribute("missing", Boolean.TRUE.toString());
642    
643                            if (!_missingReferences.contains(referenceKey)) {
644                                    _missingReferences.add(referenceKey);
645    
646                                    doAddReferenceElement(
647                                            referrerClassedModel, null, classedModel, className,
648                                            binPath, referenceType, true);
649                            }
650                    }
651                    else {
652                            _references.add(referenceKey);
653    
654                            cleanUpMissingReferences(classedModel);
655                    }
656    
657                    return referenceElement;
658            }
659    
660            @Override
661            public boolean addScopedPrimaryKey(Class<?> clazz, String primaryKey) {
662                    boolean value = hasScopedPrimaryKey(clazz, primaryKey);
663    
664                    if (!value) {
665                            _scopedPrimaryKeys.add(getPrimaryKeyString(clazz, primaryKey));
666                    }
667    
668                    return value;
669            }
670    
671            @Override
672            public void addZipEntry(String path, byte[] bytes) throws SystemException {
673                    if (_portletDataContextListener != null) {
674                            _portletDataContextListener.onAddZipEntry(path);
675                    }
676    
677                    try {
678                            ZipWriter zipWriter = getZipWriter();
679    
680                            zipWriter.addEntry(path, bytes);
681                    }
682                    catch (IOException ioe) {
683                            throw new SystemException(ioe);
684                    }
685            }
686    
687            @Override
688            public void addZipEntry(String path, InputStream is)
689                    throws SystemException {
690    
691                    if (_portletDataContextListener != null) {
692                            _portletDataContextListener.onAddZipEntry(path);
693                    }
694    
695                    try {
696                            ZipWriter zipWriter = getZipWriter();
697    
698                            zipWriter.addEntry(path, is);
699                    }
700                    catch (IOException ioe) {
701                            throw new SystemException(ioe);
702                    }
703            }
704    
705            @Override
706            public void addZipEntry(String path, Object object) throws SystemException {
707                    addZipEntry(path, toXML(object));
708            }
709    
710            @Override
711            public void addZipEntry(String path, String s) throws SystemException {
712                    if (_portletDataContextListener != null) {
713                            _portletDataContextListener.onAddZipEntry(path);
714                    }
715    
716                    try {
717                            ZipWriter zipWriter = getZipWriter();
718    
719                            zipWriter.addEntry(path, s);
720                    }
721                    catch (IOException ioe) {
722                            throw new SystemException(ioe);
723                    }
724            }
725    
726            @Override
727            public void addZipEntry(String path, StringBuilder sb)
728                    throws SystemException {
729    
730                    if (_portletDataContextListener != null) {
731                            _portletDataContextListener.onAddZipEntry(path);
732                    }
733    
734                    try {
735                            ZipWriter zipWriter = getZipWriter();
736    
737                            zipWriter.addEntry(path, sb);
738                    }
739                    catch (IOException ioe) {
740                            throw new SystemException(ioe);
741                    }
742            }
743    
744            @Override
745            public void cleanUpMissingReferences(ClassedModel classedModel) {
746                    String referenceKey = getReferenceKey(classedModel);
747    
748                    if (_missingReferences.contains(referenceKey)) {
749                            _missingReferences.remove(referenceKey);
750    
751                            Element missingReferenceElement = getMissingReferenceElement(
752                                    classedModel);
753    
754                            _missingReferencesElement.remove(missingReferenceElement);
755                    }
756            }
757    
758            @Override
759            public void clearScopedPrimaryKeys() {
760                    _scopedPrimaryKeys.clear();
761            }
762    
763            @Override
764            public ServiceContext createServiceContext(
765                    Element element, ClassedModel classedModel) {
766    
767                    return createServiceContext(
768                            element, null, classedModel, classedModel.getModelClass());
769            }
770    
771            /**
772             * @deprecated As of 6.2.0, replaced by {@link
773             *             #createServiceContext(Element, ClassedModel)}
774             */
775            @Override
776            public ServiceContext createServiceContext(
777                    Element element, ClassedModel classedModel, String namespace) {
778    
779                    return createServiceContext(element, classedModel);
780            }
781    
782            @Override
783            public ServiceContext createServiceContext(StagedModel stagedModel) {
784    
785                    return createServiceContext(stagedModel, stagedModel.getModelClass());
786            }
787    
788            @Override
789            public ServiceContext createServiceContext(
790                    StagedModel stagedModel, Class<?> clazz) {
791    
792                    return createServiceContext(
793                            null, ExportImportPathUtil.getModelPath(stagedModel), stagedModel,
794                            clazz);
795            }
796    
797            @Override
798            public ServiceContext createServiceContext(
799                    String path, ClassedModel classedModel) {
800    
801                    return createServiceContext(
802                            null, path, classedModel, classedModel.getModelClass());
803            }
804    
805            /**
806             * @deprecated As of 6.2.0, replaced by {@link #createServiceContext(String,
807             *             ClassedModel)}
808             */
809            @Override
810            public ServiceContext createServiceContext(
811                    String path, ClassedModel classedModel, String namespace) {
812    
813                    return createServiceContext(path, classedModel);
814            }
815    
816            @Override
817            public Object fromXML(byte[] bytes) {
818                    if (ArrayUtil.isEmpty(bytes)) {
819                            return null;
820                    }
821    
822                    return _xStream.fromXML(new String(bytes));
823            }
824    
825            @Override
826            public Object fromXML(String xml) {
827                    if (Validator.isNull(xml)) {
828                            return null;
829                    }
830    
831                    return _xStream.fromXML(xml);
832            }
833    
834            @Override
835            public long[] getAssetCategoryIds(Class<?> clazz, long classPK) {
836                    return _assetCategoryIdsMap.get(getPrimaryKeyString(clazz, classPK));
837            }
838    
839            /**
840             * @deprecated As of 7.0.0
841             */
842            @Deprecated
843            @Override
844            public Map<String, long[]> getAssetCategoryIdsMap() {
845                    return _assetCategoryIdsMap;
846            }
847    
848            @Override
849            public Map<String, String[]> getAssetCategoryUuidsMap() {
850                    return _assetCategoryUuidsMap;
851            }
852    
853            @Override
854            public Map<String, List<AssetLink>> getAssetLinksMap() {
855                    return _assetLinksMap;
856            }
857    
858            @Override
859            public String[] getAssetTagNames(Class<?> clazz, long classPK) {
860                    return _assetTagNamesMap.get(getPrimaryKeyString(clazz, classPK));
861            }
862    
863            @Override
864            public String[] getAssetTagNames(String className, long classPK) {
865                    return _assetTagNamesMap.get(getPrimaryKeyString(className, classPK));
866            }
867    
868            @Override
869            public Map<String, String[]> getAssetTagNamesMap() {
870                    return _assetTagNamesMap;
871            }
872    
873            @Override
874            public boolean getBooleanParameter(String namespace, String name) {
875                    return getBooleanParameter(namespace, name, true);
876            }
877    
878            @Override
879            public boolean getBooleanParameter(
880                    String namespace, String name, boolean useDefaultValue) {
881    
882                    if (!useDefaultValue) {
883                            return MapUtil.getBoolean(
884                                    getParameterMap(),
885                                    PortletDataHandlerControl.getNamespacedControlName(
886                                            namespace, name));
887                    }
888    
889                    boolean defaultValue = MapUtil.getBoolean(
890                            getParameterMap(),
891                            PortletDataHandlerKeys.PORTLET_DATA_CONTROL_DEFAULT, true);
892    
893                    return MapUtil.getBoolean(
894                            getParameterMap(),
895                            PortletDataHandlerControl.getNamespacedControlName(namespace, name),
896                            defaultValue);
897            }
898    
899            @Override
900            public ClassLoader getClassLoader() {
901                    return _xStream.getClassLoader();
902            }
903    
904            @Override
905            public Map<String, List<MBMessage>> getComments() {
906                    return _commentsMap;
907            }
908    
909            @Override
910            public long getCompanyGroupId() {
911                    return _companyGroupId;
912            }
913    
914            @Override
915            public long getCompanyId() {
916                    return _companyId;
917            }
918    
919            @Override
920            public String getDataStrategy() {
921                    return _dataStrategy;
922            }
923    
924            @Override
925            public DateRange getDateRange() {
926                    DateRange dateRange = null;
927    
928                    if (hasDateRange()) {
929                            dateRange = new DateRange(_startDate, _endDate);
930                    }
931    
932                    return dateRange;
933            }
934    
935            @Override
936            public Criterion getDateRangeCriteria(String propertyName) {
937                    if (!hasDateRange()) {
938                            return null;
939                    }
940    
941                    Conjunction conjunction = RestrictionsFactoryUtil.conjunction();
942    
943                    Property property = PropertyFactoryUtil.forName(propertyName);
944    
945                    conjunction.add(property.le(_endDate));
946                    conjunction.add(property.ge(_startDate));
947    
948                    return conjunction;
949            }
950    
951            @Override
952            public Set<StagedModelType> getDeletionSystemEventStagedModelTypes() {
953                    return _deletionSystemEventModelTypes;
954            }
955    
956            @Override
957            public Date getEndDate() {
958                    return _endDate;
959            }
960    
961            @Override
962            public Map<String, List<ExpandoColumn>> getExpandoColumns() {
963                    return _expandoColumnsMap;
964            }
965    
966            @Override
967            public Element getExportDataElement(ClassedModel classedModel) {
968                    return getExportDataElement(
969                            classedModel,
970                            ExportImportClassedModelUtil.getClassSimpleName(classedModel));
971            }
972    
973            /**
974             * @deprecated As of 7.0.0, replaced by {@link
975             *             #getExportDataElement(ClassedModel, String)}
976             */
977            @Deprecated
978            @Override
979            public Element getExportDataElement(
980                    ClassedModel classedModel, Class<?> modelClass) {
981    
982                    return getExportDataElement(classedModel, modelClass.getSimpleName());
983            }
984    
985            @Override
986            public Element getExportDataElement(
987                    ClassedModel classedModel, String modelClassSimpleName) {
988    
989                    Element groupElement = getExportDataGroupElement(modelClassSimpleName);
990    
991                    Element element = null;
992    
993                    if (classedModel instanceof StagedModel) {
994                            StagedModel stagedModel = (StagedModel)classedModel;
995    
996                            String path = ExportImportPathUtil.getModelPath(stagedModel);
997    
998                            element = getDataElement(groupElement, "path", path);
999    
1000                            if (element != null) {
1001                                    return element;
1002                            }
1003    
1004                            element = getDataElement(
1005                                    groupElement, "uuid", stagedModel.getUuid());
1006    
1007                            if (element != null) {
1008                                    return element;
1009                            }
1010                    }
1011    
1012                    element = groupElement.addElement("staged-model");
1013    
1014                    if (classedModel instanceof StagedGroupedModel) {
1015                            StagedGroupedModel stagedGroupedModel =
1016                                    (StagedGroupedModel)classedModel;
1017    
1018                            element.addAttribute(
1019                                    "group-id",String.valueOf(stagedGroupedModel.getGroupId()));
1020                            element.addAttribute("uuid", stagedGroupedModel.getUuid());
1021                    }
1022                    else if (classedModel instanceof StagedModel) {
1023                            StagedModel stagedModel = (StagedModel)classedModel;
1024    
1025                            element.addAttribute("uuid", stagedModel.getUuid());
1026                    }
1027    
1028                    return element;
1029            }
1030    
1031            @Override
1032            public Element getExportDataGroupElement(
1033                    Class<? extends StagedModel> clazz) {
1034    
1035                    return getExportDataGroupElement(clazz.getSimpleName());
1036            }
1037    
1038            @Override
1039            public Element getExportDataRootElement() {
1040                    return _exportDataRootElement;
1041            }
1042    
1043            @Override
1044            public long getGroupId() {
1045                    return _groupId;
1046            }
1047    
1048            @Override
1049            public Element getImportDataElement(StagedModel stagedModel) {
1050                    return getImportDataElement(
1051                            ExportImportClassedModelUtil.getClassSimpleName(stagedModel),
1052                            "uuid", stagedModel.getUuid());
1053            }
1054    
1055            @Override
1056            public Element getImportDataElement(
1057                    String name, String attribute, String value) {
1058    
1059                    Element groupElement = getImportDataGroupElement(name);
1060    
1061                    return getDataElement(groupElement, attribute, value);
1062            }
1063    
1064            @Override
1065            public Element getImportDataGroupElement(
1066                    Class<? extends StagedModel> clazz) {
1067    
1068                    return getImportDataGroupElement(clazz.getSimpleName());
1069            }
1070    
1071            @Override
1072            public Element getImportDataRootElement() {
1073                    return _importDataRootElement;
1074            }
1075    
1076            @Override
1077            public Element getImportDataStagedModelElement(StagedModel stagedModel) {
1078                    String path = ExportImportPathUtil.getModelPath(stagedModel);
1079    
1080                    return getImportDataElement(
1081                            ExportImportClassedModelUtil.getClassSimpleName(stagedModel),
1082                            "path", path);
1083            }
1084    
1085            @Override
1086            public String getLayoutPath(long plid) {
1087                    return ExportImportPathUtil.getLayoutPath(this, plid);
1088            }
1089    
1090            @Override
1091            public Map<String, Lock> getLocks() {
1092                    return _locksMap;
1093            }
1094    
1095            @Override
1096            public ManifestSummary getManifestSummary() {
1097                    return _manifestSummary;
1098            }
1099    
1100            @Override
1101            public Element getMissingReferencesElement() {
1102                    return _missingReferencesElement;
1103            }
1104    
1105            @Override
1106            public List<Layout> getNewLayouts() {
1107                    return _newLayouts;
1108            }
1109    
1110            @Override
1111            public Map<?, ?> getNewPrimaryKeysMap(Class<?> clazz) {
1112                    return getNewPrimaryKeysMap(clazz.getName());
1113            }
1114    
1115            @Override
1116            public Map<String, Map<?, ?>> getNewPrimaryKeysMaps() {
1117                    return _newPrimaryKeysMaps;
1118            }
1119    
1120            @Override
1121            public Map<?, ?> getNewPrimaryKeysMap(String className) {
1122                    Map<?, ?> map = _newPrimaryKeysMaps.get(className);
1123    
1124                    if (map == null) {
1125                            map = new HashMap<Object, Object>();
1126    
1127                            _newPrimaryKeysMaps.put(className, map);
1128                    }
1129    
1130                    return map;
1131            }
1132    
1133            /**
1134             * @deprecated As of 7.0.0
1135             */
1136            @Deprecated
1137            @Override
1138            public long getOldPlid() {
1139                    return _oldPlid;
1140            }
1141    
1142            @Override
1143            public Map<String, String[]> getParameterMap() {
1144                    return _parameterMap;
1145            }
1146    
1147            @Override
1148            public Map<String, List<KeyValuePair>> getPermissions() {
1149                    return _permissionsMap;
1150            }
1151    
1152            @Override
1153            public long getPlid() {
1154                    return _plid;
1155            }
1156    
1157            @Override
1158            public String getRootPortletId() {
1159                    return _rootPortletId;
1160            }
1161    
1162            @Override
1163            public String getPortletPath(String portletId) {
1164                    return ExportImportPathUtil.getPortletPath(this, portletId);
1165            }
1166    
1167            @Override
1168            public Set<String> getPrimaryKeys() {
1169                    return _primaryKeys;
1170            }
1171    
1172            @Override
1173            public Map<String, List<RatingsEntry>> getRatingsEntries() {
1174                    return _ratingsEntriesMap;
1175            }
1176    
1177            @Override
1178            public Element getReferenceDataElement(
1179                    Element parentElement, Class<?> clazz, long classPK) {
1180    
1181                    List<Element> referenceElements = getReferenceElements(
1182                            parentElement, clazz, 0, null, classPK, null);
1183    
1184                    List<Element> referenceDataElements = getReferenceDataElements(
1185                            referenceElements, clazz);
1186    
1187                    if (referenceDataElements.isEmpty()) {
1188                            return null;
1189                    }
1190    
1191                    return referenceDataElements.get(0);
1192            }
1193    
1194            @Override
1195            public Element getReferenceDataElement(
1196                    Element parentElement, Class<?> clazz, long groupId, String uuid) {
1197    
1198                    List<Element> referenceElements = getReferenceElements(
1199                            parentElement, clazz, groupId, uuid, 0, null);
1200    
1201                    List<Element> referenceDataElements = getReferenceDataElements(
1202                            referenceElements, clazz);
1203    
1204                    if (referenceDataElements.isEmpty()) {
1205                            return null;
1206                    }
1207    
1208                    return referenceDataElements.get(0);
1209            }
1210    
1211            @Override
1212            public Element getReferenceDataElement(
1213                    StagedModel parentStagedModel, Class<?> clazz, long classPK) {
1214    
1215                    Element parentElement = getImportDataStagedModelElement(
1216                            parentStagedModel);
1217    
1218                    return getReferenceDataElement(parentElement, clazz, classPK);
1219            }
1220    
1221            @Override
1222            public Element getReferenceDataElement(
1223                    StagedModel parentStagedModel, Class<?> clazz, long groupId,
1224                    String uuid) {
1225    
1226                    Element parentElement = getImportDataStagedModelElement(
1227                            parentStagedModel);
1228    
1229                    return getReferenceDataElement(parentElement, clazz, groupId, uuid);
1230            }
1231    
1232            /**
1233             * @deprecated As of 7.0.0
1234             */
1235            @Deprecated
1236            @Override
1237            public List<Element> getReferenceDataElements(
1238                    Element parentElement, Class<?> clazz) {
1239    
1240                    return getReferenceDataElements(parentElement, clazz, null);
1241            }
1242    
1243            @Override
1244            public List<Element> getReferenceDataElements(
1245                    Element parentElement, Class<?> clazz, String referenceType) {
1246    
1247                    List<Element> referenceElements = getReferenceElements(
1248                            parentElement, clazz, 0, null, 0, referenceType);
1249    
1250                    return getReferenceDataElements(referenceElements, clazz);
1251            }
1252    
1253            @Override
1254            public List<Element> getReferenceDataElements(
1255                    StagedModel parentStagedModel, Class<?> clazz) {
1256    
1257                    return getReferenceDataElements(parentStagedModel, clazz, null);
1258            }
1259    
1260            @Override
1261            public List<Element> getReferenceDataElements(
1262                    StagedModel parentStagedModel, Class<?> clazz, String referenceType) {
1263    
1264                    List<Element> referenceElements = getReferenceElements(
1265                            parentStagedModel, clazz, 0, referenceType);
1266    
1267                    return getReferenceDataElements(referenceElements, clazz);
1268            }
1269    
1270            @Override
1271            public Element getReferenceElement(
1272                    Element parentElement, Class<?> clazz, long groupId, String uuid,
1273                    String referenceType) {
1274    
1275                    List<Element> referenceElements = getReferenceElements(
1276                            parentElement, clazz, groupId, uuid, 0, referenceType);
1277    
1278                    if (!referenceElements.isEmpty()) {
1279                            return referenceElements.get(0);
1280                    }
1281    
1282                    return null;
1283            }
1284    
1285            @Override
1286            public Element getReferenceElement(
1287                    StagedModel parentStagedModel, Class<?> clazz, long classPK) {
1288    
1289                    List<Element> referenceElements = getReferenceElements(
1290                            parentStagedModel, clazz, classPK, null);
1291    
1292                    if (!referenceElements.isEmpty()) {
1293                            return referenceElements.get(0);
1294                    }
1295    
1296                    return null;
1297            }
1298    
1299            @Override
1300            public List<Element> getReferenceElements(
1301                    StagedModel parentStagedModel, Class<?> clazz) {
1302    
1303                    return getReferenceElements(parentStagedModel, clazz, 0, null);
1304            }
1305    
1306            @Override
1307            public String getRootPath() {
1308                    return ExportImportPathUtil.getRootPath(this);
1309            }
1310    
1311            /**
1312             * @deprecated As of 7.0.0
1313             */
1314            @Deprecated
1315            @Override
1316            public Set<String> getScopedPrimaryKeys() {
1317                    return _scopedPrimaryKeys;
1318            }
1319    
1320            @Override
1321            public long getScopeGroupId() {
1322                    return _scopeGroupId;
1323            }
1324    
1325            @Override
1326            public String getScopeLayoutUuid() {
1327                    return _scopeLayoutUuid;
1328            }
1329    
1330            @Override
1331            public String getScopeType() {
1332                    return _scopeType;
1333            }
1334    
1335            @Override
1336            public long getSourceCompanyGroupId() {
1337                    return _sourceCompanyGroupId;
1338            }
1339    
1340            @Override
1341            public long getSourceCompanyId() {
1342                    return _sourceCompanyId;
1343            }
1344    
1345            @Override
1346            public long getSourceGroupId() {
1347                    return _sourceGroupId;
1348            }
1349    
1350            @Override
1351            public String getSourceLayoutPath(long layoutId) {
1352                    return ExportImportPathUtil.getSourceLayoutPath(this, layoutId);
1353            }
1354    
1355            @Override
1356            public String getSourcePortletPath(String portletId) {
1357                    return ExportImportPathUtil.getSourcePortletPath(this, portletId);
1358            }
1359    
1360            @Override
1361            public String getSourceRootPath() {
1362                    return ExportImportPathUtil.getSourceRootPath(this);
1363            }
1364    
1365            @Override
1366            public long getSourceUserPersonalSiteGroupId() {
1367                    return _sourceUserPersonalSiteGroupId;
1368            }
1369    
1370            @Override
1371            public Date getStartDate() {
1372                    return _startDate;
1373            }
1374    
1375            @Override
1376            public long getUserId(String userUuid) {
1377                    return _userIdStrategy.getUserId(userUuid);
1378            }
1379    
1380            @Override
1381            public UserIdStrategy getUserIdStrategy() {
1382                    return _userIdStrategy;
1383            }
1384    
1385            @Override
1386            public long getUserPersonalSiteGroupId() {
1387                    return _userPersonalSiteGroupId;
1388            }
1389    
1390            /**
1391             * @deprecated As of 7.0.0
1392             */
1393            @Deprecated
1394            @Override
1395            public List<String> getZipEntries() {
1396                    return getZipReader().getEntries();
1397            }
1398    
1399            @Override
1400            public byte[] getZipEntryAsByteArray(String path) {
1401                    if (!Validator.isFilePath(path, false)) {
1402                            return null;
1403                    }
1404    
1405                    if (_portletDataContextListener != null) {
1406                            _portletDataContextListener.onGetZipEntry(path);
1407                    }
1408    
1409                    return getZipReader().getEntryAsByteArray(path);
1410            }
1411    
1412            @Override
1413            public InputStream getZipEntryAsInputStream(String path) {
1414                    if (!Validator.isFilePath(path, false)) {
1415                            return null;
1416                    }
1417    
1418                    if (_portletDataContextListener != null) {
1419                            _portletDataContextListener.onGetZipEntry(path);
1420                    }
1421    
1422                    return getZipReader().getEntryAsInputStream(path);
1423            }
1424    
1425            @Override
1426            public Object getZipEntryAsObject(Element element, String path) {
1427                    Object object = fromXML(getZipEntryAsString(path));
1428    
1429                    Attribute classNameAttribute = element.attribute("class-name");
1430    
1431                    if (classNameAttribute != null) {
1432                            BeanPropertiesUtil.setProperty(
1433                                    object, "className", classNameAttribute.getText());
1434                    }
1435    
1436                    return object;
1437            }
1438    
1439            @Override
1440            public Object getZipEntryAsObject(String path) {
1441                    return fromXML(getZipEntryAsString(path));
1442            }
1443    
1444            @Override
1445            public String getZipEntryAsString(String path) {
1446                    if (!Validator.isFilePath(path, false)) {
1447                            return null;
1448                    }
1449    
1450                    if (_portletDataContextListener != null) {
1451                            _portletDataContextListener.onGetZipEntry(path);
1452                    }
1453    
1454                    return getZipReader().getEntryAsString(path);
1455            }
1456    
1457            /**
1458             * @deprecated As of 7.0.0
1459             */
1460            @Deprecated
1461            @Override
1462            public List<String> getZipFolderEntries() {
1463                    return getZipFolderEntries(StringPool.SLASH);
1464            }
1465    
1466            @Override
1467            public List<String> getZipFolderEntries(String path) {
1468                    if (!Validator.isFilePath(path, false)) {
1469                            return null;
1470                    }
1471    
1472                    return getZipReader().getFolderEntries(path);
1473            }
1474    
1475            @Override
1476            public ZipReader getZipReader() {
1477                    return _zipReader;
1478            }
1479    
1480            @Override
1481            public ZipWriter getZipWriter() {
1482                    return _zipWriter;
1483            }
1484    
1485            @Override
1486            public boolean hasDateRange() {
1487                    if (_startDate != null) {
1488                            return true;
1489                    }
1490                    else {
1491                            return false;
1492                    }
1493            }
1494    
1495            @Override
1496            public boolean hasNotUniquePerLayout(String dataKey) {
1497                    return _notUniquePerLayout.contains(dataKey);
1498            }
1499    
1500            @Override
1501            public boolean hasPrimaryKey(Class<?> clazz, String primaryKey) {
1502                    return _primaryKeys.contains(getPrimaryKeyString(clazz, primaryKey));
1503            }
1504    
1505            @Override
1506            public boolean hasScopedPrimaryKey(Class<?> clazz, String primaryKey) {
1507                    return _scopedPrimaryKeys.contains(
1508                            getPrimaryKeyString(clazz, primaryKey));
1509            }
1510    
1511            @Override
1512            public void importClassedModel(
1513                            ClassedModel classedModel, ClassedModel newClassedModel)
1514                    throws PortalException, SystemException {
1515    
1516                    importClassedModel(
1517                            classedModel, newClassedModel, classedModel.getModelClass());
1518            }
1519    
1520            @Override
1521            public void importClassedModel(
1522                            ClassedModel classedModel, ClassedModel newClassedModel,
1523                            Class<?> clazz)
1524                    throws PortalException, SystemException {
1525    
1526                    if (!isResourceMain(classedModel)) {
1527                            return;
1528                    }
1529    
1530                    long classPK = getClassPK(classedModel);
1531    
1532                    long newClassPK = getClassPK(newClassedModel);
1533    
1534                    Map<Long, Long> newPrimaryKeysMap =
1535                            (Map<Long, Long>)getNewPrimaryKeysMap(clazz);
1536    
1537                    newPrimaryKeysMap.put(classPK, newClassPK);
1538    
1539                    importLocks(clazz, String.valueOf(classPK), String.valueOf(newClassPK));
1540                    importPermissions(clazz, classPK, newClassPK);
1541    
1542                    boolean portletDataAll = MapUtil.getBoolean(
1543                            getParameterMap(), PortletDataHandlerKeys.PORTLET_DATA_ALL);
1544    
1545                    if (portletDataAll ||
1546                            MapUtil.getBoolean(
1547                                    getParameterMap(), PortletDataHandlerKeys.COMMENTS)) {
1548    
1549                            importComments(clazz, classPK, newClassPK, getScopeGroupId());
1550                    }
1551    
1552                    if (portletDataAll ||
1553                            MapUtil.getBoolean(
1554                                    getParameterMap(), PortletDataHandlerKeys.RATINGS)) {
1555    
1556                            importRatingsEntries(clazz, classPK, newClassPK);
1557                    }
1558            }
1559    
1560            /**
1561             * @deprecated As of 6.2.0, replaced by {@link
1562             *             #importClassedModel(ClassedModel, ClassedModel, Class)}
1563             */
1564            @Override
1565            public void importClassedModel(
1566                            ClassedModel classedModel, ClassedModel newClassedModel,
1567                            Class<?> clazz, String namespace)
1568                    throws PortalException, SystemException {
1569    
1570                    importClassedModel(classedModel, newClassedModel, clazz);
1571            }
1572    
1573            /**
1574             * @deprecated As of 6.2.0, replaced by {@link
1575             *             #importClassedModel(ClassedModel, ClassedModel)}
1576             */
1577            @Override
1578            public void importClassedModel(
1579                            ClassedModel classedModel, ClassedModel newClassedModel,
1580                            String namespace)
1581                    throws PortalException, SystemException {
1582    
1583                    importClassedModel(
1584                            classedModel, newClassedModel, classedModel.getModelClass());
1585            }
1586    
1587            @Override
1588            public void importComments(
1589                            Class<?> clazz, long classPK, long newClassPK, long groupId)
1590                    throws PortalException, SystemException {
1591    
1592                    Map<Long, Long> messagePKs = new HashMap<Long, Long>();
1593                    Map<Long, Long> threadPKs = new HashMap<Long, Long>();
1594    
1595                    List<MBMessage> messages = _commentsMap.get(
1596                            getPrimaryKeyString(clazz, classPK));
1597    
1598                    if ((messages == null) || messages.isEmpty()) {
1599                            return;
1600                    }
1601    
1602                    MBMessage firstMessage = messages.get(0);
1603    
1604                    if ((messages.size() == 1) && firstMessage.isRoot()) {
1605                            return;
1606                    }
1607    
1608                    long classNameId = PortalUtil.getClassNameId(clazz);
1609    
1610                    MBDiscussion discussion = MBDiscussionUtil.fetchByC_C(
1611                            classNameId, newClassPK);
1612    
1613                    for (MBMessage message : messages) {
1614                            long userId = getUserId(message.getUserUuid());
1615                            long parentMessageId = MapUtil.getLong(
1616                                    messagePKs, message.getParentMessageId(),
1617                                    message.getParentMessageId());
1618                            long threadId = MapUtil.getLong(
1619                                    threadPKs, message.getThreadId(), message.getThreadId());
1620    
1621                            if (message.isRoot()) {
1622                                    if (discussion != null) {
1623                                            MBThread thread = MBThreadLocalServiceUtil.getThread(
1624                                                    discussion.getThreadId());
1625    
1626                                            long rootMessageId = thread.getRootMessageId();
1627    
1628                                            messagePKs.put(message.getMessageId(), rootMessageId);
1629                                            threadPKs.put(message.getThreadId(), thread.getThreadId());
1630                                    }
1631                                    else if (clazz == Layout.class) {
1632                                            MBMessage importedMessage =
1633                                                    MBMessageLocalServiceUtil.addDiscussionMessage(
1634                                                            userId, message.getUserName(), groupId,
1635                                                            clazz.getName(), newClassPK,
1636                                                            WorkflowConstants.ACTION_PUBLISH);
1637    
1638                                            messagePKs.put(
1639                                                    message.getMessageId(), importedMessage.getMessageId());
1640                                            threadPKs.put(
1641                                                    message.getThreadId(), importedMessage.getThreadId());
1642                                    }
1643                            }
1644                            else {
1645                                    ServiceContext serviceContext = new ServiceContext();
1646    
1647                                    serviceContext.setCreateDate(message.getCreateDate());
1648                                    serviceContext.setModifiedDate(message.getModifiedDate());
1649                                    serviceContext.setScopeGroupId(groupId);
1650    
1651                                    MBMessage importedMessage = null;
1652    
1653                                    if (_dataStrategy.equals(
1654                                                    PortletDataHandlerKeys.DATA_STRATEGY_MIRROR) ||
1655                                            _dataStrategy.equals(
1656                                                    PortletDataHandlerKeys.
1657                                                            DATA_STRATEGY_MIRROR_OVERWRITE)) {
1658    
1659                                            MBMessage existingMessage = MBMessageUtil.fetchByUUID_G(
1660                                                    message.getUuid(), groupId);
1661    
1662                                            if (existingMessage == null) {
1663                                                    serviceContext.setUuid(message.getUuid());
1664    
1665                                                    importedMessage =
1666                                                            MBMessageLocalServiceUtil.addDiscussionMessage(
1667                                                                    userId, message.getUserName(), groupId,
1668                                                                    clazz.getName(), newClassPK, threadId,
1669                                                                    parentMessageId, message.getSubject(),
1670                                                                    message.getBody(), serviceContext);
1671                                            }
1672                                            else {
1673                                                    serviceContext.setWorkflowAction(
1674                                                            WorkflowConstants.ACTION_PUBLISH);
1675    
1676                                                    importedMessage =
1677                                                            MBMessageLocalServiceUtil.updateDiscussionMessage(
1678                                                                    userId, existingMessage.getMessageId(),
1679                                                                    clazz.getName(), newClassPK,
1680                                                                    message.getSubject(), message.getBody(),
1681                                                                    serviceContext);
1682                                            }
1683                                    }
1684                                    else {
1685                                            importedMessage =
1686                                                    MBMessageLocalServiceUtil.addDiscussionMessage(
1687                                                            userId, message.getUserName(), groupId,
1688                                                            clazz.getName(), newClassPK, threadId,
1689                                                            parentMessageId, message.getSubject(),
1690                                                            message.getBody(), serviceContext);
1691                                    }
1692    
1693                                    messagePKs.put(
1694                                            message.getMessageId(), importedMessage.getMessageId());
1695                                    threadPKs.put(
1696                                            message.getThreadId(), importedMessage.getThreadId());
1697                            }
1698    
1699                            importRatingsEntries(
1700                                    MBDiscussion.class, message.getPrimaryKey(),
1701                                    messagePKs.get(message.getPrimaryKey()));
1702                    }
1703            }
1704    
1705            @Override
1706            public void importLocks(Class<?> clazz, String key, String newKey)
1707                    throws PortalException, SystemException {
1708    
1709                    Lock lock = _locksMap.get(getPrimaryKeyString(clazz, key));
1710    
1711                    if (lock == null) {
1712                            return;
1713                    }
1714    
1715                    long userId = getUserId(lock.getUserUuid());
1716    
1717                    long expirationTime = 0;
1718    
1719                    if (lock.getExpirationDate() != null) {
1720                            Date expirationDate = lock.getExpirationDate();
1721    
1722                            expirationTime = expirationDate.getTime();
1723                    }
1724    
1725                    LockLocalServiceUtil.lock(
1726                            userId, clazz.getName(), newKey, lock.getOwner(),
1727                            lock.isInheritable(), expirationTime);
1728            }
1729    
1730            @Override
1731            public void importPermissions(Class<?> clazz, long classPK, long newClassPK)
1732                    throws PortalException, SystemException {
1733    
1734                    importPermissions(clazz.getName(), classPK, newClassPK);
1735            }
1736    
1737            @Override
1738            public void importPermissions(
1739                            String resourceName, long resourcePK, long newResourcePK)
1740                    throws PortalException, SystemException {
1741    
1742                    if (!MapUtil.getBoolean(
1743                                    _parameterMap, PortletDataHandlerKeys.PERMISSIONS)) {
1744    
1745                            return;
1746                    }
1747    
1748                    List<KeyValuePair> permissions = _permissionsMap.get(
1749                            getPrimaryKeyString(resourceName, resourcePK));
1750    
1751                    if (permissions == null) {
1752                            return;
1753                    }
1754    
1755                    Map<Long, String[]> roleIdsToActionIds = new HashMap<Long, String[]>();
1756    
1757                    for (KeyValuePair permission : permissions) {
1758                            String roleName = permission.getKey();
1759    
1760                            Role role = null;
1761    
1762                            Team team = null;
1763    
1764                            if (roleName.startsWith(PermissionExporter.ROLE_TEAM_PREFIX)) {
1765                                    roleName = roleName.substring(
1766                                            PermissionExporter.ROLE_TEAM_PREFIX.length());
1767    
1768                                    try {
1769                                            team = TeamLocalServiceUtil.getTeam(_groupId, roleName);
1770                                    }
1771                                    catch (NoSuchTeamException nste) {
1772                                            if (_log.isWarnEnabled()) {
1773                                                    _log.warn("Team " + roleName + " does not exist");
1774                                            }
1775    
1776                                            continue;
1777                                    }
1778                            }
1779    
1780                            try {
1781                                    if (team != null) {
1782                                            role = RoleLocalServiceUtil.getTeamRole(
1783                                                    _companyId, team.getTeamId());
1784                                    }
1785                                    else {
1786                                            role = RoleLocalServiceUtil.getRole(_companyId, roleName);
1787                                    }
1788                            }
1789                            catch (NoSuchRoleException nsre) {
1790                                    if (_log.isWarnEnabled()) {
1791                                            _log.warn("Role " + roleName + " does not exist");
1792                                    }
1793    
1794                                    continue;
1795                            }
1796    
1797                            String[] actionIds = StringUtil.split(permission.getValue());
1798    
1799                            roleIdsToActionIds.put(role.getRoleId(), actionIds);
1800                    }
1801    
1802                    if (roleIdsToActionIds.isEmpty()) {
1803                            return;
1804                    }
1805    
1806                    if (ResourceBlockLocalServiceUtil.isSupported(resourceName)) {
1807                            ResourceBlockLocalServiceUtil.setIndividualScopePermissions(
1808                                    _companyId, _groupId, resourceName, newResourcePK,
1809                                    roleIdsToActionIds);
1810                    }
1811                    else {
1812                            ResourcePermissionLocalServiceUtil.setResourcePermissions(
1813                                    _companyId, resourceName, ResourceConstants.SCOPE_INDIVIDUAL,
1814                                    String.valueOf(newResourcePK), roleIdsToActionIds);
1815                    }
1816            }
1817    
1818            @Override
1819            public void importPortalPermissions()
1820                    throws PortalException, SystemException {
1821    
1822                    importPermissions(
1823                            PortletKeys.PORTAL, getSourceCompanyId(), getCompanyId());
1824            }
1825    
1826            @Override
1827            public void importPortletPermissions(String resourceName)
1828                    throws PortalException, SystemException {
1829    
1830                    importPermissions(resourceName, getSourceGroupId(), getScopeGroupId());
1831            }
1832    
1833            @Override
1834            public void importRatingsEntries(
1835                            Class<?> clazz, long classPK, long newClassPK)
1836                    throws PortalException, SystemException {
1837    
1838                    List<RatingsEntry> ratingsEntries = _ratingsEntriesMap.get(
1839                            getPrimaryKeyString(clazz, classPK));
1840    
1841                    if ((ratingsEntries == null) || ratingsEntries.isEmpty()) {
1842                            return;
1843                    }
1844    
1845                    ServiceContext serviceContext = new ServiceContext();
1846    
1847                    for (RatingsEntry ratingsEntry : ratingsEntries) {
1848                            long userId = getUserId(ratingsEntry.getUserUuid());
1849    
1850                            serviceContext.setCreateDate(ratingsEntry.getCreateDate());
1851                            serviceContext.setModifiedDate(ratingsEntry.getModifiedDate());
1852    
1853                            RatingsEntryLocalServiceUtil.updateEntry(
1854                                    userId, clazz.getName(), newClassPK, ratingsEntry.getScore(),
1855                                    serviceContext);
1856                    }
1857            }
1858    
1859            @Override
1860            public boolean isCompanyStagedGroupedModel(
1861                    StagedGroupedModel stagedGroupedModel) {
1862    
1863                    if ((stagedGroupedModel.getGroupId() == getCompanyGroupId()) &&
1864                            (getGroupId() != getCompanyGroupId())) {
1865    
1866                            return true;
1867                    }
1868    
1869                    return false;
1870            }
1871    
1872            @Override
1873            public boolean isDataStrategyMirror() {
1874                    if (_dataStrategy.equals(PortletDataHandlerKeys.DATA_STRATEGY_MIRROR) ||
1875                            _dataStrategy.equals(
1876                                    PortletDataHandlerKeys.DATA_STRATEGY_MIRROR_OVERWRITE)) {
1877    
1878                            return true;
1879                    }
1880                    else {
1881                            return false;
1882                    }
1883            }
1884    
1885            @Override
1886            public boolean isDataStrategyMirrorWithOverwriting() {
1887                    if (_dataStrategy.equals(
1888                                    PortletDataHandlerKeys.DATA_STRATEGY_MIRROR_OVERWRITE)) {
1889    
1890                            return true;
1891                    }
1892                    else {
1893                            return false;
1894                    }
1895            }
1896    
1897            @Override
1898            public boolean isInitialPublication() {
1899                    Group group = null;
1900    
1901                    try {
1902                            group = GroupLocalServiceUtil.getGroup(getGroupId());
1903                    }
1904                    catch (Exception e) {
1905                    }
1906    
1907                    if (ExportImportThreadLocal.isStagingInProcess() && (group != null) &&
1908                            group.hasStagingGroup()) {
1909    
1910                            return true;
1911                    }
1912    
1913                    return false;
1914            }
1915    
1916            @Override
1917            public boolean isMissingReference(Element referenceElement) {
1918                    Attribute missingAttribute = referenceElement.attribute("missing");
1919    
1920                    if ((missingAttribute != null) &&
1921                            !GetterUtil.getBoolean(
1922                                    referenceElement.attributeValue("missing"))) {
1923    
1924                            return false;
1925                    }
1926    
1927                    if (_missingReferences.isEmpty()) {
1928                            List<Element> missingReferenceElements =
1929                                    _missingReferencesElement.elements();
1930    
1931                            for (Element missingReferenceElement : missingReferenceElements) {
1932                                    String missingReferenceClassName =
1933                                            missingReferenceElement.attributeValue("class-name");
1934                                    String missingReferenceClassPK =
1935                                            missingReferenceElement.attributeValue("class-pk");
1936    
1937                                    String missingReferenceKey = getReferenceKey(
1938                                            missingReferenceClassName, missingReferenceClassPK);
1939    
1940                                    _missingReferences.add(missingReferenceKey);
1941                            }
1942                    }
1943    
1944                    String className = referenceElement.attributeValue("class-name");
1945                    String classPK = referenceElement.attributeValue("class-pk");
1946    
1947                    String referenceKey = getReferenceKey(className, classPK);
1948    
1949                    return _missingReferences.contains(referenceKey);
1950            }
1951    
1952            @Override
1953            public boolean isModelCounted(String className, long classPK) {
1954                    String modelCountedPrimaryKey = className.concat(
1955                            StringPool.POUND).concat(String.valueOf(classPK));
1956    
1957                    return addPrimaryKey(String.class, modelCountedPrimaryKey);
1958            }
1959    
1960            @Override
1961            public boolean isPathExportedInScope(String path) {
1962                    return addScopedPrimaryKey(String.class, path);
1963            }
1964    
1965            /**
1966             * @deprecated As of 7.0.0
1967             */
1968            @Deprecated
1969            @Override
1970            public boolean isPathNotExportedInScope(String path) {
1971                    return !isPathExportedInScope(path);
1972            }
1973    
1974            @Override
1975            public boolean isPathNotProcessed(String path) {
1976                    return !isPathProcessed(path);
1977            }
1978    
1979            @Override
1980            public boolean isPathProcessed(String path) {
1981                    addScopedPrimaryKey(String.class, path);
1982    
1983                    return addPrimaryKey(String.class, path);
1984            }
1985    
1986            @Override
1987            public boolean isPerformDirectBinaryImport() {
1988                    return MapUtil.getBoolean(
1989                            _parameterMap, PortletDataHandlerKeys.PERFORM_DIRECT_BINARY_IMPORT);
1990            }
1991    
1992            @Override
1993            public boolean isPrivateLayout() {
1994                    return _privateLayout;
1995            }
1996    
1997            @Override
1998            public boolean isStagedModelCounted(StagedModel stagedModel) {
1999                    StagedModelType stagedModelType = stagedModel.getStagedModelType();
2000    
2001                    return isModelCounted(
2002                            stagedModelType.getClassName(),
2003                            (Long)stagedModel.getPrimaryKeyObj());
2004            }
2005    
2006            /**
2007             * @see #addDateRangeCriteria(DynamicQuery, String)
2008             */
2009            @Override
2010            public boolean isWithinDateRange(Date modifiedDate) {
2011                    if (!hasDateRange()) {
2012                            return true;
2013                    }
2014                    else if ((_startDate.compareTo(modifiedDate) <= 0) &&
2015                                     _endDate.after(modifiedDate)) {
2016    
2017                            return true;
2018                    }
2019                    else {
2020                            return false;
2021                    }
2022            }
2023    
2024            @Override
2025            public void putNotUniquePerLayout(String dataKey) {
2026                    _notUniquePerLayout.add(dataKey);
2027            }
2028    
2029            @Override
2030            public void setClassLoader(ClassLoader classLoader) {
2031                    _xStream.setClassLoader(classLoader);
2032            }
2033    
2034            @Override
2035            public void setCompanyGroupId(long companyGroupId) {
2036                    _companyGroupId = companyGroupId;
2037            }
2038    
2039            @Override
2040            public void setCompanyId(long companyId) {
2041                    _companyId = companyId;
2042            }
2043    
2044            @Override
2045            public void setDataStrategy(String dataStrategy) {
2046                    _dataStrategy = dataStrategy;
2047            }
2048    
2049            @Override
2050            public void setEndDate(Date endDate) {
2051                    _endDate = endDate;
2052            }
2053    
2054            @Override
2055            public void setExportDataRootElement(Element exportDataRootElement) {
2056                    _exportDataRootElement = exportDataRootElement;
2057            }
2058    
2059            @Override
2060            public void setGroupId(long groupId) {
2061                    _groupId = groupId;
2062            }
2063    
2064            @Override
2065            public void setImportDataRootElement(Element importDataRootElement) {
2066                    _importDataRootElement = importDataRootElement;
2067            }
2068    
2069            @Override
2070            public void setManifestSummary(ManifestSummary manifestSummary) {
2071                    _manifestSummary = manifestSummary;
2072            }
2073    
2074            @Override
2075            public void setMissingReferencesElement(Element missingReferencesElement) {
2076                    _missingReferencesElement = missingReferencesElement;
2077            }
2078    
2079            @Override
2080            public void setNewLayouts(List<Layout> newLayouts) {
2081                    _newLayouts = newLayouts;
2082            }
2083    
2084            @Override
2085            public void setNewPrimaryKeysMaps(
2086                    Map<String, Map<?, ?>> newPrimaryKeysMaps) {
2087    
2088                    _newPrimaryKeysMaps = newPrimaryKeysMaps;
2089            }
2090    
2091            @Override
2092            public void setOldPlid(long oldPlid) {
2093                    _oldPlid = oldPlid;
2094            }
2095    
2096            @Override
2097            public void setParameterMap(Map<String, String[]> parameterMap) {
2098                    _parameterMap = parameterMap;
2099            }
2100    
2101            @Override
2102            public void setPlid(long plid) {
2103                    _plid = plid;
2104            }
2105    
2106            @Override
2107            public void setPortetDataContextListener(
2108                    PortletDataContextListener portletDataContextListener) {
2109    
2110                    _portletDataContextListener = portletDataContextListener;
2111            }
2112    
2113            @Override
2114            public void setPrivateLayout(boolean privateLayout) {
2115                    _privateLayout = privateLayout;
2116            }
2117    
2118            @Override
2119            public void setRootPortletId(String rootPortletId) {
2120                    _rootPortletId = rootPortletId;
2121            }
2122    
2123            @Override
2124            public void setScopeGroupId(long scopeGroupId) {
2125                    _scopeGroupId = scopeGroupId;
2126            }
2127    
2128            @Override
2129            public void setScopeLayoutUuid(String scopeLayoutUuid) {
2130                    _scopeLayoutUuid = scopeLayoutUuid;
2131            }
2132    
2133            @Override
2134            public void setScopeType(String scopeType) {
2135                    _scopeType = scopeType;
2136            }
2137    
2138            @Override
2139            public void setSourceCompanyGroupId(long sourceCompanyGroupId) {
2140                    _sourceCompanyGroupId = sourceCompanyGroupId;
2141            }
2142    
2143            @Override
2144            public void setSourceCompanyId(long sourceCompanyId) {
2145                    _sourceCompanyId = sourceCompanyId;
2146            }
2147    
2148            @Override
2149            public void setSourceGroupId(long sourceGroupId) {
2150                    _sourceGroupId = sourceGroupId;
2151            }
2152    
2153            @Override
2154            public void setSourceUserPersonalSiteGroupId(
2155                    long sourceUserPersonalSiteGroupId) {
2156    
2157                    _sourceUserPersonalSiteGroupId = sourceUserPersonalSiteGroupId;
2158            }
2159    
2160            @Override
2161            public void setStartDate(Date startDate) {
2162                    _startDate = startDate;
2163            }
2164    
2165            @Override
2166            public void setUserIdStrategy(UserIdStrategy userIdStrategy) {
2167                    _userIdStrategy = userIdStrategy;
2168            }
2169    
2170            @Override
2171            public void setUserPersonalSiteGroupId(long userPersonalSiteGroupId) {
2172                    _userPersonalSiteGroupId = userPersonalSiteGroupId;
2173            }
2174    
2175            @Override
2176            public void setZipReader(ZipReader zipReader) {
2177                    _zipReader = zipReader;
2178            }
2179    
2180            @Override
2181            public void setZipWriter(ZipWriter zipWriter) {
2182                    _zipWriter = zipWriter;
2183            }
2184    
2185            @Override
2186            public String toXML(Object object) {
2187                    return _xStream.toXML(object);
2188            }
2189    
2190            protected void addExpando(
2191                            Element element, String path, ClassedModel classedModel,
2192                            Class<?> clazz)
2193                    throws PortalException, SystemException {
2194    
2195                    String className = clazz.getName();
2196    
2197                    if (!_expandoColumnsMap.containsKey(className)) {
2198                            List<ExpandoColumn> expandoColumns =
2199                                    ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
2200                                            _companyId, className);
2201    
2202                            for (ExpandoColumn expandoColumn : expandoColumns) {
2203                                    addPermissions(
2204                                            ExpandoColumn.class, expandoColumn.getColumnId());
2205                            }
2206    
2207                            _expandoColumnsMap.put(className, expandoColumns);
2208                    }
2209    
2210                    ExpandoBridge expandoBridge = classedModel.getExpandoBridge();
2211    
2212                    Map<String, Serializable> expandoBridgeAttributes =
2213                            expandoBridge.getAttributes();
2214    
2215                    if (!expandoBridgeAttributes.isEmpty()) {
2216                            String expandoPath = ExportImportPathUtil.getExpandoPath(path);
2217    
2218                            element.addAttribute("expando-path", expandoPath);
2219    
2220                            addZipEntry(expandoPath, expandoBridgeAttributes);
2221                    }
2222            }
2223    
2224            protected ServiceContext createServiceContext(
2225                    Element element, String path, ClassedModel classedModel,
2226                    Class<?> clazz) {
2227    
2228                    long classPK = getClassPK(classedModel);
2229    
2230                    ServiceContext serviceContext = new ServiceContext();
2231    
2232                    // Theme display
2233    
2234                    serviceContext.setCompanyId(getCompanyId());
2235                    serviceContext.setScopeGroupId(getScopeGroupId());
2236    
2237                    // Dates
2238    
2239                    if (classedModel instanceof AuditedModel) {
2240                            AuditedModel auditedModel = (AuditedModel)classedModel;
2241    
2242                            serviceContext.setUserId(getUserId(auditedModel));
2243                            serviceContext.setCreateDate(auditedModel.getCreateDate());
2244                            serviceContext.setModifiedDate(auditedModel.getModifiedDate());
2245                    }
2246    
2247                    // Permissions
2248    
2249                    if (!MapUtil.getBoolean(
2250                                    _parameterMap, PortletDataHandlerKeys.PERMISSIONS)) {
2251    
2252                            serviceContext.setAddGroupPermissions(true);
2253                            serviceContext.setAddGuestPermissions(true);
2254                    }
2255    
2256                    // Asset
2257    
2258                    if (isResourceMain(classedModel)) {
2259                            long[] assetCategoryIds = getAssetCategoryIds(clazz, classPK);
2260    
2261                            serviceContext.setAssetCategoryIds(assetCategoryIds);
2262    
2263                            String[] assetTagNames = getAssetTagNames(clazz, classPK);
2264    
2265                            serviceContext.setAssetTagNames(assetTagNames);
2266                    }
2267    
2268                    // Expando
2269    
2270                    String expandoPath = null;
2271    
2272                    if (element != null) {
2273                            expandoPath = element.attributeValue("expando-path");
2274                    }
2275                    else {
2276                            expandoPath = ExportImportPathUtil.getExpandoPath(path);
2277                    }
2278    
2279                    if (Validator.isNotNull(expandoPath)) {
2280                            try {
2281                                    Map<String, Serializable> expandoBridgeAttributes =
2282                                            (Map<String, Serializable>)getZipEntryAsObject(expandoPath);
2283    
2284                                    if (expandoBridgeAttributes != null) {
2285                                            serviceContext.setExpandoBridgeAttributes(
2286                                                    expandoBridgeAttributes);
2287                                    }
2288                            }
2289                            catch (Exception e) {
2290                                    if (_log.isDebugEnabled()) {
2291                                            _log.debug(e, e);
2292                                    }
2293                            }
2294                    }
2295    
2296                    return serviceContext;
2297            }
2298    
2299            protected Element doAddReferenceElement(
2300                    ClassedModel referrerClassedModel, Element element,
2301                    ClassedModel classedModel, String className, String binPath,
2302                    String referenceType, boolean missing) {
2303    
2304                    Element referenceElement = null;
2305    
2306                    if (missing) {
2307                            Element referencesElement = _missingReferencesElement;
2308    
2309                            referenceElement = referencesElement.addElement(
2310                                    "missing-reference");
2311                    }
2312                    else {
2313                            Element referencesElement = element.element("references");
2314    
2315                            if (referencesElement == null) {
2316                                    referencesElement = element.addElement("references");
2317                            }
2318    
2319                            referenceElement = referencesElement.addElement("reference");
2320                    }
2321    
2322                    referenceElement.addAttribute("class-name", className);
2323    
2324                    referenceElement.addAttribute(
2325                            "class-pk", String.valueOf(classedModel.getPrimaryKeyObj()));
2326    
2327                    if (missing) {
2328                            if (classedModel instanceof StagedModel) {
2329                                    referenceElement.addAttribute(
2330                                            "display-name",
2331                                            StagedModelDataHandlerUtil.getDisplayName(
2332                                                    (StagedModel)classedModel));
2333                            }
2334                            else {
2335                                    referenceElement.addAttribute(
2336                                            "display-name",
2337                                            String.valueOf(classedModel.getPrimaryKeyObj()));
2338                            }
2339                    }
2340    
2341                    if (classedModel instanceof StagedGroupedModel) {
2342                            StagedGroupedModel stagedGroupedModel =
2343                                    (StagedGroupedModel)classedModel;
2344    
2345                            referenceElement.addAttribute(
2346                                    "group-id", String.valueOf(stagedGroupedModel.getGroupId()));
2347                    }
2348    
2349                    if (Validator.isNotNull(binPath)) {
2350                            referenceElement.addAttribute("path", binPath);
2351                    }
2352    
2353                    referenceElement.addAttribute("type", referenceType);
2354    
2355                    if (missing) {
2356                            referenceElement.addAttribute(
2357                                    "referrer-class-name",
2358                                    ExportImportClassedModelUtil.getClassName(
2359                                            referrerClassedModel));
2360    
2361                            if (referrerClassedModel instanceof PortletModel) {
2362                                    Portlet portlet = (Portlet)referrerClassedModel;
2363    
2364                                    referenceElement.addAttribute(
2365                                            "referrer-display-name", portlet.getRootPortletId());
2366                            }
2367                            else if (referrerClassedModel instanceof StagedModel) {
2368                                    StagedModel referrerStagedModel =
2369                                            (StagedModel)referrerClassedModel;
2370    
2371                                    referenceElement.addAttribute(
2372                                            "referrer-display-name",
2373                                            StagedModelDataHandlerUtil.getDisplayName(
2374                                                    referrerStagedModel));
2375                            }
2376                    }
2377    
2378                    if (classedModel instanceof StagedModel) {
2379                            StagedModel stagedModel = (StagedModel)classedModel;
2380    
2381                            referenceElement.addAttribute("uuid", stagedModel.getUuid());
2382                            referenceElement.addAttribute(
2383                                    "company-id", String.valueOf(stagedModel.getCompanyId()));
2384    
2385                            Map<String, String> referenceAttributes =
2386                                    StagedModelDataHandlerUtil.getReferenceAttributes(
2387                                            this, stagedModel);
2388    
2389                            for (Map.Entry<String, String> referenceAttribute :
2390                                            referenceAttributes.entrySet()) {
2391    
2392                                    referenceElement.addAttribute(
2393                                            referenceAttribute.getKey(), referenceAttribute.getValue());
2394                            }
2395                    }
2396    
2397                    return referenceElement;
2398            }
2399    
2400            protected Map<Long, Set<String>> getActionIds(
2401                            long companyId, long[] roleIds, String className, long primKey,
2402                            List<String> actionIds)
2403                    throws PortalException, SystemException {
2404    
2405                    if (ResourceBlockLocalServiceUtil.isSupported(className)) {
2406                            return ResourceBlockPermissionLocalServiceUtil.
2407                                    getAvailableResourceBlockPermissionActionIds(
2408                                            roleIds, className, primKey, actionIds);
2409                    }
2410                    else {
2411                            return ResourcePermissionLocalServiceUtil.
2412                                    getAvailableResourcePermissionActionIds(
2413                                            companyId, className, ResourceConstants.SCOPE_INDIVIDUAL,
2414                                            String.valueOf(primKey), roleIds, actionIds);
2415                    }
2416            }
2417    
2418            protected long getClassPK(ClassedModel classedModel) {
2419                    if (classedModel instanceof ResourcedModel) {
2420                            ResourcedModel resourcedModel = (ResourcedModel)classedModel;
2421    
2422                            return resourcedModel.getResourcePrimKey();
2423                    }
2424                    else {
2425                            return (Long)classedModel.getPrimaryKeyObj();
2426                    }
2427            }
2428    
2429            protected Element getDataElement(
2430                    Element parentElement, String attribute, String value) {
2431    
2432                    if (parentElement == null) {
2433                            return null;
2434                    }
2435    
2436                    StringBundler sb = new StringBundler(6);
2437    
2438                    sb.append("staged-model");
2439                    sb.append("[@");
2440                    sb.append(attribute);
2441                    sb.append(StringPool.EQUAL);
2442                    sb.append(HtmlUtil.escapeXPathAttribute(value));
2443                    sb.append(StringPool.CLOSE_BRACKET);
2444    
2445                    XPath xPath = SAXReaderUtil.createXPath(sb.toString());
2446    
2447                    return (Element)xPath.selectSingleNode(parentElement);
2448            }
2449    
2450            protected String getExpandoPath(String path) {
2451                    return ExportImportPathUtil.getExpandoPath(path);
2452            }
2453    
2454            protected Element getExportDataGroupElement(String name) {
2455                    if (_exportDataRootElement == null) {
2456                            throw new IllegalStateException(
2457                                    "Root data element not initialized");
2458                    }
2459    
2460                    Element groupElement = _exportDataRootElement.element(name);
2461    
2462                    if (groupElement == null) {
2463                            groupElement = _exportDataRootElement.addElement(name);
2464                    }
2465    
2466                    return groupElement;
2467            }
2468    
2469            protected Element getImportDataGroupElement(String name) {
2470                    if (_importDataRootElement == null) {
2471                            throw new IllegalStateException(
2472                                    "Root data element not initialized");
2473                    }
2474    
2475                    if (Validator.isNull(name)) {
2476                            return SAXReaderUtil.createElement("EMPTY-ELEMENT");
2477                    }
2478    
2479                    Element groupElement = _importDataRootElement.element(name);
2480    
2481                    if (groupElement == null) {
2482                            return SAXReaderUtil.createElement("EMPTY-ELEMENT");
2483                    }
2484    
2485                    return groupElement;
2486            }
2487    
2488            protected Element getMissingReferenceElement(ClassedModel classedModel) {
2489                    StringBundler sb = new StringBundler(5);
2490    
2491                    sb.append("missing-reference[@class-name='");
2492                    sb.append(ExportImportClassedModelUtil.getClassName(classedModel));
2493                    sb.append("' and @class-pk='");
2494                    sb.append(String.valueOf(classedModel.getPrimaryKeyObj()));
2495                    sb.append("']");
2496    
2497                    XPath xPath = SAXReaderUtil.createXPath(sb.toString());
2498    
2499                    Node node = xPath.selectSingleNode(_missingReferencesElement);
2500    
2501                    return (Element)node;
2502            }
2503    
2504            protected String getPrimaryKeyString(Class<?> clazz, long classPK) {
2505                    return getPrimaryKeyString(clazz.getName(), String.valueOf(classPK));
2506            }
2507    
2508            protected String getPrimaryKeyString(Class<?> clazz, String primaryKey) {
2509                    return getPrimaryKeyString(clazz.getName(), primaryKey);
2510            }
2511    
2512            protected String getPrimaryKeyString(String className, long classPK) {
2513                    return getPrimaryKeyString(className, String.valueOf(classPK));
2514            }
2515    
2516            protected String getPrimaryKeyString(String className, String primaryKey) {
2517                    return className.concat(StringPool.POUND).concat(primaryKey);
2518            }
2519    
2520            protected List<Element> getReferenceDataElements(
2521                    List<Element> referenceElements, Class<?> clazz) {
2522    
2523                    List<Element> referenceDataElements = new ArrayList<Element>();
2524    
2525                    for (Element referenceElement : referenceElements) {
2526                            Element referenceDataElement = null;
2527    
2528                            String path = referenceElement.attributeValue("path");
2529    
2530                            if (Validator.isNotNull(path)) {
2531                                    referenceDataElement = getImportDataElement(
2532                                            clazz.getSimpleName(), "path", path);
2533                            }
2534                            else {
2535                                    String groupId = referenceElement.attributeValue("group-id");
2536                                    String uuid = referenceElement.attributeValue("uuid");
2537    
2538                                    StringBuilder sb = new StringBuilder(5);
2539    
2540                                    sb.append("staged-model[@uuid=");
2541                                    sb.append(HtmlUtil.escapeXPathAttribute(uuid));
2542    
2543                                    if (groupId != null) {
2544                                            sb.append(" and @group-id=");
2545                                            sb.append(HtmlUtil.escapeXPathAttribute(groupId));
2546                                    }
2547    
2548                                    sb.append(StringPool.CLOSE_BRACKET);
2549    
2550                                    XPath xPath = SAXReaderUtil.createXPath(sb.toString());
2551    
2552                                    Element groupElement = getImportDataGroupElement(
2553                                            clazz.getSimpleName());
2554    
2555                                    referenceDataElement = (Element)xPath.selectSingleNode(
2556                                            groupElement);
2557                            }
2558    
2559                            if (referenceDataElement == null) {
2560                                    continue;
2561                            }
2562    
2563                            referenceDataElements.add(referenceDataElement);
2564                    }
2565    
2566                    return referenceDataElements;
2567            }
2568    
2569            protected List<Element> getReferenceElements(
2570                    Element parentElement, Class<?> clazz, long groupId, String uuid,
2571                    long classPK, String referenceType) {
2572    
2573                    if (parentElement == null) {
2574                            return Collections.emptyList();
2575                    }
2576    
2577                    Element referencesElement = parentElement.element("references");
2578    
2579                    if (referencesElement == null) {
2580                            return Collections.emptyList();
2581                    }
2582    
2583                    StringBundler sb = new StringBundler(13);
2584    
2585                    sb.append("reference[@class-name=");
2586                    sb.append(HtmlUtil.escapeXPathAttribute(clazz.getName()));
2587    
2588                    if (groupId > 0) {
2589                            sb.append(" and @group-id='");
2590                            sb.append(groupId);
2591                            sb.append(StringPool.APOSTROPHE);
2592                    }
2593    
2594                    if (Validator.isNotNull(uuid)) {
2595                            sb.append(" and @uuid=");
2596                            sb.append(HtmlUtil.escapeXPathAttribute(uuid));
2597                    }
2598    
2599                    if (classPK > 0) {
2600                            sb.append(" and @class-pk='");
2601                            sb.append(classPK);
2602                            sb.append(StringPool.APOSTROPHE);
2603                    }
2604    
2605                    if (referenceType != null) {
2606                            sb.append(" and @type=");
2607                            sb.append(HtmlUtil.escapeXPathAttribute(referenceType));
2608                    }
2609    
2610                    sb.append(StringPool.CLOSE_BRACKET);
2611    
2612                    XPath xPath = SAXReaderUtil.createXPath(sb.toString());
2613    
2614                    List<Node> nodes = xPath.selectNodes(referencesElement);
2615    
2616                    return ListUtil.fromArray(nodes.toArray(new Element[nodes.size()]));
2617            }
2618    
2619            protected List<Element> getReferenceElements(
2620                    StagedModel parentStagedModel, Class<?> clazz, long classPK,
2621                    String referenceType) {
2622    
2623                    Element stagedModelElement = getImportDataStagedModelElement(
2624                            parentStagedModel);
2625    
2626                    return getReferenceElements(
2627                            stagedModelElement, clazz, 0, null, classPK, referenceType);
2628            }
2629    
2630            protected String getReferenceKey(ClassedModel classedModel) {
2631                    return getReferenceKey(
2632                            ExportImportClassedModelUtil.getClassName(classedModel),
2633                            String.valueOf(classedModel.getPrimaryKeyObj()));
2634            }
2635    
2636            protected String getReferenceKey(String className, String classPK) {
2637                    return className.concat(StringPool.POUND).concat(classPK);
2638            }
2639    
2640            protected long getUserId(AuditedModel auditedModel) {
2641                    try {
2642                            String userUuid = auditedModel.getUserUuid();
2643    
2644                            return getUserId(userUuid);
2645                    }
2646                    catch (SystemException se) {
2647                            if (_log.isErrorEnabled()) {
2648                                    _log.error(se, se);
2649                            }
2650                    }
2651    
2652                    return 0;
2653            }
2654    
2655            protected void initXStream() {
2656                    _xStream = new XStream();
2657    
2658                    // Aliases
2659    
2660                    _xStream.alias("BlogsEntry", BlogsEntryImpl.class);
2661                    _xStream.alias("BookmarksFolder", BookmarksFolderImpl.class);
2662                    _xStream.alias("BookmarksEntry", BookmarksEntryImpl.class);
2663                    _xStream.alias("DLFolder", DLFolderImpl.class);
2664                    _xStream.alias("DLFileEntry", DLFileEntryImpl.class);
2665                    _xStream.alias("DLFileShortcut", DLFileShortcutImpl.class);
2666                    _xStream.alias("JournalArticle", JournalArticleImpl.class);
2667                    _xStream.alias("JournalFeed", JournalFeedImpl.class);
2668                    _xStream.alias("Lock", LockImpl.class);
2669                    _xStream.alias("MBBan", MBBanImpl.class);
2670                    _xStream.alias("MBCategory", MBCategoryImpl.class);
2671                    _xStream.alias("MBMessage", MBMessageImpl.class);
2672                    _xStream.alias("MBThreadFlag", MBThreadFlagImpl.class);
2673                    _xStream.alias("PollsQuestion", PollsQuestionImpl.class);
2674                    _xStream.alias("PollsChoice", PollsChoiceImpl.class);
2675                    _xStream.alias("PollsVote", PollsVoteImpl.class);
2676                    _xStream.alias("RatingsEntry", RatingsEntryImpl.class);
2677                    _xStream.alias("WikiNode", WikiNodeImpl.class);
2678                    _xStream.alias("WikiPage", WikiPageImpl.class);
2679    
2680                    // Omit fields
2681    
2682                    _xStream.omitField(HashMap.class, "cache_bitmask");
2683    
2684                    // Register converters
2685    
2686                    _xStream.registerConverter(
2687                            new FolderConverter(), XStream.PRIORITY_VERY_HIGH);
2688                    _xStream.registerConverter(
2689                            new FileEntryConverter(), XStream.PRIORITY_VERY_HIGH);
2690                    _xStream.registerConverter(
2691                            new FileVersionConverter(), XStream.PRIORITY_VERY_HIGH);
2692    
2693                    if (!PropsValues.STAGING_XSTREAM_SECURITY_ENABLED) {
2694                            return;
2695                    }
2696    
2697                    // Permissions
2698    
2699                    // Wipe all of them
2700    
2701                    _xStream.addPermission(NoTypePermission.NONE);
2702    
2703                    // Add permissions
2704    
2705                    _xStream.addPermission(PrimitiveTypePermission.PRIMITIVES);
2706    
2707                    List<String> allowedTypes = new ArrayList<String>();
2708    
2709                    allowedTypes.addAll(
2710                            ListUtil.toList(_XSTREAM_DEFAULT_ALLOWED_CLASS_NAMES));
2711                    allowedTypes.addAll(
2712                            ListUtil.toList(PropsValues.STAGING_XSTREAM_CLASS_WHITELIST));
2713    
2714                    _xStream.allowTypes(allowedTypes.toArray(new String[0]));
2715    
2716                    _xStream.allowTypeHierarchy(AssetLink.class);
2717                    _xStream.allowTypeHierarchy(AssetTag.class);
2718                    _xStream.allowTypeHierarchy(List.class);
2719                    _xStream.allowTypeHierarchy(Lock.class);
2720                    _xStream.allowTypeHierarchy(Map.class);
2721                    _xStream.allowTypeHierarchy(OrgLabor.class);
2722                    _xStream.allowTypeHierarchy(RatingsEntry.class);
2723                    _xStream.allowTypeHierarchy(StagedModel.class);
2724    
2725                    _xStream.allowTypesByWildcard(
2726                            new String[] {
2727                                    "com.liferay.portal.model.*", "com.liferay.portal.model.impl.*",
2728                                    "com.thoughtworks.xstream.mapper.DynamicProxyMapper*"
2729                            }
2730                    );
2731            }
2732    
2733            protected boolean isResourceMain(ClassedModel classedModel) {
2734                    if (classedModel instanceof ResourcedModel) {
2735                            ResourcedModel resourcedModel = (ResourcedModel)classedModel;
2736    
2737                            return resourcedModel.isResourceMain();
2738                    }
2739    
2740                    return true;
2741            }
2742    
2743            private static final String[] _XSTREAM_DEFAULT_ALLOWED_CLASS_NAMES =
2744                    new String[] {
2745                            byte[].class.getName(), Date.class.getName(), Field.class.getName(),
2746                            Fields.class.getName(), InputStream.class.getName(),
2747                            Locale.class.getName(), String.class.getName(),
2748                            Time.class.getName(), Timestamp.class.getName()
2749                    };
2750    
2751            private static Log _log = LogFactoryUtil.getLog(
2752                    PortletDataContextImpl.class);
2753    
2754            private Map<String, long[]> _assetCategoryIdsMap =
2755                    new HashMap<String, long[]>();
2756            private Map<String, String[]> _assetCategoryUuidsMap =
2757                    new HashMap<String, String[]>();
2758            private Map<String, List<AssetLink>> _assetLinksMap =
2759                    new HashMap<String, List<AssetLink>>();
2760            private Map<String, String[]> _assetTagNamesMap =
2761                    new HashMap<String, String[]>();
2762            private Map<String, List<MBMessage>> _commentsMap =
2763                    new HashMap<String, List<MBMessage>>();
2764            private long _companyGroupId;
2765            private long _companyId;
2766            private String _dataStrategy;
2767            private Set<StagedModelType> _deletionSystemEventModelTypes =
2768                    new HashSet<StagedModelType>();
2769            private Date _endDate;
2770            private Map<String, List<ExpandoColumn>> _expandoColumnsMap =
2771                    new HashMap<String, List<ExpandoColumn>>();
2772            private transient Element _exportDataRootElement;
2773            private long _groupId;
2774            private transient Element _importDataRootElement;
2775            private transient Map<String, Lock> _locksMap = new HashMap<String, Lock>();
2776            private transient ManifestSummary _manifestSummary = new ManifestSummary();
2777            private transient Set<String> _missingReferences = new HashSet<String>();
2778            private transient Element _missingReferencesElement;
2779            private transient List<Layout> _newLayouts;
2780            private Map<String, Map<?, ?>> _newPrimaryKeysMaps =
2781                    new HashMap<String, Map<?, ?>>();
2782            private Set<String> _notUniquePerLayout = new HashSet<String>();
2783            private long _oldPlid;
2784            private Map<String, String[]> _parameterMap;
2785            private Map<String, List<KeyValuePair>> _permissionsMap =
2786                    new HashMap<String, List<KeyValuePair>>();
2787            private long _plid;
2788            private transient PortletDataContextListener _portletDataContextListener;
2789            private String _rootPortletId;
2790            private Set<String> _primaryKeys = new HashSet<String>();
2791            private boolean _privateLayout;
2792            private transient Map<String, List<RatingsEntry>> _ratingsEntriesMap =
2793                    new HashMap<String, List<RatingsEntry>>();
2794            private Set<String> _references = new HashSet<String>();
2795            private Set<String> _scopedPrimaryKeys = new HashSet<String>();
2796            private long _scopeGroupId;
2797            private String _scopeLayoutUuid;
2798            private String _scopeType;
2799            private long _sourceCompanyGroupId;
2800            private long _sourceCompanyId;
2801            private long _sourceGroupId;
2802            private long _sourceUserPersonalSiteGroupId;
2803            private Date _startDate;
2804            private transient UserIdStrategy _userIdStrategy;
2805            private long _userPersonalSiteGroupId;
2806            private transient XStream _xStream;
2807            private transient ZipReader _zipReader;
2808            private transient ZipWriter _zipWriter;
2809    
2810    }