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