1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.blogs.lar;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.xml.Document;
23  import com.liferay.portal.kernel.xml.Element;
24  import com.liferay.portal.kernel.xml.SAXReaderUtil;
25  import com.liferay.portal.lar.BasePortletDataHandler;
26  import com.liferay.portal.lar.PortletDataContext;
27  import com.liferay.portal.lar.PortletDataException;
28  import com.liferay.portal.lar.PortletDataHandlerBoolean;
29  import com.liferay.portal.lar.PortletDataHandlerControl;
30  import com.liferay.portal.lar.PortletDataHandlerKeys;
31  import com.liferay.portal.service.ServiceContext;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portlet.blogs.model.BlogsEntry;
34  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
35  import com.liferay.portlet.blogs.service.persistence.BlogsEntryUtil;
36  
37  import java.util.Calendar;
38  import java.util.List;
39  
40  import javax.portlet.PortletPreferences;
41  
42  /**
43   * <a href="BlogsPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Bruno Farache
46   * @author Raymond Augé
47   */
48  public class BlogsPortletDataHandlerImpl extends BasePortletDataHandler {
49  
50      public PortletPreferences deleteData(
51              PortletDataContext context, String portletId,
52              PortletPreferences preferences)
53          throws PortletDataException {
54  
55          try {
56              if (!context.addPrimaryKey(
57                      BlogsPortletDataHandlerImpl.class, "deleteData")) {
58  
59                  BlogsEntryLocalServiceUtil.deleteEntries(
60                      context.getScopeGroupId());
61              }
62  
63              return null;
64          }
65          catch (Exception e) {
66              throw new PortletDataException(e);
67          }
68      }
69  
70      public String exportData(
71              PortletDataContext context, String portletId,
72              PortletPreferences preferences)
73          throws PortletDataException {
74  
75          try {
76              context.addPermissions(
77                  "com.liferay.portlet.blogs", context.getScopeGroupId());
78  
79              Document doc = SAXReaderUtil.createDocument();
80  
81              Element root = doc.addElement("blogs-data");
82  
83              root.addAttribute(
84                  "group-id", String.valueOf(context.getScopeGroupId()));
85  
86              List<BlogsEntry> entries = BlogsEntryUtil.findByGroupId(
87                  context.getScopeGroupId());
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, _comments, _ratings, _tags
103         };
104     }
105 
106     public PortletDataHandlerControl[] getImportControls() {
107         return new PortletDataHandlerControl[] {
108             _entries, _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.getScopeGroupId());
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         String path = getEntryPath(context, entry);
161 
162         if (!context.isPathNotProcessed(path)) {
163             return;
164         }
165 
166         Element entryEl = root.addElement("entry");
167 
168         entryEl.addAttribute("path", path);
169 
170         context.addPermissions(BlogsEntry.class, entry.getEntryId());
171 
172         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
173             context.addComments(BlogsEntry.class, entry.getEntryId());
174         }
175 
176         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
177             context.addRatingsEntries(BlogsEntry.class, entry.getEntryId());
178         }
179 
180         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
181             context.addTagsEntries(BlogsEntry.class, entry.getEntryId());
182         }
183 
184         entry.setUserUuid(entry.getUserUuid());
185 
186         context.addZipEntry(path, entry);
187     }
188 
189     protected String getEntryPath(
190         PortletDataContext context, BlogsEntry entry) {
191 
192         StringBundler sb = new StringBundler(4);
193 
194         sb.append(context.getPortletPath(PortletKeys.BLOGS));
195         sb.append("/entries/");
196         sb.append(entry.getEntryId());
197         sb.append(".xml");
198 
199         return sb.toString();
200     }
201 
202     protected void importEntry(PortletDataContext context, BlogsEntry entry)
203         throws Exception {
204 
205         long userId = context.getUserId(entry.getUserUuid());
206 
207         Calendar displayDateCal = CalendarFactoryUtil.getCalendar();
208 
209         displayDateCal.setTime(entry.getDisplayDate());
210 
211         int displayDateMonth = displayDateCal.get(Calendar.MONTH);
212         int displayDateDay = displayDateCal.get(Calendar.DATE);
213         int displayDateYear = displayDateCal.get(Calendar.YEAR);
214         int displayDateHour = displayDateCal.get(Calendar.HOUR);
215         int displayDateMinute = displayDateCal.get(Calendar.MINUTE);
216 
217         if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
218             displayDateHour += 12;
219         }
220 
221         boolean draft = entry.isDraft();
222         boolean allowTrackbacks = entry.isAllowTrackbacks();
223         String[] trackbacks = StringUtil.split(entry.getTrackbacks());
224 
225         String[] tagsEntries = null;
226 
227         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
228             tagsEntries = context.getTagsEntries(
229                 BlogsEntry.class, entry.getEntryId());
230         }
231 
232         ServiceContext serviceContext = new ServiceContext();
233 
234         serviceContext.setAddCommunityPermissions(true);
235         serviceContext.setAddGuestPermissions(true);
236         serviceContext.setCreateDate(entry.getCreateDate());
237         serviceContext.setModifiedDate(entry.getModifiedDate());
238         serviceContext.setScopeGroupId(context.getScopeGroupId());
239         serviceContext.setTagsEntries(tagsEntries);
240 
241         BlogsEntry existingEntry = null;
242 
243         if (context.getDataStrategy().equals(
244                 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
245 
246             existingEntry = BlogsEntryUtil.fetchByUUID_G(
247                 entry.getUuid(), context.getScopeGroupId());
248 
249             if (existingEntry == null) {
250                 existingEntry = BlogsEntryLocalServiceUtil.addEntry(
251                     entry.getUuid(), userId, entry.getTitle(),
252                     entry.getContent(), displayDateMonth, displayDateDay,
253                     displayDateYear, displayDateHour, displayDateMinute,
254                     draft, allowTrackbacks, trackbacks, serviceContext);
255             }
256             else {
257                 existingEntry = BlogsEntryLocalServiceUtil.updateEntry(
258                     userId, existingEntry.getEntryId(), entry.getTitle(),
259                     entry.getContent(), displayDateMonth, displayDateDay,
260                     displayDateYear, displayDateHour, displayDateMinute,
261                     draft, allowTrackbacks, trackbacks, serviceContext);
262             }
263         }
264         else {
265             existingEntry = BlogsEntryLocalServiceUtil.addEntry(
266                 userId, entry.getTitle(), entry.getContent(), displayDateMonth,
267                 displayDateDay, displayDateYear, displayDateHour,
268                 displayDateMinute, draft, allowTrackbacks, trackbacks,
269                 serviceContext);
270         }
271 
272         context.importPermissions(
273             BlogsEntry.class, entry.getEntryId(), existingEntry.getEntryId());
274 
275         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
276             context.importComments(
277                 BlogsEntry.class, entry.getEntryId(),
278                 existingEntry.getEntryId(), context.getScopeGroupId());
279         }
280 
281         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
282             context.importRatingsEntries(
283                 BlogsEntry.class, entry.getEntryId(),
284                 existingEntry.getEntryId());
285         }
286     }
287 
288     private static final String _NAMESPACE = "blogs";
289 
290     private static final PortletDataHandlerBoolean _entries =
291         new PortletDataHandlerBoolean(_NAMESPACE, "entries", true, true);
292 
293     private static final PortletDataHandlerBoolean _comments =
294         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
295 
296     private static final PortletDataHandlerBoolean _ratings =
297         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
298 
299     private static final PortletDataHandlerBoolean _tags =
300         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
301 
302     private static final PortletDataHandlerBoolean _wordpress =
303         new PortletDataHandlerBoolean(_NAMESPACE, "wordpress");
304 
305 }