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