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.MapUtil;
44  
45  import java.util.HashMap;
46  import java.util.Iterator;
47  import java.util.List;
48  import java.util.Map;
49  import java.util.Set;
50  
51  /**
52   * <a href="PortletDataContextImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * <p>
55   * Holds context information that is used during exporting adn importing portlet
56   * data.
57   * </p>
58   *
59   * @author Brian Wing Shun Chan
60   * @author Raymond Augé
61   * @author Bruno Farache
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 getNewPrimaryKeysMap(Class classObj) {
146         Map map = _newPrimaryKeysMaps.get(classObj.getName());
147 
148         if (map == null) {
149             map = new HashMap();
150 
151             _newPrimaryKeysMaps.put(classObj.getName(), map);
152         }
153 
154         return map;
155     }
156 
157     public Map getComments() {
158         return _commentsMap;
159     }
160 
161     public void addComments(Class classObj, Object primaryKey)
162         throws PortalException, SystemException {
163 
164         List messages = MBMessageLocalServiceUtil.getMessages(
165             classObj.getName(), ((Long)primaryKey).longValue());
166 
167         if (messages.size() == 0) {
168             return;
169         }
170 
171         Iterator itr = messages.iterator();
172 
173         while (itr.hasNext()) {
174             MBMessage message = (MBMessage)itr.next();
175 
176             message.setUserUuid(message.getUserUuid());
177         }
178 
179         _commentsMap.put(getPrimaryKeyString(classObj, primaryKey), messages);
180     }
181 
182     public void addComments(String className, Object primaryKey, List messages)
183         throws PortalException, SystemException {
184 
185         _commentsMap.put(
186             getPrimaryKeyString(className, primaryKey), messages);
187     }
188 
189     public void importComments(
190             Class classObj, Object primaryKey, Object newPrimaryKey,
191             long groupId)
192         throws PortalException, SystemException {
193 
194         Map messagePKs = new HashMap();
195         Map threadPKs = new HashMap();
196 
197         List messages = (List)_commentsMap.get(
198             getPrimaryKeyString(classObj, primaryKey));
199 
200         if (messages == null) {
201             return;
202         }
203 
204         Iterator itr = messages.iterator();
205 
206         while (itr.hasNext()) {
207             MBMessage message = (MBMessage)itr.next();
208 
209             long userId = getUserId(message.getUserUuid());
210             long parentMessageId = MapUtil.getLong(
211                 messagePKs, message.getParentMessageId(),
212                 message.getParentMessageId());
213             long threadId = MapUtil.getLong(
214                 threadPKs, message.getThreadId(), message.getThreadId());
215 
216             MBMessage newMessage =
217                 MBMessageLocalServiceUtil.addDiscussionMessage(
218                     userId, groupId, classObj.getName(),
219                     ((Long)newPrimaryKey).longValue(), threadId,
220                     parentMessageId, message.getSubject(), message.getBody());
221 
222             messagePKs.put(
223                 message.getPrimaryKeyObj(), newMessage.getPrimaryKeyObj());
224             threadPKs.put(
225                 new Long(message.getThreadId()),
226                 new Long(newMessage.getThreadId()));
227         }
228     }
229 
230     public Map getRatingsEntries() {
231         return _ratingsEntriesMap;
232     }
233 
234     public void addRatingsEntries(Class classObj, Object primaryKey)
235         throws PortalException, SystemException {
236 
237         List entries = RatingsEntryLocalServiceUtil.getEntries(
238             classObj.getName(), ((Long)primaryKey).longValue());
239 
240         if (entries.size() == 0) {
241             return;
242         }
243 
244         Iterator itr = entries.iterator();
245 
246         while (itr.hasNext()) {
247             RatingsEntry entry = (RatingsEntry)itr.next();
248 
249             entry.setUserUuid(entry.getUserUuid());
250         }
251 
252         _ratingsEntriesMap.put(
253             getPrimaryKeyString(classObj, primaryKey), entries);
254     }
255 
256     public void addRatingsEntries(
257             String className, Object primaryKey, List entries)
258         throws PortalException, SystemException {
259 
260         _ratingsEntriesMap.put(
261             getPrimaryKeyString(className, primaryKey), entries);
262     }
263 
264     public void importRatingsEntries(
265             Class classObj, Object primaryKey, Object newPrimaryKey)
266         throws PortalException, SystemException {
267 
268         List entries = (List)_ratingsEntriesMap.get(
269             getPrimaryKeyString(classObj, primaryKey));
270 
271         if (entries == null) {
272             return;
273         }
274 
275         Iterator itr = entries.iterator();
276 
277         while (itr.hasNext()) {
278             RatingsEntry entry = (RatingsEntry)itr.next();
279 
280             long userId = getUserId(entry.getUserUuid());
281 
282             RatingsEntryLocalServiceUtil.updateEntry(
283                 userId, classObj.getName(), ((Long)newPrimaryKey).longValue(),
284                 entry.getScore());
285         }
286     }
287 
288     public String[] getTagsEntries(Class classObj, Object primaryKey) {
289         return (String[])_tagsEntriesMap.get(
290             getPrimaryKeyString(classObj, primaryKey));
291     }
292 
293     public String[] getTagsEntries(String className, Object primaryKey) {
294         return (String[])_tagsEntriesMap.get(
295             getPrimaryKeyString(className, primaryKey));
296     }
297 
298     public Map getTagsEntries() {
299         return _tagsEntriesMap;
300     }
301 
302     public void addTagsEntries(Class classObj, Object classPK)
303         throws PortalException, SystemException {
304 
305         TagsAsset tagsAsset = null;
306 
307         try {
308             tagsAsset = TagsAssetLocalServiceUtil.getAsset(
309                 classObj.getName(), ((Long)classPK).longValue());
310         }
311         catch (NoSuchAssetException nsae) {
312 
313             // LEP-4979
314 
315             return;
316         }
317 
318         List tagsEntriesList = tagsAsset.getEntries();
319 
320         if (tagsEntriesList.size() == 0) {
321             return;
322         }
323 
324         String[] tagsEntries = new String[tagsEntriesList.size()];
325 
326         Iterator itr = tagsEntriesList.iterator();
327 
328         for (int i = 0; itr.hasNext(); i++) {
329             TagsEntry tagsEntry = (TagsEntry)itr.next();
330 
331             tagsEntries[i] = tagsEntry.getName();
332         }
333 
334         _tagsEntriesMap.put(
335             getPrimaryKeyString(classObj, classPK), tagsEntries);
336     }
337 
338     public void addTagsEntries(
339             String className, Object classPK, String[] values)
340         throws PortalException, SystemException {
341 
342         _tagsEntriesMap.put(getPrimaryKeyString(className, classPK), values);
343     }
344 
345     public String getDataStrategy() {
346          return _dataStrategy;
347     }
348 
349     public UserIdStrategy getUserIdStrategy() {
350         return _userIdStrategy;
351     }
352 
353     public long getUserId(String userUuid) throws SystemException {
354         return _userIdStrategy.getUserId(userUuid);
355     }
356 
357     public ZipReader getZipReader() {
358         return _zipReader;
359     }
360 
361     public ZipWriter getZipWriter() {
362         return _zipWriter;
363     }
364 
365     protected String getPrimaryKeyString(Class classObj, Object primaryKey) {
366         return getPrimaryKeyString(classObj.getName(), primaryKey);
367     }
368 
369     protected String getPrimaryKeyString(String className, Object primaryKey) {
370         StringMaker sm = new StringMaker();
371 
372         sm.append(className);
373         sm.append(StringPool.POUND);
374         sm.append(primaryKey);
375 
376         return sm.toString();
377     }
378 
379     private long _companyId;
380     private long _groupId;
381     private long _plid;
382     private Map _commentsMap = new HashMap();
383     private Map _parameterMap;
384     private Map _ratingsEntriesMap = new HashMap();
385     private Map _tagsEntriesMap = new HashMap();
386     private Set _primaryKeys;
387     private Map<String, Map> _newPrimaryKeysMaps = new HashMap();
388     private String _dataStrategy;
389     private UserIdStrategy _userIdStrategy;
390     private ZipReader _zipReader;
391     private ZipWriter _zipWriter;
392 
393 }