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.service.JournalArticleLocalServiceUtil;
39 import com.liferay.portlet.journal.service.persistence.JournalStructureUtil;
40 import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
41 import com.liferay.util.MapUtil;
42 import com.liferay.util.xml.XMLFormatter;
43
44 import java.util.Map;
45
46 import javax.portlet.PortletPreferences;
47
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50
51 import org.dom4j.Document;
52 import org.dom4j.DocumentHelper;
53 import org.dom4j.Element;
54
55
86 public class JournalContentPortletDataHandlerImpl
87 implements PortletDataHandler {
88
89 public PortletPreferences deleteData(
90 PortletDataContext context, String portletId,
91 PortletPreferences prefs)
92 throws PortletDataException {
93
94 try {
95 prefs.setValue("group-id", StringPool.BLANK);
96 prefs.setValue("article-id", StringPool.BLANK);
97
98 return prefs;
99 }
100 catch (Exception e) {
101 throw new PortletDataException(e);
102 }
103 }
104
105 public String exportData(
106 PortletDataContext context, String portletId,
107 PortletPreferences prefs)
108 throws PortletDataException {
109
110 try {
111 String articleId = prefs.getValue("article-id", null);
112
113 if (articleId == null) {
114 if (_log.isWarnEnabled()) {
115 _log.warn(
116 "No article id found in preferences of portlet " +
117 portletId);
118 }
119
120 return StringPool.BLANK;
121 }
122
123 long articleGroupId = GetterUtil.getLong(
124 prefs.getValue("group-id", StringPool.BLANK));
125
126 if (articleGroupId <= 0) {
127 if (_log.isWarnEnabled()) {
128 _log.warn(
129 "No group id found in preferences of portlet " +
130 portletId);
131 }
132
133 return StringPool.BLANK;
134 }
135
136 JournalArticle article = null;
137
138 try {
139 article = JournalArticleLocalServiceUtil.getLatestArticle(
140 articleGroupId, articleId);
141 }
142 catch (NoSuchArticleException nsae) {
143 if (_log.isWarnEnabled()) {
144 _log.warn(nsae);
145 }
146 }
147
148 if (article == null) {
149 return StringPool.BLANK;
150 }
151
152 Document doc = DocumentHelper.createDocument();
153
154 Element root = doc.addElement("journal-content-data");
155
156 JournalPortletDataHandlerImpl.exportArticle(context, root, article);
157
158 String structureId = article.getStructureId();
159
160 if (Validator.isNotNull(structureId)) {
161 JournalStructure structure = JournalStructureUtil.findByG_S(
162 article.getGroupId(), structureId);
163
164 JournalPortletDataHandlerImpl.exportStructure(
165 context, root, structure);
166 }
167
168 String templateId = article.getTemplateId();
169
170 if (Validator.isNotNull(templateId)) {
171 JournalTemplate template = JournalTemplateUtil.findByG_T(
172 article.getGroupId(), templateId);
173
174 JournalPortletDataHandlerImpl.exportTemplate(
175 context, root, template);
176 }
177
178 return XMLFormatter.toString(doc);
179 }
180 catch (Exception e) {
181 throw new PortletDataException(e);
182 }
183 }
184
185 public PortletDataHandlerControl[] getExportControls() {
186 return new PortletDataHandlerControl[] {
187 _selectedArticles, _images, _comments, _ratings, _tags
188 };
189 }
190
191 public PortletDataHandlerControl[] getImportControls() {
192 return new PortletDataHandlerControl[] {
193 _selectedArticles, _images, _comments, _ratings, _tags
194 };
195 }
196
197 public PortletPreferences importData(
198 PortletDataContext context, String portletId,
199 PortletPreferences prefs, String data)
200 throws PortletDataException {
201
202 try {
203 if (Validator.isNull(data)) {
204 return null;
205 }
206
207 Document doc = DocumentUtil.readDocumentFromXML(data);
208
209 Element root = doc.getRootElement();
210
211 Element structureEl = root.element("structure");
212
213 Map<String, String> structureIds = context.getNewPrimaryKeysMap(
214 JournalStructure.class);
215
216 if (structureEl != null) {
217 JournalPortletDataHandlerImpl.importStructure(
218 context, structureIds, structureEl);
219 }
220
221 Element templateEl = root.element("template");
222
223 Map<String, String> templateIds = context.getNewPrimaryKeysMap(
224 JournalTemplate.class);
225
226 if (templateEl != null) {
227 JournalPortletDataHandlerImpl.importTemplate(
228 context, structureIds, templateIds, templateEl);
229 }
230
231 Element articleEl = root.element("article");
232
233 Map<String, String> articleIds = context.getNewPrimaryKeysMap(
234 JournalArticle.class);
235
236 if (articleEl != null) {
237 JournalPortletDataHandlerImpl.importArticle(
238 context, structureIds, templateIds, articleIds, articleEl);
239 }
240
241 String articleId = prefs.getValue("article-id", StringPool.BLANK);
242
243 if (Validator.isNotNull(articleId)) {
244 articleId = MapUtil.getString(articleIds, articleId, articleId);
245
246 prefs.setValue(
247 "group-id", String.valueOf(context.getGroupId()));
248 prefs.setValue("article-id", articleId);
249 }
250
251 return prefs;
252 }
253 catch (Exception e) {
254 throw new PortletDataException(e);
255 }
256 }
257
258 public boolean isPublishToLiveByDefault() {
259 return true;
260 }
261
262 private static final String _NAMESPACE = "journal";
263
264 private static final PortletDataHandlerBoolean _selectedArticles =
265 new PortletDataHandlerBoolean(
266 _NAMESPACE, "selected-articles", true, true);
267
268 private static final PortletDataHandlerBoolean _images =
269 new PortletDataHandlerBoolean(_NAMESPACE, "images");
270
271 private static final PortletDataHandlerBoolean _comments =
272 new PortletDataHandlerBoolean(_NAMESPACE, "comments");
273
274 private static final PortletDataHandlerBoolean _ratings =
275 new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
276
277 private static final PortletDataHandlerBoolean _tags =
278 new PortletDataHandlerBoolean(_NAMESPACE, "tags");
279
280 private static Log _log =
281 LogFactory.getLog(JournalContentPortletDataHandlerImpl.class);
282
283 }