1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.blogs.lar;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.lar.BasePortletDataHandler;
20  import com.liferay.portal.kernel.lar.PortletDataContext;
21  import com.liferay.portal.kernel.lar.PortletDataException;
22  import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
23  import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
24  import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
25  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
26  import com.liferay.portal.kernel.util.StringBundler;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.workflow.WorkflowConstants;
29  import com.liferay.portal.kernel.xml.Document;
30  import com.liferay.portal.kernel.xml.Element;
31  import com.liferay.portal.kernel.xml.SAXReaderUtil;
32  import com.liferay.portal.service.ServiceContext;
33  import com.liferay.portal.util.PortletKeys;
34  import com.liferay.portlet.blogs.model.BlogsEntry;
35  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
36  import com.liferay.portlet.blogs.service.persistence.BlogsEntryUtil;
37  
38  import java.util.Calendar;
39  import java.util.List;
40  
41  import javax.portlet.PortletPreferences;
42  
43  /**
44   * <a href="BlogsPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Bruno Farache
47   * @author Raymond Augé
48   * @author Juan Fernández
49   */
50  public class BlogsPortletDataHandlerImpl extends BasePortletDataHandler {
51  
52      public PortletPreferences deleteData(
53              PortletDataContext context, String portletId,
54              PortletPreferences preferences)
55          throws PortletDataException {
56  
57          try {
58              if (!context.addPrimaryKey(
59                      BlogsPortletDataHandlerImpl.class, "deleteData")) {
60  
61                  BlogsEntryLocalServiceUtil.deleteEntries(context.getGroupId());
62              }
63  
64              return null;
65          }
66          catch (Exception e) {
67              throw new PortletDataException(e);
68          }
69      }
70  
71      public String exportData(
72              PortletDataContext context, String portletId,
73              PortletPreferences preferences)
74          throws PortletDataException {
75  
76          try {
77              context.addPermissions(
78                  "com.liferay.portlet.blogs", context.getGroupId());
79  
80              Document doc = SAXReaderUtil.createDocument();
81  
82              Element root = doc.addElement("blogs-data");
83  
84              root.addAttribute("group-id", String.valueOf(context.getGroupId()));
85  
86              List<BlogsEntry> entries = BlogsEntryUtil.findByGroupId(
87                  context.getGroupId());
88  
89              for (BlogsEntry entry : entries) {
90                  exportEntry(context, root, entry);
91              }
92  
93              return doc.formattedString();
94          }
95          catch (Exception e) {
96              throw new PortletDataException(e);
97          }
98      }
99  
100     public PortletDataHandlerControl[] getExportControls() {
101         return new PortletDataHandlerControl[] {
102             _entries, _categories, _comments, _ratings, _tags
103         };
104     }
105 
106     public PortletDataHandlerControl[] getImportControls() {
107         return new PortletDataHandlerControl[] {
108             _entries, _categories, _comments, _ratings, _tags, _wordpress
109         };
110     }
111 
112     public PortletPreferences importData(
113             PortletDataContext context, String portletId,
114             PortletPreferences preferences, String data)
115         throws PortletDataException {
116 
117         try {
118             context.importPermissions(
119                 "com.liferay.portlet.blogs", context.getSourceGroupId(),
120                 context.getGroupId());
121 
122             Document doc = SAXReaderUtil.read(data);
123 
124             Element root = doc.getRootElement();
125 
126             List<Element> entryEls = root.elements("entry");
127 
128             for (Element entryEl : entryEls) {
129                 String path = entryEl.attributeValue("path");
130 
131                 if (!context.isPathNotProcessed(path)) {
132                     continue;
133                 }
134 
135                 BlogsEntry entry = (BlogsEntry)context.getZipEntryAsObject(
136                     path);
137 
138                 importEntry(context, entry);
139             }
140 
141             if (context.getBooleanParameter(_NAMESPACE, "wordpress")) {
142                 WordPressImporter.importData(context);
143             }
144 
145             return null;
146         }
147         catch (Exception e) {
148             throw new PortletDataException(e);
149         }
150     }
151 
152     protected void exportEntry(
153             PortletDataContext context, Element root, BlogsEntry entry)
154         throws PortalException, SystemException {
155 
156         if (!context.isWithinDateRange(entry.getModifiedDate())) {
157             return;
158         }
159 
160         if (entry.getStatus() != WorkflowConstants.STATUS_APPROVED) {
161             return;
162         }
163 
164         String path = getEntryPath(context, entry);
165 
166         if (!context.isPathNotProcessed(path)) {
167             return;
168         }
169 
170         Element entryEl = root.addElement("entry");
171 
172         entryEl.addAttribute("path", path);
173 
174         context.addPermissions(BlogsEntry.class, entry.getEntryId());
175 
176         if (context.getBooleanParameter(_NAMESPACE, "categories")) {
177             context.addAssetCategories(BlogsEntry.class, entry.getEntryId());
178         }
179 
180         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
181             context.addComments(BlogsEntry.class, entry.getEntryId());
182         }
183 
184         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
185             context.addRatingsEntries(BlogsEntry.class, entry.getEntryId());
186         }
187 
188         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
189             context.addAssetTags(BlogsEntry.class, entry.getEntryId());
190         }
191 
192         entry.setUserUuid(entry.getUserUuid());
193 
194         context.addZipEntry(path, entry);
195     }
196 
197     protected String getEntryPath(
198         PortletDataContext context, BlogsEntry entry) {
199 
200         StringBundler sb = new StringBundler(4);
201 
202         sb.append(context.getPortletPath(PortletKeys.BLOGS));
203         sb.append("/entries/");
204         sb.append(entry.getEntryId());
205         sb.append(".xml");
206 
207         return sb.toString();
208     }
209 
210     protected void importEntry(PortletDataContext context, BlogsEntry entry)
211         throws Exception {
212 
213         long userId = context.getUserId(entry.getUserUuid());
214 
215         Calendar displayDateCal = CalendarFactoryUtil.getCalendar();
216 
217         displayDateCal.setTime(entry.getDisplayDate());
218 
219         int displayDateMonth = displayDateCal.get(Calendar.MONTH);
220         int displayDateDay = displayDateCal.get(Calendar.DATE);
221         int displayDateYear = displayDateCal.get(Calendar.YEAR);
222         int displayDateHour = displayDateCal.get(Calendar.HOUR);
223         int displayDateMinute = displayDateCal.get(Calendar.MINUTE);
224 
225         if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
226             displayDateHour += 12;
227         }
228 
229         boolean allowPingbacks = entry.isAllowPingbacks();
230         boolean allowTrackbacks = entry.isAllowTrackbacks();
231         String[] trackbacks = StringUtil.split(entry.getTrackbacks());
232         int status = entry.getStatus();
233 
234         long[] assetCategoryIds = null;
235         String[] assetTagNames = null;
236 
237         if (context.getBooleanParameter(_NAMESPACE, "categories")) {
238             assetCategoryIds = context.getAssetCategoryIds(
239                 BlogsEntry.class, entry.getEntryId());
240         }
241 
242         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
243             assetTagNames = context.getAssetTagNames(
244                 BlogsEntry.class, entry.getEntryId());
245         }
246 
247         ServiceContext serviceContext = new ServiceContext();
248 
249         serviceContext.setAddCommunityPermissions(true);
250         serviceContext.setAddGuestPermissions(true);
251         serviceContext.setAssetCategoryIds(assetCategoryIds);
252         serviceContext.setAssetTagNames(assetTagNames);
253         serviceContext.setCreateDate(entry.getCreateDate());
254         serviceContext.setModifiedDate(entry.getModifiedDate());
255         serviceContext.setScopeGroupId(context.getGroupId());
256 
257         if (status != WorkflowConstants.STATUS_APPROVED) {
258             serviceContext.setWorkflowAction(
259                 WorkflowConstants.ACTION_SAVE_DRAFT);
260         }
261 
262         BlogsEntry importedEntry = null;
263 
264         if (context.getDataStrategy().equals(
265                 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
266 
267             BlogsEntry existingEntry = BlogsEntryUtil.fetchByUUID_G(
268                 entry.getUuid(), context.getGroupId());
269 
270             if (existingEntry == null) {
271                 importedEntry = BlogsEntryLocalServiceUtil.addEntry(
272                     entry.getUuid(), userId, entry.getTitle(),
273                     entry.getContent(), displayDateMonth, displayDateDay,
274                     displayDateYear, displayDateHour, displayDateMinute,
275                     allowPingbacks, allowTrackbacks, trackbacks,
276                     serviceContext);
277             }
278             else {
279                 importedEntry = BlogsEntryLocalServiceUtil.updateEntry(
280                     userId, existingEntry.getEntryId(), entry.getTitle(),
281                     entry.getContent(), displayDateMonth, displayDateDay,
282                     displayDateYear, displayDateHour, displayDateMinute,
283                     allowPingbacks, allowTrackbacks, trackbacks,
284                     serviceContext);
285             }
286         }
287         else {
288             importedEntry = BlogsEntryLocalServiceUtil.addEntry(
289                 null, userId, entry.getTitle(), entry.getContent(),
290                 displayDateMonth, displayDateDay, displayDateYear,
291                 displayDateHour, displayDateMinute, allowPingbacks,
292                 allowTrackbacks, trackbacks, serviceContext);
293         }
294 
295         context.importPermissions(
296             BlogsEntry.class, entry.getEntryId(), importedEntry.getEntryId());
297 
298         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
299             context.importComments(
300                 BlogsEntry.class, entry.getEntryId(),
301                 importedEntry.getEntryId(), context.getGroupId());
302         }
303 
304         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
305             context.importRatingsEntries(
306                 BlogsEntry.class, entry.getEntryId(),
307                 importedEntry.getEntryId());
308         }
309     }
310 
311     private static final String _NAMESPACE = "blogs";
312 
313     private static PortletDataHandlerBoolean _categories =
314         new PortletDataHandlerBoolean(_NAMESPACE, "categories");
315 
316     private static PortletDataHandlerBoolean _comments =
317         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
318 
319     private static PortletDataHandlerBoolean _entries =
320         new PortletDataHandlerBoolean(_NAMESPACE, "entries", true, true);
321 
322     private static PortletDataHandlerBoolean _ratings =
323         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
324 
325     private static PortletDataHandlerBoolean _tags =
326         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
327 
328     private static PortletDataHandlerBoolean _wordpress =
329         new PortletDataHandlerBoolean(_NAMESPACE, "wordpress");
330 
331 }