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