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