1
22
23 package com.liferay.portal.lar;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.util.MapUtil;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.zip.ZipReader;
30 import com.liferay.portal.kernel.zip.ZipWriter;
31 import com.liferay.portal.service.ServiceContext;
32 import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
33 import com.liferay.portlet.bookmarks.model.impl.BookmarksEntryImpl;
34 import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
35 import com.liferay.portlet.calendar.model.impl.CalEventImpl;
36 import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
37 import com.liferay.portlet.documentlibrary.model.impl.DLFileRankImpl;
38 import com.liferay.portlet.documentlibrary.model.impl.DLFileShortcutImpl;
39 import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
40 import com.liferay.portlet.imagegallery.model.impl.IGFolderImpl;
41 import com.liferay.portlet.imagegallery.model.impl.IGImageImpl;
42 import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
43 import com.liferay.portlet.journal.model.impl.JournalFeedImpl;
44 import com.liferay.portlet.journal.model.impl.JournalStructureImpl;
45 import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
46 import com.liferay.portlet.messageboards.NoSuchDiscussionException;
47 import com.liferay.portlet.messageboards.model.MBDiscussion;
48 import com.liferay.portlet.messageboards.model.MBMessage;
49 import com.liferay.portlet.messageboards.model.MBThread;
50 import com.liferay.portlet.messageboards.model.impl.MBBanImpl;
51 import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
52 import com.liferay.portlet.messageboards.model.impl.MBMessageFlagImpl;
53 import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
54 import com.liferay.portlet.messageboards.service.MBDiscussionLocalServiceUtil;
55 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
56 import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
57 import com.liferay.portlet.polls.model.impl.PollsChoiceImpl;
58 import com.liferay.portlet.polls.model.impl.PollsQuestionImpl;
59 import com.liferay.portlet.polls.model.impl.PollsVoteImpl;
60 import com.liferay.portlet.ratings.model.RatingsEntry;
61 import com.liferay.portlet.ratings.model.impl.RatingsEntryImpl;
62 import com.liferay.portlet.ratings.service.RatingsEntryLocalServiceUtil;
63 import com.liferay.portlet.tags.model.TagsEntryConstants;
64 import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
65 import com.liferay.portlet.wiki.model.impl.WikiNodeImpl;
66 import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
67
68 import com.thoughtworks.xstream.XStream;
69
70 import java.io.IOException;
71 import java.io.InputStream;
72
73 import java.util.Date;
74 import java.util.HashMap;
75 import java.util.HashSet;
76 import java.util.Iterator;
77 import java.util.List;
78 import java.util.Map;
79 import java.util.Set;
80
81
94 public class PortletDataContextImpl implements PortletDataContext {
95
96 public PortletDataContextImpl(
97 long companyId, long groupId, Map<String, String[]> parameterMap,
98 Set<String> primaryKeys, Date startDate, Date endDate,
99 ZipWriter zipWriter)
100 throws PortletDataException {
101
102 validateDateRange(startDate, endDate);
103
104 _companyId = companyId;
105 _groupId = groupId;
106 _scopeGroupId = groupId;
107 _parameterMap = parameterMap;
108 _primaryKeys = primaryKeys;
109 _dataStrategy = null;
110 _userIdStrategy = null;
111 _startDate = startDate;
112 _endDate = endDate;
113 _zipReader = null;
114 _zipWriter = zipWriter;
115
116 initXStream();
117 }
118
119 public PortletDataContextImpl(
120 long companyId, long groupId, Map<String, String[]> parameterMap,
121 Set<String> primaryKeys, UserIdStrategy userIdStrategy,
122 ZipReader zipReader) {
123
124 _companyId = companyId;
125 _groupId = groupId;
126 _scopeGroupId = groupId;
127 _parameterMap = parameterMap;
128 _primaryKeys = primaryKeys;
129 _dataStrategy = MapUtil.getString(
130 parameterMap, PortletDataHandlerKeys.DATA_STRATEGY,
131 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR);
132 _userIdStrategy = userIdStrategy;
133 _zipReader = zipReader;
134 _zipWriter = null;
135
136 initXStream();
137 }
138
139 public void addComments(Class<?> classObj, long classPK)
140 throws SystemException {
141
142 List<MBMessage> messages = MBMessageLocalServiceUtil.getMessages(
143 classObj.getName(), classPK);
144
145 if (messages.size() == 0) {
146 return;
147 }
148
149 Iterator<MBMessage> itr = messages.iterator();
150
151 while (itr.hasNext()) {
152 MBMessage message = itr.next();
153
154 message.setUserUuid(message.getUserUuid());
155
156 addRatingsEntries(MBMessage.class, message.getPrimaryKey());
157 }
158
159 _commentsMap.put(getPrimaryKeyString(classObj, classPK), messages);
160 }
161
162 public void addComments(
163 String className, long classPK, List<MBMessage> messages) {
164
165 _commentsMap.put(getPrimaryKeyString(className, classPK), messages);
166 }
167
168 public boolean addPrimaryKey(Class<?> classObj, String primaryKey) {
169 boolean value = hasPrimaryKey(classObj, primaryKey);
170
171 if (!value) {
172 _primaryKeys.add(getPrimaryKeyString(classObj, primaryKey));
173 }
174
175 return value;
176 }
177
178 public void addRatingsEntries(Class<?> classObj, long classPK)
179 throws SystemException {
180
181 List<RatingsEntry> ratingsEntries =
182 RatingsEntryLocalServiceUtil.getEntries(
183 classObj.getName(), classPK);
184
185 if (ratingsEntries.size() == 0) {
186 return;
187 }
188
189 Iterator<RatingsEntry> itr = ratingsEntries.iterator();
190
191 while (itr.hasNext()) {
192 RatingsEntry entry = itr.next();
193
194 entry.setUserUuid(entry.getUserUuid());
195 }
196
197 _ratingsEntriesMap.put(
198 getPrimaryKeyString(classObj, classPK), ratingsEntries);
199 }
200
201 public void addRatingsEntries(
202 String className, long classPK, List<RatingsEntry> ratingsEntries) {
203
204 _ratingsEntriesMap.put(
205 getPrimaryKeyString(className, classPK), ratingsEntries);
206 }
207
208 public void addTagsCategories(Class<?> classObj, long classPK)
209 throws SystemException {
210
211 String[] tagsCategories = TagsEntryLocalServiceUtil.getEntryNames(
212 classObj.getName(), classPK,
213 TagsEntryConstants.FOLKSONOMY_CATEGORY);
214
215 if (tagsCategories.length == 0) {
216 return;
217 }
218
219 _tagsCategoriesMap.put(
220 getPrimaryKeyString(classObj, classPK), tagsCategories);
221 }
222
223 public void addTagsCategories(
224 String className, long classPK, String[] tagsCategories) {
225
226 _tagsCategoriesMap.put(
227 getPrimaryKeyString(className, classPK), tagsCategories);
228 }
229
230 public void addTagsEntries(Class<?> classObj, long classPK)
231 throws SystemException {
232
233 String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
234 classObj.getName(), classPK, TagsEntryConstants.FOLKSONOMY_TAG);
235
236 if (tagsEntries.length == 0) {
237 return;
238 }
239
240 _tagsEntriesMap.put(
241 getPrimaryKeyString(classObj, classPK), tagsEntries);
242 }
243
244 public void addTagsEntries(
245 String className, long classPK, String[] tagsEntries) {
246
247 _tagsEntriesMap.put(
248 getPrimaryKeyString(className, classPK), tagsEntries);
249 }
250
251 public void addZipEntry(String path, byte[] bytes) throws SystemException {
252 try {
253 getZipWriter().addEntry(path, bytes);
254 }
255 catch (IOException ioe) {
256 throw new SystemException(ioe);
257 }
258 }
259
260 public void addZipEntry(String path, InputStream is)
261 throws SystemException {
262
263 try {
264 getZipWriter().addEntry(path, is);
265 }
266 catch (IOException ioe) {
267 throw new SystemException(ioe);
268 }
269 }
270
271 public void addZipEntry(String path, Object object) throws SystemException {
272 addZipEntry(path, toXML(object));
273 }
274
275 public void addZipEntry(String path, String s) throws SystemException {
276 try {
277 getZipWriter().addEntry(path, s);
278 }
279 catch (IOException ioe) {
280 throw new SystemException(ioe);
281 }
282 }
283
284 public void addZipEntry(String path, StringBuilder sb)
285 throws SystemException {
286
287 try {
288 getZipWriter().addEntry(path, sb);
289 }
290 catch (IOException ioe) {
291 throw new SystemException(ioe);
292 }
293 }
294
295 public Object fromXML(byte[] bytes) {
296 return _xStream.fromXML(new String(bytes));
297 }
298
299 public Object fromXML(String xml) {
300 return _xStream.fromXML(xml);
301 }
302
303 public boolean getBooleanParameter(String namespace, String name) {
304 boolean defaultValue = MapUtil.getBoolean(
305 getParameterMap(),
306 PortletDataHandlerKeys.PORTLET_DATA_CONTROL_DEFAULT, true);
307
308 return MapUtil.getBoolean(
309 getParameterMap(),
310 PortletDataHandlerControl.getNamespacedControlName(namespace, name),
311 defaultValue);
312 }
313
314 public ClassLoader getClassLoader() {
315 return _xStream.getClassLoader();
316 }
317
318 public Map<String, List<MBMessage>> getComments() {
319 return _commentsMap;
320 }
321
322 public long getCompanyId() {
323 return _companyId;
324 }
325
326 public String getDataStrategy() {
327 return _dataStrategy;
328 }
329
330 public Date getEndDate() {
331 return _endDate;
332 }
333
334 public long getGroupId() {
335 return _groupId;
336 }
337
338 public String getLayoutPath(long layoutId) {
339 return getRootPath() + ROOT_PATH_LAYOUTS + layoutId;
340 }
341
342 public Map<?, ?> getNewPrimaryKeysMap(Class<?> classObj) {
343 Map<?, ?> map = _newPrimaryKeysMaps.get(classObj.getName());
344
345 if (map == null) {
346 map = new HashMap<Object, Object>();
347
348 _newPrimaryKeysMaps.put(classObj.getName(), map);
349 }
350
351 return map;
352 }
353
354 public long getOldPlid() {
355 return _oldPlid;
356 }
357
358 public Map<String, String[]> getParameterMap() {
359 return _parameterMap;
360 }
361
362 public long getPlid() {
363 return _plid;
364 }
365
366 public String getPortletPath(String portletId) {
367 return getRootPath() + ROOT_PATH_PORTLETS + portletId;
368 }
369
370 public Set<String> getPrimaryKeys() {
371 return _primaryKeys;
372 }
373
374 public Map<String, List<RatingsEntry>> getRatingsEntries() {
375 return _ratingsEntriesMap;
376 }
377
378 public String getRootPath() {
379 return ROOT_PATH_GROUPS + getScopeGroupId();
380 }
381
382 public long getScopeGroupId() {
383 return _scopeGroupId;
384 }
385
386 public long getScopeLayoutId() {
387 return _scopeLayoutId;
388 }
389
390 public long getSourceGroupId() {
391 return _sourceGroupId;
392 }
393
394 public String getSourceLayoutPath(long layoutId) {
395 return getSourceRootPath() + ROOT_PATH_LAYOUTS + layoutId;
396 }
397
398 public String getSourcePortletPath(String portletId) {
399 return getSourceRootPath() + ROOT_PATH_PORTLETS + portletId;
400 }
401
402 public String getSourceRootPath() {
403 return ROOT_PATH_GROUPS + getSourceGroupId();
404 }
405
406 public Date getStartDate() {
407 return _startDate;
408 }
409
410 public Map<String, String[]> getTagsCategories() {
411 return _tagsCategoriesMap;
412 }
413
414 public String[] getTagsCategories(Class<?> classObj, long classPK) {
415 return _tagsCategoriesMap.get(
416 getPrimaryKeyString(classObj, classPK));
417 }
418
419 public Map<String, String[]> getTagsEntries() {
420 return _tagsEntriesMap;
421 }
422
423 public String[] getTagsEntries(Class<?> classObj, long classPK) {
424 return _tagsEntriesMap.get(getPrimaryKeyString(classObj, classPK));
425 }
426
427 public String[] getTagsEntries(String className, long classPK) {
428 return _tagsEntriesMap.get(getPrimaryKeyString(className, classPK));
429 }
430
431 public long getUserId(String userUuid) throws SystemException {
432 return _userIdStrategy.getUserId(userUuid);
433 }
434
435 public UserIdStrategy getUserIdStrategy() {
436 return _userIdStrategy;
437 }
438
439 public List<String> getZipEntries() {
440 return getZipReader().getEntries();
441 }
442
443 public byte[] getZipEntryAsByteArray(String path) {
444 return getZipReader().getEntryAsByteArray(path);
445 }
446
447 public InputStream getZipEntryAsInputStream(String path) {
448 return getZipReader().getEntryAsInputStream(path);
449 }
450
451 public Object getZipEntryAsObject(String path) {
452 return fromXML(getZipEntryAsString(path));
453 }
454
455 public String getZipEntryAsString(String path) {
456 return getZipReader().getEntryAsString(path);
457 }
458
459 public List<String> getZipFolderEntries() {
460 return getZipFolderEntries(StringPool.SLASH);
461 }
462
463 public List<String> getZipFolderEntries(String path) {
464 return getZipReader().getFolderEntries(path);
465 }
466
467 public ZipReader getZipReader() {
468 return _zipReader;
469 }
470
471 public ZipWriter getZipWriter() {
472 return _zipWriter;
473 }
474
475 public boolean hasDateRange() {
476 if (_startDate != null) {
477 return true;
478 }
479 else {
480 return false;
481 }
482 }
483
484 public boolean hasNotUniquePerLayout(String dataKey) {
485 return _notUniquePerLayout.contains(dataKey);
486 }
487
488 public boolean hasPrimaryKey(Class<?> classObj, String primaryKey) {
489 return _primaryKeys.contains(getPrimaryKeyString(classObj, primaryKey));
490 }
491
492 public void importComments(
493 Class<?> classObj, long classPK, long newClassPK, long groupId)
494 throws PortalException, SystemException {
495
496 Map<Long, Long> messagePKs = new HashMap<Long, Long>();
497 Map<Long, Long> threadPKs = new HashMap<Long, Long>();
498
499 List<MBMessage> messages = _commentsMap.get(
500 getPrimaryKeyString(classObj, classPK));
501
502 if (messages == null) {
503 return;
504 }
505
506 MBDiscussion discussion = null;
507
508 try {
509 discussion = MBDiscussionLocalServiceUtil.getDiscussion(
510 classObj.getName(), newClassPK);
511 }
512 catch (NoSuchDiscussionException nsde) {
513 }
514
515 for (MBMessage message : messages) {
516 long userId = getUserId(message.getUserUuid());
517 long parentMessageId = MapUtil.getLong(
518 messagePKs, message.getParentMessageId(),
519 message.getParentMessageId());
520 long threadId = MapUtil.getLong(
521 threadPKs, message.getThreadId(), message.getThreadId());
522
523 if ((message.getParentMessageId() ==
524 MBMessageImpl.DEFAULT_PARENT_MESSAGE_ID) &&
525 (discussion != null)) {
526
527 MBThread thread = MBThreadLocalServiceUtil.getThread(
528 discussion.getThreadId());
529
530 long rootMessageId = thread.getRootMessageId();
531
532 messagePKs.put(message.getMessageId(), rootMessageId);
533 threadPKs.put(message.getThreadId(), thread.getThreadId());
534 }
535 else {
536 ServiceContext serviceContext = new ServiceContext();
537
538 serviceContext.setScopeGroupId(groupId);
539
540 MBMessage newMessage =
541 MBMessageLocalServiceUtil.addDiscussionMessage(
542 userId, message.getUserName(), classObj.getName(),
543 newClassPK, threadId, parentMessageId,
544 message.getSubject(), message.getBody(),
545 serviceContext);
546
547 messagePKs.put(
548 message.getMessageId(), newMessage.getMessageId());
549 threadPKs.put(message.getThreadId(), newMessage.getThreadId());
550 }
551
552 importRatingsEntries(
553 MBMessage.class, message.getPrimaryKey(),
554 messagePKs.get(message.getPrimaryKey()));
555 }
556 }
557
558 public void importRatingsEntries(
559 Class<?> classObj, long classPK, long newClassPK)
560 throws PortalException, SystemException {
561
562 List<RatingsEntry> ratingsEntries = _ratingsEntriesMap.get(
563 getPrimaryKeyString(classObj, classPK));
564
565 if (ratingsEntries == null) {
566 return;
567 }
568
569 for (RatingsEntry ratingsEntry : ratingsEntries) {
570 long userId = getUserId(ratingsEntry.getUserUuid());
571
572 RatingsEntryLocalServiceUtil.updateEntry(
573 userId, classObj.getName(), ((Long)newClassPK).longValue(),
574 ratingsEntry.getScore());
575 }
576 }
577
578 public boolean isPathNotProcessed(String path) {
579 return !addPrimaryKey(String.class, path);
580 }
581
582 public boolean isPrivateLayout() {
583 return _privateLayout;
584 }
585
586 public boolean isWithinDateRange(Date modifiedDate) {
587 if (!hasDateRange()) {
588 return true;
589 }
590 else if ((_startDate.compareTo(modifiedDate) <= 0) &&
591 (_endDate.after(modifiedDate))) {
592
593 return true;
594 }
595 else {
596 return false;
597 }
598 }
599
600 public void putNotUniquePerLayout(String dataKey) {
601 _notUniquePerLayout.add(dataKey);
602 }
603
604 public void setClassLoader(ClassLoader classLoader) {
605 _xStream.setClassLoader(classLoader);
606 }
607
608 public void setGroupId(long groupId) {
609 _groupId = groupId;
610 }
611
612 public void setOldPlid(long oldPlid) {
613 _oldPlid = oldPlid;
614 }
615
616 public void setPlid(long plid) {
617 _plid = plid;
618 }
619
620 public void setPrivateLayout(boolean privateLayout) {
621 _privateLayout = privateLayout;
622 }
623
624 public void setScopeGroupId(long scopeGroupId) {
625 _scopeGroupId = scopeGroupId;
626 }
627
628 public void setScopeLayoutId(long scopeLayoutId) {
629 _scopeLayoutId = scopeLayoutId;
630 }
631
632 public void setSourceGroupId(long sourceGroupId) {
633 _sourceGroupId = sourceGroupId;
634 }
635
636 public String toXML(Object object) {
637 return _xStream.toXML(object);
638 }
639
640 protected String getPrimaryKeyString(Class<?> classObj, long classPK) {
641 return getPrimaryKeyString(classObj.getName(), String.valueOf(classPK));
642 }
643
644 protected String getPrimaryKeyString(Class<?> classObj, String primaryKey) {
645 return getPrimaryKeyString(classObj.getName(), primaryKey);
646 }
647
648 protected String getPrimaryKeyString(String className, long classPK) {
649 return getPrimaryKeyString(className, String.valueOf(classPK));
650 }
651
652 protected String getPrimaryKeyString(String className, String primaryKey) {
653 StringBuilder sb = new StringBuilder();
654
655 sb.append(className);
656 sb.append(StringPool.POUND);
657 sb.append(primaryKey);
658
659 return sb.toString();
660 }
661
662 protected void initXStream() {
663 _xStream = new XStream();
664
665 _xStream.alias("BlogsEntry", BlogsEntryImpl.class);
666 _xStream.alias("BookmarksFolder", BookmarksFolderImpl.class);
667 _xStream.alias("BookmarksEntry", BookmarksEntryImpl.class);
668 _xStream.alias("CalEvent", CalEventImpl.class);
669 _xStream.alias("DLFolder", DLFolderImpl.class);
670 _xStream.alias("DLFileEntry", DLFileEntryImpl.class);
671 _xStream.alias("DLFileShortcut", DLFileShortcutImpl.class);
672 _xStream.alias("DLFileRank", DLFileRankImpl.class);
673 _xStream.alias("IGFolder", IGFolderImpl.class);
674 _xStream.alias("IGImage", IGImageImpl.class);
675 _xStream.alias("JournalArticle", JournalArticleImpl.class);
676 _xStream.alias("JournalFeed", JournalFeedImpl.class);
677 _xStream.alias("JournalStructure", JournalStructureImpl.class);
678 _xStream.alias("JournalTemplate", JournalTemplateImpl.class);
679 _xStream.alias("MBCategory", MBCategoryImpl.class);
680 _xStream.alias("MBMessage", MBMessageImpl.class);
681 _xStream.alias("MBMessageFlag", MBMessageFlagImpl.class);
682 _xStream.alias("MBBan", MBBanImpl.class);
683 _xStream.alias("PollsQuestion", PollsQuestionImpl.class);
684 _xStream.alias("PollsChoice", PollsChoiceImpl.class);
685 _xStream.alias("PollsVote", PollsVoteImpl.class);
686 _xStream.alias("RatingsEntry", RatingsEntryImpl.class);
687 _xStream.alias("WikiNode", WikiNodeImpl.class);
688 _xStream.alias("WikiPage", WikiPageImpl.class);
689 }
690
691 protected void validateDateRange(Date startDate, Date endDate)
692 throws PortletDataException {
693
694 if ((startDate == null) ^ (endDate == null)) {
695 throw new PortletDataException(
696 "Both start and end dates must have valid values or be null");
697 }
698
699 if (startDate != null) {
700 if (startDate.after(endDate) || startDate.equals(endDate)) {
701 throw new PortletDataException(
702 "The start date cannot be after the end date");
703 }
704
705 Date now = new Date();
706
707 if (startDate.after(now) || endDate.after(now)) {
708 throw new PortletDataException(
709 "Dates must not be in the future");
710 }
711 }
712 }
713
714 private Map<String, List<MBMessage>> _commentsMap =
715 new HashMap<String, List<MBMessage>>();
716 private long _companyId;
717 private String _dataStrategy;
718 private Date _endDate;
719 private long _groupId;
720 private Map<String, Map<?, ?>> _newPrimaryKeysMaps =
721 new HashMap<String, Map<?, ?>>();
722 private Set<String> _notUniquePerLayout = new HashSet<String>();
723 private long _oldPlid;
724 private Map<String, String[]> _parameterMap;
725 private long _plid;
726 private Set<String> _primaryKeys;
727 private boolean _privateLayout;
728 private Map<String, List<RatingsEntry>> _ratingsEntriesMap =
729 new HashMap<String, List<RatingsEntry>>();
730 private long _scopeGroupId;
731 private long _scopeLayoutId;
732 private long _sourceGroupId;
733 private Date _startDate;
734 private Map<String, String[]> _tagsCategoriesMap =
735 new HashMap<String, String[]>();
736 private Map<String, String[]> _tagsEntriesMap =
737 new HashMap<String, String[]>();
738 private UserIdStrategy _userIdStrategy;
739 private XStream _xStream;
740 private ZipReader _zipReader;
741 private ZipWriter _zipWriter;
742
743 }