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