1   /**
2    * Copyright (c) 2000-2009 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.portlet.blogs.lar;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
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.lar.BasePortletDataHandler;
33  import com.liferay.portal.lar.PortletDataContext;
34  import com.liferay.portal.lar.PortletDataException;
35  import com.liferay.portal.lar.PortletDataHandlerBoolean;
36  import com.liferay.portal.lar.PortletDataHandlerControl;
37  import com.liferay.portal.lar.PortletDataHandlerKeys;
38  import com.liferay.portal.service.ServiceContext;
39  import com.liferay.portal.util.PortletKeys;
40  import com.liferay.portlet.blogs.model.BlogsEntry;
41  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
42  import com.liferay.portlet.blogs.service.persistence.BlogsEntryUtil;
43  
44  import java.util.Calendar;
45  import java.util.List;
46  
47  import javax.portlet.PortletPreferences;
48  
49  /**
50   * <a href="BlogsPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Bruno Farache
53   * @author Raymond Augé
54   *
55   */
56  public class BlogsPortletDataHandlerImpl extends BasePortletDataHandler {
57  
58      public PortletPreferences deleteData(
59              PortletDataContext context, String portletId,
60              PortletPreferences preferences)
61          throws PortletDataException {
62  
63          try {
64              if (!context.addPrimaryKey(
65                      BlogsPortletDataHandlerImpl.class, "deleteData")) {
66  
67                  BlogsEntryLocalServiceUtil.deleteEntries(context.getGroupId());
68              }
69  
70              return null;
71          }
72          catch (Exception e) {
73              throw new PortletDataException(e);
74          }
75      }
76  
77      public String exportData(
78              PortletDataContext context, String portletId,
79              PortletPreferences preferences)
80          throws PortletDataException {
81  
82          try {
83              Document doc = SAXReaderUtil.createDocument();
84  
85              Element root = doc.addElement("blogs-data");
86  
87              root.addAttribute("group-id", String.valueOf(context.getGroupId()));
88  
89              List<BlogsEntry> entries = BlogsEntryUtil.findByGroupId(
90                  context.getGroupId());
91  
92              for (BlogsEntry entry : entries) {
93                  exportEntry(context, root, entry);
94              }
95  
96              return doc.formattedString();
97          }
98          catch (Exception e) {
99              throw new PortletDataException(e);
100         }
101     }
102 
103     public PortletDataHandlerControl[] getExportControls() {
104         return new PortletDataHandlerControl[] {
105             _entries, _comments, _ratings, _tags
106         };
107     }
108 
109     public PortletDataHandlerControl[] getImportControls() {
110         return new PortletDataHandlerControl[] {
111             _entries, _comments, _ratings, _tags, _wordpress
112         };
113     }
114 
115     public PortletPreferences importData(
116             PortletDataContext context, String portletId,
117             PortletPreferences preferences, String data)
118         throws PortletDataException {
119 
120         try {
121             Document doc = SAXReaderUtil.read(data);
122 
123             Element root = doc.getRootElement();
124 
125             List<Element> entryEls = root.elements("entry");
126 
127             for (Element entryEl : entryEls) {
128                 String path = entryEl.attributeValue("path");
129 
130                 if (!context.isPathNotProcessed(path)) {
131                     continue;
132                 }
133 
134                 BlogsEntry entry = (BlogsEntry)context.getZipEntryAsObject(
135                     path);
136 
137                 importEntry(context, entry);
138             }
139 
140             if (context.getBooleanParameter(_NAMESPACE, "wordpress")) {
141                 WordPressImporter.importData(context);
142             }
143 
144             return null;
145         }
146         catch (Exception e) {
147             throw new PortletDataException(e);
148         }
149     }
150 
151     protected void exportEntry(
152             PortletDataContext context, Element root, BlogsEntry entry)
153         throws PortalException, SystemException {
154 
155         if (!context.isWithinDateRange(entry.getModifiedDate())) {
156             return;
157         }
158 
159         String path = getEntryPath(context, entry);
160 
161         if (!context.isPathNotProcessed(path)) {
162             return;
163         }
164 
165         Element entryEl = root.addElement("entry");
166 
167         entryEl.addAttribute("path", path);
168 
169         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
170             context.addComments(BlogsEntry.class, entry.getEntryId());
171         }
172 
173         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
174             context.addRatingsEntries(BlogsEntry.class, entry.getEntryId());
175         }
176 
177         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
178             context.addTagsEntries(BlogsEntry.class, entry.getEntryId());
179         }
180 
181         entry.setUserUuid(entry.getUserUuid());
182 
183         context.addZipEntry(path, entry);
184     }
185 
186     protected String getEntryPath(
187         PortletDataContext context, BlogsEntry entry) {
188 
189         StringBuilder sb = new StringBuilder();
190 
191         sb.append(context.getPortletPath(PortletKeys.BLOGS));
192         sb.append("/entries/");
193         sb.append(entry.getEntryId());
194         sb.append(".xml");
195 
196         return sb.toString();
197     }
198 
199     protected void importEntry(PortletDataContext context, BlogsEntry entry)
200         throws Exception {
201 
202         long userId = context.getUserId(entry.getUserUuid());
203 
204         Calendar displayDateCal = CalendarFactoryUtil.getCalendar();
205 
206         displayDateCal.setTime(entry.getDisplayDate());
207 
208         int displayDateMonth = displayDateCal.get(Calendar.MONTH);
209         int displayDateDay = displayDateCal.get(Calendar.DATE);
210         int displayDateYear = displayDateCal.get(Calendar.YEAR);
211         int displayDateHour = displayDateCal.get(Calendar.HOUR);
212         int displayDateMinute = displayDateCal.get(Calendar.MINUTE);
213 
214         if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
215             displayDateHour += 12;
216         }
217 
218         boolean draft = entry.isDraft();
219         boolean allowTrackbacks = entry.isAllowTrackbacks();
220         String[] trackbacks = StringUtil.split(entry.getTrackbacks());
221 
222         String[] tagsEntries = null;
223 
224         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
225             tagsEntries = context.getTagsEntries(
226                 BlogsEntry.class, entry.getEntryId());
227         }
228 
229         ServiceContext serviceContext = new ServiceContext();
230 
231         serviceContext.setAddCommunityPermissions(true);
232         serviceContext.setAddGuestPermissions(true);
233         serviceContext.setScopeGroupId(context.getGroupId());
234         serviceContext.setTagsEntries(tagsEntries);
235 
236         BlogsEntry existingEntry = null;
237 
238         if (context.getDataStrategy().equals(
239                 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
240 
241             existingEntry = BlogsEntryUtil.fetchByUUID_G(
242                 entry.getUuid(), context.getGroupId());
243 
244             if (existingEntry == null) {
245                 existingEntry = BlogsEntryLocalServiceUtil.addEntry(
246                     entry.getUuid(), userId, entry.getTitle(),
247                     entry.getContent(), displayDateMonth, displayDateDay,
248                     displayDateYear, displayDateHour, displayDateMinute,
249                     draft, allowTrackbacks, trackbacks, serviceContext);
250             }
251             else {
252                 existingEntry = BlogsEntryLocalServiceUtil.updateEntry(
253                     userId, existingEntry.getEntryId(), entry.getTitle(),
254                     entry.getContent(), displayDateMonth, displayDateDay,
255                     displayDateYear, displayDateHour, displayDateMinute,
256                     draft, allowTrackbacks, trackbacks, serviceContext);
257             }
258         }
259         else {
260             existingEntry = BlogsEntryLocalServiceUtil.addEntry(
261                 userId, entry.getTitle(), entry.getContent(), displayDateMonth,
262                 displayDateDay, displayDateYear, displayDateHour,
263                 displayDateMinute, draft, allowTrackbacks, trackbacks,
264                 serviceContext);
265         }
266 
267         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
268             context.importComments(
269                 BlogsEntry.class, entry.getEntryId(),
270                 existingEntry.getEntryId(), context.getGroupId());
271         }
272 
273         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
274             context.importRatingsEntries(
275                 BlogsEntry.class, entry.getEntryId(),
276                 existingEntry.getEntryId());
277         }
278     }
279 
280     private static final String _NAMESPACE = "blogs";
281 
282     private static final PortletDataHandlerBoolean _entries =
283         new PortletDataHandlerBoolean(_NAMESPACE, "entries", true, true);
284 
285     private static final PortletDataHandlerBoolean _comments =
286         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
287 
288     private static final PortletDataHandlerBoolean _ratings =
289         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
290 
291     private static final PortletDataHandlerBoolean _tags =
292         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
293 
294     private static final PortletDataHandlerBoolean _wordpress =
295         new PortletDataHandlerBoolean(_NAMESPACE, "wordpress");
296 
297 }