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