1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.lar.PortletDataContext;
28  import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
29  import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
30  import com.liferay.portal.kernel.lar.UserIdStrategy;
31  import com.liferay.portal.kernel.util.StringMaker;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.zip.ZipReader;
34  import com.liferay.portal.kernel.zip.ZipWriter;
35  import com.liferay.portlet.messageboards.model.MBMessage;
36  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
37  import com.liferay.portlet.ratings.model.RatingsEntry;
38  import com.liferay.portlet.ratings.service.RatingsEntryLocalServiceUtil;
39  import com.liferay.portlet.tags.NoSuchAssetException;
40  import com.liferay.portlet.tags.model.TagsAsset;
41  import com.liferay.portlet.tags.model.TagsEntry;
42  import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
43  import com.liferay.util.CollectionFactory;
44  import com.liferay.util.MapUtil;
45  
46  import java.util.HashMap;
47  import java.util.Iterator;
48  import java.util.List;
49  import java.util.Map;
50  import java.util.Set;
51  
52  /**
53   * <a href="PortletDataContextImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * <p>
56   * Holds context information that is used during exporting adn importing portlet
57   * data.
58   * </p>
59   *
60   * @author Brian Wing Shun Chan
61   * @author Raymond Aug?
62   *
63   */
64  public class PortletDataContextImpl implements PortletDataContext {
65  
66      public PortletDataContextImpl(
67          long companyId, long groupId, Map parameterMap, Set primaryKeys,
68          UserIdStrategy userIdStrategy, ZipReader zipReader) {
69  
70          _companyId = companyId;
71          _groupId = groupId;
72          _parameterMap = parameterMap;
73          _primaryKeys = primaryKeys;
74          _dataStrategy =  MapUtil.getString(
75              parameterMap, PortletDataHandlerKeys.DATA_STRATEGY,
76              PortletDataHandlerKeys.DATA_STRATEGY_MIRROR);
77          _userIdStrategy = userIdStrategy;
78          _zipReader = zipReader;
79          _zipWriter = null;
80      }
81  
82      public PortletDataContextImpl(
83          long companyId, long groupId, Map parameterMap, Set primaryKeys,
84          ZipWriter zipWriter) {
85  
86          _companyId = companyId;
87          _groupId = groupId;
88          _parameterMap = parameterMap;
89          _primaryKeys = primaryKeys;
90          _dataStrategy =  null;
91          _userIdStrategy = null;
92          _zipReader = null;
93          _zipWriter = zipWriter;
94      }
95  
96      public long getCompanyId() {
97          return _companyId;
98      }
99  
100     public long getGroupId() {
101         return _groupId;
102     }
103 
104     public long getPlid() {
105         return _plid;
106     }
107 
108     public void setPlid(long plid) {
109         _plid = plid;
110     }
111 
112     public Map getParameterMap() {
113         return _parameterMap;
114     }
115 
116     public boolean getBooleanParameter(String namespace, String name) {
117         boolean defaultValue = MapUtil.getBoolean(
118             getParameterMap(),
119             PortletDataHandlerKeys.PORTLET_DATA_CONTROL_DEFAULT, true);
120 
121         return MapUtil.getBoolean(
122             getParameterMap(),
123             PortletDataHandlerControl.getNamespacedControlName(namespace, name),
124             defaultValue);
125     }
126 
127     public Set getPrimaryKeys() {
128         return _primaryKeys;
129     }
130 
131     public boolean addPrimaryKey(Class classObj, Object primaryKey) {
132         boolean value = hasPrimaryKey(classObj, primaryKey);
133 
134         if (!value) {
135             _primaryKeys.add(getPrimaryKeyString(classObj, primaryKey));
136         }
137 
138         return value;
139     }
140 
141     public boolean hasPrimaryKey(Class classObj, Object primaryKey) {
142         return _primaryKeys.contains(getPrimaryKeyString(classObj, primaryKey));
143     }
144 
145     public Map getComments() {
146         return _commentsMap;
147     }
148 
149     public void addComments(Class classObj, Object primaryKey)
150         throws PortalException, SystemException {
151 
152         List messages = MBMessageLocalServiceUtil.getMessages(
153             classObj.getName(), ((Long)primaryKey).longValue());
154 
155         if (messages.size() == 0) {
156             return;
157         }
158 
159         Iterator itr = messages.iterator();
160 
161         while (itr.hasNext()) {
162             MBMessage message = (MBMessage)itr.next();
163 
164             message.setUserUuid(message.getUserUuid());
165         }
166 
167         _commentsMap.put(getPrimaryKeyString(classObj, primaryKey), messages);
168     }
169 
170     public void addComments(String className, Object primaryKey, List messages)
171         throws PortalException, SystemException {
172 
173         _commentsMap.put(
174             getPrimaryKeyString(className, primaryKey), messages);
175     }
176 
177     public void importComments(
178             Class classObj, Object primaryKey, Object newPrimaryKey,
179             long groupId)
180         throws PortalException, SystemException {
181 
182         Map messagePKs = CollectionFactory.getHashMap();
183         Map threadPKs = CollectionFactory.getHashMap();
184 
185         List messages = (List)_commentsMap.get(
186             getPrimaryKeyString(classObj, primaryKey));
187 
188         if (messages == null) {
189             return;
190         }
191 
192         Iterator itr = messages.iterator();
193 
194         while (itr.hasNext()) {
195             MBMessage message = (MBMessage)itr.next();
196 
197             long userId = getUserId(message.getUserUuid());
198             long parentMessageId = MapUtil.getLong(
199                 messagePKs, message.getParentMessageId(),
200                 message.getParentMessageId());
201             long threadId = MapUtil.getLong(
202                 threadPKs, message.getThreadId(), message.getThreadId());
203 
204             MBMessage newMessage =
205                 MBMessageLocalServiceUtil.addDiscussionMessage(
206                     userId, groupId, classObj.getName(),
207                     ((Long)newPrimaryKey).longValue(), threadId,
208                     parentMessageId, message.getSubject(), message.getBody());
209 
210             messagePKs.put(
211                 message.getPrimaryKeyObj(), newMessage.getPrimaryKeyObj());
212             threadPKs.put(
213                 new Long(message.getThreadId()),
214                 new Long(newMessage.getThreadId()));
215         }
216     }
217 
218     public Map getRatingsEntries() {
219         return _ratingsEntriesMap;
220     }
221 
222     public void addRatingsEntries(Class classObj, Object primaryKey)
223         throws PortalException, SystemException {
224 
225         List entries = RatingsEntryLocalServiceUtil.getEntries(
226             classObj.getName(), ((Long)primaryKey).longValue());
227 
228         if (entries.size() == 0) {
229             return;
230         }
231 
232         Iterator itr = entries.iterator();
233 
234         while (itr.hasNext()) {
235             RatingsEntry entry = (RatingsEntry)itr.next();
236 
237             entry.setUserUuid(entry.getUserUuid());
238         }
239 
240         _ratingsEntriesMap.put(
241             getPrimaryKeyString(classObj, primaryKey), entries);
242     }
243 
244     public void addRatingsEntries(
245             String className, Object primaryKey, List entries)
246         throws PortalException, SystemException {
247 
248         _ratingsEntriesMap.put(
249             getPrimaryKeyString(className, primaryKey), entries);
250     }
251 
252     public void importRatingsEntries(
253             Class classObj, Object primaryKey, Object newPrimaryKey)
254         throws PortalException, SystemException {
255 
256         List entries = (List)_ratingsEntriesMap.get(
257             getPrimaryKeyString(classObj, primaryKey));
258 
259         if (entries == null) {
260             return;
261         }
262 
263         Iterator itr = entries.iterator();
264 
265         while (itr.hasNext()) {
266             RatingsEntry entry = (RatingsEntry)itr.next();
267 
268             long userId = getUserId(entry.getUserUuid());
269 
270             RatingsEntryLocalServiceUtil.updateEntry(
271                 userId, classObj.getName(), ((Long)newPrimaryKey).longValue(),
272                 entry.getScore());
273         }
274     }
275 
276     public String[] getTagsEntries(Class classObj, Object primaryKey) {
277         return (String[])_tagsEntriesMap.get(
278             getPrimaryKeyString(classObj, primaryKey));
279     }
280 
281     public String[] getTagsEntries(String className, Object primaryKey) {
282         return (String[])_tagsEntriesMap.get(
283             getPrimaryKeyString(className, primaryKey));
284     }
285 
286     public Map getTagsEntries() {
287         return _tagsEntriesMap;
288     }
289 
290     public void addTagsEntries(Class classObj, Object classPK)
291         throws PortalException, SystemException {
292 
293         TagsAsset tagsAsset = null;
294 
295         try {
296             tagsAsset = TagsAssetLocalServiceUtil.getAsset(
297                 classObj.getName(), ((Long)classPK).longValue());
298         }
299         catch (NoSuchAssetException nsae) {
300 
301             // LEP-4979
302 
303             return;
304         }
305 
306         List tagsEntriesList = tagsAsset.getEntries();
307 
308         if (tagsEntriesList.size() == 0) {
309             return;
310         }
311 
312         String[] tagsEntries = new String[tagsEntriesList.size()];
313 
314         Iterator itr = tagsEntriesList.iterator();
315 
316         for (int i = 0; itr.hasNext(); i++) {
317             TagsEntry tagsEntry = (TagsEntry)itr.next();
318 
319             tagsEntries[i] = tagsEntry.getName();
320         }
321 
322         _tagsEntriesMap.put(
323             getPrimaryKeyString(classObj, classPK), tagsEntries);
324     }
325 
326     public void addTagsEntries(
327             String className, Object classPK, String[] values)
328         throws PortalException, SystemException {
329 
330         _tagsEntriesMap.put(getPrimaryKeyString(className, classPK), values);
331     }
332 
333     public String getDataStrategy() {
334          return _dataStrategy;
335     }
336 
337     public UserIdStrategy getUserIdStrategy() {
338         return _userIdStrategy;
339     }
340 
341     public long getUserId(String userUuid) throws SystemException {
342         return _userIdStrategy.getUserId(userUuid);
343     }
344 
345     public ZipReader getZipReader() {
346         return _zipReader;
347     }
348 
349     public ZipWriter getZipWriter() {
350         return _zipWriter;
351     }
352 
353     protected String getPrimaryKeyString(Class classObj, Object primaryKey) {
354         return getPrimaryKeyString(classObj.getName(), primaryKey);
355     }
356 
357     protected String getPrimaryKeyString(String className, Object primaryKey) {
358         StringMaker sm = new StringMaker();
359 
360         sm.append(className);
361         sm.append(StringPool.POUND);
362         sm.append(primaryKey);
363 
364         return sm.toString();
365     }
366 
367     private long _companyId;
368     private long _groupId;
369     private long _plid;
370     private Map _commentsMap = new HashMap();
371     private Map _parameterMap;
372     private Map _ratingsEntriesMap = new HashMap();
373     private Map _tagsEntriesMap = new HashMap();
374     private Set _primaryKeys;
375     private String _dataStrategy;
376     private UserIdStrategy _userIdStrategy;
377     private ZipReader _zipReader;
378     private ZipWriter _zipWriter;
379 
380 }