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.PortalUtil;
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.CollectionFactory;
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
72 public class JournalContentPortletDataHandlerImpl
73 implements PortletDataHandler {
74
75 public PortletPreferences deleteData(
76 PortletDataContext context, String portletId,
77 PortletPreferences prefs)
78 throws PortletDataException {
79
80 try {
81 prefs.setValue("group-id", StringPool.BLANK);
82 prefs.setValue("article-id", StringPool.BLANK);
83
84 return prefs;
85 }
86 catch (Exception e) {
87 throw new PortletDataException(e);
88 }
89 }
90
91 public String exportData(
92 PortletDataContext context, String portletId,
93 PortletPreferences prefs)
94 throws PortletDataException {
95
96 try {
97 String articleId = prefs.getValue("article-id", null);
98
99 if (articleId == null) {
100 if (_log.isWarnEnabled()) {
101 _log.warn(
102 "No article id found in preferences of portlet " +
103 portletId);
104 }
105
106 return StringPool.BLANK;
107 }
108
109 long articleGroupId = GetterUtil.getLong(
110 prefs.getValue("group-id", StringPool.BLANK));
111
112 if (articleGroupId <= 0) {
113 if (_log.isWarnEnabled()) {
114 _log.warn(
115 "No group id found in preferences of portlet " +
116 portletId);
117 }
118
119 return StringPool.BLANK;
120 }
121
122 JournalArticle article = null;
123
124 try {
125 article = JournalArticleLocalServiceUtil.getLatestArticle(
126 articleGroupId, articleId);
127 }
128 catch (NoSuchArticleException nsae) {
129 if (_log.isWarnEnabled()) {
130 _log.warn(nsae);
131 }
132 }
133
134 if (article == null) {
135 return StringPool.BLANK;
136 }
137
138 XStream xStream = new XStream();
139
140 Document doc = DocumentHelper.createDocument();
141
142 Element root = doc.addElement("journal-content");
143
144 List content = root.content();
145
146 if (!context.addPrimaryKey(
147 JournalArticle.class, article.getPrimaryKeyObj())) {
148
149 JournalPortletDataHandlerImpl.exportArticle(context, article);
150
151 String xml = xStream.toXML(article);
152
153 Document tempDoc = PortalUtil.readDocumentFromXML(xml);
154
155 content.add(tempDoc.getRootElement().createCopy());
156 }
157
158 String structureId = article.getStructureId();
159
160 if (Validator.isNotNull(structureId)) {
161 JournalStructure structure = JournalStructureUtil.findByG_S(
162 article.getGroupId(), structureId);
163
164 if (!context.addPrimaryKey(
165 JournalStructure.class, structure.getPrimaryKeyObj())) {
166
167 JournalPortletDataHandlerImpl.exportStructure(structure);
168
169 String xml = xStream.toXML(structure);
170
171 Document tempDoc = PortalUtil.readDocumentFromXML(xml);
172
173 content.add(tempDoc.getRootElement().createCopy());
174 }
175 }
176
177 String templateId = article.getTemplateId();
178
179 if (Validator.isNotNull(templateId)) {
180 JournalTemplate template = JournalTemplateUtil.findByG_T(
181 article.getGroupId(), templateId);
182
183 if (!context.addPrimaryKey(
184 JournalTemplate.class, template.getPrimaryKeyObj())) {
185
186 JournalPortletDataHandlerImpl.exportTemplate(
187 context, template);
188
189 String xml = xStream.toXML(template);
190
191 Document tempDoc = PortalUtil.readDocumentFromXML(xml);
192
193 content.add(tempDoc.getRootElement().createCopy());
194 }
195 }
196
197 return doc.asXML();
198
199 }
200 catch (Exception e) {
201 throw new PortletDataException(e);
202 }
203 }
204
205 public PortletDataHandlerControl[] getExportControls()
206 throws PortletDataException {
207
208 return new PortletDataHandlerControl[] {
209 _selectedArticles, _images, _comments, _ratings, _tags
210 };
211 }
212
213 public PortletDataHandlerControl[] getImportControls()
214 throws PortletDataException {
215
216 return new PortletDataHandlerControl[] {
217 _selectedArticles, _images, _comments, _ratings, _tags
218 };
219 }
220
221 public PortletPreferences importData(
222 PortletDataContext context, String portletId,
223 PortletPreferences prefs, String data)
224 throws PortletDataException {
225
226 try {
227 if (Validator.isNull(data)) {
228 return null;
229 }
230
231 XStream xStream = new XStream();
232
233 Document doc = PortalUtil.readDocumentFromXML(data);
234
235 Element root = doc.getRootElement();
236
237 Element el = root.element(JournalStructureImpl.class.getName());
238
239 Document tempDoc = DocumentHelper.createDocument();
240
241 Map structurePKs = CollectionFactory.getHashMap();
242
243 if (el != null) {
244 tempDoc.content().add(el.createCopy());
245
246 JournalStructure structure = (JournalStructure)xStream.fromXML(
247 tempDoc.asXML());
248
249 JournalPortletDataHandlerImpl.importStructure(
250 context, structurePKs, structure);
251 }
252
253 el = root.element(JournalTemplateImpl.class.getName());
254
255 Map templatePKs = CollectionFactory.getHashMap();
256
257 if (el != null) {
258 tempDoc = DocumentHelper.createDocument();
259
260 tempDoc.content().add(el.createCopy());
261
262 JournalTemplate template = (JournalTemplate)xStream.fromXML(
263 tempDoc.asXML());
264
265 JournalPortletDataHandlerImpl.importTemplate(
266 context, structurePKs, templatePKs, template);
267 }
268
269 el = root.element(JournalArticleImpl.class.getName());
270
271 if (el != null) {
272 tempDoc = DocumentHelper.createDocument();
273
274 tempDoc.content().add(el.createCopy());
275
276 JournalArticle article = (JournalArticle)xStream.fromXML(
277 tempDoc.asXML());
278
279 article = JournalPortletDataHandlerImpl.importArticle(
280 context, structurePKs, templatePKs, article);
281
282 prefs.setValue(
283 "group-id", String.valueOf(context.getGroupId()));
284 prefs.setValue("article-id", article.getArticleId());
285 }
286
287 return prefs;
288 }
289 catch (Exception e) {
290 throw new PortletDataException(e);
291 }
292 }
293
294 private static final String _NAMESPACE = "journal_content";
295
296 private static final PortletDataHandlerBoolean _selectedArticles =
297 new PortletDataHandlerBoolean(
298 _NAMESPACE, "selected-articles", true, true);
299
300 private static final PortletDataHandlerBoolean _images =
301 new PortletDataHandlerBoolean(_NAMESPACE, "images");
302
303 private static final PortletDataHandlerBoolean _comments =
304 new PortletDataHandlerBoolean(_NAMESPACE, "comments");
305
306 private static final PortletDataHandlerBoolean _ratings =
307 new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
308
309 private static final PortletDataHandlerBoolean _tags =
310 new PortletDataHandlerBoolean(_NAMESPACE, "tags");
311
312 private static Log _log =
313 LogFactory.getLog(JournalContentPortletDataHandlerImpl.class);
314
315 }