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.model.TagsAsset;
40  import com.liferay.portlet.tags.model.TagsEntry;
41  import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
42  import com.liferay.util.CollectionFactory;
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   *
62   */
63  public class PortletDataContextImpl implements PortletDataContext {
64  
65      public PortletDataContextImpl(
66          long companyId, long groupId, Map parameterMap, Set primaryKeys,
67          UserIdStrategy userIdStrategy, ZipReader zipReader) {
68  
69          _companyId = companyId;
70          _groupId = groupId;
71          _parameterMap = parameterMap;
72          _primaryKeys = primaryKeys;
73          _dataStrategy =  MapUtil.getString(
74              parameterMap, PortletDataHandlerKeys.DATA_STRATEGY,
75              PortletDataHandlerKeys.DATA_STRATEGY_MIRROR);
76          _userIdStrategy = userIdStrategy;
77          _zipReader = zipReader;
78          _zipWriter = null;
79      }
80  
81      public PortletDataContextImpl(
82          long companyId, long groupId, Map parameterMap, Set primaryKeys,
83          ZipWriter zipWriter) {
84  
85          _companyId = companyId;
86          _groupId = groupId;
87          _parameterMap = parameterMap;
88          _primaryKeys = primaryKeys;
89          _dataStrategy =  null;
90          _userIdStrategy = null;
91          _zipReader = null;
92          _zipWriter = zipWriter;
93      }
94  
95      public long getCompanyId() {
96          return _companyId;
97      }
98  
99      public long getGroupId() {
100         return _groupId;
101     }
102 
103     public long getPlid() {
104         return _plid;
105     }
106 
107     public void setPlid(long plid) {
108         _plid = plid;
109     }
110 
111     public Map getParameterMap() {
112         return _parameterMap;
113     }
114 
115     public boolean getBooleanParameter(String namespace, String name) {
116         boolean defaultValue = MapUtil.getBoolean(
117             getParameterMap(),
118             PortletDataHandlerKeys.PORTLET_DATA_CONTROL_DEFAULT, true);
119 
120         return MapUtil.getBoolean(
121             getParameterMap(),
122             PortletDataHandlerControl.getNamespacedControlName(namespace, name),
123             defaultValue);
124     }
125 
126     public Set getPrimaryKeys() {
127         return _primaryKeys;
128     }
129 
130     public boolean addPrimaryKey(Class classObj, Object primaryKey) {
131         boolean value = hasPrimaryKey(classObj, primaryKey);
132 
133         if (!value) {
134             _primaryKeys.add(getPrimaryKeyString(classObj, primaryKey));
135         }
136 
137         return value;
138     }
139 
140     public boolean hasPrimaryKey(Class classObj, Object primaryKey) {
141         return _primaryKeys.contains(getPrimaryKeyString(classObj, primaryKey));
142     }
143 
144     public Map getComments() {
145         return _commentsMap;
146     }
147 
148     public void addComments(Class classObj, Object primaryKey)
149         throws PortalException, SystemException {
150 
151         List messages = MBMessageLocalServiceUtil.getMessages(
152             classObj.getName(), ((Long)primaryKey).longValue());
153 
154         if (messages.size() == 0) {
155             return;
156         }
157 
158         Iterator itr = messages.iterator();
159 
160         while (itr.hasNext()) {
161             MBMessage message = (MBMessage)itr.next();
162 
163             message.setUserUuid(message.getUserUuid());
164         }
165 
166         _commentsMap.put(getPrimaryKeyString(classObj, primaryKey), messages);
167     }
168 
169     public void addComments(String className, Object primaryKey, List messages)
170         throws PortalException, SystemException {
171 
172         _commentsMap.put(
173             getPrimaryKeyString(className, primaryKey), messages);
174     }
175 
176     public void importComments(
177             Class classObj, Object primaryKey, Object newPrimaryKey,
178             long groupId)
179         throws PortalException, SystemException {
180 
181         Map messagePKs = CollectionFactory.getHashMap();
182         Map threadPKs = CollectionFactory.getHashMap();
183 
184         List messages = (List)_commentsMap.get(
185             getPrimaryKeyString(classObj, primaryKey));
186 
187         if (messages == null) {
188             return;
189         }
190 
191         Iterator itr = messages.iterator();
192 
193         while (itr.hasNext()) {
194             MBMessage message = (MBMessage)itr.next();
195 
196             long userId = getUserId(message.getUserUuid());
197             long parentMessageId = MapUtil.getLong(
198                 messagePKs, message.getParentMessageId(),
199                 message.getParentMessageId());
200             long threadId = MapUtil.getLong(
201                 threadPKs, message.getThreadId(), message.getThreadId());
202 
203             MBMessage newMessage =
204                 MBMessageLocalServiceUtil.addDiscussionMessage(
205                     userId, groupId, classObj.getName(),
206                     ((Long)newPrimaryKey).longValue(), threadId,
207                     parentMessageId, message.getSubject(), message.getBody());
208 
209             messagePKs.put(
210                 message.getPrimaryKeyObj(), newMessage.getPrimaryKeyObj());
211             threadPKs.put(
212                 new Long(message.getThreadId()),
213                 new Long(newMessage.getThreadId()));
214         }
215     }
216 
217     public Map getRatingsEntries() {
218         return _ratingsEntriesMap;
219     }
220 
221     public void addRatingsEntries(Class classObj, Object primaryKey)
222         throws PortalException, SystemException {
223 
224         List entries = RatingsEntryLocalServiceUtil.getEntries(
225             classObj.getName(), ((Long)primaryKey).longValue());
226 
227         if (entries.size() == 0) {
228             return;
229         }
230 
231         Iterator itr = entries.iterator();
232 
233         while (itr.hasNext()) {
234             RatingsEntry entry = (RatingsEntry)itr.next();
235 
236             entry.setUserUuid(entry.getUserUuid());
237         }
238 
239         _ratingsEntriesMap.put(
240             getPrimaryKeyString(classObj, primaryKey), entries);
241     }
242 
243     public void addRatingsEntries(
244             String className, Object primaryKey, List entries)
245         throws PortalException, SystemException {
246 
247         _ratingsEntriesMap.put(
248             getPrimaryKeyString(className, primaryKey), entries);
249     }
250 
251     public void importRatingsEntries(
252             Class classObj, Object primaryKey, Object newPrimaryKey)
253         throws PortalException, SystemException {
254 
255         List entries = (List)_ratingsEntriesMap.get(
256             getPrimaryKeyString(classObj, primaryKey));
257 
258         if (entries == null) {
259             return;
260         }
261 
262         Iterator itr = entries.iterator();
263 
264         while (itr.hasNext()) {
265             RatingsEntry entry = (RatingsEntry)itr.next();
266 
267             long userId = getUserId(entry.getUserUuid());
268 
269             RatingsEntryLocalServiceUtil.updateEntry(
270                 userId, classObj.getName(), ((Long)newPrimaryKey).longValue(),
271                 entry.getScore());
272         }
273     }
274 
275     public String[] getTagsEntries(Class classObj, Object primaryKey) {
276         return (String[])_tagsEntriesMap.get(
277             getPrimaryKeyString(classObj, primaryKey));
278     }
279 
280     public String[] getTagsEntries(String className, Object primaryKey) {
281         return (String[])_tagsEntriesMap.get(
282             getPrimaryKeyString(className, primaryKey));
283     }
284 
285     public Map getTagsEntries() {
286         return _tagsEntriesMap;
287     }
288 
289     public void addTagsEntries(Class classObj, Object classPK)
290         throws PortalException, SystemException {
291 
292         TagsAsset tagsAsset = TagsAssetLocalServiceUtil.getAsset(
293             classObj.getName(), ((Long)classPK).longValue());
294 
295         List tagsEntriesList = tagsAsset.getEntries();
296 
297         if (tagsEntriesList.size() == 0) {
298             return;
299         }
300 
301         String[] tagsEntries = new String[tagsEntriesList.size()];
302 
303         Iterator itr = tagsEntriesList.iterator();
304 
305         for (int i = 0; itr.hasNext(); i++) {
306             TagsEntry tagsEntry = (TagsEntry)itr.next();
307 
308             tagsEntries[i] = tagsEntry.getName();
309         }
310 
311         _tagsEntriesMap.put(
312             getPrimaryKeyString(classObj, classPK), tagsEntries);
313     }
314 
315     public void addTagsEntries(
316             String className, Object classPK, String[] values)
317         throws PortalException, SystemException {
318 
319         _tagsEntriesMap.put(getPrimaryKeyString(className, classPK), values);
320     }
321 
322     public String getDataStrategy() {
323          return _dataStrategy;
324     }
325 
326     public UserIdStrategy getUserIdStrategy() {
327         return _userIdStrategy;
328     }
329 
330     public long getUserId(String userUuid) throws SystemException {
331         return _userIdStrategy.getUserId(userUuid);
332     }
333 
334     public ZipReader getZipReader() {
335         return _zipReader;
336     }
337 
338     public ZipWriter getZipWriter() {
339         return _zipWriter;
340     }
341 
342     protected String getPrimaryKeyString(Class classObj, Object primaryKey) {
343         return getPrimaryKeyString(classObj.getName(), primaryKey);
344     }
345 
346     protected String getPrimaryKeyString(String className, Object primaryKey) {
347         StringMaker sm = new StringMaker();
348 
349         sm.append(className);
350         sm.append(StringPool.POUND);
351         sm.append(primaryKey);
352 
353         return sm.toString();
354     }
355 
356     private long _companyId;
357     private long _groupId;
358     private long _plid;
359     private Map _commentsMap = new HashMap();
360     private Map _parameterMap;
361     private Map _ratingsEntriesMap = new HashMap();
362     private Map _tagsEntriesMap = new HashMap();
363     private Set _primaryKeys;
364     private String _dataStrategy;
365     private UserIdStrategy _userIdStrategy;
366     private ZipReader _zipReader;
367     private ZipWriter _zipWriter;
368 
369 }