001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.lar;
016    
017    import com.liferay.portal.NoSuchRoleException;
018    import com.liferay.portal.NoSuchTeamException;
019    import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.lar.PortletDataContext;
023    import com.liferay.portal.kernel.lar.PortletDataContextListener;
024    import com.liferay.portal.kernel.lar.PortletDataException;
025    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
026    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
027    import com.liferay.portal.kernel.lar.UserIdStrategy;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.KeyValuePair;
031    import com.liferay.portal.kernel.util.ListUtil;
032    import com.liferay.portal.kernel.util.MapUtil;
033    import com.liferay.portal.kernel.util.PrimitiveLongList;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.kernel.workflow.WorkflowConstants;
038    import com.liferay.portal.kernel.xml.Element;
039    import com.liferay.portal.kernel.zip.ZipReader;
040    import com.liferay.portal.kernel.zip.ZipWriter;
041    import com.liferay.portal.model.AttachedModel;
042    import com.liferay.portal.model.AuditedModel;
043    import com.liferay.portal.model.ClassedModel;
044    import com.liferay.portal.model.Group;
045    import com.liferay.portal.model.Layout;
046    import com.liferay.portal.model.Lock;
047    import com.liferay.portal.model.ResourceConstants;
048    import com.liferay.portal.model.ResourcedModel;
049    import com.liferay.portal.model.Role;
050    import com.liferay.portal.model.RoleConstants;
051    import com.liferay.portal.model.Team;
052    import com.liferay.portal.model.impl.LockImpl;
053    import com.liferay.portal.security.permission.ResourceActionsUtil;
054    import com.liferay.portal.service.GroupLocalServiceUtil;
055    import com.liferay.portal.service.LockLocalServiceUtil;
056    import com.liferay.portal.service.ResourceBlockLocalServiceUtil;
057    import com.liferay.portal.service.ResourceBlockPermissionLocalServiceUtil;
058    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
059    import com.liferay.portal.service.RoleLocalServiceUtil;
060    import com.liferay.portal.service.ServiceContext;
061    import com.liferay.portal.service.TeamLocalServiceUtil;
062    import com.liferay.portal.util.PortalUtil;
063    import com.liferay.portlet.asset.NoSuchEntryException;
064    import com.liferay.portlet.asset.model.AssetCategory;
065    import com.liferay.portlet.asset.model.AssetEntry;
066    import com.liferay.portlet.asset.model.AssetLink;
067    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
068    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
069    import com.liferay.portlet.asset.service.AssetLinkLocalServiceUtil;
070    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
071    import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
072    import com.liferay.portlet.bookmarks.model.impl.BookmarksEntryImpl;
073    import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
074    import com.liferay.portlet.calendar.model.impl.CalEventImpl;
075    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
076    import com.liferay.portlet.documentlibrary.model.impl.DLFileRankImpl;
077    import com.liferay.portlet.documentlibrary.model.impl.DLFileShortcutImpl;
078    import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
079    import com.liferay.portlet.expando.model.ExpandoBridge;
080    import com.liferay.portlet.expando.model.ExpandoColumn;
081    import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
082    import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
083    import com.liferay.portlet.journal.model.impl.JournalFeedImpl;
084    import com.liferay.portlet.journal.model.impl.JournalStructureImpl;
085    import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
086    import com.liferay.portlet.messageboards.model.MBDiscussion;
087    import com.liferay.portlet.messageboards.model.MBMessage;
088    import com.liferay.portlet.messageboards.model.MBThread;
089    import com.liferay.portlet.messageboards.model.impl.MBBanImpl;
090    import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
091    import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
092    import com.liferay.portlet.messageboards.model.impl.MBThreadFlagImpl;
093    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
094    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
095    import com.liferay.portlet.messageboards.service.persistence.MBDiscussionUtil;
096    import com.liferay.portlet.messageboards.service.persistence.MBMessageUtil;
097    import com.liferay.portlet.polls.model.impl.PollsChoiceImpl;
098    import com.liferay.portlet.polls.model.impl.PollsQuestionImpl;
099    import com.liferay.portlet.polls.model.impl.PollsVoteImpl;
100    import com.liferay.portlet.ratings.model.RatingsEntry;
101    import com.liferay.portlet.ratings.model.impl.RatingsEntryImpl;
102    import com.liferay.portlet.ratings.service.RatingsEntryLocalServiceUtil;
103    import com.liferay.portlet.wiki.model.impl.WikiNodeImpl;
104    import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
105    
106    import com.thoughtworks.xstream.XStream;
107    
108    import java.io.IOException;
109    import java.io.InputStream;
110    import java.io.Serializable;
111    
112    import java.util.ArrayList;
113    import java.util.Collections;
114    import java.util.Date;
115    import java.util.HashMap;
116    import java.util.HashSet;
117    import java.util.List;
118    import java.util.Map;
119    import java.util.Set;
120    
121    import jodd.bean.BeanUtil;
122    
123    /**
124     * <p>
125     * Holds context information that is used during exporting and importing portlet
126     * data.
127     * </p>
128     *
129     * @author Brian Wing Shun Chan
130     * @author Raymond Augé
131     * @author Bruno Farache
132     * @author Alex Chow
133     */
134    public class PortletDataContextImpl implements PortletDataContext {
135    
136            public PortletDataContextImpl(
137                            long companyId, long groupId, Map<String, String[]> parameterMap,
138                            Set<String> primaryKeys, Date startDate, Date endDate,
139                            ZipWriter zipWriter)
140                    throws PortletDataException {
141    
142                    validateDateRange(startDate, endDate);
143    
144                    _companyId = companyId;
145                    _groupId = groupId;
146                    _scopeGroupId = groupId;
147                    _parameterMap = parameterMap;
148                    _primaryKeys = primaryKeys;
149                    _dataStrategy = null;
150                    _userIdStrategy = null;
151                    _startDate = startDate;
152                    _endDate = endDate;
153                    _zipReader = null;
154                    _zipWriter = zipWriter;
155    
156                    initXStream();
157            }
158    
159            public PortletDataContextImpl(
160                    long companyId, long groupId, Map<String, String[]> parameterMap,
161                    Set<String> primaryKeys, UserIdStrategy userIdStrategy,
162                    ZipReader zipReader) {
163    
164                    _companyId = companyId;
165                    _groupId = groupId;
166                    _scopeGroupId = groupId;
167                    _parameterMap = parameterMap;
168                    _primaryKeys = primaryKeys;
169                    _dataStrategy = MapUtil.getString(
170                            parameterMap, PortletDataHandlerKeys.DATA_STRATEGY,
171                            PortletDataHandlerKeys.DATA_STRATEGY_MIRROR);
172                    _userIdStrategy = userIdStrategy;
173                    _zipReader = zipReader;
174                    _zipWriter = null;
175    
176                    initXStream();
177            }
178    
179            public void addAssetCategories(Class<?> clazz, long classPK)
180                    throws SystemException {
181    
182                    List<AssetCategory> assetCategories =
183                            AssetCategoryLocalServiceUtil.getCategories(
184                                    clazz.getName(), classPK);
185    
186                    _assetCategoryUuidsMap.put(
187                            getPrimaryKeyString(clazz, classPK),
188                            StringUtil.split(
189                                    ListUtil.toString(
190                                            assetCategories, AssetCategory.UUID_ACCESSOR)));
191                    _assetCategoryIdsMap.put(
192                            getPrimaryKeyString(clazz, classPK),
193                            StringUtil.split(
194                                    ListUtil.toString(
195                                            assetCategories, AssetCategory.CATEGORY_ID_ACCESSOR), 0L));
196            }
197    
198            public void addAssetCategories(
199                    String className, long classPK, long[] assetCategoryIds) {
200    
201                    _assetCategoryIdsMap.put(
202                            getPrimaryKeyString(className, classPK), assetCategoryIds);
203            }
204    
205            public void addAssetLinks(Class<?> clazz, long classPK)
206                    throws PortalException, SystemException {
207    
208                    AssetEntry assetEntry = null;
209    
210                    try {
211                            assetEntry = AssetEntryLocalServiceUtil.getEntry(
212                                    clazz.getName(), classPK);
213                    }
214                    catch (NoSuchEntryException nsee) {
215                            return;
216                    }
217    
218                    List<AssetLink> directAssetLinks =
219                            AssetLinkLocalServiceUtil.getDirectLinks(assetEntry.getEntryId());
220    
221                    if (directAssetLinks.isEmpty()) {
222                            return;
223                    }
224    
225                    Map<Integer, List<AssetLink>> assetLinksMap =
226                            new HashMap<Integer, List<AssetLink>>();
227    
228                    for (AssetLink assetLink : directAssetLinks) {
229                            List<AssetLink> assetLinks = assetLinksMap.get(assetLink.getType());
230    
231                            if (assetLinks == null) {
232                                    assetLinks = new ArrayList<AssetLink>();
233    
234                                    assetLinksMap.put(assetLink.getType(), assetLinks);
235                            }
236    
237                            assetLinks.add(assetLink);
238                    }
239    
240                    for (Map.Entry<Integer, List<AssetLink>> entry :
241                                    assetLinksMap.entrySet()) {
242    
243                            int assetLinkType = entry.getKey();
244                            List<AssetLink> assetLinks = entry.getValue();
245    
246                            List<String> assetLinkUuids = new ArrayList<String>(
247                                    directAssetLinks.size());
248    
249                            for (AssetLink assetLink : assetLinks) {
250                                    try {
251                                            AssetEntry assetLinkEntry =
252                                                    AssetEntryLocalServiceUtil.getEntry(
253                                                            assetLink.getEntryId2());
254    
255                                            assetLinkUuids.add(assetLinkEntry.getClassUuid());
256                                    }
257                                    catch (NoSuchEntryException nsee) {
258                                    }
259                            }
260    
261                            _assetLinkUuidsMap.put(
262                                    getPrimaryKeyString(
263                                            assetEntry.getClassUuid(), String.valueOf(assetLinkType)),
264                                    assetLinkUuids.toArray(new String[assetLinkUuids.size()]));
265                    }
266            }
267    
268            public void addAssetTags(Class<?> clazz, long classPK)
269                    throws SystemException {
270    
271                    String[] tagNames = AssetTagLocalServiceUtil.getTagNames(
272                            clazz.getName(), classPK);
273    
274                    _assetTagNamesMap.put(getPrimaryKeyString(clazz, classPK), tagNames);
275            }
276    
277            public void addAssetTags(
278                    String className, long classPK, String[] assetTagNames) {
279    
280                    _assetTagNamesMap.put(
281                            getPrimaryKeyString(className, classPK), assetTagNames);
282            }
283    
284            public void addClassedModel(
285                            Element element, String path, ClassedModel classedModel,
286                            String namespace)
287                    throws PortalException, SystemException {
288    
289                    element.addAttribute("path", path);
290    
291                    if (classedModel instanceof AttachedModel) {
292                            AttachedModel attachedModel = (AttachedModel)classedModel;
293    
294                            element.addAttribute("class-name", attachedModel.getClassName());
295                    }
296                    else if (BeanUtil.hasProperty(classedModel, "className")) {
297                            String className = BeanPropertiesUtil.getStringSilent(
298                                    classedModel, "className");
299    
300                            if (className != null) {
301                                    element.addAttribute("class-name", className);
302                            }
303                    }
304    
305                    if (classedModel instanceof AuditedModel) {
306                            AuditedModel auditedModel = (AuditedModel)classedModel;
307    
308                            auditedModel.setUserUuid(auditedModel.getUserUuid());
309                    }
310    
311                    if (isResourceMain(classedModel)) {
312                            Class<?> clazz = classedModel.getModelClass();
313                            long classPK = getClassPK(classedModel);
314    
315                            addAssetLinks(clazz, classPK);
316                            addExpando(element, path, classedModel);
317                            addLocks(clazz, String.valueOf(classPK));
318                            addPermissions(clazz, classPK);
319    
320                            boolean portletMetadataAll = getBooleanParameter(
321                                    namespace, PortletDataHandlerKeys.PORTLET_METADATA_ALL);
322    
323                            if (portletMetadataAll ||
324                                    getBooleanParameter(namespace, "categories")) {
325    
326                                    addAssetCategories(clazz, classPK);
327                            }
328    
329                            if (portletMetadataAll ||
330                                    getBooleanParameter(namespace, "comments")) {
331    
332                                    addComments(clazz, classPK);
333                            }
334    
335                            if (portletMetadataAll ||
336                                    getBooleanParameter(namespace, "ratings")) {
337    
338                                    addRatingsEntries(clazz, classPK);
339                            }
340    
341                            if (portletMetadataAll || getBooleanParameter(namespace, "tags")) {
342                                    addAssetTags(clazz, classPK);
343                            }
344                    }
345    
346                    addZipEntry(path, classedModel);
347            }
348    
349            public void addComments(Class<?> clazz, long classPK)
350                    throws SystemException {
351    
352                    long classNameId = PortalUtil.getClassNameId(clazz);
353    
354                    MBDiscussion discussion = MBDiscussionUtil.fetchByC_C(
355                            classNameId, classPK);
356    
357                    if (discussion == null) {
358                            return;
359                    }
360    
361                    List<MBMessage> messages = MBMessageLocalServiceUtil.getThreadMessages(
362                            discussion.getThreadId(), WorkflowConstants.STATUS_APPROVED);
363    
364                    if (messages.isEmpty()) {
365                            return;
366                    }
367    
368                    MBMessage firstMessage = messages.get(0);
369    
370                    if ((messages.size() == 1) && firstMessage.isRoot()) {
371                            return;
372                    }
373    
374                    for (MBMessage message : messages) {
375                            message.setUserUuid(message.getUserUuid());
376    
377                            addRatingsEntries(MBDiscussion.class, message.getPrimaryKey());
378                    }
379    
380                    _commentsMap.put(getPrimaryKeyString(clazz, classPK), messages);
381            }
382    
383            public void addComments(
384                    String className, long classPK, List<MBMessage> messages) {
385    
386                    _commentsMap.put(getPrimaryKeyString(className, classPK), messages);
387            }
388    
389            public void addExpando(
390                            Element element, String path, ClassedModel classedModel)
391                    throws PortalException, SystemException {
392    
393                    Class<?> clazz = classedModel.getModelClass();
394    
395                    String className = clazz.getName();
396    
397                    if (!_expandoColumnsMap.containsKey(className)) {
398                            List<ExpandoColumn> expandoColumns =
399                                    ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
400                                            _companyId, className);
401    
402                            for (ExpandoColumn expandoColumn : expandoColumns) {
403                                    addPermissions(
404                                            ExpandoColumn.class, expandoColumn.getColumnId());
405                            }
406    
407                            _expandoColumnsMap.put(className, expandoColumns);
408                    }
409    
410                    ExpandoBridge expandoBridge = classedModel.getExpandoBridge();
411    
412                    Map<String, Serializable> expandoBridgeAttributes =
413                            expandoBridge.getAttributes();
414    
415                    if (!expandoBridgeAttributes.isEmpty()) {
416                            String expandoPath = getExpandoPath(path);
417    
418                            element.addAttribute("expando-path", expandoPath);
419    
420                            addZipEntry(expandoPath, expandoBridgeAttributes);
421                    }
422            }
423    
424            public void addLocks(Class<?> clazz, String key)
425                    throws PortalException, SystemException {
426    
427                    if (!_locksMap.containsKey(getPrimaryKeyString(clazz, key)) &&
428                            LockLocalServiceUtil.isLocked(clazz.getName(), key)) {
429    
430                            Lock lock = LockLocalServiceUtil.getLock(clazz.getName(), key);
431    
432                            addLocks(clazz.getName(), key, lock);
433                    }
434            }
435    
436            public void addLocks(String className, String key, Lock lock) {
437                    _locksMap.put(getPrimaryKeyString(className, key), lock);
438            }
439    
440            public void addPermissions(Class<?> clazz, long classPK)
441                    throws PortalException, SystemException {
442    
443                    addPermissions(clazz.getName(), classPK);
444            }
445    
446            public void addPermissions(String resourceName, long resourcePK)
447                    throws PortalException, SystemException {
448    
449                    if (!MapUtil.getBoolean(
450                                    _parameterMap, PortletDataHandlerKeys.PERMISSIONS)) {
451    
452                            return;
453                    }
454    
455                    List<KeyValuePair> permissions = new ArrayList<KeyValuePair>();
456    
457                    Group group = GroupLocalServiceUtil.getGroup(_groupId);
458    
459                    List<Role> roles = RoleLocalServiceUtil.getRoles(_companyId);
460    
461                    PrimitiveLongList roleIds = new PrimitiveLongList(roles.size());
462                    Map<Long, String> roleIdsToNames = new HashMap<Long, String>();
463    
464                    for (Role role : roles) {
465                            int type = role.getType();
466    
467                            if ((type == RoleConstants.TYPE_REGULAR) ||
468                                    ((type == RoleConstants.TYPE_ORGANIZATION) &&
469                                     group.isOrganization()) ||
470                                    ((type == RoleConstants.TYPE_SITE) &&
471                                     (group.isLayoutSetPrototype() || group.isSite()))) {
472    
473                                    String name = role.getName();
474    
475                                    roleIds.add(role.getRoleId());
476                                    roleIdsToNames.put(role.getRoleId(), name);
477                            }
478                            else if ((type == RoleConstants.TYPE_PROVIDER) && role.isTeam()) {
479                                    Team team = TeamLocalServiceUtil.getTeam(role.getClassPK());
480    
481                                    if (team.getGroupId() == _groupId) {
482                                            String name =
483                                                    PermissionExporter.ROLE_TEAM_PREFIX + team.getName();
484    
485                                            roleIds.add(role.getRoleId());
486                                            roleIdsToNames.put(role.getRoleId(), name);
487                                    }
488                            }
489                    }
490    
491                    List<String> actionIds = ResourceActionsUtil.getModelResourceActions(
492                            resourceName);
493    
494                    Map<Long, Set<String>> roleIdsToActionIds = getActionIds(
495                            _companyId, roleIds.getArray(), resourceName, resourcePK,
496                            actionIds);
497    
498                    for (Map.Entry<Long, String> entry : roleIdsToNames.entrySet()) {
499                            long roleId = entry.getKey();
500                            String name = entry.getValue();
501    
502                            Set<String> availableActionIds = roleIdsToActionIds.get(roleId);
503    
504                            if (availableActionIds == null) {
505                                    availableActionIds = Collections.emptySet();
506                            }
507    
508                            KeyValuePair permission = new KeyValuePair(
509                                    name, StringUtil.merge(availableActionIds));
510    
511                            permissions.add(permission);
512                    }
513    
514                    _permissionsMap.put(
515                            getPrimaryKeyString(resourceName, resourcePK), permissions);
516            }
517    
518            public void addPermissions(
519                    String resourceName, long resourcePK, List<KeyValuePair> permissions) {
520    
521                    _permissionsMap.put(
522                            getPrimaryKeyString(resourceName, resourcePK), permissions);
523            }
524    
525            public boolean addPrimaryKey(Class<?> clazz, String primaryKey) {
526                    boolean value = hasPrimaryKey(clazz, primaryKey);
527    
528                    if (!value) {
529                            _primaryKeys.add(getPrimaryKeyString(clazz, primaryKey));
530                    }
531    
532                    return value;
533            }
534    
535            public void addRatingsEntries(Class<?> clazz, long classPK)
536                    throws SystemException {
537    
538                    List<RatingsEntry> ratingsEntries =
539                            RatingsEntryLocalServiceUtil.getEntries(clazz.getName(), classPK);
540    
541                    if (ratingsEntries.size() == 0) {
542                            return;
543                    }
544    
545                    for (RatingsEntry entry : ratingsEntries) {
546                            entry.setUserUuid(entry.getUserUuid());
547                    }
548    
549                    _ratingsEntriesMap.put(
550                            getPrimaryKeyString(clazz, classPK), ratingsEntries);
551            }
552    
553            public void addRatingsEntries(
554                    String className, long classPK, List<RatingsEntry> ratingsEntries) {
555    
556                    _ratingsEntriesMap.put(
557                            getPrimaryKeyString(className, classPK), ratingsEntries);
558            }
559    
560            public void addZipEntry(String path, byte[] bytes) throws SystemException {
561                    if (_portletDataContextListener != null) {
562                            _portletDataContextListener.onAddZipEntry(path);
563                    }
564    
565                    try {
566                            ZipWriter zipWriter = getZipWriter();
567    
568                            zipWriter.addEntry(path, bytes);
569                    }
570                    catch (IOException ioe) {
571                            throw new SystemException(ioe);
572                    }
573            }
574    
575            public void addZipEntry(String path, InputStream is)
576                    throws SystemException {
577    
578                    if (_portletDataContextListener != null) {
579                            _portletDataContextListener.onAddZipEntry(path);
580                    }
581    
582                    try {
583                            ZipWriter zipWriter = getZipWriter();
584    
585                            zipWriter.addEntry(path, is);
586                    }
587                    catch (IOException ioe) {
588                            throw new SystemException(ioe);
589                    }
590            }
591    
592            public void addZipEntry(String path, Object object) throws SystemException {
593                    addZipEntry(path, toXML(object));
594            }
595    
596            public void addZipEntry(String path, String s) throws SystemException {
597                    if (_portletDataContextListener != null) {
598                            _portletDataContextListener.onAddZipEntry(path);
599                    }
600    
601                    try {
602                            ZipWriter zipWriter = getZipWriter();
603    
604                            zipWriter.addEntry(path, s);
605                    }
606                    catch (IOException ioe) {
607                            throw new SystemException(ioe);
608                    }
609            }
610    
611            public void addZipEntry(String path, StringBuilder sb)
612                    throws SystemException {
613    
614                    if (_portletDataContextListener != null) {
615                            _portletDataContextListener.onAddZipEntry(path);
616                    }
617    
618                    try {
619                            ZipWriter zipWriter = getZipWriter();
620    
621                            zipWriter.addEntry(path, sb);
622                    }
623                    catch (IOException ioe) {
624                            throw new SystemException(ioe);
625                    }
626            }
627    
628            public ServiceContext createServiceContext(
629                    Element element, ClassedModel classedModel, String namespace) {
630    
631                    return createServiceContext(element, null, classedModel, namespace);
632            }
633    
634            public ServiceContext createServiceContext(
635                    String path, ClassedModel classedModel, String namespace) {
636    
637                    return createServiceContext(null, path, classedModel, namespace);
638            }
639    
640            public Object fromXML(byte[] bytes) {
641                    if ((bytes == null) || (bytes.length == 0)) {
642                            return null;
643                    }
644    
645                    return _xStream.fromXML(new String(bytes));
646            }
647    
648            public Object fromXML(String xml) {
649                    if (Validator.isNull(xml)) {
650                            return null;
651                    }
652    
653                    return _xStream.fromXML(xml);
654            }
655    
656            public long[] getAssetCategoryIds(Class<?> clazz, long classPK) {
657                    return _assetCategoryIdsMap.get(getPrimaryKeyString(clazz, classPK));
658            }
659    
660            public Map<String, long[]> getAssetCategoryIdsMap() {
661                    return _assetCategoryIdsMap;
662            }
663    
664            public Map<String, String[]> getAssetCategoryUuidsMap() {
665                    return _assetCategoryUuidsMap;
666            }
667    
668            public Map<String, String[]> getAssetLinkUuidsMap() {
669                    return _assetLinkUuidsMap;
670            }
671    
672            public String[] getAssetTagNames(Class<?> clazz, long classPK) {
673                    return _assetTagNamesMap.get(getPrimaryKeyString(clazz, classPK));
674            }
675    
676            public String[] getAssetTagNames(String className, long classPK) {
677                    return _assetTagNamesMap.get(getPrimaryKeyString(className, classPK));
678            }
679    
680            public Map<String, String[]> getAssetTagNamesMap() {
681                    return _assetTagNamesMap;
682            }
683    
684            public boolean getBooleanParameter(String namespace, String name) {
685                    boolean defaultValue = MapUtil.getBoolean(
686                            getParameterMap(),
687                            PortletDataHandlerKeys.PORTLET_DATA_CONTROL_DEFAULT, true);
688    
689                    return MapUtil.getBoolean(
690                            getParameterMap(),
691                            PortletDataHandlerControl.getNamespacedControlName(namespace, name),
692                            defaultValue);
693            }
694    
695            public ClassLoader getClassLoader() {
696                    return _xStream.getClassLoader();
697            }
698    
699            public Map<String, List<MBMessage>> getComments() {
700                    return _commentsMap;
701            }
702    
703            public long getCompanyId() {
704                    return _companyId;
705            }
706    
707            public String getDataStrategy() {
708                    return _dataStrategy;
709            }
710    
711            public Date getEndDate() {
712                    return _endDate;
713            }
714    
715            public Map<String, List<ExpandoColumn>> getExpandoColumns() {
716                    return _expandoColumnsMap;
717            }
718    
719            public long getGroupId() {
720                    return _groupId;
721            }
722    
723            public String getLayoutPath(long layoutId) {
724                    return getRootPath() + ROOT_PATH_LAYOUTS + layoutId;
725            }
726    
727            public Map<String, Lock> getLocks() {
728                    return _locksMap;
729            }
730    
731            public Map<?, ?> getNewPrimaryKeysMap(Class<?> clazz) {
732                    return getNewPrimaryKeysMap(clazz.getName());
733            }
734    
735            public Map<?, ?> getNewPrimaryKeysMap(String className) {
736                    Map<?, ?> map = _newPrimaryKeysMaps.get(className);
737    
738                    if (map == null) {
739                            map = new HashMap<Object, Object>();
740    
741                            _newPrimaryKeysMaps.put(className, map);
742                    }
743    
744                    return map;
745            }
746    
747            public long getOldPlid() {
748                    return _oldPlid;
749            }
750    
751            public Map<String, String[]> getParameterMap() {
752                    return _parameterMap;
753            }
754    
755            public Map<String, List<KeyValuePair>> getPermissions() {
756                    return _permissionsMap;
757            }
758    
759            public long getPlid() {
760                    return _plid;
761            }
762    
763            public String getPortletPath(String portletId) {
764                    return getRootPath() + ROOT_PATH_PORTLETS + portletId;
765            }
766    
767            public Set<String> getPrimaryKeys() {
768                    return _primaryKeys;
769            }
770    
771            public Map<String, List<RatingsEntry>> getRatingsEntries() {
772                    return _ratingsEntriesMap;
773            }
774    
775            public String getRootPath() {
776                    return ROOT_PATH_GROUPS + getScopeGroupId();
777            }
778    
779            public long getScopeGroupId() {
780                    return _scopeGroupId;
781            }
782    
783            public String getScopeLayoutUuid() {
784                    return _scopeLayoutUuid;
785            }
786    
787            public String getScopeType() {
788                    return _scopeType;
789            }
790    
791            public long getSourceGroupId() {
792                    return _sourceGroupId;
793            }
794    
795            public String getSourceLayoutPath(long layoutId) {
796                    return getSourceRootPath() + ROOT_PATH_LAYOUTS + layoutId;
797            }
798    
799            public String getSourcePortletPath(String portletId) {
800                    return getSourceRootPath() + ROOT_PATH_PORTLETS + portletId;
801            }
802    
803            public String getSourceRootPath() {
804                    return ROOT_PATH_GROUPS + getSourceGroupId();
805            }
806    
807            public Date getStartDate() {
808                    return _startDate;
809            }
810    
811            public long getUserId(String userUuid) {
812                    return _userIdStrategy.getUserId(userUuid);
813            }
814    
815            public UserIdStrategy getUserIdStrategy() {
816                    return _userIdStrategy;
817            }
818    
819            public List<String> getZipEntries() {
820                    return getZipReader().getEntries();
821            }
822    
823            public byte[] getZipEntryAsByteArray(String path) {
824                    if (!Validator.isFilePath(path, false)) {
825                            return null;
826                    }
827    
828                    if (_portletDataContextListener != null) {
829                            _portletDataContextListener.onGetZipEntry(path);
830                    }
831    
832                    return getZipReader().getEntryAsByteArray(path);
833            }
834    
835            public InputStream getZipEntryAsInputStream(String path) {
836                    if (!Validator.isFilePath(path, false)) {
837                            return null;
838                    }
839    
840                    if (_portletDataContextListener != null) {
841                            _portletDataContextListener.onGetZipEntry(path);
842                    }
843    
844                    return getZipReader().getEntryAsInputStream(path);
845            }
846    
847            public Object getZipEntryAsObject(Element element, String path) {
848                    Object object = fromXML(getZipEntryAsString(path));
849    
850                    Element classNameElement = element.element("class-name");
851    
852                    if (classNameElement != null) {
853                            BeanPropertiesUtil.setProperty(
854                                    object, "className", classNameElement.getText());
855                    }
856    
857                    return object;
858            }
859    
860            public Object getZipEntryAsObject(String path) {
861                    return fromXML(getZipEntryAsString(path));
862            }
863    
864            public String getZipEntryAsString(String path) {
865                    if (!Validator.isFilePath(path, false)) {
866                            return null;
867                    }
868    
869                    if (_portletDataContextListener != null) {
870                            _portletDataContextListener.onGetZipEntry(path);
871                    }
872    
873                    return getZipReader().getEntryAsString(path);
874            }
875    
876            public List<String> getZipFolderEntries() {
877                    return getZipFolderEntries(StringPool.SLASH);
878            }
879    
880            public List<String> getZipFolderEntries(String path) {
881                    if (!Validator.isFilePath(path, false)) {
882                            return null;
883                    }
884    
885                    return getZipReader().getFolderEntries(path);
886            }
887    
888            public ZipReader getZipReader() {
889                    return _zipReader;
890            }
891    
892            public ZipWriter getZipWriter() {
893                    return _zipWriter;
894            }
895    
896            public boolean hasDateRange() {
897                    if (_startDate != null) {
898                            return true;
899                    }
900                    else {
901                            return false;
902                    }
903            }
904    
905            public boolean hasNotUniquePerLayout(String dataKey) {
906                    return _notUniquePerLayout.contains(dataKey);
907            }
908    
909            public boolean hasPrimaryKey(Class<?> clazz, String primaryKey) {
910                    return _primaryKeys.contains(getPrimaryKeyString(clazz, primaryKey));
911            }
912    
913            public void importClassedModel(
914                            ClassedModel classedModel, ClassedModel newClassedModel,
915                            String namespace)
916                    throws PortalException, SystemException {
917    
918                    if (!isResourceMain(classedModel)) {
919                            return;
920                    }
921    
922                    Class<?> clazz = classedModel.getModelClass();
923                    long classPK = getClassPK(classedModel);
924    
925                    long newClassPK = getClassPK(newClassedModel);
926    
927                    Map<Long, Long> newPrimaryKeysMap =
928                            (Map<Long, Long>)getNewPrimaryKeysMap(clazz);
929    
930                    newPrimaryKeysMap.put(classPK, newClassPK);
931    
932                    importLocks(clazz, String.valueOf(classPK), String.valueOf(newClassPK));
933                    importPermissions(clazz, classPK, newClassPK);
934    
935                    boolean portletMetadataAll = getBooleanParameter(
936                            namespace, PortletDataHandlerKeys.PORTLET_METADATA_ALL);
937    
938                    if (portletMetadataAll || getBooleanParameter(namespace, "comments")) {
939                            importComments(clazz, classPK, newClassPK, getScopeGroupId());
940                    }
941    
942                    if (portletMetadataAll || getBooleanParameter(namespace, "ratings")) {
943                            importRatingsEntries(clazz, classPK, newClassPK);
944                    }
945            }
946    
947            public void importComments(
948                            Class<?> clazz, long classPK, long newClassPK, long groupId)
949                    throws PortalException, SystemException {
950    
951                    Map<Long, Long> messagePKs = new HashMap<Long, Long>();
952                    Map<Long, Long> threadPKs = new HashMap<Long, Long>();
953    
954                    List<MBMessage> messages = _commentsMap.get(
955                            getPrimaryKeyString(clazz, classPK));
956    
957                    if (messages == null) {
958                            return;
959                    }
960    
961                    MBMessage firstMessage = messages.get(0);
962    
963                    if ((messages.size() == 1) && firstMessage.isRoot()) {
964                            return;
965                    }
966    
967                    long classNameId = PortalUtil.getClassNameId(clazz);
968    
969                    MBDiscussion discussion = MBDiscussionUtil.fetchByC_C(
970                            classNameId, newClassPK);
971    
972                    for (MBMessage message : messages) {
973                            long userId = getUserId(message.getUserUuid());
974                            long parentMessageId = MapUtil.getLong(
975                                    messagePKs, message.getParentMessageId(),
976                                    message.getParentMessageId());
977                            long threadId = MapUtil.getLong(
978                                    threadPKs, message.getThreadId(), message.getThreadId());
979    
980                            if (message.isRoot()) {
981                                    if (discussion != null) {
982                                            MBThread thread = MBThreadLocalServiceUtil.getThread(
983                                                    discussion.getThreadId());
984    
985                                            long rootMessageId = thread.getRootMessageId();
986    
987                                            messagePKs.put(message.getMessageId(), rootMessageId);
988                                            threadPKs.put(message.getThreadId(), thread.getThreadId());
989                                    }
990                                    else if (clazz == Layout.class) {
991                                            MBMessage importedMessage =
992                                                    MBMessageLocalServiceUtil.addDiscussionMessage(
993                                                            userId, message.getUserName(), groupId,
994                                                            clazz.getName(), newClassPK,
995                                                            WorkflowConstants.ACTION_PUBLISH);
996    
997                                            messagePKs.put(
998                                                    message.getMessageId(), importedMessage.getMessageId());
999                                            threadPKs.put(
1000                                                    message.getThreadId(), importedMessage.getThreadId());
1001                                    }
1002                            }
1003                            else {
1004                                    ServiceContext serviceContext = new ServiceContext();
1005    
1006                                    serviceContext.setCreateDate(message.getCreateDate());
1007                                    serviceContext.setModifiedDate(message.getModifiedDate());
1008                                    serviceContext.setScopeGroupId(groupId);
1009    
1010                                    MBMessage importedMessage = null;
1011    
1012                                    if (_dataStrategy.equals(
1013                                                    PortletDataHandlerKeys.DATA_STRATEGY_MIRROR) ||
1014                                            _dataStrategy.equals(
1015                                                    PortletDataHandlerKeys.
1016                                                            DATA_STRATEGY_MIRROR_OVERWRITE)) {
1017    
1018                                            MBMessage existingMessage = MBMessageUtil.fetchByUUID_G(
1019                                                    message.getUuid(), groupId);
1020    
1021                                            if (existingMessage == null) {
1022                                                    serviceContext.setUuid(message.getUuid());
1023    
1024                                                    importedMessage =
1025                                                            MBMessageLocalServiceUtil.addDiscussionMessage(
1026                                                                    userId, message.getUserName(), groupId,
1027                                                                    clazz.getName(), newClassPK, threadId,
1028                                                                    parentMessageId, message.getSubject(),
1029                                                                    message.getBody(), serviceContext);
1030                                            }
1031                                            else {
1032                                                    serviceContext.setWorkflowAction(
1033                                                            WorkflowConstants.ACTION_PUBLISH);
1034    
1035                                                    importedMessage =
1036                                                            MBMessageLocalServiceUtil.updateDiscussionMessage(
1037                                                                    userId, existingMessage.getMessageId(),
1038                                                                    clazz.getName(), newClassPK,
1039                                                                    message.getSubject(), message.getBody(),
1040                                                                    serviceContext);
1041                                            }
1042                                    }
1043                                    else {
1044                                            importedMessage =
1045                                                    MBMessageLocalServiceUtil.addDiscussionMessage(
1046                                                            userId, message.getUserName(), groupId,
1047                                                            clazz.getName(), newClassPK, threadId,
1048                                                            parentMessageId, message.getSubject(),
1049                                                            message.getBody(), serviceContext);
1050                                    }
1051    
1052                                    messagePKs.put(
1053                                            message.getMessageId(), importedMessage.getMessageId());
1054                                    threadPKs.put(
1055                                            message.getThreadId(), importedMessage.getThreadId());
1056                            }
1057    
1058                            importRatingsEntries(
1059                                    MBDiscussion.class, message.getPrimaryKey(),
1060                                    messagePKs.get(message.getPrimaryKey()));
1061                    }
1062            }
1063    
1064            public void importLocks(Class<?> clazz, String key, String newKey)
1065                    throws PortalException, SystemException {
1066    
1067                    Lock lock = _locksMap.get(getPrimaryKeyString(clazz, key));
1068    
1069                    if (lock == null) {
1070                            return;
1071                    }
1072    
1073                    long userId = getUserId(lock.getUserUuid());
1074    
1075                    long expirationTime = 0;
1076    
1077                    if (lock.getExpirationDate() != null) {
1078                            Date expirationDate = lock.getExpirationDate();
1079    
1080                            expirationTime = expirationDate.getTime();
1081                    }
1082    
1083                    LockLocalServiceUtil.lock(
1084                            userId, clazz.getName(), newKey, lock.getOwner(),
1085                            lock.isInheritable(), expirationTime);
1086            }
1087    
1088            public void importPermissions(Class<?> clazz, long classPK, long newClassPK)
1089                    throws PortalException, SystemException {
1090    
1091                    importPermissions(clazz.getName(), classPK, newClassPK);
1092            }
1093    
1094            public void importPermissions(
1095                            String resourceName, long resourcePK, long newResourcePK)
1096                    throws PortalException, SystemException {
1097    
1098                    if (!MapUtil.getBoolean(
1099                                    _parameterMap, PortletDataHandlerKeys.PERMISSIONS)) {
1100    
1101                            return;
1102                    }
1103    
1104                    List<KeyValuePair> permissions = _permissionsMap.get(
1105                            getPrimaryKeyString(resourceName, resourcePK));
1106    
1107                    if (permissions == null) {
1108                            return;
1109                    }
1110    
1111                    Map<Long, String[]> roleIdsToActionIds = new HashMap<Long, String[]>();
1112    
1113                    for (KeyValuePair permission : permissions) {
1114                            String roleName = permission.getKey();
1115    
1116                            Role role = null;
1117    
1118                            Team team = null;
1119    
1120                            if (roleName.startsWith(PermissionExporter.ROLE_TEAM_PREFIX)) {
1121                                    roleName = roleName.substring(
1122                                            PermissionExporter.ROLE_TEAM_PREFIX.length());
1123    
1124                                    try {
1125                                            team = TeamLocalServiceUtil.getTeam(_groupId, roleName);
1126                                    }
1127                                    catch (NoSuchTeamException nste) {
1128                                            if (_log.isWarnEnabled()) {
1129                                                    _log.warn("Team " + roleName + " does not exist");
1130                                            }
1131    
1132                                            continue;
1133                                    }
1134                            }
1135    
1136                            try {
1137                                    if (team != null) {
1138                                            role = RoleLocalServiceUtil.getTeamRole(
1139                                                    _companyId, team.getTeamId());
1140                                    }
1141                                    else {
1142                                            role = RoleLocalServiceUtil.getRole(_companyId, roleName);
1143                                    }
1144                            }
1145                            catch (NoSuchRoleException nsre) {
1146                                    if (_log.isWarnEnabled()) {
1147                                            _log.warn("Role " + roleName + " does not exist");
1148                                    }
1149    
1150                                    continue;
1151                            }
1152    
1153                            String[] actionIds = StringUtil.split(permission.getValue());
1154    
1155                            roleIdsToActionIds.put(role.getRoleId(), actionIds);
1156                    }
1157    
1158                    if (roleIdsToActionIds.isEmpty()) {
1159                            return;
1160                    }
1161    
1162                    if (ResourceBlockLocalServiceUtil.isSupported(resourceName)) {
1163                            ResourceBlockLocalServiceUtil.setIndividualScopePermissions(
1164                                    _companyId, _groupId, resourceName, newResourcePK,
1165                                    roleIdsToActionIds);
1166                    }
1167                    else {
1168                            ResourcePermissionLocalServiceUtil.setResourcePermissions(
1169                                    _companyId, resourceName, ResourceConstants.SCOPE_INDIVIDUAL,
1170                                    String.valueOf(newResourcePK), roleIdsToActionIds);
1171                    }
1172            }
1173    
1174            public void importRatingsEntries(
1175                            Class<?> clazz, long classPK, long newClassPK)
1176                    throws PortalException, SystemException {
1177    
1178                    List<RatingsEntry> ratingsEntries = _ratingsEntriesMap.get(
1179                            getPrimaryKeyString(clazz, classPK));
1180    
1181                    if (ratingsEntries == null) {
1182                            return;
1183                    }
1184    
1185                    ServiceContext serviceContext = new ServiceContext();
1186    
1187                    for (RatingsEntry ratingsEntry : ratingsEntries) {
1188                            long userId = getUserId(ratingsEntry.getUserUuid());
1189    
1190                            serviceContext.setCreateDate(ratingsEntry.getCreateDate());
1191                            serviceContext.setModifiedDate(ratingsEntry.getModifiedDate());
1192    
1193                            RatingsEntryLocalServiceUtil.updateEntry(
1194                                    userId, clazz.getName(), newClassPK, ratingsEntry.getScore(),
1195                                    serviceContext);
1196                    }
1197            }
1198    
1199            public boolean isDataStrategyMirror() {
1200                    if (_dataStrategy.equals(PortletDataHandlerKeys.DATA_STRATEGY_MIRROR) ||
1201                            _dataStrategy.equals(
1202                                    PortletDataHandlerKeys.DATA_STRATEGY_MIRROR_OVERWRITE)) {
1203    
1204                            return true;
1205                    }
1206                    else {
1207                            return false;
1208                    }
1209            }
1210    
1211            public boolean isDataStrategyMirrorWithOverwriting() {
1212                    if (_dataStrategy.equals(
1213                                    PortletDataHandlerKeys.DATA_STRATEGY_MIRROR_OVERWRITE)) {
1214    
1215                            return true;
1216                    }
1217                    else {
1218                            return false;
1219                    }
1220            }
1221    
1222            public boolean isPathNotProcessed(String path) {
1223                    return !addPrimaryKey(String.class, path);
1224            }
1225    
1226            public boolean isPerformDirectBinaryImport() {
1227                    return MapUtil.getBoolean(
1228                            _parameterMap, PortletDataHandlerKeys.PERFORM_DIRECT_BINARY_IMPORT);
1229            }
1230    
1231            public boolean isPrivateLayout() {
1232                    return _privateLayout;
1233            }
1234    
1235            public boolean isWithinDateRange(Date modifiedDate) {
1236                    if (!hasDateRange()) {
1237                            return true;
1238                    }
1239                    else if ((_startDate.compareTo(modifiedDate) <= 0) &&
1240                                     _endDate.after(modifiedDate)) {
1241    
1242                            return true;
1243                    }
1244                    else {
1245                            return false;
1246                    }
1247            }
1248    
1249            public void putNotUniquePerLayout(String dataKey) {
1250                    _notUniquePerLayout.add(dataKey);
1251            }
1252    
1253            public void setClassLoader(ClassLoader classLoader) {
1254                    _xStream.setClassLoader(classLoader);
1255            }
1256    
1257            public void setGroupId(long groupId) {
1258                    _groupId = groupId;
1259            }
1260    
1261            public void setOldPlid(long oldPlid) {
1262                    _oldPlid = oldPlid;
1263            }
1264    
1265            public void setPlid(long plid) {
1266                    _plid = plid;
1267            }
1268    
1269            public void setPortetDataContextListener(
1270                    PortletDataContextListener portletDataContextListener) {
1271    
1272                    _portletDataContextListener = portletDataContextListener;
1273            }
1274    
1275            public void setPrivateLayout(boolean privateLayout) {
1276                    _privateLayout = privateLayout;
1277            }
1278    
1279            public void setScopeGroupId(long scopeGroupId) {
1280                    _scopeGroupId = scopeGroupId;
1281            }
1282    
1283            public void setScopeLayoutUuid(String scopeLayoutUuid) {
1284                    _scopeLayoutUuid = scopeLayoutUuid;
1285            }
1286    
1287            public void setScopeType(String scopeType) {
1288                    _scopeType = scopeType;
1289            }
1290    
1291            public void setSourceGroupId(long sourceGroupId) {
1292                    _sourceGroupId = sourceGroupId;
1293            }
1294    
1295            public void setStartDate(Date startDate) {
1296                    _startDate = startDate;
1297            }
1298    
1299            public String toXML(Object object) {
1300                    return _xStream.toXML(object);
1301            }
1302    
1303            protected ServiceContext createServiceContext(
1304                    Element element, String path, ClassedModel classedModel,
1305                    String namespace) {
1306    
1307                    Class<?> clazz = classedModel.getModelClass();
1308                    long classPK = getClassPK(classedModel);
1309    
1310                    ServiceContext serviceContext = new ServiceContext();
1311    
1312                    // Theme display
1313    
1314                    serviceContext.setCompanyId(getCompanyId());
1315                    serviceContext.setScopeGroupId(getScopeGroupId());
1316    
1317                    // Dates
1318    
1319                    if (classedModel instanceof AuditedModel) {
1320                            AuditedModel auditedModel = (AuditedModel)classedModel;
1321    
1322                            serviceContext.setCreateDate(auditedModel.getCreateDate());
1323                            serviceContext.setModifiedDate(auditedModel.getModifiedDate());
1324                    }
1325    
1326                    // Permissions
1327    
1328                    if (!MapUtil.getBoolean(
1329                                    _parameterMap, PortletDataHandlerKeys.PERMISSIONS)) {
1330    
1331                            serviceContext.setAddGroupPermissions(true);
1332                            serviceContext.setAddGuestPermissions(true);
1333                    }
1334    
1335                    // Asset
1336    
1337                    boolean portletMetadataAll = getBooleanParameter(
1338                            namespace, PortletDataHandlerKeys.PORTLET_METADATA_ALL);
1339    
1340                    if (isResourceMain(classedModel)) {
1341                            if (portletMetadataAll ||
1342                                    getBooleanParameter(namespace, "categories")) {
1343    
1344                                    long[] assetCategoryIds = getAssetCategoryIds(clazz, classPK);
1345    
1346                                    serviceContext.setAssetCategoryIds(assetCategoryIds);
1347                            }
1348    
1349                            if (portletMetadataAll || getBooleanParameter(namespace, "tags")) {
1350                                    String[] assetTagNames = getAssetTagNames(clazz, classPK);
1351    
1352                                    serviceContext.setAssetTagNames(assetTagNames);
1353                            }
1354                    }
1355    
1356                    // Expando
1357    
1358                    String expandoPath = null;
1359    
1360                    if (element != null) {
1361                            expandoPath = element.attributeValue("expando-path");
1362                    }
1363                    else {
1364                            expandoPath = getExpandoPath(path);
1365                    }
1366    
1367                    if (Validator.isNotNull(expandoPath)) {
1368                            try {
1369                                    Map<String, Serializable> expandoBridgeAttributes =
1370                                            (Map<String, Serializable>)getZipEntryAsObject(expandoPath);
1371    
1372                                    serviceContext.setExpandoBridgeAttributes(
1373                                            expandoBridgeAttributes);
1374                            }
1375                            catch (Exception e) {
1376                                    if (_log.isDebugEnabled()) {
1377                                            _log.debug(e, e);
1378                                    }
1379                            }
1380                    }
1381    
1382                    return serviceContext;
1383            }
1384    
1385            protected Map<Long, Set<String>> getActionIds(
1386                            long companyId, long[] roleIds, String className, long primKey,
1387                            List<String> actionIds)
1388                    throws PortalException, SystemException {
1389    
1390                    if (ResourceBlockLocalServiceUtil.isSupported(className)) {
1391                            return ResourceBlockPermissionLocalServiceUtil.
1392                                    getAvailableResourceBlockPermissionActionIds(
1393                                            roleIds, className, primKey, actionIds);
1394                    }
1395                    else {
1396                            return ResourcePermissionLocalServiceUtil.
1397                                    getAvailableResourcePermissionActionIds(
1398                                            companyId, className, ResourceConstants.SCOPE_INDIVIDUAL,
1399                                            String.valueOf(primKey), roleIds, actionIds);
1400                    }
1401            }
1402    
1403            protected long getClassPK(ClassedModel classedModel) {
1404                    if (classedModel instanceof ResourcedModel) {
1405                            ResourcedModel resourcedModel = (ResourcedModel)classedModel;
1406    
1407                            return resourcedModel.getResourcePrimKey();
1408                    }
1409                    else {
1410                            return (Long)classedModel.getPrimaryKeyObj();
1411                    }
1412            }
1413    
1414            protected String getExpandoPath(String path) {
1415                    if (!Validator.isFilePath(path, false)) {
1416                            throw new IllegalArgumentException(
1417                                    path + " is located outside of the lar");
1418                    }
1419    
1420                    int pos = path.lastIndexOf(".xml");
1421    
1422                    if (pos == -1) {
1423                            throw new IllegalArgumentException(
1424                                    path + " does not end with .xml");
1425                    }
1426    
1427                    return path.substring(0, pos).concat("-expando").concat(
1428                            path.substring(pos));
1429            }
1430    
1431            protected String getPrimaryKeyString(Class<?> clazz, long classPK) {
1432                    return getPrimaryKeyString(clazz.getName(), String.valueOf(classPK));
1433            }
1434    
1435            protected String getPrimaryKeyString(Class<?> clazz, String primaryKey) {
1436                    return getPrimaryKeyString(clazz.getName(), primaryKey);
1437            }
1438    
1439            protected String getPrimaryKeyString(String className, long classPK) {
1440                    return getPrimaryKeyString(className, String.valueOf(classPK));
1441            }
1442    
1443            protected String getPrimaryKeyString(String className, String primaryKey) {
1444                    return className.concat(StringPool.POUND).concat(primaryKey);
1445            }
1446    
1447            protected void initXStream() {
1448                    _xStream = new XStream();
1449    
1450                    _xStream.alias("BlogsEntry", BlogsEntryImpl.class);
1451                    _xStream.alias("BookmarksFolder", BookmarksFolderImpl.class);
1452                    _xStream.alias("BookmarksEntry", BookmarksEntryImpl.class);
1453                    _xStream.alias("CalEvent", CalEventImpl.class);
1454                    _xStream.alias("DLFolder", DLFolderImpl.class);
1455                    _xStream.alias("DLFileEntry", DLFileEntryImpl.class);
1456                    _xStream.alias("DLFileShortcut", DLFileShortcutImpl.class);
1457                    _xStream.alias("DLFileRank", DLFileRankImpl.class);
1458                    _xStream.alias("JournalArticle", JournalArticleImpl.class);
1459                    _xStream.alias("JournalFeed", JournalFeedImpl.class);
1460                    _xStream.alias("JournalStructure", JournalStructureImpl.class);
1461                    _xStream.alias("JournalTemplate", JournalTemplateImpl.class);
1462                    _xStream.alias("Lock", LockImpl.class);
1463                    _xStream.alias("MBBan", MBBanImpl.class);
1464                    _xStream.alias("MBCategory", MBCategoryImpl.class);
1465                    _xStream.alias("MBMessage", MBMessageImpl.class);
1466                    _xStream.alias("MBThreadFlag", MBThreadFlagImpl.class);
1467                    _xStream.alias("PollsQuestion", PollsQuestionImpl.class);
1468                    _xStream.alias("PollsChoice", PollsChoiceImpl.class);
1469                    _xStream.alias("PollsVote", PollsVoteImpl.class);
1470                    _xStream.alias("RatingsEntry", RatingsEntryImpl.class);
1471                    _xStream.alias("WikiNode", WikiNodeImpl.class);
1472                    _xStream.alias("WikiPage", WikiPageImpl.class);
1473            }
1474    
1475            protected boolean isResourceMain(ClassedModel classedModel) {
1476                    if (classedModel instanceof ResourcedModel) {
1477                            ResourcedModel resourcedModel = (ResourcedModel)classedModel;
1478    
1479                            return resourcedModel.isResourceMain();
1480                    }
1481    
1482                    return true;
1483            }
1484    
1485            protected void validateDateRange(Date startDate, Date endDate)
1486                    throws PortletDataException {
1487    
1488                    if ((startDate == null) && (endDate != null)) {
1489                            throw new PortletDataException(
1490                                    PortletDataException.END_DATE_IS_MISSING_START_DATE);
1491                    }
1492                    else if ((startDate != null) && (endDate == null)) {
1493                            throw new PortletDataException(
1494                                    PortletDataException.START_DATE_IS_MISSING_END_DATE);
1495                    }
1496    
1497                    if (startDate != null) {
1498                            if (startDate.after(endDate) || startDate.equals(endDate)) {
1499                                    throw new PortletDataException(
1500                                            PortletDataException.START_DATE_AFTER_END_DATE);
1501                            }
1502    
1503                            Date now = new Date();
1504    
1505                            if (startDate.after(now)) {
1506                                    throw new PortletDataException(
1507                                            PortletDataException.FUTURE_START_DATE);
1508                            }
1509    
1510                            if (endDate.after(now)) {
1511                                    throw new PortletDataException(
1512                                            PortletDataException.FUTURE_END_DATE);
1513                            }
1514                    }
1515            }
1516    
1517            private static Log _log = LogFactoryUtil.getLog(
1518                    PortletDataContextImpl.class);
1519    
1520            private Map<String, long[]> _assetCategoryIdsMap =
1521                    new HashMap<String, long[]>();
1522            private Map<String, String[]> _assetCategoryUuidsMap =
1523                    new HashMap<String, String[]>();
1524            private Map<String, String[]> _assetLinkUuidsMap =
1525                    new HashMap<String, String[]>();
1526            private Map<String, String[]> _assetTagNamesMap =
1527                    new HashMap<String, String[]>();
1528            private Map<String, List<MBMessage>> _commentsMap =
1529                    new HashMap<String, List<MBMessage>>();
1530            private long _companyId;
1531            private String _dataStrategy;
1532            private Date _endDate;
1533            private Map<String, List<ExpandoColumn>> _expandoColumnsMap =
1534                    new HashMap<String, List<ExpandoColumn>>();
1535            private long _groupId;
1536            private Map<String, Lock> _locksMap = new HashMap<String, Lock>();
1537            private Map<String, Map<?, ?>> _newPrimaryKeysMaps =
1538                    new HashMap<String, Map<?, ?>>();
1539            private Set<String> _notUniquePerLayout = new HashSet<String>();
1540            private long _oldPlid;
1541            private Map<String, String[]> _parameterMap;
1542            private Map<String, List<KeyValuePair>> _permissionsMap =
1543                    new HashMap<String, List<KeyValuePair>>();
1544            private long _plid;
1545            private PortletDataContextListener _portletDataContextListener;
1546            private Set<String> _primaryKeys;
1547            private boolean _privateLayout;
1548            private Map<String, List<RatingsEntry>> _ratingsEntriesMap =
1549                    new HashMap<String, List<RatingsEntry>>();
1550            private long _scopeGroupId;
1551            private String _scopeLayoutUuid;
1552            private String _scopeType;
1553            private long _sourceGroupId;
1554            private Date _startDate;
1555            private UserIdStrategy _userIdStrategy;
1556            private XStream _xStream;
1557            private ZipReader _zipReader;
1558            private ZipWriter _zipWriter;
1559    
1560    }