1
14
15 package com.liferay.portlet.blogs.lar;
16
17 import com.liferay.portal.kernel.exception.PortalException;
18 import com.liferay.portal.kernel.exception.SystemException;
19 import com.liferay.portal.kernel.util.CalendarFactoryUtil;
20 import com.liferay.portal.kernel.util.StringBundler;
21 import com.liferay.portal.kernel.util.StringUtil;
22 import com.liferay.portal.kernel.xml.Document;
23 import com.liferay.portal.kernel.xml.Element;
24 import com.liferay.portal.kernel.xml.SAXReaderUtil;
25 import com.liferay.portal.lar.BasePortletDataHandler;
26 import com.liferay.portal.lar.PortletDataContext;
27 import com.liferay.portal.lar.PortletDataException;
28 import com.liferay.portal.lar.PortletDataHandlerBoolean;
29 import com.liferay.portal.lar.PortletDataHandlerControl;
30 import com.liferay.portal.lar.PortletDataHandlerKeys;
31 import com.liferay.portal.service.ServiceContext;
32 import com.liferay.portal.util.PortletKeys;
33 import com.liferay.portlet.blogs.model.BlogsEntry;
34 import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
35 import com.liferay.portlet.blogs.service.persistence.BlogsEntryUtil;
36
37 import java.util.Calendar;
38 import java.util.List;
39
40 import javax.portlet.PortletPreferences;
41
42
48 public class BlogsPortletDataHandlerImpl extends BasePortletDataHandler {
49
50 public PortletPreferences deleteData(
51 PortletDataContext context, String portletId,
52 PortletPreferences preferences)
53 throws PortletDataException {
54
55 try {
56 if (!context.addPrimaryKey(
57 BlogsPortletDataHandlerImpl.class, "deleteData")) {
58
59 BlogsEntryLocalServiceUtil.deleteEntries(context.getGroupId());
60 }
61
62 return null;
63 }
64 catch (Exception e) {
65 throw new PortletDataException(e);
66 }
67 }
68
69 public String exportData(
70 PortletDataContext context, String portletId,
71 PortletPreferences preferences)
72 throws PortletDataException {
73
74 try {
75 context.addPermissions(
76 "com.liferay.portlet.blogs", context.getGroupId());
77
78 Document doc = SAXReaderUtil.createDocument();
79
80 Element root = doc.addElement("blogs-data");
81
82 root.addAttribute("group-id", String.valueOf(context.getGroupId()));
83
84 List<BlogsEntry> entries = BlogsEntryUtil.findByGroupId(
85 context.getGroupId());
86
87 for (BlogsEntry entry : entries) {
88 exportEntry(context, root, entry);
89 }
90
91 return doc.formattedString();
92 }
93 catch (Exception e) {
94 throw new PortletDataException(e);
95 }
96 }
97
98 public PortletDataHandlerControl[] getExportControls() {
99 return new PortletDataHandlerControl[] {
100 _entries, _comments, _ratings, _tags
101 };
102 }
103
104 public PortletDataHandlerControl[] getImportControls() {
105 return new PortletDataHandlerControl[] {
106 _entries, _comments, _ratings, _tags, _wordpress
107 };
108 }
109
110 public PortletPreferences importData(
111 PortletDataContext context, String portletId,
112 PortletPreferences preferences, String data)
113 throws PortletDataException {
114
115 try {
116 context.importPermissions(
117 "com.liferay.portlet.blogs", context.getSourceGroupId(),
118 context.getGroupId());
119
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 context.addPermissions(BlogsEntry.class, entry.getEntryId());
169
170 if (context.getBooleanParameter(_NAMESPACE, "comments")) {
171 context.addComments(BlogsEntry.class, entry.getEntryId());
172 }
173
174 if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
175 context.addRatingsEntries(BlogsEntry.class, entry.getEntryId());
176 }
177
178 if (context.getBooleanParameter(_NAMESPACE, "tags")) {
179 context.addAssetTags(BlogsEntry.class, entry.getEntryId());
180 }
181
182 entry.setUserUuid(entry.getUserUuid());
183
184 context.addZipEntry(path, entry);
185 }
186
187 protected String getEntryPath(
188 PortletDataContext context, BlogsEntry entry) {
189
190 StringBundler sb = new StringBundler(4);
191
192 sb.append(context.getPortletPath(PortletKeys.BLOGS));
193 sb.append("/entries/");
194 sb.append(entry.getEntryId());
195 sb.append(".xml");
196
197 return sb.toString();
198 }
199
200 protected void importEntry(PortletDataContext context, BlogsEntry entry)
201 throws Exception {
202
203 long userId = context.getUserId(entry.getUserUuid());
204
205 Calendar displayDateCal = CalendarFactoryUtil.getCalendar();
206
207 displayDateCal.setTime(entry.getDisplayDate());
208
209 int displayDateMonth = displayDateCal.get(Calendar.MONTH);
210 int displayDateDay = displayDateCal.get(Calendar.DATE);
211 int displayDateYear = displayDateCal.get(Calendar.YEAR);
212 int displayDateHour = displayDateCal.get(Calendar.HOUR);
213 int displayDateMinute = displayDateCal.get(Calendar.MINUTE);
214
215 if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
216 displayDateHour += 12;
217 }
218
219 int status = entry.getStatus();
220 boolean allowPingbacks = entry.isAllowPingbacks();
221 boolean allowTrackbacks = entry.isAllowTrackbacks();
222 String[] trackbacks = StringUtil.split(entry.getTrackbacks());
223
224 String[] assetTagNames = null;
225
226 if (context.getBooleanParameter(_NAMESPACE, "tags")) {
227 assetTagNames = context.getAssetTagNames(
228 BlogsEntry.class, entry.getEntryId());
229 }
230
231 ServiceContext serviceContext = new ServiceContext();
232
233 serviceContext.setAddCommunityPermissions(true);
234 serviceContext.setAddGuestPermissions(true);
235 serviceContext.setAssetTagNames(assetTagNames);
236 serviceContext.setCreateDate(entry.getCreateDate());
237 serviceContext.setModifiedDate(entry.getModifiedDate());
238 serviceContext.setScopeGroupId(context.getGroupId());
239 serviceContext.setStartWorkflow(false);
240 serviceContext.setStatus(status);
241
242 BlogsEntry importedEntry = null;
243
244 if (context.getDataStrategy().equals(
245 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
246
247 BlogsEntry existingEntry = BlogsEntryUtil.fetchByUUID_G(
248 entry.getUuid(), context.getGroupId());
249
250 if (existingEntry == null) {
251 importedEntry = BlogsEntryLocalServiceUtil.addEntry(
252 entry.getUuid(), userId, entry.getTitle(),
253 entry.getContent(), displayDateMonth, displayDateDay,
254 displayDateYear, displayDateHour, displayDateMinute,
255 allowPingbacks, allowTrackbacks, trackbacks,
256 serviceContext);
257 }
258 else {
259 importedEntry = BlogsEntryLocalServiceUtil.updateEntry(
260 userId, existingEntry.getEntryId(), entry.getTitle(),
261 entry.getContent(), displayDateMonth, displayDateDay,
262 displayDateYear, displayDateHour, displayDateMinute,
263 allowPingbacks, allowTrackbacks, trackbacks,
264 serviceContext);
265 }
266 }
267 else {
268 importedEntry = BlogsEntryLocalServiceUtil.addEntry(
269 null, userId, entry.getTitle(), entry.getContent(),
270 displayDateMonth, displayDateDay, displayDateYear,
271 displayDateHour, displayDateMinute, allowPingbacks,
272 allowTrackbacks, trackbacks, serviceContext);
273 }
274
275 context.importPermissions(
276 BlogsEntry.class, entry.getEntryId(), importedEntry.getEntryId());
277
278 if (context.getBooleanParameter(_NAMESPACE, "comments")) {
279 context.importComments(
280 BlogsEntry.class, entry.getEntryId(),
281 importedEntry.getEntryId(), context.getGroupId());
282 }
283
284 if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
285 context.importRatingsEntries(
286 BlogsEntry.class, entry.getEntryId(),
287 importedEntry.getEntryId());
288 }
289 }
290
291 private static final String _NAMESPACE = "blogs";
292
293 private static final PortletDataHandlerBoolean _entries =
294 new PortletDataHandlerBoolean(_NAMESPACE, "entries", true, true);
295
296 private static final PortletDataHandlerBoolean _comments =
297 new PortletDataHandlerBoolean(_NAMESPACE, "comments");
298
299 private static final PortletDataHandlerBoolean _ratings =
300 new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
301
302 private static final PortletDataHandlerBoolean _tags =
303 new PortletDataHandlerBoolean(_NAMESPACE, "tags");
304
305 private static final PortletDataHandlerBoolean _wordpress =
306 new PortletDataHandlerBoolean(_NAMESPACE, "wordpress");
307
308 }