001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.lar;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.lar.PortletDataContext;
026: import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
027: import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
028: import com.liferay.portal.kernel.lar.UserIdStrategy;
029: import com.liferay.portal.kernel.util.StringMaker;
030: import com.liferay.portal.kernel.util.StringPool;
031: import com.liferay.portal.kernel.zip.ZipReader;
032: import com.liferay.portal.kernel.zip.ZipWriter;
033: import com.liferay.portlet.messageboards.model.MBMessage;
034: import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
035: import com.liferay.portlet.ratings.model.RatingsEntry;
036: import com.liferay.portlet.ratings.service.RatingsEntryLocalServiceUtil;
037: import com.liferay.portlet.tags.NoSuchAssetException;
038: import com.liferay.portlet.tags.model.TagsAsset;
039: import com.liferay.portlet.tags.model.TagsEntry;
040: import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
041: import com.liferay.util.CollectionFactory;
042: import com.liferay.util.MapUtil;
043:
044: import java.util.HashMap;
045: import java.util.Iterator;
046: import java.util.List;
047: import java.util.Map;
048: import java.util.Set;
049:
050: /**
051: * <a href="PortletDataContextImpl.java.html"><b><i>View Source</i></b></a>
052: *
053: * <p>
054: * Holds context information that is used during exporting adn importing portlet
055: * data.
056: * </p>
057: *
058: * @author Brian Wing Shun Chan
059: * @author Raymond Augé
060: *
061: */
062: public class PortletDataContextImpl implements PortletDataContext {
063:
064: public PortletDataContextImpl(long companyId, long groupId,
065: Map parameterMap, Set primaryKeys,
066: UserIdStrategy userIdStrategy, ZipReader zipReader) {
067:
068: _companyId = companyId;
069: _groupId = groupId;
070: _parameterMap = parameterMap;
071: _primaryKeys = primaryKeys;
072: _dataStrategy = MapUtil.getString(parameterMap,
073: PortletDataHandlerKeys.DATA_STRATEGY,
074: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR);
075: _userIdStrategy = userIdStrategy;
076: _zipReader = zipReader;
077: _zipWriter = null;
078: }
079:
080: public PortletDataContextImpl(long companyId, long groupId,
081: Map parameterMap, Set primaryKeys, ZipWriter zipWriter) {
082:
083: _companyId = companyId;
084: _groupId = groupId;
085: _parameterMap = parameterMap;
086: _primaryKeys = primaryKeys;
087: _dataStrategy = null;
088: _userIdStrategy = null;
089: _zipReader = null;
090: _zipWriter = zipWriter;
091: }
092:
093: public long getCompanyId() {
094: return _companyId;
095: }
096:
097: public long getGroupId() {
098: return _groupId;
099: }
100:
101: public long getPlid() {
102: return _plid;
103: }
104:
105: public void setPlid(long plid) {
106: _plid = plid;
107: }
108:
109: public Map getParameterMap() {
110: return _parameterMap;
111: }
112:
113: public boolean getBooleanParameter(String namespace, String name) {
114: boolean defaultValue = MapUtil.getBoolean(getParameterMap(),
115: PortletDataHandlerKeys.PORTLET_DATA_CONTROL_DEFAULT,
116: true);
117:
118: return MapUtil.getBoolean(getParameterMap(),
119: PortletDataHandlerControl.getNamespacedControlName(
120: namespace, name), defaultValue);
121: }
122:
123: public Set getPrimaryKeys() {
124: return _primaryKeys;
125: }
126:
127: public boolean addPrimaryKey(Class classObj, Object primaryKey) {
128: boolean value = hasPrimaryKey(classObj, primaryKey);
129:
130: if (!value) {
131: _primaryKeys.add(getPrimaryKeyString(classObj, primaryKey));
132: }
133:
134: return value;
135: }
136:
137: public boolean hasPrimaryKey(Class classObj, Object primaryKey) {
138: return _primaryKeys.contains(getPrimaryKeyString(classObj,
139: primaryKey));
140: }
141:
142: public Map getComments() {
143: return _commentsMap;
144: }
145:
146: public void addComments(Class classObj, Object primaryKey)
147: throws PortalException, SystemException {
148:
149: List messages = MBMessageLocalServiceUtil.getMessages(classObj
150: .getName(), ((Long) primaryKey).longValue());
151:
152: if (messages.size() == 0) {
153: return;
154: }
155:
156: Iterator itr = messages.iterator();
157:
158: while (itr.hasNext()) {
159: MBMessage message = (MBMessage) itr.next();
160:
161: message.setUserUuid(message.getUserUuid());
162: }
163:
164: _commentsMap.put(getPrimaryKeyString(classObj, primaryKey),
165: messages);
166: }
167:
168: public void addComments(String className, Object primaryKey,
169: List messages) throws PortalException, SystemException {
170:
171: _commentsMap.put(getPrimaryKeyString(className, primaryKey),
172: messages);
173: }
174:
175: public void importComments(Class classObj, Object primaryKey,
176: Object newPrimaryKey, long groupId) throws PortalException,
177: SystemException {
178:
179: Map messagePKs = CollectionFactory.getHashMap();
180: Map threadPKs = CollectionFactory.getHashMap();
181:
182: List messages = (List) _commentsMap.get(getPrimaryKeyString(
183: classObj, primaryKey));
184:
185: if (messages == null) {
186: return;
187: }
188:
189: Iterator itr = messages.iterator();
190:
191: while (itr.hasNext()) {
192: MBMessage message = (MBMessage) itr.next();
193:
194: long userId = getUserId(message.getUserUuid());
195: long parentMessageId = MapUtil
196: .getLong(messagePKs, message.getParentMessageId(),
197: message.getParentMessageId());
198: long threadId = MapUtil.getLong(threadPKs, message
199: .getThreadId(), message.getThreadId());
200:
201: MBMessage newMessage = MBMessageLocalServiceUtil
202: .addDiscussionMessage(userId, groupId, classObj
203: .getName(), ((Long) newPrimaryKey)
204: .longValue(), threadId, parentMessageId,
205: message.getSubject(), message.getBody());
206:
207: messagePKs.put(message.getPrimaryKeyObj(), newMessage
208: .getPrimaryKeyObj());
209: threadPKs.put(new Long(message.getThreadId()), new Long(
210: newMessage.getThreadId()));
211: }
212: }
213:
214: public Map getRatingsEntries() {
215: return _ratingsEntriesMap;
216: }
217:
218: public void addRatingsEntries(Class classObj, Object primaryKey)
219: throws PortalException, SystemException {
220:
221: List entries = RatingsEntryLocalServiceUtil.getEntries(classObj
222: .getName(), ((Long) primaryKey).longValue());
223:
224: if (entries.size() == 0) {
225: return;
226: }
227:
228: Iterator itr = entries.iterator();
229:
230: while (itr.hasNext()) {
231: RatingsEntry entry = (RatingsEntry) itr.next();
232:
233: entry.setUserUuid(entry.getUserUuid());
234: }
235:
236: _ratingsEntriesMap.put(
237: getPrimaryKeyString(classObj, primaryKey), entries);
238: }
239:
240: public void addRatingsEntries(String className, Object primaryKey,
241: List entries) throws PortalException, SystemException {
242:
243: _ratingsEntriesMap.put(getPrimaryKeyString(className,
244: primaryKey), entries);
245: }
246:
247: public void importRatingsEntries(Class classObj, Object primaryKey,
248: Object newPrimaryKey) throws PortalException,
249: SystemException {
250:
251: List entries = (List) _ratingsEntriesMap
252: .get(getPrimaryKeyString(classObj, primaryKey));
253:
254: if (entries == null) {
255: return;
256: }
257:
258: Iterator itr = entries.iterator();
259:
260: while (itr.hasNext()) {
261: RatingsEntry entry = (RatingsEntry) itr.next();
262:
263: long userId = getUserId(entry.getUserUuid());
264:
265: RatingsEntryLocalServiceUtil.updateEntry(userId, classObj
266: .getName(), ((Long) newPrimaryKey).longValue(),
267: entry.getScore());
268: }
269: }
270:
271: public String[] getTagsEntries(Class classObj, Object primaryKey) {
272: return (String[]) _tagsEntriesMap.get(getPrimaryKeyString(
273: classObj, primaryKey));
274: }
275:
276: public String[] getTagsEntries(String className, Object primaryKey) {
277: return (String[]) _tagsEntriesMap.get(getPrimaryKeyString(
278: className, primaryKey));
279: }
280:
281: public Map getTagsEntries() {
282: return _tagsEntriesMap;
283: }
284:
285: public void addTagsEntries(Class classObj, Object classPK)
286: throws PortalException, SystemException {
287:
288: TagsAsset tagsAsset = null;
289:
290: try {
291: tagsAsset = TagsAssetLocalServiceUtil.getAsset(classObj
292: .getName(), ((Long) classPK).longValue());
293: } catch (NoSuchAssetException nsae) {
294:
295: // LEP-4979
296:
297: return;
298: }
299:
300: List tagsEntriesList = tagsAsset.getEntries();
301:
302: if (tagsEntriesList.size() == 0) {
303: return;
304: }
305:
306: String[] tagsEntries = new String[tagsEntriesList.size()];
307:
308: Iterator itr = tagsEntriesList.iterator();
309:
310: for (int i = 0; itr.hasNext(); i++) {
311: TagsEntry tagsEntry = (TagsEntry) itr.next();
312:
313: tagsEntries[i] = tagsEntry.getName();
314: }
315:
316: _tagsEntriesMap.put(getPrimaryKeyString(classObj, classPK),
317: tagsEntries);
318: }
319:
320: public void addTagsEntries(String className, Object classPK,
321: String[] values) throws PortalException, SystemException {
322:
323: _tagsEntriesMap.put(getPrimaryKeyString(className, classPK),
324: values);
325: }
326:
327: public String getDataStrategy() {
328: return _dataStrategy;
329: }
330:
331: public UserIdStrategy getUserIdStrategy() {
332: return _userIdStrategy;
333: }
334:
335: public long getUserId(String userUuid) throws SystemException {
336: return _userIdStrategy.getUserId(userUuid);
337: }
338:
339: public ZipReader getZipReader() {
340: return _zipReader;
341: }
342:
343: public ZipWriter getZipWriter() {
344: return _zipWriter;
345: }
346:
347: protected String getPrimaryKeyString(Class classObj,
348: Object primaryKey) {
349: return getPrimaryKeyString(classObj.getName(), primaryKey);
350: }
351:
352: protected String getPrimaryKeyString(String className,
353: Object primaryKey) {
354: StringMaker sm = new StringMaker();
355:
356: sm.append(className);
357: sm.append(StringPool.POUND);
358: sm.append(primaryKey);
359:
360: return sm.toString();
361: }
362:
363: private long _companyId;
364: private long _groupId;
365: private long _plid;
366: private Map _commentsMap = new HashMap();
367: private Map _parameterMap;
368: private Map _ratingsEntriesMap = new HashMap();
369: private Map _tagsEntriesMap = new HashMap();
370: private Set _primaryKeys;
371: private String _dataStrategy;
372: private UserIdStrategy _userIdStrategy;
373: private ZipReader _zipReader;
374: private ZipWriter _zipWriter;
375:
376: }
|