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.PortalUtil;
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 entries = BlogsEntryUtil.findByGroupId(context.getGroupId());
97  
98              Iterator itr = entries.iterator();
99  
100             while (itr.hasNext()) {
101                 BlogsEntry entry = (BlogsEntry)itr.next();
102 
103                 if (context.addPrimaryKey(
104                         BlogsEntry.class, entry.getPrimaryKeyObj())) {
105 
106                     itr.remove();
107                 }
108                 else {
109                     entry.setUserUuid(entry.getUserUuid());
110 
111                     if (context.getBooleanParameter(_NAMESPACE, "comments")) {
112                         context.addComments(
113                             BlogsEntry.class, entry.getPrimaryKeyObj());
114                     }
115 
116                     if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
117                         context.addRatingsEntries(
118                             BlogsEntry.class, entry.getPrimaryKeyObj());
119                     }
120 
121                     if (context.getBooleanParameter(_NAMESPACE, "tags")) {
122                         context.addTagsEntries(
123                             BlogsEntry.class, entry.getPrimaryKeyObj());
124                     }
125                 }
126             }
127 
128             String xml = xStream.toXML(entries);
129 
130             Element el = root.addElement("blog-entries");
131 
132             Document tempDoc = PortalUtil.readDocumentFromXML(xml);
133 
134             el.content().add(tempDoc.getRootElement().createCopy());
135 
136             return doc.asXML();
137         }
138         catch (Exception e) {
139             throw new PortletDataException(e);
140         }
141     }
142 
143     public PortletDataHandlerControl[] getExportControls()
144         throws PortletDataException {
145 
146         return new PortletDataHandlerControl[] {
147             _entries, _comments, _ratings, _tags
148         };
149     }
150 
151     public PortletDataHandlerControl[] getImportControls()
152         throws PortletDataException {
153 
154         return new PortletDataHandlerControl[] {
155             _entries, _comments, _ratings, _tags
156         };
157     }
158 
159     public PortletPreferences importData(
160             PortletDataContext context, String portletId,
161             PortletPreferences prefs, String data)
162         throws PortletDataException {
163 
164         try {
165             XStream xStream = new XStream();
166 
167             Document doc = PortalUtil.readDocumentFromXML(data);
168 
169             Element root = doc.getRootElement();
170 
171             // Entries
172 
173             Element el = root.element("blog-entries").element("list");
174 
175             Document tempDoc = DocumentHelper.createDocument();
176 
177             tempDoc.content().add(el.createCopy());
178 
179             List entries = (List)xStream.fromXML(tempDoc.asXML());
180 
181             Iterator itr = entries.iterator();
182 
183             while (itr.hasNext()) {
184                 BlogsEntry entry = (BlogsEntry)itr.next();
185 
186                 importEntry(context, entry);
187             }
188 
189             return null;
190         }
191         catch (Exception e) {
192             throw new PortletDataException(e);
193         }
194     }
195 
196     protected void importEntry(PortletDataContext context, BlogsEntry entry)
197         throws Exception {
198 
199         long userId = context.getUserId(entry.getUserUuid());
200         long plid = context.getPlid();
201 
202         Calendar displayDateCal = CalendarFactoryUtil.getCalendar();
203 
204         displayDateCal.setTime(entry.getDisplayDate());
205 
206         int displayDateMonth = displayDateCal.get(Calendar.MONTH);
207         int displayDateDay = displayDateCal.get(Calendar.DATE);
208         int displayDateYear = displayDateCal.get(Calendar.YEAR);
209         int displayDateHour = displayDateCal.get(Calendar.HOUR);
210         int displayDateMinute = displayDateCal.get(Calendar.MINUTE);
211 
212         String[] tagsEntries = null;
213 
214         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
215             tagsEntries = context.getTagsEntries(
216                 BlogsEntry.class, entry.getPrimaryKeyObj());
217         }
218 
219         boolean addCommunityPermissions = true;
220         boolean addGuestPermissions = true;
221 
222         ThemeDisplay themeDisplay = null;
223 
224         BlogsEntry existingEntry = null;
225 
226         if (context.getDataStrategy().equals(
227                 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
228 
229             existingEntry = BlogsEntryUtil.fetchByUUID_G(
230                 entry.getUuid(), context.getGroupId());
231 
232             if (existingEntry == null) {
233                 existingEntry = BlogsEntryLocalServiceUtil.addEntry(
234                     entry.getUuid(), userId, plid, entry.getTitle(),
235                     entry.getContent(), displayDateMonth, displayDateDay,
236                     displayDateYear, displayDateHour, displayDateMinute,
237                     tagsEntries, addCommunityPermissions, addGuestPermissions,
238                     themeDisplay);
239             }
240             else {
241                 existingEntry = BlogsEntryLocalServiceUtil.updateEntry(
242                     userId, existingEntry.getEntryId(), entry.getTitle(),
243                     entry.getContent(), displayDateMonth, displayDateDay,
244                     displayDateYear, displayDateHour, displayDateMinute,
245                     tagsEntries, themeDisplay);
246             }
247         }
248         else {
249             existingEntry = BlogsEntryLocalServiceUtil.addEntry(
250                 userId, plid, entry.getTitle(), entry.getContent(),
251                 displayDateMonth, displayDateDay, displayDateYear,
252                 displayDateHour, displayDateMinute, tagsEntries,
253                 addCommunityPermissions, addGuestPermissions, themeDisplay);
254         }
255 
256         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
257             context.importComments(
258                 BlogsEntry.class, entry.getPrimaryKeyObj(),
259                 existingEntry.getPrimaryKeyObj(), context.getGroupId());
260         }
261 
262         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
263             context.importRatingsEntries(
264                 BlogsEntry.class, entry.getPrimaryKeyObj(),
265                 existingEntry.getPrimaryKeyObj());
266         }
267     }
268 
269     private static final String _NAMESPACE = "blogs";
270 
271     private static final PortletDataHandlerBoolean _entries =
272         new PortletDataHandlerBoolean(_NAMESPACE, "entries", true, true);
273 
274     private static final PortletDataHandlerBoolean _comments =
275         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
276 
277     private static final PortletDataHandlerBoolean _ratings =
278         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
279 
280     private static final PortletDataHandlerBoolean _tags =
281         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
282 
283 }