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.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.PortletDataContext;
33  import com.liferay.portal.lar.PortletDataException;
34  import com.liferay.portal.lar.PortletDataHandler;
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.theme.ThemeDisplay;
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 implements PortletDataHandler {
57  
58      public PortletPreferences deleteData(
59              PortletDataContext context, String portletId,
60              PortletPreferences prefs)
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 prefs)
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
112         };
113     }
114 
115     public PortletPreferences importData(
116             PortletDataContext context, String portletId,
117             PortletPreferences prefs, 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             return null;
141         }
142         catch (Exception e) {
143             throw new PortletDataException(e);
144         }
145     }
146 
147     public boolean isPublishToLiveByDefault() {
148         return false;
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         long plid = context.getPlid();
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         boolean addCommunityPermissions = true;
231         boolean addGuestPermissions = true;
232 
233         ThemeDisplay themeDisplay = null;
234 
235         BlogsEntry existingEntry = null;
236 
237         if (context.getDataStrategy().equals(
238                 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
239 
240             existingEntry = BlogsEntryUtil.fetchByUUID_G(
241                 entry.getUuid(), context.getGroupId());
242 
243             if (existingEntry == null) {
244                 existingEntry = BlogsEntryLocalServiceUtil.addEntry(
245                     entry.getUuid(), userId, plid, entry.getTitle(),
246                     entry.getContent(), displayDateMonth, displayDateDay,
247                     displayDateYear, displayDateHour, displayDateMinute,
248                     draft, allowTrackbacks, trackbacks, tagsEntries,
249                     addCommunityPermissions, addGuestPermissions, themeDisplay);
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, tagsEntries,
257                     themeDisplay);
258             }
259         }
260         else {
261             existingEntry = BlogsEntryLocalServiceUtil.addEntry(
262                 userId, plid, entry.getTitle(), entry.getContent(),
263                 displayDateMonth, displayDateDay, displayDateYear,
264                 displayDateHour, displayDateMinute, draft, allowTrackbacks,
265                 trackbacks, tagsEntries, addCommunityPermissions,
266                 addGuestPermissions, themeDisplay);
267         }
268 
269         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
270             context.importComments(
271                 BlogsEntry.class, entry.getEntryId(),
272                 existingEntry.getEntryId(), context.getGroupId());
273         }
274 
275         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
276             context.importRatingsEntries(
277                 BlogsEntry.class, entry.getEntryId(),
278                 existingEntry.getEntryId());
279         }
280     }
281 
282     private static final String _NAMESPACE = "blogs";
283 
284     private static final PortletDataHandlerBoolean _entries =
285         new PortletDataHandlerBoolean(_NAMESPACE, "entries", true, true);
286 
287     private static final PortletDataHandlerBoolean _comments =
288         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
289 
290     private static final PortletDataHandlerBoolean _ratings =
291         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
292 
293     private static final PortletDataHandlerBoolean _tags =
294         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
295 
296 }