1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.SystemException;
26  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.xml.Document;
29  import com.liferay.portal.kernel.xml.Element;
30  import com.liferay.portal.kernel.xml.SAXReaderUtil;
31  import com.liferay.portal.lar.BasePortletDataHandler;
32  import com.liferay.portal.lar.PortletDataContext;
33  import com.liferay.portal.lar.PortletDataException;
34  import com.liferay.portal.lar.PortletDataHandlerBoolean;
35  import com.liferay.portal.lar.PortletDataHandlerControl;
36  import com.liferay.portal.lar.PortletDataHandlerKeys;
37  import com.liferay.portal.service.ServiceContext;
38  import com.liferay.portal.util.PortletKeys;
39  import com.liferay.portlet.blogs.model.BlogsEntry;
40  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
41  import com.liferay.portlet.blogs.service.persistence.BlogsEntryUtil;
42  
43  import java.util.Calendar;
44  import java.util.List;
45  
46  import javax.portlet.PortletPreferences;
47  
48  /**
49   * <a href="BlogsPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Bruno Farache
52   * @author Raymond Augé
53   */
54  public class BlogsPortletDataHandlerImpl extends BasePortletDataHandler {
55  
56      public PortletPreferences deleteData(
57              PortletDataContext context, String portletId,
58              PortletPreferences preferences)
59          throws PortletDataException {
60  
61          try {
62              if (!context.addPrimaryKey(
63                      BlogsPortletDataHandlerImpl.class, "deleteData")) {
64  
65                  BlogsEntryLocalServiceUtil.deleteEntries(context.getGroupId());
66              }
67  
68              return null;
69          }
70          catch (Exception e) {
71              throw new PortletDataException(e);
72          }
73      }
74  
75      public String exportData(
76              PortletDataContext context, String portletId,
77              PortletPreferences preferences)
78          throws PortletDataException {
79  
80          try {
81              Document doc = SAXReaderUtil.createDocument();
82  
83              Element root = doc.addElement("blogs-data");
84  
85              root.addAttribute("group-id", String.valueOf(context.getGroupId()));
86  
87              List<BlogsEntry> entries = BlogsEntryUtil.findByGroupId(
88                  context.getGroupId());
89  
90              for (BlogsEntry entry : entries) {
91                  exportEntry(context, root, entry);
92              }
93  
94              return doc.formattedString();
95          }
96          catch (Exception e) {
97              throw new PortletDataException(e);
98          }
99      }
100 
101     public PortletDataHandlerControl[] getExportControls() {
102         return new PortletDataHandlerControl[] {
103             _entries, _comments, _ratings, _tags
104         };
105     }
106 
107     public PortletDataHandlerControl[] getImportControls() {
108         return new PortletDataHandlerControl[] {
109             _entries, _comments, _ratings, _tags, _wordpress
110         };
111     }
112 
113     public PortletPreferences importData(
114             PortletDataContext context, String portletId,
115             PortletPreferences preferences, String data)
116         throws PortletDataException {
117 
118         try {
119             Document doc = SAXReaderUtil.read(data);
120 
121             Element root = doc.getRootElement();
122 
123             List<Element> entryEls = root.elements("entry");
124 
125             for (Element entryEl : entryEls) {
126                 String path = entryEl.attributeValue("path");
127 
128                 if (!context.isPathNotProcessed(path)) {
129                     continue;
130                 }
131 
132                 BlogsEntry entry = (BlogsEntry)context.getZipEntryAsObject(
133                     path);
134 
135                 importEntry(context, entry);
136             }
137 
138             if (context.getBooleanParameter(_NAMESPACE, "wordpress")) {
139                 WordPressImporter.importData(context);
140             }
141 
142             return null;
143         }
144         catch (Exception e) {
145             throw new PortletDataException(e);
146         }
147     }
148 
149     protected void exportEntry(
150             PortletDataContext context, Element root, BlogsEntry entry)
151         throws SystemException {
152 
153         if (!context.isWithinDateRange(entry.getModifiedDate())) {
154             return;
155         }
156 
157         String path = getEntryPath(context, entry);
158 
159         if (!context.isPathNotProcessed(path)) {
160             return;
161         }
162 
163         Element entryEl = root.addElement("entry");
164 
165         entryEl.addAttribute("path", path);
166 
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     protected String getEntryPath(
185         PortletDataContext context, BlogsEntry entry) {
186 
187         StringBuilder sb = new StringBuilder();
188 
189         sb.append(context.getPortletPath(PortletKeys.BLOGS));
190         sb.append("/entries/");
191         sb.append(entry.getEntryId());
192         sb.append(".xml");
193 
194         return sb.toString();
195     }
196 
197     protected void importEntry(PortletDataContext context, BlogsEntry entry)
198         throws Exception {
199 
200         long userId = context.getUserId(entry.getUserUuid());
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         if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
213             displayDateHour += 12;
214         }
215 
216         boolean draft = entry.isDraft();
217         boolean allowTrackbacks = entry.isAllowTrackbacks();
218         String[] trackbacks = StringUtil.split(entry.getTrackbacks());
219 
220         String[] tagsEntries = null;
221 
222         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
223             tagsEntries = context.getTagsEntries(
224                 BlogsEntry.class, entry.getEntryId());
225         }
226 
227         ServiceContext serviceContext = new ServiceContext();
228 
229         serviceContext.setAddCommunityPermissions(true);
230         serviceContext.setAddGuestPermissions(true);
231         serviceContext.setScopeGroupId(context.getGroupId());
232         serviceContext.setTagsEntries(tagsEntries);
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, entry.getTitle(),
245                     entry.getContent(), displayDateMonth, displayDateDay,
246                     displayDateYear, displayDateHour, displayDateMinute,
247                     draft, allowTrackbacks, trackbacks, serviceContext);
248             }
249             else {
250                 existingEntry = BlogsEntryLocalServiceUtil.updateEntry(
251                     userId, existingEntry.getEntryId(), entry.getTitle(),
252                     entry.getContent(), displayDateMonth, displayDateDay,
253                     displayDateYear, displayDateHour, displayDateMinute,
254                     draft, allowTrackbacks, trackbacks, serviceContext);
255             }
256         }
257         else {
258             existingEntry = BlogsEntryLocalServiceUtil.addEntry(
259                 userId, entry.getTitle(), entry.getContent(), displayDateMonth,
260                 displayDateDay, displayDateYear, displayDateHour,
261                 displayDateMinute, draft, allowTrackbacks, trackbacks,
262                 serviceContext);
263         }
264 
265         if (context.getBooleanParameter(_NAMESPACE, "comments")) {
266             context.importComments(
267                 BlogsEntry.class, entry.getEntryId(),
268                 existingEntry.getEntryId(), context.getGroupId());
269         }
270 
271         if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
272             context.importRatingsEntries(
273                 BlogsEntry.class, entry.getEntryId(),
274                 existingEntry.getEntryId());
275         }
276     }
277 
278     private static final String _NAMESPACE = "blogs";
279 
280     private static final PortletDataHandlerBoolean _entries =
281         new PortletDataHandlerBoolean(_NAMESPACE, "entries", true, true);
282 
283     private static final PortletDataHandlerBoolean _comments =
284         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
285 
286     private static final PortletDataHandlerBoolean _ratings =
287         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
288 
289     private static final PortletDataHandlerBoolean _tags =
290         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
291 
292     private static final PortletDataHandlerBoolean _wordpress =
293         new PortletDataHandlerBoolean(_NAMESPACE, "wordpress");
294 
295 }