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