1
22
23 package com.liferay.portlet.journal.lar;
24
25 import com.liferay.portal.kernel.lar.PortletDataContext;
26 import com.liferay.portal.kernel.lar.PortletDataException;
27 import com.liferay.portal.kernel.lar.PortletDataHandler;
28 import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
29 import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
30 import com.liferay.portal.kernel.util.GetterUtil;
31 import com.liferay.portal.kernel.util.StringPool;
32 import com.liferay.portal.kernel.util.Validator;
33 import com.liferay.portal.util.DocumentUtil;
34 import com.liferay.portlet.journal.NoSuchArticleException;
35 import com.liferay.portlet.journal.model.JournalArticle;
36 import com.liferay.portlet.journal.model.JournalStructure;
37 import com.liferay.portlet.journal.model.JournalTemplate;
38 import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
39 import com.liferay.portlet.journal.model.impl.JournalStructureImpl;
40 import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
41 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
42 import com.liferay.portlet.journal.service.persistence.JournalStructureUtil;
43 import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
44 import com.liferay.util.MapUtil;
45
46 import com.thoughtworks.xstream.XStream;
47
48 import java.util.List;
49 import java.util.Map;
50
51 import javax.portlet.PortletPreferences;
52
53 import org.apache.commons.logging.Log;
54 import org.apache.commons.logging.LogFactory;
55
56 import org.dom4j.Document;
57 import org.dom4j.DocumentHelper;
58 import org.dom4j.Element;
59
60
91 public class JournalContentPortletDataHandlerImpl
92 implements PortletDataHandler {
93
94 public PortletPreferences deleteData(
95 PortletDataContext context, String portletId,
96 PortletPreferences prefs)
97 throws PortletDataException {
98
99 try {
100 prefs.setValue("group-id", StringPool.BLANK);
101 prefs.setValue("article-id", StringPool.BLANK);
102
103 return prefs;
104 }
105 catch (Exception e) {
106 throw new PortletDataException(e);
107 }
108 }
109
110 public String exportData(
111 PortletDataContext context, String portletId,
112 PortletPreferences prefs)
113 throws PortletDataException {
114
115 try {
116 String articleId = prefs.getValue("article-id", null);
117
118 if (articleId == null) {
119 if (_log.isWarnEnabled()) {
120 _log.warn(
121 "No article id found in preferences of portlet " +
122 portletId);
123 }
124
125 return StringPool.BLANK;
126 }
127
128 long articleGroupId = GetterUtil.getLong(
129 prefs.getValue("group-id", StringPool.BLANK));
130
131 if (articleGroupId <= 0) {
132 if (_log.isWarnEnabled()) {
133 _log.warn(
134 "No group id found in preferences of portlet " +
135 portletId);
136 }
137
138 return StringPool.BLANK;
139 }
140
141 JournalArticle article = null;
142
143 try {
144 article = JournalArticleLocalServiceUtil.getLatestArticle(
145 articleGroupId, articleId);
146 }
147 catch (NoSuchArticleException nsae) {
148 if (_log.isWarnEnabled()) {
149 _log.warn(nsae);
150 }
151 }
152
153 if (article == null) {
154 return StringPool.BLANK;
155 }
156
157 XStream xStream = new XStream();
158
159 Document doc = DocumentHelper.createDocument();
160
161 Element root = doc.addElement("journal-content");
162
163 List<Element> content = root.content();
164
165 if (!context.addPrimaryKey(
166 JournalArticle.class, article.getPrimaryKeyObj())) {
167
168 JournalPortletDataHandlerImpl.exportArticle(context, article);
169
170 String xml = xStream.toXML(article);
171
172 Document tempDoc = DocumentUtil.readDocumentFromXML(xml);
173
174 content.add(tempDoc.getRootElement().createCopy());
175 }
176
177 String structureId = article.getStructureId();
178
179 if (Validator.isNotNull(structureId)) {
180 JournalStructure structure = JournalStructureUtil.findByG_S(
181 article.getGroupId(), structureId);
182
183 if (!context.addPrimaryKey(
184 JournalStructure.class, structure.getPrimaryKeyObj())) {
185
186 JournalPortletDataHandlerImpl.exportStructure(structure);
187
188 String xml = xStream.toXML(structure);
189
190 Document tempDoc = DocumentUtil.readDocumentFromXML(xml);
191
192 content.add(tempDoc.getRootElement().createCopy());
193 }
194 }
195
196 String templateId = article.getTemplateId();
197
198 if (Validator.isNotNull(templateId)) {
199 JournalTemplate template = JournalTemplateUtil.findByG_T(
200 article.getGroupId(), templateId);
201
202 if (!context.addPrimaryKey(
203 JournalTemplate.class, template.getPrimaryKeyObj())) {
204
205 JournalPortletDataHandlerImpl.exportTemplate(
206 context, template);
207
208 String xml = xStream.toXML(template);
209
210 Document tempDoc = DocumentUtil.readDocumentFromXML(xml);
211
212 content.add(tempDoc.getRootElement().createCopy());
213 }
214 }
215
216 return doc.asXML();
217
218 }
219 catch (Exception e) {
220 throw new PortletDataException(e);
221 }
222 }
223
224 public PortletDataHandlerControl[] getExportControls()
225 throws PortletDataException {
226
227 return new PortletDataHandlerControl[] {
228 _selectedArticles, _images, _comments, _ratings, _tags
229 };
230 }
231
232 public PortletDataHandlerControl[] getImportControls()
233 throws PortletDataException {
234
235 return new PortletDataHandlerControl[] {
236 _selectedArticles, _images, _comments, _ratings, _tags
237 };
238 }
239
240 public PortletPreferences importData(
241 PortletDataContext context, String portletId,
242 PortletPreferences prefs, String data)
243 throws PortletDataException {
244
245 try {
246 if (Validator.isNull(data)) {
247 return null;
248 }
249
250 XStream xStream = new XStream();
251
252 Document doc = DocumentUtil.readDocumentFromXML(data);
253
254 Element root = doc.getRootElement();
255
256 Element el = root.element(JournalStructureImpl.class.getName());
257
258 Document tempDoc = DocumentHelper.createDocument();
259
260 Map<String, String> structureIds = context.getNewPrimaryKeysMap(
261 JournalStructure.class);
262
263 if (el != null) {
264 tempDoc.content().add(el.createCopy());
265
266 JournalStructure structure = (JournalStructure)xStream.fromXML(
267 tempDoc.asXML());
268
269 JournalPortletDataHandlerImpl.importStructure(
270 context, structureIds, structure);
271 }
272
273 el = root.element(JournalTemplateImpl.class.getName());
274
275 Map<String, String> templateIds = context.getNewPrimaryKeysMap(
276 JournalTemplate.class);
277
278 if (el != null) {
279 tempDoc = DocumentHelper.createDocument();
280
281 tempDoc.content().add(el.createCopy());
282
283 JournalTemplate template = (JournalTemplate)xStream.fromXML(
284 tempDoc.asXML());
285
286 JournalPortletDataHandlerImpl.importTemplate(
287 context, structureIds, templateIds, template);
288 }
289
290 el = root.element(JournalArticleImpl.class.getName());
291
292 Map<String, String> articleIds = context.getNewPrimaryKeysMap(
293 JournalArticle.class);
294
295 if (el != null) {
296 tempDoc = DocumentHelper.createDocument();
297
298 tempDoc.content().add(el.createCopy());
299
300 JournalArticle article = (JournalArticle)xStream.fromXML(
301 tempDoc.asXML());
302
303 JournalPortletDataHandlerImpl.importArticle(
304 context, structureIds, templateIds, articleIds, article);
305 }
306
307 String articleId = prefs.getValue("article-id", StringPool.BLANK);
308
309 if (Validator.isNotNull(articleId)) {
310 articleId = MapUtil.getString(articleIds, articleId, articleId);
311
312 prefs.setValue(
313 "group-id", String.valueOf(context.getGroupId()));
314 prefs.setValue("article-id", articleId);
315 }
316
317 return prefs;
318 }
319 catch (Exception e) {
320 throw new PortletDataException(e);
321 }
322 }
323
324 public boolean isPublishToLiveByDefault() {
325 return true;
326 }
327
328 private static final String _NAMESPACE = "journal_content";
329
330 private static final PortletDataHandlerBoolean _selectedArticles =
331 new PortletDataHandlerBoolean(
332 _NAMESPACE, "selected-articles", true, true);
333
334 private static final PortletDataHandlerBoolean _images =
335 new PortletDataHandlerBoolean(_NAMESPACE, "images");
336
337 private static final PortletDataHandlerBoolean _comments =
338 new PortletDataHandlerBoolean(_NAMESPACE, "comments");
339
340 private static final PortletDataHandlerBoolean _ratings =
341 new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
342
343 private static final PortletDataHandlerBoolean _tags =
344 new PortletDataHandlerBoolean(_NAMESPACE, "tags");
345
346 private static Log _log =
347 LogFactory.getLog(JournalContentPortletDataHandlerImpl.class);
348
349 }