001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.blogs.lar;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
020    import com.liferay.portal.kernel.lar.PortletDataContext;
021    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
022    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
023    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.kernel.xml.Document;
028    import com.liferay.portal.kernel.xml.Element;
029    import com.liferay.portal.kernel.xml.SAXReaderUtil;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.util.PortletKeys;
032    import com.liferay.portlet.blogs.model.BlogsEntry;
033    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
034    import com.liferay.portlet.blogs.service.persistence.BlogsEntryUtil;
035    
036    import java.util.Calendar;
037    import java.util.List;
038    
039    import javax.portlet.PortletPreferences;
040    
041    /**
042     * @author Bruno Farache
043     * @author Raymond Augé
044     * @author Juan Fernández
045     */
046    public class BlogsPortletDataHandlerImpl extends BasePortletDataHandler {
047    
048            public PortletDataHandlerControl[] getExportControls() {
049                    return new PortletDataHandlerControl[] {
050                            _entries, _categories, _comments, _ratings, _tags
051                    };
052            }
053    
054            public PortletDataHandlerControl[] getImportControls() {
055                    return new PortletDataHandlerControl[] {
056                            _entries, _categories, _comments, _ratings, _tags, _wordpress
057                    };
058            }
059    
060            protected PortletPreferences doDeleteData(
061                            PortletDataContext context, String portletId,
062                            PortletPreferences preferences)
063                    throws Exception {
064    
065                    if (!context.addPrimaryKey(
066                                    BlogsPortletDataHandlerImpl.class, "deleteData")) {
067    
068                            BlogsEntryLocalServiceUtil.deleteEntries(context.getScopeGroupId());
069                    }
070    
071                    return null;
072            }
073    
074            protected String doExportData(
075                            PortletDataContext context, String portletId,
076                            PortletPreferences preferences)
077                    throws Exception {
078    
079                    context.addPermissions(
080                            "com.liferay.portlet.blogs", context.getScopeGroupId());
081    
082                    Document document = SAXReaderUtil.createDocument();
083    
084                    Element rootElement = document.addElement("blogs-data");
085    
086                    rootElement.addAttribute(
087                            "group-id", String.valueOf(context.getScopeGroupId()));
088    
089                    List<BlogsEntry> entries = BlogsEntryUtil.findByGroupId(
090                            context.getScopeGroupId());
091    
092                    for (BlogsEntry entry : entries) {
093                            exportEntry(context, rootElement, entry);
094                    }
095    
096                    return document.formattedString();
097            }
098    
099            protected PortletPreferences doImportData(
100                            PortletDataContext context, String portletId,
101                            PortletPreferences preferences, String data)
102                    throws Exception {
103    
104                    context.importPermissions(
105                            "com.liferay.portlet.blogs", context.getSourceGroupId(),
106                            context.getScopeGroupId());
107    
108                    Document document = SAXReaderUtil.read(data);
109    
110                    Element rootElement = document.getRootElement();
111    
112                    for (Element entryElement : rootElement.elements("entry")) {
113                            String path = entryElement.attributeValue("path");
114    
115                            if (!context.isPathNotProcessed(path)) {
116                                    continue;
117                            }
118    
119                            BlogsEntry entry = (BlogsEntry)context.getZipEntryAsObject(path);
120    
121                            importEntry(context, entry);
122                    }
123    
124                    if (context.getBooleanParameter(_NAMESPACE, "wordpress")) {
125                            WordPressImporter.importData(context);
126                    }
127    
128                    return null;
129            }
130    
131            protected void exportEntry(
132                            PortletDataContext context, Element root, BlogsEntry entry)
133                    throws PortalException, SystemException {
134    
135                    if (!context.isWithinDateRange(entry.getModifiedDate())) {
136                            return;
137                    }
138    
139                    if (entry.getStatus() != WorkflowConstants.STATUS_APPROVED) {
140                            return;
141                    }
142    
143                    String path = getEntryPath(context, entry);
144    
145                    if (!context.isPathNotProcessed(path)) {
146                            return;
147                    }
148    
149                    Element entryElement = root.addElement("entry");
150    
151                    entryElement.addAttribute("path", path);
152    
153                    context.addPermissions(BlogsEntry.class, entry.getEntryId());
154    
155                    if (context.getBooleanParameter(_NAMESPACE, "categories")) {
156                            context.addAssetCategories(BlogsEntry.class, entry.getEntryId());
157                    }
158    
159                    if (context.getBooleanParameter(_NAMESPACE, "comments")) {
160                            context.addComments(BlogsEntry.class, entry.getEntryId());
161                    }
162    
163                    if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
164                            context.addRatingsEntries(BlogsEntry.class, entry.getEntryId());
165                    }
166    
167                    if (context.getBooleanParameter(_NAMESPACE, "tags")) {
168                            context.addAssetTags(BlogsEntry.class, entry.getEntryId());
169                    }
170    
171                    entry.setUserUuid(entry.getUserUuid());
172    
173                    context.addZipEntry(path, entry);
174            }
175    
176            protected String getEntryPath(
177                    PortletDataContext context, BlogsEntry entry) {
178    
179                    StringBundler sb = new StringBundler(4);
180    
181                    sb.append(context.getPortletPath(PortletKeys.BLOGS));
182                    sb.append("/entries/");
183                    sb.append(entry.getEntryId());
184                    sb.append(".xml");
185    
186                    return sb.toString();
187            }
188    
189            protected void importEntry(PortletDataContext context, BlogsEntry entry)
190                    throws Exception {
191    
192                    long userId = context.getUserId(entry.getUserUuid());
193    
194                    Calendar displayDateCal = CalendarFactoryUtil.getCalendar();
195    
196                    displayDateCal.setTime(entry.getDisplayDate());
197    
198                    int displayDateMonth = displayDateCal.get(Calendar.MONTH);
199                    int displayDateDay = displayDateCal.get(Calendar.DATE);
200                    int displayDateYear = displayDateCal.get(Calendar.YEAR);
201                    int displayDateHour = displayDateCal.get(Calendar.HOUR);
202                    int displayDateMinute = displayDateCal.get(Calendar.MINUTE);
203    
204                    if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
205                            displayDateHour += 12;
206                    }
207    
208                    boolean allowPingbacks = entry.isAllowPingbacks();
209                    boolean allowTrackbacks = entry.isAllowTrackbacks();
210                    String[] trackbacks = StringUtil.split(entry.getTrackbacks());
211                    int status = entry.getStatus();
212    
213                    long[] assetCategoryIds = null;
214                    String[] assetTagNames = null;
215    
216                    if (context.getBooleanParameter(_NAMESPACE, "categories")) {
217                            assetCategoryIds = context.getAssetCategoryIds(
218                                    BlogsEntry.class, entry.getEntryId());
219                    }
220    
221                    if (context.getBooleanParameter(_NAMESPACE, "tags")) {
222                            assetTagNames = context.getAssetTagNames(
223                                    BlogsEntry.class, entry.getEntryId());
224                    }
225    
226                    ServiceContext serviceContext = new ServiceContext();
227    
228                    serviceContext.setAddCommunityPermissions(true);
229                    serviceContext.setAddGuestPermissions(true);
230                    serviceContext.setAssetCategoryIds(assetCategoryIds);
231                    serviceContext.setAssetTagNames(assetTagNames);
232                    serviceContext.setCreateDate(entry.getCreateDate());
233                    serviceContext.setModifiedDate(entry.getModifiedDate());
234                    serviceContext.setScopeGroupId(context.getScopeGroupId());
235    
236                    if (status != WorkflowConstants.STATUS_APPROVED) {
237                            serviceContext.setWorkflowAction(
238                                    WorkflowConstants.ACTION_SAVE_DRAFT);
239                    }
240    
241                    BlogsEntry importedEntry = null;
242    
243                    if (context.isDataStrategyMirror()) {
244                            BlogsEntry existingEntry = BlogsEntryUtil.fetchByUUID_G(
245                                    entry.getUuid(), context.getScopeGroupId());
246    
247                            if (existingEntry == null) {
248                                    serviceContext.setUuid(entry.getUuid());
249    
250                                    importedEntry = BlogsEntryLocalServiceUtil.addEntry(
251                                            userId, entry.getTitle(), entry.getContent(),
252                                            displayDateMonth, displayDateDay, displayDateYear,
253                                            displayDateHour, displayDateMinute, allowPingbacks,
254                                            allowTrackbacks, trackbacks, serviceContext);
255                            }
256                            else {
257                                    importedEntry = BlogsEntryLocalServiceUtil.updateEntry(
258                                            userId, existingEntry.getEntryId(), entry.getTitle(),
259                                            entry.getContent(), displayDateMonth, displayDateDay,
260                                            displayDateYear, displayDateHour, displayDateMinute,
261                                            allowPingbacks, allowTrackbacks, trackbacks,
262                                            serviceContext);
263                            }
264                    }
265                    else {
266                            importedEntry = BlogsEntryLocalServiceUtil.addEntry(
267                                    userId, entry.getTitle(), entry.getContent(), displayDateMonth,
268                                    displayDateDay, displayDateYear, displayDateHour,
269                                    displayDateMinute, allowPingbacks, allowTrackbacks, trackbacks,
270                                    serviceContext);
271                    }
272    
273                    context.importPermissions(
274                            BlogsEntry.class, entry.getEntryId(), importedEntry.getEntryId());
275    
276                    if (context.getBooleanParameter(_NAMESPACE, "comments")) {
277                            context.importComments(
278                                    BlogsEntry.class, entry.getEntryId(),
279                                    importedEntry.getEntryId(), context.getScopeGroupId());
280                    }
281    
282                    if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
283                            context.importRatingsEntries(
284                                    BlogsEntry.class, entry.getEntryId(),
285                                    importedEntry.getEntryId());
286                    }
287            }
288    
289            private static final String _NAMESPACE = "blogs";
290    
291            private static PortletDataHandlerBoolean _categories =
292                    new PortletDataHandlerBoolean(_NAMESPACE, "categories");
293    
294            private static PortletDataHandlerBoolean _comments =
295                    new PortletDataHandlerBoolean(_NAMESPACE, "comments");
296    
297            private static PortletDataHandlerBoolean _entries =
298                    new PortletDataHandlerBoolean(_NAMESPACE, "entries", true, true);
299    
300            private static PortletDataHandlerBoolean _ratings =
301                    new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
302    
303            private static PortletDataHandlerBoolean _tags =
304                    new PortletDataHandlerBoolean(_NAMESPACE, "tags");
305    
306            private static PortletDataHandlerBoolean _wordpress =
307                    new PortletDataHandlerBoolean(_NAMESPACE, "wordpress");
308    
309    }