1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.lar;
16  
17  import com.liferay.portal.NoSuchResourceException;
18  import com.liferay.portal.NoSuchRoleException;
19  import com.liferay.portal.PortalException;
20  import com.liferay.portal.SystemException;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.portal.kernel.util.KeyValuePair;
24  import com.liferay.portal.kernel.util.MapUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.zip.ZipReader;
28  import com.liferay.portal.kernel.zip.ZipWriter;
29  import com.liferay.portal.model.Group;
30  import com.liferay.portal.model.Lock;
31  import com.liferay.portal.model.Resource;
32  import com.liferay.portal.model.ResourceConstants;
33  import com.liferay.portal.model.Role;
34  import com.liferay.portal.model.RoleConstants;
35  import com.liferay.portal.model.impl.LockImpl;
36  import com.liferay.portal.security.permission.ResourceActionsUtil;
37  import com.liferay.portal.service.GroupLocalServiceUtil;
38  import com.liferay.portal.service.LockLocalServiceUtil;
39  import com.liferay.portal.service.PermissionLocalServiceUtil;
40  import com.liferay.portal.service.ResourceLocalServiceUtil;
41  import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
42  import com.liferay.portal.service.RoleLocalServiceUtil;
43  import com.liferay.portal.service.ServiceContext;
44  import com.liferay.portal.util.PropsValues;
45  import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
46  import com.liferay.portlet.bookmarks.model.impl.BookmarksEntryImpl;
47  import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
48  import com.liferay.portlet.calendar.model.impl.CalEventImpl;
49  import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
50  import com.liferay.portlet.documentlibrary.model.impl.DLFileRankImpl;
51  import com.liferay.portlet.documentlibrary.model.impl.DLFileShortcutImpl;
52  import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
53  import com.liferay.portlet.imagegallery.model.impl.IGFolderImpl;
54  import com.liferay.portlet.imagegallery.model.impl.IGImageImpl;
55  import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
56  import com.liferay.portlet.journal.model.impl.JournalFeedImpl;
57  import com.liferay.portlet.journal.model.impl.JournalStructureImpl;
58  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
59  import com.liferay.portlet.messageboards.NoSuchDiscussionException;
60  import com.liferay.portlet.messageboards.model.MBDiscussion;
61  import com.liferay.portlet.messageboards.model.MBMessage;
62  import com.liferay.portlet.messageboards.model.MBThread;
63  import com.liferay.portlet.messageboards.model.impl.MBBanImpl;
64  import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
65  import com.liferay.portlet.messageboards.model.impl.MBMessageFlagImpl;
66  import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
67  import com.liferay.portlet.messageboards.service.MBDiscussionLocalServiceUtil;
68  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
69  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
70  import com.liferay.portlet.polls.model.impl.PollsChoiceImpl;
71  import com.liferay.portlet.polls.model.impl.PollsQuestionImpl;
72  import com.liferay.portlet.polls.model.impl.PollsVoteImpl;
73  import com.liferay.portlet.ratings.model.RatingsEntry;
74  import com.liferay.portlet.ratings.model.impl.RatingsEntryImpl;
75  import com.liferay.portlet.ratings.service.RatingsEntryLocalServiceUtil;
76  import com.liferay.portlet.tags.model.TagsEntryConstants;
77  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
78  import com.liferay.portlet.wiki.model.impl.WikiNodeImpl;
79  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
80  
81  import com.thoughtworks.xstream.XStream;
82  
83  import java.io.IOException;
84  import java.io.InputStream;
85  
86  import java.util.ArrayList;
87  import java.util.Date;
88  import java.util.HashMap;
89  import java.util.HashSet;
90  import java.util.Iterator;
91  import java.util.List;
92  import java.util.Map;
93  import java.util.Set;
94  
95  /**
96   * <a href="PortletDataContextImpl.java.html"><b><i>View Source</i></b></a>
97   *
98   * <p>
99   * Holds context information that is used during exporting and importing portlet
100  * data.
101  * </p>
102  *
103  * @author Brian Wing Shun Chan
104  * @author Raymond Augé
105  * @author Bruno Farache
106  * @author Alex Chow
107  */
108 public class PortletDataContextImpl implements PortletDataContext {
109 
110     public PortletDataContextImpl(
111             long companyId, long groupId, Map<String, String[]> parameterMap,
112             Set<String> primaryKeys, Date startDate, Date endDate,
113             ZipWriter zipWriter)
114         throws PortletDataException {
115 
116         validateDateRange(startDate, endDate);
117 
118         _companyId = companyId;
119         _groupId = groupId;
120         _scopeGroupId = groupId;
121         _parameterMap = parameterMap;
122         _primaryKeys = primaryKeys;
123         _dataStrategy =  null;
124         _userIdStrategy = null;
125         _startDate = startDate;
126         _endDate = endDate;
127         _zipReader = null;
128         _zipWriter = zipWriter;
129 
130         initXStream();
131     }
132 
133     public PortletDataContextImpl(
134         long companyId, long groupId, Map<String, String[]> parameterMap,
135         Set<String> primaryKeys, UserIdStrategy userIdStrategy,
136         ZipReader zipReader) {
137 
138         _companyId = companyId;
139         _groupId = groupId;
140         _scopeGroupId = groupId;
141         _parameterMap = parameterMap;
142         _primaryKeys = primaryKeys;
143         _dataStrategy =  MapUtil.getString(
144             parameterMap, PortletDataHandlerKeys.DATA_STRATEGY,
145             PortletDataHandlerKeys.DATA_STRATEGY_MIRROR);
146         _userIdStrategy = userIdStrategy;
147         _zipReader = zipReader;
148         _zipWriter = null;
149 
150         initXStream();
151     }
152 
153     public void addComments(Class<?> classObj, long classPK)
154         throws SystemException {
155 
156         List<MBMessage> messages = MBMessageLocalServiceUtil.getMessages(
157             classObj.getName(), classPK);
158 
159         if (messages.size() == 0) {
160             return;
161         }
162 
163         Iterator<MBMessage> itr = messages.iterator();
164 
165         while (itr.hasNext()) {
166             MBMessage message = itr.next();
167 
168             message.setUserUuid(message.getUserUuid());
169 
170             addRatingsEntries(MBMessage.class, message.getPrimaryKey());
171         }
172 
173         _commentsMap.put(getPrimaryKeyString(classObj, classPK), messages);
174     }
175 
176     public void addComments(
177         String className, long classPK, List<MBMessage> messages) {
178 
179         _commentsMap.put(getPrimaryKeyString(className, classPK), messages);
180     }
181 
182     public void addLocks(Class<?> classObj, String key)
183         throws PortalException, SystemException {
184 
185         if (!_locksMap.containsKey(getPrimaryKeyString(classObj, key)) &&
186             LockLocalServiceUtil.isLocked(classObj.getName(), key)) {
187 
188             Lock lock = LockLocalServiceUtil.getLock(classObj.getName(), key);
189 
190             addLocks(classObj.getName(), key, lock);
191         }
192     }
193 
194     public void addLocks(String className, String key, Lock lock) {
195         _locksMap.put(getPrimaryKeyString(className, key), lock);
196     }
197 
198     public void addPermissions(Class<?> classObj, long classPK)
199         throws PortalException, SystemException {
200 
201         addPermissions(classObj.getName(), classPK);
202     }
203 
204     public void addPermissions(String resourceName, long resourcePK)
205         throws PortalException, SystemException {
206 
207         if (((PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 5) &&
208              (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6)) ||
209             (!MapUtil.getBoolean(
210                 _parameterMap, PortletDataHandlerKeys.PERMISSIONS))) {
211 
212             return;
213         }
214 
215         List<KeyValuePair> permissions = new ArrayList<KeyValuePair>();
216 
217         Group group = GroupLocalServiceUtil.getGroup(_groupId);
218 
219         List<Role> roles = RoleLocalServiceUtil.getRoles(_companyId);
220 
221         for (Role role : roles) {
222             int type = role.getType();
223 
224             if ((type == RoleConstants.TYPE_REGULAR) ||
225                 ((type == RoleConstants.TYPE_COMMUNITY) &&
226                  (group.isCommunity())) ||
227                 ((type == RoleConstants.TYPE_ORGANIZATION) &&
228                  (group.isOrganization()))) {
229 
230                 String name = role.getName();
231                 String actionIds = getActionIds(
232                     role, resourceName, String.valueOf(resourcePK));
233 
234                 KeyValuePair permission = new KeyValuePair(name, actionIds);
235 
236                 permissions.add(permission);
237             }
238         }
239 
240         _permissionsMap.put(
241             getPrimaryKeyString(resourceName, resourcePK), permissions);
242     }
243 
244     public void addPermissions(
245         String resourceName, long resourcePK, List<KeyValuePair> permissions) {
246 
247         if ((PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 5) &&
248             (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6)) {
249 
250             return;
251         }
252 
253         _permissionsMap.put(
254             getPrimaryKeyString(resourceName, resourcePK), permissions);
255     }
256 
257     public boolean addPrimaryKey(Class<?> classObj, String primaryKey) {
258         boolean value = hasPrimaryKey(classObj, primaryKey);
259 
260         if (!value) {
261             _primaryKeys.add(getPrimaryKeyString(classObj, primaryKey));
262         }
263 
264         return value;
265     }
266 
267     public void addRatingsEntries(Class<?> classObj, long classPK)
268         throws SystemException {
269 
270         List<RatingsEntry> ratingsEntries =
271             RatingsEntryLocalServiceUtil.getEntries(
272                 classObj.getName(), classPK);
273 
274         if (ratingsEntries.size() == 0) {
275             return;
276         }
277 
278         Iterator<RatingsEntry> itr = ratingsEntries.iterator();
279 
280         while (itr.hasNext()) {
281             RatingsEntry entry = itr.next();
282 
283             entry.setUserUuid(entry.getUserUuid());
284         }
285 
286         _ratingsEntriesMap.put(
287             getPrimaryKeyString(classObj, classPK), ratingsEntries);
288     }
289 
290     public void addRatingsEntries(
291         String className, long classPK, List<RatingsEntry> ratingsEntries) {
292 
293         _ratingsEntriesMap.put(
294             getPrimaryKeyString(className, classPK), ratingsEntries);
295     }
296 
297     public void addTagsCategories(Class<?> classObj, long classPK)
298         throws SystemException {
299 
300         String[] tagsCategories = TagsEntryLocalServiceUtil.getEntryNames(
301             classObj.getName(), classPK,
302             TagsEntryConstants.FOLKSONOMY_CATEGORY);
303 
304         if (tagsCategories.length == 0) {
305             return;
306         }
307 
308         _tagsCategoriesMap.put(
309             getPrimaryKeyString(classObj, classPK), tagsCategories);
310     }
311 
312     public void addTagsCategories(
313         String className, long classPK, String[] tagsCategories) {
314 
315         _tagsCategoriesMap.put(
316             getPrimaryKeyString(className, classPK), tagsCategories);
317     }
318 
319     public void addTagsEntries(Class<?> classObj, long classPK)
320         throws SystemException {
321 
322         String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
323             classObj.getName(), classPK, TagsEntryConstants.FOLKSONOMY_TAG);
324 
325         if (tagsEntries.length == 0) {
326             return;
327         }
328 
329         _tagsEntriesMap.put(
330             getPrimaryKeyString(classObj, classPK), tagsEntries);
331     }
332 
333     public void addTagsEntries(
334         String className, long classPK, String[] tagsEntries) {
335 
336         _tagsEntriesMap.put(
337             getPrimaryKeyString(className, classPK), tagsEntries);
338     }
339 
340     public void addZipEntry(String path, byte[] bytes) throws SystemException {
341         try {
342             getZipWriter().addEntry(path, bytes);
343         }
344         catch (IOException ioe) {
345             throw new SystemException(ioe);
346         }
347     }
348 
349     public void addZipEntry(String path, InputStream is)
350         throws SystemException {
351 
352         try {
353             getZipWriter().addEntry(path, is);
354         }
355         catch (IOException ioe) {
356             throw new SystemException(ioe);
357         }
358     }
359 
360     public void addZipEntry(String path, Object object) throws SystemException {
361         addZipEntry(path, toXML(object));
362     }
363 
364     public void addZipEntry(String path, String s) throws SystemException {
365         try {
366             getZipWriter().addEntry(path, s);
367         }
368         catch (IOException ioe) {
369             throw new SystemException(ioe);
370         }
371     }
372 
373     public void addZipEntry(String path, StringBuilder sb)
374         throws SystemException {
375 
376         try {
377             getZipWriter().addEntry(path, sb);
378         }
379         catch (IOException ioe) {
380             throw new SystemException(ioe);
381         }
382     }
383 
384     public Object fromXML(byte[] bytes) {
385         return _xStream.fromXML(new String(bytes));
386     }
387 
388     public Object fromXML(String xml) {
389         return _xStream.fromXML(xml);
390     }
391 
392     public boolean getBooleanParameter(String namespace, String name) {
393         boolean defaultValue = MapUtil.getBoolean(
394             getParameterMap(),
395             PortletDataHandlerKeys.PORTLET_DATA_CONTROL_DEFAULT, true);
396 
397         return MapUtil.getBoolean(
398             getParameterMap(),
399             PortletDataHandlerControl.getNamespacedControlName(namespace, name),
400             defaultValue);
401     }
402 
403     public ClassLoader getClassLoader() {
404         return _xStream.getClassLoader();
405     }
406 
407     public Map<String, List<MBMessage>> getComments() {
408         return _commentsMap;
409     }
410 
411     public long getCompanyId() {
412         return _companyId;
413     }
414 
415     public String getDataStrategy() {
416         return _dataStrategy;
417     }
418 
419     public Date getEndDate() {
420         return _endDate;
421     }
422 
423     public long getGroupId() {
424         return _groupId;
425     }
426 
427     public String getLayoutPath(long layoutId) {
428         return getRootPath() + ROOT_PATH_LAYOUTS + layoutId;
429     }
430 
431     public Map<String, Lock> getLocks() {
432         return _locksMap;
433     }
434 
435     public Map<?, ?> getNewPrimaryKeysMap(Class<?> classObj) {
436         return getNewPrimaryKeysMap(classObj.getName());
437     }
438 
439     public Map<?, ?> getNewPrimaryKeysMap(String className) {
440         Map<?, ?> map = _newPrimaryKeysMaps.get(className);
441 
442         if (map == null) {
443             map = new HashMap<Object, Object>();
444 
445             _newPrimaryKeysMaps.put(className, map);
446         }
447 
448         return map;
449     }
450 
451     public long getOldPlid() {
452         return _oldPlid;
453     }
454 
455     public Map<String, String[]> getParameterMap() {
456         return _parameterMap;
457     }
458 
459     public Map<String, List<KeyValuePair>> getPermissions() {
460         return _permissionsMap;
461     }
462 
463     public long getPlid() {
464         return _plid;
465     }
466 
467     public String getPortletPath(String portletId) {
468         return getRootPath() + ROOT_PATH_PORTLETS + portletId;
469     }
470 
471     public Set<String> getPrimaryKeys() {
472         return _primaryKeys;
473     }
474 
475     public Map<String, List<RatingsEntry>> getRatingsEntries() {
476         return _ratingsEntriesMap;
477     }
478 
479     public String getRootPath() {
480         return ROOT_PATH_GROUPS + getScopeGroupId();
481     }
482 
483     public long getScopeGroupId() {
484         return _scopeGroupId;
485     }
486 
487     public long getScopeLayoutId() {
488         return _scopeLayoutId;
489     }
490 
491     public long getSourceGroupId() {
492         return _sourceGroupId;
493     }
494 
495     public String getSourceLayoutPath(long layoutId) {
496         return getSourceRootPath() + ROOT_PATH_LAYOUTS + layoutId;
497     }
498 
499     public String getSourcePortletPath(String portletId) {
500         return getSourceRootPath() + ROOT_PATH_PORTLETS + portletId;
501     }
502 
503     public String getSourceRootPath() {
504         return ROOT_PATH_GROUPS + getSourceGroupId();
505     }
506 
507     public Date getStartDate() {
508         return _startDate;
509     }
510 
511     public Map<String, String[]> getTagsCategories() {
512         return _tagsCategoriesMap;
513     }
514 
515     public String[] getTagsCategories(Class<?> classObj, long classPK) {
516         return _tagsCategoriesMap.get(
517             getPrimaryKeyString(classObj, classPK));
518     }
519 
520     public Map<String, String[]> getTagsEntries() {
521         return _tagsEntriesMap;
522     }
523 
524     public String[] getTagsEntries(Class<?> classObj, long classPK) {
525         return _tagsEntriesMap.get(getPrimaryKeyString(classObj, classPK));
526     }
527 
528     public String[] getTagsEntries(String className, long classPK) {
529         return _tagsEntriesMap.get(getPrimaryKeyString(className, classPK));
530     }
531 
532     public long getUserId(String userUuid) throws SystemException {
533         return _userIdStrategy.getUserId(userUuid);
534     }
535 
536     public UserIdStrategy getUserIdStrategy() {
537         return _userIdStrategy;
538     }
539 
540     public List<String> getZipEntries() {
541         return getZipReader().getEntries();
542     }
543 
544     public byte[] getZipEntryAsByteArray(String path) {
545         return getZipReader().getEntryAsByteArray(path);
546     }
547 
548     public InputStream getZipEntryAsInputStream(String path) {
549         return getZipReader().getEntryAsInputStream(path);
550     }
551 
552     public Object getZipEntryAsObject(String path) {
553         return fromXML(getZipEntryAsString(path));
554     }
555 
556     public String getZipEntryAsString(String path) {
557         return getZipReader().getEntryAsString(path);
558     }
559 
560     public List<String> getZipFolderEntries() {
561         return getZipFolderEntries(StringPool.SLASH);
562     }
563 
564     public List<String> getZipFolderEntries(String path) {
565         return getZipReader().getFolderEntries(path);
566     }
567 
568     public ZipReader getZipReader() {
569         return _zipReader;
570     }
571 
572     public ZipWriter getZipWriter() {
573         return _zipWriter;
574     }
575 
576     public boolean hasDateRange() {
577         if (_startDate != null) {
578             return true;
579         }
580         else {
581             return false;
582         }
583     }
584 
585     public boolean hasNotUniquePerLayout(String dataKey) {
586         return _notUniquePerLayout.contains(dataKey);
587     }
588 
589     public boolean hasPrimaryKey(Class<?> classObj, String primaryKey) {
590         return _primaryKeys.contains(getPrimaryKeyString(classObj, primaryKey));
591     }
592 
593     public void importComments(
594             Class<?> classObj, long classPK, long newClassPK, long groupId)
595         throws PortalException, SystemException {
596 
597         Map<Long, Long> messagePKs = new HashMap<Long, Long>();
598         Map<Long, Long> threadPKs = new HashMap<Long, Long>();
599 
600         List<MBMessage> messages = _commentsMap.get(
601             getPrimaryKeyString(classObj, classPK));
602 
603         if (messages == null) {
604             return;
605         }
606 
607         MBDiscussion discussion = null;
608 
609         try {
610             discussion = MBDiscussionLocalServiceUtil.getDiscussion(
611                 classObj.getName(), newClassPK);
612         }
613         catch (NoSuchDiscussionException nsde) {
614         }
615 
616         for (MBMessage message : messages) {
617             long userId = getUserId(message.getUserUuid());
618             long parentMessageId = MapUtil.getLong(
619                 messagePKs, message.getParentMessageId(),
620                 message.getParentMessageId());
621             long threadId = MapUtil.getLong(
622                 threadPKs, message.getThreadId(), message.getThreadId());
623 
624             if ((message.getParentMessageId() ==
625                     MBMessageImpl.DEFAULT_PARENT_MESSAGE_ID) &&
626                 (discussion != null)) {
627 
628                 MBThread thread = MBThreadLocalServiceUtil.getThread(
629                     discussion.getThreadId());
630 
631                 long rootMessageId = thread.getRootMessageId();
632 
633                 messagePKs.put(message.getMessageId(), rootMessageId);
634                 threadPKs.put(message.getThreadId(), thread.getThreadId());
635             }
636             else {
637                 ServiceContext serviceContext = new ServiceContext();
638 
639                 serviceContext.setScopeGroupId(groupId);
640 
641                 MBMessage newMessage =
642                     MBMessageLocalServiceUtil.addDiscussionMessage(
643                         userId, message.getUserName(), classObj.getName(),
644                         newClassPK, threadId, parentMessageId,
645                         message.getSubject(), message.getBody(),
646                         serviceContext);
647 
648                 messagePKs.put(
649                     message.getMessageId(), newMessage.getMessageId());
650                 threadPKs.put(message.getThreadId(), newMessage.getThreadId());
651             }
652 
653             importRatingsEntries(
654                 MBMessage.class, message.getPrimaryKey(),
655                 messagePKs.get(message.getPrimaryKey()));
656         }
657     }
658 
659     public void importLocks(Class<?> classObj, String key, String newKey)
660         throws PortalException, SystemException {
661 
662         Lock lock = _locksMap.get(getPrimaryKeyString(classObj, key));
663 
664         if (lock == null) {
665             return;
666         }
667 
668         long userId = getUserId(lock.getUserUuid());
669 
670         long expirationTime = 0;
671 
672         if (lock.getExpirationDate() != null) {
673             Date expirationDate = lock.getExpirationDate();
674 
675             expirationTime = expirationDate.getTime();
676         }
677 
678         LockLocalServiceUtil.lock(
679             userId, classObj.getName(), newKey, lock.getOwner(),
680             lock.getInheritable(), expirationTime);
681     }
682 
683     public void importPermissions(
684             Class<?> classObj, long classPK, long newClassPK)
685         throws PortalException, SystemException {
686 
687         importPermissions(classObj.getName(), classPK, newClassPK);
688     }
689 
690     public void importPermissions(
691             String resourceName, long resourcePK, long newResourcePK)
692         throws PortalException, SystemException {
693 
694         if (((PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 5) &&
695              (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6)) ||
696             (!MapUtil.getBoolean(
697                 _parameterMap, PortletDataHandlerKeys.PERMISSIONS))) {
698 
699             return;
700         }
701 
702         List<KeyValuePair> permissions = _permissionsMap.get(
703             getPrimaryKeyString(resourceName, resourcePK));
704 
705         if (permissions == null) {
706             return;
707         }
708 
709         for (KeyValuePair permission : permissions) {
710             String roleName = permission.getKey();
711 
712             Role role = null;
713 
714             try {
715                 role = RoleLocalServiceUtil.getRole(_companyId, roleName);
716             }
717             catch (NoSuchRoleException nsre) {
718                 if (_log.isWarnEnabled()) {
719                     _log.warn("Role " + roleName + " does not exist");
720                 }
721 
722                 continue;
723             }
724 
725             String[] actionIds = StringUtil.split(permission.getValue());
726 
727             if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
728                 Resource resource = null;
729 
730                 try {
731                     resource = ResourceLocalServiceUtil.getResource(
732                         _companyId, resourceName,
733                         ResourceConstants.SCOPE_INDIVIDUAL,
734                         String.valueOf(newResourcePK));
735                 }
736                 catch (NoSuchResourceException nsre) {
737                     resource = ResourceLocalServiceUtil.addResource(
738                         _companyId, resourceName,
739                         ResourceConstants.SCOPE_INDIVIDUAL,
740                         String.valueOf(newResourcePK));
741                 }
742 
743                 PermissionLocalServiceUtil.setRolePermissions(
744                     role.getRoleId(), actionIds, resource.getResourceId());
745             }
746             else if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
747                 ResourcePermissionLocalServiceUtil.setResourcePermissions(
748                     _companyId, resourceName,
749                     ResourceConstants.SCOPE_INDIVIDUAL,
750                     String.valueOf(newResourcePK), role.getRoleId(), actionIds);
751             }
752         }
753     }
754 
755     public void importRatingsEntries(
756             Class<?> classObj, long classPK, long newClassPK)
757         throws PortalException, SystemException {
758 
759         List<RatingsEntry> ratingsEntries = _ratingsEntriesMap.get(
760             getPrimaryKeyString(classObj, classPK));
761 
762         if (ratingsEntries == null) {
763             return;
764         }
765 
766         ServiceContext serviceContext = new ServiceContext();
767 
768         for (RatingsEntry ratingsEntry : ratingsEntries) {
769             long userId = getUserId(ratingsEntry.getUserUuid());
770 
771             serviceContext.setCreateDate(ratingsEntry.getCreateDate());
772             serviceContext.setModifiedDate(ratingsEntry.getModifiedDate());
773 
774             RatingsEntryLocalServiceUtil.updateEntry(
775                 userId, classObj.getName(), ((Long)newClassPK).longValue(),
776                 ratingsEntry.getScore(), serviceContext);
777         }
778     }
779 
780     public boolean isPathNotProcessed(String path) {
781         return !addPrimaryKey(String.class, path);
782     }
783 
784     public boolean isPrivateLayout() {
785         return _privateLayout;
786     }
787 
788     public boolean isWithinDateRange(Date modifiedDate) {
789         if (!hasDateRange()) {
790             return true;
791         }
792         else if ((_startDate.compareTo(modifiedDate) <= 0) &&
793                  (_endDate.after(modifiedDate))) {
794 
795             return true;
796         }
797         else {
798             return false;
799         }
800     }
801 
802     public void putNotUniquePerLayout(String dataKey) {
803         _notUniquePerLayout.add(dataKey);
804     }
805 
806     public void setClassLoader(ClassLoader classLoader) {
807         _xStream.setClassLoader(classLoader);
808     }
809 
810     public void setGroupId(long groupId) {
811         _groupId = groupId;
812     }
813 
814     public void setOldPlid(long oldPlid) {
815         _oldPlid = oldPlid;
816     }
817 
818     public void setPlid(long plid) {
819         _plid = plid;
820     }
821 
822     public void setPrivateLayout(boolean privateLayout) {
823         _privateLayout = privateLayout;
824     }
825 
826     public void setScopeGroupId(long scopeGroupId) {
827         _scopeGroupId = scopeGroupId;
828     }
829 
830     public void setScopeLayoutId(long scopeLayoutId) {
831         _scopeLayoutId = scopeLayoutId;
832     }
833 
834     public void setSourceGroupId(long sourceGroupId) {
835         _sourceGroupId = sourceGroupId;
836     }
837 
838     public String toXML(Object object) {
839         return _xStream.toXML(object);
840     }
841 
842     protected String getActionIds(
843             Role role, String className, String primKey)
844         throws PortalException, SystemException {
845 
846         List<String> allActionIds = ResourceActionsUtil.getModelResourceActions(
847             className);
848 
849         List<String> actionIds = new ArrayList<String>(allActionIds.size());
850 
851         for (String actionId : allActionIds) {
852             if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
853                 if (PermissionLocalServiceUtil.hasRolePermission(
854                         role.getRoleId(), role.getCompanyId(), className,
855                         ResourceConstants.SCOPE_INDIVIDUAL, primKey,
856                         actionId)) {
857 
858                     actionIds.add(actionId);
859                 }
860             }
861             else if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
862                 if (ResourcePermissionLocalServiceUtil.hasResourcePermission(
863                         role.getCompanyId(), className,
864                         ResourceConstants.SCOPE_INDIVIDUAL, primKey,
865                         role.getRoleId(), actionId)) {
866 
867                     actionIds.add(actionId);
868                 }
869             }
870         }
871 
872         return StringUtil.merge(actionIds);
873     }
874 
875     protected String getPrimaryKeyString(Class<?> classObj, long classPK) {
876         return getPrimaryKeyString(classObj.getName(), String.valueOf(classPK));
877     }
878 
879     protected String getPrimaryKeyString(Class<?> classObj, String primaryKey) {
880         return getPrimaryKeyString(classObj.getName(), primaryKey);
881     }
882 
883     protected String getPrimaryKeyString(String className, long classPK) {
884         return getPrimaryKeyString(className, String.valueOf(classPK));
885     }
886 
887     protected String getPrimaryKeyString(String className, String primaryKey) {
888         return className.concat(StringPool.POUND).concat(primaryKey);
889     }
890 
891     protected void initXStream() {
892         _xStream = new XStream();
893 
894         _xStream.alias("BlogsEntry", BlogsEntryImpl.class);
895         _xStream.alias("BookmarksFolder", BookmarksFolderImpl.class);
896         _xStream.alias("BookmarksEntry", BookmarksEntryImpl.class);
897         _xStream.alias("CalEvent", CalEventImpl.class);
898         _xStream.alias("DLFolder", DLFolderImpl.class);
899         _xStream.alias("DLFileEntry", DLFileEntryImpl.class);
900         _xStream.alias("DLFileShortcut", DLFileShortcutImpl.class);
901         _xStream.alias("DLFileRank", DLFileRankImpl.class);
902         _xStream.alias("IGFolder", IGFolderImpl.class);
903         _xStream.alias("IGImage", IGImageImpl.class);
904         _xStream.alias("JournalArticle", JournalArticleImpl.class);
905         _xStream.alias("JournalFeed", JournalFeedImpl.class);
906         _xStream.alias("JournalStructure", JournalStructureImpl.class);
907         _xStream.alias("JournalTemplate", JournalTemplateImpl.class);
908         _xStream.alias("Lock", LockImpl.class);
909         _xStream.alias("MBCategory", MBCategoryImpl.class);
910         _xStream.alias("MBMessage", MBMessageImpl.class);
911         _xStream.alias("MBMessageFlag", MBMessageFlagImpl.class);
912         _xStream.alias("MBBan", MBBanImpl.class);
913         _xStream.alias("PollsQuestion", PollsQuestionImpl.class);
914         _xStream.alias("PollsChoice", PollsChoiceImpl.class);
915         _xStream.alias("PollsVote", PollsVoteImpl.class);
916         _xStream.alias("RatingsEntry", RatingsEntryImpl.class);
917         _xStream.alias("WikiNode", WikiNodeImpl.class);
918         _xStream.alias("WikiPage", WikiPageImpl.class);
919     }
920 
921     protected void validateDateRange(Date startDate, Date endDate)
922         throws PortletDataException {
923 
924         if ((startDate == null) ^ (endDate == null)) {
925             throw new PortletDataException(
926                 "Both start and end dates must have valid values or be null");
927         }
928 
929         if (startDate != null) {
930             if (startDate.after(endDate) || startDate.equals(endDate)) {
931                 throw new PortletDataException(
932                     "The start date cannot be after the end date");
933             }
934 
935             Date now = new Date();
936 
937             if (startDate.after(now) || endDate.after(now)) {
938                 throw new PortletDataException(
939                     "Dates must not be in the future");
940             }
941         }
942     }
943 
944     private static Log _log = LogFactoryUtil.getLog(
945         PortletDataContextImpl.class);
946 
947     private Map<String, List<MBMessage>> _commentsMap =
948         new HashMap<String, List<MBMessage>>();
949     private long _companyId;
950     private String _dataStrategy;
951     private Date _endDate;
952     private long _groupId;
953     private Map<String, Lock> _locksMap = new HashMap<String, Lock>();
954     private Map<String, Map<?, ?>> _newPrimaryKeysMaps =
955         new HashMap<String, Map<?, ?>>();
956     private Set<String> _notUniquePerLayout = new HashSet<String>();
957     private long _oldPlid;
958     private Map<String, String[]> _parameterMap;
959     private Map<String, List<KeyValuePair>> _permissionsMap =
960         new HashMap<String, List<KeyValuePair>>();
961     private long _plid;
962     private Set<String> _primaryKeys;
963     private boolean _privateLayout;
964     private Map<String, List<RatingsEntry>> _ratingsEntriesMap =
965         new HashMap<String, List<RatingsEntry>>();
966     private long _scopeGroupId;
967     private long _scopeLayoutId;
968     private long _sourceGroupId;
969     private Date _startDate;
970     private Map<String, String[]> _tagsCategoriesMap =
971         new HashMap<String, String[]>();
972     private Map<String, String[]> _tagsEntriesMap =
973         new HashMap<String, String[]>();
974     private UserIdStrategy _userIdStrategy;
975     private XStream _xStream;
976     private ZipReader _zipReader;
977     private ZipWriter _zipWriter;
978 
979 }