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