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.kernel.lar.PortletDataContext;
26  import com.liferay.portal.kernel.lar.PortletDataException;
27  import com.liferay.portal.kernel.lar.PortletDataHandler;
28  import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
29  import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
30  import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
31  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.DocumentUtil;
34  import com.liferay.portlet.blogs.model.BlogsEntry;
35  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
36  import com.liferay.portlet.blogs.service.persistence.BlogsEntryUtil;
37  
38  import com.thoughtworks.xstream.XStream;
39  
40  import java.util.Calendar;
41  import java.util.Iterator;
42  import java.util.List;
43  
44  import javax.portlet.PortletPreferences;
45  
46  import org.dom4j.Document;
47  import org.dom4j.DocumentHelper;
48  import org.dom4j.Element;
49  
50  /**
51   * <a href="BlogsPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Bruno Farache
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  
65              // Entries
66  
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              XStream xStream = new XStream();
87  
88              Document doc = DocumentHelper.createDocument();
89  
90              Element root = doc.addElement("blogs-data");
91  
92              root.addAttribute("group-id", String.valueOf(context.getGroupId()));
93  
94              // Entries
95  
96              List<BlogsEntry> entries = BlogsEntryUtil.findByGroupId(
97                  context.getGroupId());
98  
99              Iterator<BlogsEntry> itr = entries.iterator();
100 
101             while (itr.hasNext()) {
102                 BlogsEntry entry = itr.next();
103 
104                 if (context.addPrimaryKey(
105                         BlogsEntry.class, entry.getPrimaryKeyObj())) {
106 
107                     itr.remove();
108                 }
109                 else {
110                     entry.setUserUuid(entry.getUserUuid());
111 
112                     if (context.getBooleanParameter(_NAMESPACE, "comments")) {
113                         context.addComments(
114                             BlogsEntry.class, entry.getPrimaryKeyObj());
115                     }
116 
117                     if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
118                         context.addRatingsEntries(
119                             BlogsEntry.class, entry.getPrimaryKeyObj());
120                     }
121 
122                     if (context.getBooleanParameter(_NAMESPACE, "tags")) {
123                         context.addTagsEntries(
124                             BlogsEntry.class, entry.getPrimaryKeyObj());
125                     }
126                 }
127             }
128 
129             String xml = xStream.toXML(entries);
130 
131             Element el = root.addElement("blog-entries");
132 
133             Document tempDoc = DocumentUtil.readDocumentFromXML(xml);
134 
135             el.content().add(tempDoc.getRootElement().createCopy());
136 
137             return doc.asXML();
138         }
139         catch (Exception e) {
140             throw new PortletDataException(e);
141         }
142     }
143 
144     public PortletDataHandlerControl[] getExportControls()
145         throws PortletDataException {
146 
147         return new PortletDataHandlerControl[] {
148             _entries, _comments, _ratings, _tags
149         };
150     }
151 
152     public PortletDataHandlerControl[] getImportControls()
153         throws PortletDataException {
154 
155         return new PortletDataHandlerControl[] {
156             _entries, _comments, _ratings, _tags
157         };
158     }
159 
160     public PortletPreferences importData(
161             PortletDataContext context, String portletId,
162             PortletPreferences prefs, String data)
163         throws PortletDataException {
164 
165         try {
166             XStream xStream = new XStream();
167 
168             Document doc = DocumentUtil.readDocumentFromXML(data);
169 
170             Element root = doc.getRootElement();
171 
172             // Entries
173 
174             Element el = root.element("blog-entries").element("list");
175 
176             Document tempDoc = DocumentHelper.createDocument();
177 
178             tempDoc.content().add(el.createCopy());
179 
180             List<BlogsEntry> entries = (List<BlogsEntry>)xStream.fromXML(
181                 tempDoc.asXML());
182 
183             Iterator<BlogsEntry> itr = entries.iterator();
184 
185             while (itr.hasNext()) {
186                 BlogsEntry entry = itr.next();
187 
188                 importEntry(context, entry);
189             }
190 
191             return null;
192         }
193         catch (Exception e) {
194             throw new PortletDataException(e);
195         }
196     }
197 
198     public boolean isPublishToLiveByDefault() {
199         return false;
200     }
201 
202     protected void importEntry(PortletDataContext context, BlogsEntry entry)
203         throws Exception {
204 
205         long userId = context.getUserId(entry.getUserUuid());
206         long plid = context.getPlid();
207 
208         Calendar displayDateCal = CalendarFactoryUtil.getCalendar();
209 
210         displayDateCal.setTime(entry.getDisplayDate());
211 
212         int displayDateMonth = displayDateCal.get(Calendar.MONTH);
213         int displayDateDay = displayDateCal.get(Calendar.DATE);
214         int displayDateYear = displayDateCal.get(Calendar.YEAR);
215         int displayDateHour = displayDateCal.get(Calendar.HOUR);
216         int displayDateMinute = displayDateCal.get(Calendar.MINUTE);
217 
218         if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
219             displayDateHour += 12;
220         }
221 
222         String[] tagsEntries = null;
223 
224         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
225             tagsEntries = context.getTagsEntries(
226                 BlogsEntry.class, entry.getPrimaryKeyObj());
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                     tagsEntries, addCommunityPermissions, addGuestPermissions,
248                     themeDisplay);
249             }
250             else {
251                 existingEntry = BlogsEntryLocalServiceUtil.updateEntry(
252                     userId, existingEntry.getEntryId(), entry.getTitle(),
253                     entry.getContent(), displayDateMonth, displayDateDay,
254                     displayDateYear, displayDateHour, displayDateMinute,
255                     tagsEntries, themeDisplay);
256             }
257         }
258         else {
259             existingEntry = BlogsEntryLocalServiceUtil.addEntry(
260                 userId, plid, entry.getTitle(), entry.getContent(),
261                 displayDateMonth, displayDateDay, displayDateYear,
262                 displayDateHour, displayDateMinute, tagsEntries,
263                 addCommunityPermissions, addGuestPermissions, themeDisplay);
264         }
265 
266         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
267             context.importComments(
268                 BlogsEntry.class, entry.getPrimaryKeyObj(),
269                 existingEntry.getPrimaryKeyObj(), context.getGroupId());
270         }
271 
272         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
273             context.importRatingsEntries(
274                 BlogsEntry.class, entry.getPrimaryKeyObj(),
275                 existingEntry.getPrimaryKeyObj());
276         }
277     }
278 
279     private static final String _NAMESPACE = "blogs";
280 
281     private static final PortletDataHandlerBoolean _entries =
282         new PortletDataHandlerBoolean(_NAMESPACE, "entries", true, true);
283 
284     private static final PortletDataHandlerBoolean _comments =
285         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
286 
287     private static final PortletDataHandlerBoolean _ratings =
288         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
289 
290     private static final PortletDataHandlerBoolean _tags =
291         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
292 
293 }