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