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