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.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(getPrimaryKeyString(clazz, classPK));
672            }
673    
674            public Map<String, long[]> getAssetCategoryIdsMap() {
675                    return _assetCategoryIdsMap;
676            }
677    
678            public Map<String, String[]> getAssetCategoryUuidsMap() {
679                    return _assetCategoryUuidsMap;
680            }
681    
682            public Map<String, String[]> getAssetLinkUuidsMap() {
683                    return _assetLinkUuidsMap;
684            }
685    
686            public String[] getAssetTagNames(Class<?> clazz, long classPK) {
687                    return _assetTagNamesMap.get(getPrimaryKeyString(clazz, classPK));
688            }
689    
690            public String[] getAssetTagNames(String className, long classPK) {
691                    return _assetTagNamesMap.get(getPrimaryKeyString(className, classPK));
692            }
693    
694            public Map<String, String[]> getAssetTagNamesMap() {
695                    return _assetTagNamesMap;
696            }
697    
698            public boolean getBooleanParameter(String namespace, String name) {
699                    boolean defaultValue = MapUtil.getBoolean(
700                            getParameterMap(),
701                            PortletDataHandlerKeys.PORTLET_DATA_CONTROL_DEFAULT, true);
702    
703                    return MapUtil.getBoolean(
704                            getParameterMap(),
705                            PortletDataHandlerControl.getNamespacedControlName(namespace, name),
706                            defaultValue);
707            }
708    
709            public ClassLoader getClassLoader() {
710                    return _xStream.getClassLoader();
711            }
712    
713            public Map<String, List<MBMessage>> getComments() {
714                    return _commentsMap;
715            }
716    
717            public long getCompanyId() {
718                    return _companyId;
719            }
720    
721            public String getDataStrategy() {
722                    return _dataStrategy;
723            }
724    
725            public Date getEndDate() {
726                    return _endDate;
727            }
728    
729            public Map<String, List<ExpandoColumn>> getExpandoColumns() {
730                    return _expandoColumnsMap;
731            }
732    
733            public long getGroupId() {
734                    return _groupId;
735            }
736    
737            public String getLayoutPath(long layoutId) {
738                    return getRootPath() + ROOT_PATH_LAYOUTS + layoutId;
739            }
740    
741            public Map<String, Lock> getLocks() {
742                    return _locksMap;
743            }
744    
745            public Map<?, ?> getNewPrimaryKeysMap(Class<?> clazz) {
746                    return getNewPrimaryKeysMap(clazz.getName());
747            }
748    
749            public Map<?, ?> getNewPrimaryKeysMap(String className) {
750                    Map<?, ?> map = _newPrimaryKeysMaps.get(className);
751    
752                    if (map == null) {
753                            map = new HashMap<Object, Object>();
754    
755                            _newPrimaryKeysMaps.put(className, map);
756                    }
757    
758                    return map;
759            }
760    
761            public long getOldPlid() {
762                    return _oldPlid;
763            }
764    
765            public Map<String, String[]> getParameterMap() {
766                    return _parameterMap;
767            }
768    
769            public Map<String, List<KeyValuePair>> getPermissions() {
770                    return _permissionsMap;
771            }
772    
773            public long getPlid() {
774                    return _plid;
775            }
776    
777            public String getPortletPath(String portletId) {
778                    return getRootPath() + ROOT_PATH_PORTLETS + portletId;
779            }
780    
781            public Set<String> getPrimaryKeys() {
782                    return _primaryKeys;
783            }
784    
785            public Map<String, List<RatingsEntry>> getRatingsEntries() {
786                    return _ratingsEntriesMap;
787            }
788    
789            public String getRootPath() {
790                    return ROOT_PATH_GROUPS + getScopeGroupId();
791            }
792    
793            public long getScopeGroupId() {
794                    return _scopeGroupId;
795            }
796    
797            public String getScopeLayoutUuid() {
798                    return _scopeLayoutUuid;
799            }
800    
801            public String getScopeType() {
802                    return _scopeType;
803            }
804    
805            public long getSourceGroupId() {
806                    return _sourceGroupId;
807            }
808    
809            public String getSourceLayoutPath(long layoutId) {
810                    return getSourceRootPath() + ROOT_PATH_LAYOUTS + layoutId;
811            }
812    
813            public String getSourcePortletPath(String portletId) {
814                    return getSourceRootPath() + ROOT_PATH_PORTLETS + portletId;
815            }
816    
817            public String getSourceRootPath() {
818                    return ROOT_PATH_GROUPS + getSourceGroupId();
819            }
820    
821            public Date getStartDate() {
822                    return _startDate;
823            }
824    
825            public long getUserId(String userUuid) throws SystemException {
826                    return _userIdStrategy.getUserId(userUuid);
827            }
828    
829            public UserIdStrategy getUserIdStrategy() {
830                    return _userIdStrategy;
831            }
832    
833            public List<String> getZipEntries() {
834                    return getZipReader().getEntries();
835            }
836    
837            public byte[] getZipEntryAsByteArray(String path) {
838                    if (_portletDataContextListener != null) {
839                            _portletDataContextListener.onGetZipEntry(path);
840                    }
841    
842                    return getZipReader().getEntryAsByteArray(path);
843            }
844    
845            public InputStream getZipEntryAsInputStream(String path) {
846                    if (_portletDataContextListener != null) {
847                            _portletDataContextListener.onGetZipEntry(path);
848                    }
849    
850                    return getZipReader().getEntryAsInputStream(path);
851            }
852    
853            public Object getZipEntryAsObject(String path) {
854                    return fromXML(getZipEntryAsString(path));
855            }
856    
857            public String getZipEntryAsString(String path) {
858                    if (_portletDataContextListener != null) {
859                            _portletDataContextListener.onGetZipEntry(path);
860                    }
861    
862                    return getZipReader().getEntryAsString(path);
863            }
864    
865            public List<String> getZipFolderEntries() {
866                    return getZipFolderEntries(StringPool.SLASH);
867            }
868    
869            public List<String> getZipFolderEntries(String path) {
870                    return getZipReader().getFolderEntries(path);
871            }
872    
873            public ZipReader getZipReader() {
874                    return _zipReader;
875            }
876    
877            public ZipWriter getZipWriter() {
878                    return _zipWriter;
879            }
880    
881            public boolean hasDateRange() {
882                    if (_startDate != null) {
883                            return true;
884                    }
885                    else {
886                            return false;
887                    }
888            }
889    
890            public boolean hasNotUniquePerLayout(String dataKey) {
891                    return _notUniquePerLayout.contains(dataKey);
892            }
893    
894            public boolean hasPrimaryKey(Class<?> clazz, String primaryKey) {
895                    return _primaryKeys.contains(getPrimaryKeyString(clazz, primaryKey));
896            }
897    
898            public void importClassedModel(
899                            ClassedModel classedModel, ClassedModel newClassedModel,
900                            String namespace)
901                    throws PortalException, SystemException {
902    
903                    if (!isResourceMain(classedModel)) {
904                            return;
905                    }
906    
907                    Class<?> clazz = classedModel.getModelClass();
908                    long classPK = getClassPK(classedModel);
909    
910                    long newClassPK = getClassPK(newClassedModel);
911    
912                    Map<Long, Long> newPrimaryKeysMap =
913                            (Map<Long, Long>)getNewPrimaryKeysMap(clazz);
914    
915                    newPrimaryKeysMap.put(classPK, newClassPK);
916    
917                    importLocks(clazz, String.valueOf(classPK), String.valueOf(newClassPK));
918                    importPermissions(clazz, classPK, newClassPK);
919    
920                    if (getBooleanParameter(namespace, "comments")) {
921                            importComments(clazz, classPK, newClassPK, getScopeGroupId());
922                    }
923    
924                    if (getBooleanParameter(namespace, "ratings")) {
925                            importRatingsEntries(clazz, classPK, newClassPK);
926                    }
927            }
928    
929            public void importComments(
930                            Class<?> clazz, long classPK, long newClassPK, long groupId)
931                    throws PortalException, SystemException {
932    
933                    Map<Long, Long> messagePKs = new HashMap<Long, Long>();
934                    Map<Long, Long> threadPKs = new HashMap<Long, Long>();
935    
936                    List<MBMessage> messages = _commentsMap.get(
937                            getPrimaryKeyString(clazz, classPK));
938    
939                    if (messages == null) {
940                            return;
941                    }
942    
943                    MBDiscussion discussion = null;
944    
945                    try {
946                            discussion = MBDiscussionLocalServiceUtil.getDiscussion(
947                                    clazz.getName(), newClassPK);
948                    }
949                    catch (NoSuchDiscussionException nsde) {
950                    }
951    
952                    for (MBMessage message : messages) {
953                            long userId = getUserId(message.getUserUuid());
954                            long parentMessageId = MapUtil.getLong(
955                                    messagePKs, message.getParentMessageId(),
956                                    message.getParentMessageId());
957                            long threadId = MapUtil.getLong(
958                                    threadPKs, message.getThreadId(), message.getThreadId());
959    
960                            if ((message.getParentMessageId() ==
961                                            MBMessageConstants.DEFAULT_PARENT_MESSAGE_ID) &&
962                                    (discussion != null)) {
963    
964                                    MBThread thread = MBThreadLocalServiceUtil.getThread(
965                                            discussion.getThreadId());
966    
967                                    long rootMessageId = thread.getRootMessageId();
968    
969                                    messagePKs.put(message.getMessageId(), rootMessageId);
970                                    threadPKs.put(message.getThreadId(), thread.getThreadId());
971                            }
972                            else {
973                                    ServiceContext serviceContext = new ServiceContext();
974    
975                                    serviceContext.setCreateDate(message.getCreateDate());
976                                    serviceContext.setModifiedDate(message.getModifiedDate());
977                                    serviceContext.setScopeGroupId(groupId);
978    
979                                    MBMessage importedMessage = null;
980    
981                                    if (_dataStrategy.equals(
982                                                    PortletDataHandlerKeys.DATA_STRATEGY_MIRROR) ||
983                                            _dataStrategy.equals(
984                                                    PortletDataHandlerKeys.
985                                                            DATA_STRATEGY_MIRROR_OVERWRITE)) {
986    
987                                            MBMessage existingMessage = MBMessageUtil.fetchByUUID_G(
988                                                    message.getUuid(), groupId);
989    
990                                            if (existingMessage == null) {
991                                                    serviceContext.setUuid(message.getUuid());
992    
993                                                    importedMessage =
994                                                            MBMessageLocalServiceUtil.addDiscussionMessage(
995                                                                    userId, message.getUserName(), groupId,
996                                                                    clazz.getName(), newClassPK, threadId,
997                                                                    parentMessageId, message.getSubject(),
998                                                                    message.getBody(), serviceContext);
999                                            }
1000                                            else {
1001                                                    serviceContext.setWorkflowAction(
1002                                                            WorkflowConstants.ACTION_PUBLISH);
1003    
1004                                                    importedMessage =
1005                                                            MBMessageLocalServiceUtil.updateDiscussionMessage(
1006                                                                    userId, existingMessage.getMessageId(),
1007                                                                    clazz.getName(), newClassPK,
1008                                                                    message.getSubject(), message.getBody(),
1009                                                                    serviceContext);
1010                                            }
1011                                    }
1012                                    else {
1013                                            importedMessage =
1014                                                    MBMessageLocalServiceUtil.addDiscussionMessage(
1015                                                            userId, message.getUserName(), groupId,
1016                                                            clazz.getName(), newClassPK, threadId,
1017                                                            parentMessageId, message.getSubject(),
1018                                                            message.getBody(), serviceContext);
1019                                    }
1020    
1021                                    messagePKs.put(
1022                                            message.getMessageId(), importedMessage.getMessageId());
1023                                    threadPKs.put(
1024                                            message.getThreadId(), importedMessage.getThreadId());
1025                            }
1026    
1027                            importRatingsEntries(
1028                                    MBDiscussion.class, message.getPrimaryKey(),
1029                                    messagePKs.get(message.getPrimaryKey()));
1030                    }
1031            }
1032    
1033            public void importLocks(Class<?> clazz, String key, String newKey)
1034                    throws PortalException, SystemException {
1035    
1036                    Lock lock = _locksMap.get(getPrimaryKeyString(clazz, key));
1037    
1038                    if (lock == null) {
1039                            return;
1040                    }
1041    
1042                    long userId = getUserId(lock.getUserUuid());
1043    
1044                    long expirationTime = 0;
1045    
1046                    if (lock.getExpirationDate() != null) {
1047                            Date expirationDate = lock.getExpirationDate();
1048    
1049                            expirationTime = expirationDate.getTime();
1050                    }
1051    
1052                    LockLocalServiceUtil.lock(
1053                            userId, clazz.getName(), newKey, lock.getOwner(),
1054                            lock.isInheritable(), expirationTime);
1055            }
1056    
1057            public void importPermissions(Class<?> clazz, long classPK, long newClassPK)
1058                    throws PortalException, SystemException {
1059    
1060                    importPermissions(clazz.getName(), classPK, newClassPK);
1061            }
1062    
1063            public void importPermissions(
1064                            String resourceName, long resourcePK, long newResourcePK)
1065                    throws PortalException, SystemException {
1066    
1067                    if (((PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 5) &&
1068                             (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6)) ||
1069                            (!MapUtil.getBoolean(
1070                                    _parameterMap, PortletDataHandlerKeys.PERMISSIONS))) {
1071    
1072                            return;
1073                    }
1074    
1075                    List<KeyValuePair> permissions = _permissionsMap.get(
1076                            getPrimaryKeyString(resourceName, resourcePK));
1077    
1078                    if (permissions == null) {
1079                            return;
1080                    }
1081    
1082                    Map<Long, String[]> roleIdsToActionIds = new HashMap<Long, String[]>();
1083    
1084                    for (KeyValuePair permission : permissions) {
1085                            String roleName = permission.getKey();
1086    
1087                            Role role = null;
1088    
1089                            Team team = null;
1090    
1091                            if (roleName.startsWith(PermissionExporter.ROLE_TEAM_PREFIX)) {
1092                                    roleName = roleName.substring(
1093                                            PermissionExporter.ROLE_TEAM_PREFIX.length());
1094    
1095                                    try {
1096                                            team = TeamLocalServiceUtil.getTeam(_groupId, roleName);
1097                                    }
1098                                    catch (NoSuchTeamException nste) {
1099                                            if (_log.isWarnEnabled()) {
1100                                                    _log.warn("Team " + roleName + " does not exist");
1101                                            }
1102    
1103                                            continue;
1104                                    }
1105                            }
1106    
1107                            try {
1108                                    if (team != null) {
1109                                            role = RoleLocalServiceUtil.getTeamRole(
1110                                                    _companyId, team.getTeamId());
1111                                    }
1112                                    else {
1113                                            role = RoleLocalServiceUtil.getRole(_companyId, roleName);
1114                                    }
1115                            }
1116                            catch (NoSuchRoleException nsre) {
1117                                    if (_log.isWarnEnabled()) {
1118                                            _log.warn("Role " + roleName + " does not exist");
1119                                    }
1120    
1121                                    continue;
1122                            }
1123    
1124                            String[] actionIds = StringUtil.split(permission.getValue());
1125    
1126                            roleIdsToActionIds.put(role.getRoleId(), actionIds);
1127                    }
1128    
1129                    if (roleIdsToActionIds.isEmpty()) {
1130                            return;
1131                    }
1132    
1133                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
1134                            PermissionLocalServiceUtil.setRolesPermissions(
1135                                    _companyId, roleIdsToActionIds, resourceName,
1136                                    ResourceConstants.SCOPE_INDIVIDUAL,
1137                                    String.valueOf(newResourcePK));
1138                    }
1139                    else if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
1140                            ResourcePermissionLocalServiceUtil.setResourcePermissions(
1141                                    _companyId, resourceName, ResourceConstants.SCOPE_INDIVIDUAL,
1142                                    String.valueOf(newResourcePK), roleIdsToActionIds);
1143                    }
1144            }
1145    
1146            public void importRatingsEntries(
1147                            Class<?> clazz, long classPK, long newClassPK)
1148                    throws PortalException, SystemException {
1149    
1150                    List<RatingsEntry> ratingsEntries = _ratingsEntriesMap.get(
1151                            getPrimaryKeyString(clazz, classPK));
1152    
1153                    if (ratingsEntries == null) {
1154                            return;
1155                    }
1156    
1157                    ServiceContext serviceContext = new ServiceContext();
1158    
1159                    for (RatingsEntry ratingsEntry : ratingsEntries) {
1160                            long userId = getUserId(ratingsEntry.getUserUuid());
1161    
1162                            serviceContext.setCreateDate(ratingsEntry.getCreateDate());
1163                            serviceContext.setModifiedDate(ratingsEntry.getModifiedDate());
1164    
1165                            RatingsEntryLocalServiceUtil.updateEntry(
1166                                    userId, clazz.getName(), newClassPK, ratingsEntry.getScore(),
1167                                    serviceContext);
1168                    }
1169            }
1170    
1171            public boolean isDataStrategyMirror() {
1172                    if (_dataStrategy.equals(PortletDataHandlerKeys.DATA_STRATEGY_MIRROR) ||
1173                            _dataStrategy.equals(
1174                                    PortletDataHandlerKeys.DATA_STRATEGY_MIRROR_OVERWRITE)) {
1175    
1176                            return true;
1177                    }
1178                    else {
1179                            return false;
1180                    }
1181            }
1182    
1183            public boolean isDataStrategyMirrorWithOverwritting() {
1184                    if (_dataStrategy.equals(
1185                                    PortletDataHandlerKeys.DATA_STRATEGY_MIRROR_OVERWRITE)) {
1186    
1187                            return true;
1188                    }
1189                    else {
1190                            return false;
1191                    }
1192            }
1193    
1194            public boolean isPathNotProcessed(String path) {
1195                    return !addPrimaryKey(String.class, path);
1196            }
1197    
1198            public boolean isPerformDirectBinaryImport() {
1199                    return MapUtil.getBoolean(
1200                            _parameterMap, PortletDataHandlerKeys.PERFORM_DIRECT_BINARY_IMPORT);
1201            }
1202    
1203            public boolean isPrivateLayout() {
1204                    return _privateLayout;
1205            }
1206    
1207            public boolean isWithinDateRange(Date modifiedDate) {
1208                    if (!hasDateRange()) {
1209                            return true;
1210                    }
1211                    else if ((_startDate.compareTo(modifiedDate) <= 0) &&
1212                                     (_endDate.after(modifiedDate))) {
1213    
1214                            return true;
1215                    }
1216                    else {
1217                            return false;
1218                    }
1219            }
1220    
1221            public void putNotUniquePerLayout(String dataKey) {
1222                    _notUniquePerLayout.add(dataKey);
1223            }
1224    
1225            public void setClassLoader(ClassLoader classLoader) {
1226                    _xStream.setClassLoader(classLoader);
1227            }
1228    
1229            public void setGroupId(long groupId) {
1230                    _groupId = groupId;
1231            }
1232    
1233            public void setOldPlid(long oldPlid) {
1234                    _oldPlid = oldPlid;
1235            }
1236    
1237            public void setPlid(long plid) {
1238                    _plid = plid;
1239            }
1240    
1241            public void setPortetDataContextListener(
1242                    PortletDataContextListener portletDataContextListener) {
1243    
1244                    _portletDataContextListener = portletDataContextListener;
1245            }
1246    
1247            public void setPrivateLayout(boolean privateLayout) {
1248                    _privateLayout = privateLayout;
1249            }
1250    
1251            public void setScopeGroupId(long scopeGroupId) {
1252                    _scopeGroupId = scopeGroupId;
1253            }
1254    
1255            public void setScopeLayoutUuid(String scopeLayoutUuid) {
1256                    _scopeLayoutUuid = scopeLayoutUuid;
1257            }
1258    
1259            public void setScopeType(String scopeType) {
1260                    _scopeType = scopeType;
1261            }
1262    
1263            public void setSourceGroupId(long sourceGroupId) {
1264                    _sourceGroupId = sourceGroupId;
1265            }
1266    
1267            public void setStartDate(Date startDate) {
1268                    _startDate = startDate;
1269            }
1270    
1271            public String toXML(Object object) {
1272                    return _xStream.toXML(object);
1273            }
1274    
1275            protected ServiceContext createServiceContext(
1276                    Element element, String path, ClassedModel classedModel,
1277                    String namespace) {
1278    
1279                    Class<?> clazz = classedModel.getModelClass();
1280                    long classPK = getClassPK(classedModel);
1281    
1282                    ServiceContext serviceContext = new ServiceContext();
1283    
1284                    // Theme display
1285    
1286                    serviceContext.setCompanyId(getCompanyId());
1287                    serviceContext.setScopeGroupId(getScopeGroupId());
1288    
1289                    // Dates
1290    
1291                    if (classedModel instanceof AuditedModel) {
1292                            AuditedModel auditedModel = (AuditedModel)classedModel;
1293    
1294                            serviceContext.setCreateDate(auditedModel.getCreateDate());
1295                            serviceContext.setModifiedDate(auditedModel.getModifiedDate());
1296                    }
1297    
1298                    // Permissions
1299    
1300                    if (!MapUtil.getBoolean(
1301                                    _parameterMap, PortletDataHandlerKeys.PERMISSIONS)) {
1302    
1303                            serviceContext.setAddGroupPermissions(true);
1304                            serviceContext.setAddGuestPermissions(true);
1305                    }
1306    
1307                    // Asset
1308    
1309                    if (isResourceMain(classedModel)) {
1310                            if (getBooleanParameter(namespace, "categories")) {
1311                                    long[] assetCategoryIds = getAssetCategoryIds(clazz, classPK);
1312    
1313                                    serviceContext.setAssetCategoryIds(assetCategoryIds);
1314                            }
1315    
1316                            if (getBooleanParameter(namespace, "tags")) {
1317                                    String[] assetTagNames = getAssetTagNames(clazz, classPK);
1318    
1319                                    serviceContext.setAssetTagNames(assetTagNames);
1320                            }
1321                    }
1322    
1323                    // Expando
1324    
1325                    String expandoPath = null;
1326    
1327                    if (element != null) {
1328                            expandoPath = element.attributeValue("expando-path");
1329                    }
1330                    else {
1331                            expandoPath = getExpandoPath(path);
1332                    }
1333    
1334                    if (Validator.isNotNull(expandoPath)) {
1335                            try {
1336                                    Map<String, Serializable> expandoBridgeAttributes =
1337                                            (Map<String, Serializable>)getZipEntryAsObject(expandoPath);
1338    
1339                                    serviceContext.setExpandoBridgeAttributes(
1340                                            expandoBridgeAttributes);
1341                            }
1342                            catch (Exception e) {
1343                                    if (_log.isDebugEnabled()) {
1344                                            _log.debug(e, e);
1345                                    }
1346                            }
1347                    }
1348    
1349                    return serviceContext;
1350            }
1351    
1352            protected String getActionIds_5(
1353                            long companyId, long roleId, String className, String primKey,
1354                            List<String> actionIds)
1355                    throws SystemException {
1356    
1357                    List<String> availableActionIds = new ArrayList<String>(
1358                            actionIds.size());
1359    
1360                    for (String actionId : actionIds) {
1361                            if (PermissionLocalServiceUtil.hasRolePermission(
1362                                            roleId, companyId, className,
1363                                            ResourceConstants.SCOPE_INDIVIDUAL, primKey, actionId)) {
1364    
1365                                    availableActionIds.add(actionId);
1366                            }
1367                    }
1368    
1369                    return StringUtil.merge(availableActionIds);
1370            }
1371    
1372            protected Map<Long, Set<String>> getActionIds_6(
1373                            long companyId, long[] roleIds, String className, String primKey,
1374                            List<String> actionIds)
1375                    throws PortalException, SystemException {
1376    
1377                    return ResourcePermissionLocalServiceUtil.
1378                            getAvailableResourcePermissionActionIds(
1379                                    companyId, className, ResourceConstants.SCOPE_INDIVIDUAL,
1380                                    primKey, roleIds, actionIds);
1381            }
1382    
1383            protected long getClassPK(ClassedModel classedModel) {
1384                    if (classedModel instanceof ResourcedModel) {
1385                            ResourcedModel resourcedModel = (ResourcedModel)classedModel;
1386    
1387                            return resourcedModel.getResourcePrimKey();
1388                    }
1389                    else {
1390                            return (Long)classedModel.getPrimaryKeyObj();
1391                    }
1392            }
1393    
1394            protected String getExpandoPath(String path) {
1395                    int pos = path.lastIndexOf(".xml");
1396    
1397                    if (pos == -1) {
1398                            throw new IllegalArgumentException(
1399                                    path + " does not end with .xml");
1400                    }
1401    
1402                    return path.substring(0, pos).concat("-expando").concat(
1403                            path.substring(pos, path.length()));
1404            }
1405    
1406            protected String getPrimaryKeyString(Class<?> clazz, long classPK) {
1407                    return getPrimaryKeyString(clazz.getName(), String.valueOf(classPK));
1408            }
1409    
1410            protected String getPrimaryKeyString(Class<?> clazz, String primaryKey) {
1411                    return getPrimaryKeyString(clazz.getName(), primaryKey);
1412            }
1413    
1414            protected String getPrimaryKeyString(String className, long classPK) {
1415                    return getPrimaryKeyString(className, String.valueOf(classPK));
1416            }
1417    
1418            protected String getPrimaryKeyString(String className, String primaryKey) {
1419                    return className.concat(StringPool.POUND).concat(primaryKey);
1420            }
1421    
1422            protected void initXStream() {
1423                    _xStream = new XStream();
1424    
1425                    _xStream.alias("BlogsEntry", BlogsEntryImpl.class);
1426                    _xStream.alias("BookmarksFolder", BookmarksFolderImpl.class);
1427                    _xStream.alias("BookmarksEntry", BookmarksEntryImpl.class);
1428                    _xStream.alias("CalEvent", CalEventImpl.class);
1429                    _xStream.alias("DLFolder", DLFolderImpl.class);
1430                    _xStream.alias("DLFileEntry", DLFileEntryImpl.class);
1431                    _xStream.alias("DLFileShortcut", DLFileShortcutImpl.class);
1432                    _xStream.alias("DLFileRank", DLFileRankImpl.class);
1433                    _xStream.alias("JournalArticle", JournalArticleImpl.class);
1434                    _xStream.alias("JournalFeed", JournalFeedImpl.class);
1435                    _xStream.alias("JournalStructure", JournalStructureImpl.class);
1436                    _xStream.alias("JournalTemplate", JournalTemplateImpl.class);
1437                    _xStream.alias("Lock", LockImpl.class);
1438                    _xStream.alias("MBBan", MBBanImpl.class);
1439                    _xStream.alias("MBCategory", MBCategoryImpl.class);
1440                    _xStream.alias("MBMessage", MBMessageImpl.class);
1441                    _xStream.alias("MBThreadFlag", MBThreadFlagImpl.class);
1442                    _xStream.alias("PollsQuestion", PollsQuestionImpl.class);
1443                    _xStream.alias("PollsChoice", PollsChoiceImpl.class);
1444                    _xStream.alias("PollsVote", PollsVoteImpl.class);
1445                    _xStream.alias("RatingsEntry", RatingsEntryImpl.class);
1446                    _xStream.alias("WikiNode", WikiNodeImpl.class);
1447                    _xStream.alias("WikiPage", WikiPageImpl.class);
1448            }
1449    
1450            protected boolean isResourceMain(ClassedModel classedModel) {
1451                    if (classedModel instanceof ResourcedModel) {
1452                            ResourcedModel resourcedModel = (ResourcedModel)classedModel;
1453    
1454                            return resourcedModel.isResourceMain();
1455                    }
1456    
1457                    return true;
1458            }
1459    
1460            protected void validateDateRange(Date startDate, Date endDate)
1461                    throws PortletDataException {
1462    
1463                    if ((startDate == null) && (endDate != null)) {
1464                            throw new PortletDataException(
1465                                    PortletDataException.END_DATE_IS_MISSING_START_DATE);
1466                    }
1467                    else if ((startDate != null) && (endDate == null)) {
1468                            throw new PortletDataException(
1469                                    PortletDataException.START_DATE_IS_MISSING_END_DATE);
1470                    }
1471    
1472                    if (startDate != null) {
1473                            if (startDate.after(endDate) || startDate.equals(endDate)) {
1474                                    throw new PortletDataException(
1475                                            PortletDataException.START_DATE_AFTER_END_DATE);
1476                            }
1477    
1478                            Date now = new Date();
1479    
1480                            if (startDate.after(now)) {
1481                                    throw new PortletDataException(
1482                                            PortletDataException.FUTURE_START_DATE);
1483                            }
1484    
1485                            if (endDate.after(now)) {
1486                                    throw new PortletDataException(
1487                                            PortletDataException.FUTURE_END_DATE);
1488                            }
1489                    }
1490            }
1491    
1492            private static Log _log = LogFactoryUtil.getLog(
1493                    PortletDataContextImpl.class);
1494    
1495            private Map<String, long[]> _assetCategoryIdsMap =
1496                    new HashMap<String, long[]>();
1497            private Map<String, String[]> _assetCategoryUuidsMap =
1498                    new HashMap<String, String[]>();
1499            private Map<String, String[]> _assetLinkUuidsMap =
1500                    new HashMap<String, String[]>();
1501            private Map<String, String[]> _assetTagNamesMap =
1502                    new HashMap<String, String[]>();
1503            private Map<String, List<MBMessage>> _commentsMap =
1504                    new HashMap<String, List<MBMessage>>();
1505            private long _companyId;
1506            private String _dataStrategy;
1507            private Date _endDate;
1508            private Map<String, List<ExpandoColumn>> _expandoColumnsMap =
1509                    new HashMap<String, List<ExpandoColumn>>();
1510            private long _groupId;
1511            private Map<String, Lock> _locksMap = new HashMap<String, Lock>();
1512            private Map<String, Map<?, ?>> _newPrimaryKeysMaps =
1513                    new HashMap<String, Map<?, ?>>();
1514            private Set<String> _notUniquePerLayout = new HashSet<String>();
1515            private long _oldPlid;
1516            private Map<String, String[]> _parameterMap;
1517            private Map<String, List<KeyValuePair>> _permissionsMap =
1518                    new HashMap<String, List<KeyValuePair>>();
1519            private long _plid;
1520            private PortletDataContextListener _portletDataContextListener;
1521            private Set<String> _primaryKeys;
1522            private boolean _privateLayout;
1523            private Map<String, List<RatingsEntry>> _ratingsEntriesMap =
1524                    new HashMap<String, List<RatingsEntry>>();
1525            private long _scopeGroupId;
1526            private String _scopeLayoutUuid;
1527            private String _scopeType;
1528            private long _sourceGroupId;
1529            private Date _startDate;
1530            private UserIdStrategy _userIdStrategy;
1531            private XStream _xStream;
1532            private ZipReader _zipReader;
1533            private ZipWriter _zipWriter;
1534    
1535    }