001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.journal.lar;
022:
023: import com.liferay.portal.kernel.lar.PortletDataContext;
024: import com.liferay.portal.kernel.lar.PortletDataException;
025: import com.liferay.portal.kernel.lar.PortletDataHandler;
026: import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
027: import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
028: import com.liferay.portal.kernel.util.GetterUtil;
029: import com.liferay.portal.kernel.util.StringPool;
030: import com.liferay.portal.kernel.util.Validator;
031: import com.liferay.portal.util.PortalUtil;
032: import com.liferay.portlet.journal.NoSuchArticleException;
033: import com.liferay.portlet.journal.model.JournalArticle;
034: import com.liferay.portlet.journal.model.JournalStructure;
035: import com.liferay.portlet.journal.model.JournalTemplate;
036: import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
037: import com.liferay.portlet.journal.model.impl.JournalStructureImpl;
038: import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
039: import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
040: import com.liferay.portlet.journal.service.persistence.JournalStructureUtil;
041: import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
042: import com.liferay.util.CollectionFactory;
043:
044: import com.thoughtworks.xstream.XStream;
045:
046: import java.util.List;
047: import java.util.Map;
048:
049: import javax.portlet.PortletPreferences;
050:
051: import org.apache.commons.logging.Log;
052: import org.apache.commons.logging.LogFactory;
053:
054: import org.dom4j.Document;
055: import org.dom4j.DocumentHelper;
056: import org.dom4j.Element;
057:
058: /**
059: * <a href="JournalContentPortletDataHandlerImpl.java.html"><b><i>View Source
060: * </i></b></a>
061: *
062: * <p>
063: * Provides the Journal Content portlet export and import functionality, which
064: * is to clone the article, structure, and template referenced in the
065: * Journal Content portlet if the article is associated with the layout's group.
066: * Upon import, a new instance of the corresponding article, structure, and
067: * template will be created or updated. The author of the newly created
068: * objects are determined by the JournalCreationStrategy class defined in
069: * <i>portal.properties</i>.
070: * </p>
071: *
072: * <p>
073: * This <code>PortletDataHandler</code> differs from from
074: * <code>JournalPortletDataHandlerImpl</code> in that it only exports articles
075: * referenced in Journal Content portlets. Articles not displayed in Journal
076: * Content portlets will not be exported unless
077: * <code>JournalPortletDataHandlerImpl</code> is activated.
078: * </p>
079: *
080: * @author Joel Kozikowski
081: * @author Raymond Augé
082: * @author Bruno Farache
083: *
084: * @see com.liferay.portal.kernel.lar.PortletDataHandler
085: * @see com.liferay.portlet.journal.lar.JournalCreationStrategy
086: * @see com.liferay.portlet.journal.lar.JournalPortletDataHandlerImpl
087: *
088: */
089: public class JournalContentPortletDataHandlerImpl implements
090: PortletDataHandler {
091:
092: public PortletPreferences deleteData(PortletDataContext context,
093: String portletId, PortletPreferences prefs)
094: throws PortletDataException {
095:
096: try {
097: prefs.setValue("group-id", StringPool.BLANK);
098: prefs.setValue("article-id", StringPool.BLANK);
099:
100: return prefs;
101: } catch (Exception e) {
102: throw new PortletDataException(e);
103: }
104: }
105:
106: public String exportData(PortletDataContext context,
107: String portletId, 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
116: .warn("No article id found in preferences of portlet "
117: + portletId);
118: }
119:
120: return StringPool.BLANK;
121: }
122:
123: long articleGroupId = GetterUtil.getLong(prefs.getValue(
124: "group-id", StringPool.BLANK));
125:
126: if (articleGroupId <= 0) {
127: if (_log.isWarnEnabled()) {
128: _log
129: .warn("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
140: .getLatestArticle(articleGroupId, articleId);
141: } catch (NoSuchArticleException nsae) {
142: if (_log.isWarnEnabled()) {
143: _log.warn(nsae);
144: }
145: }
146:
147: if (article == null) {
148: return StringPool.BLANK;
149: }
150:
151: XStream xStream = new XStream();
152:
153: Document doc = DocumentHelper.createDocument();
154:
155: Element root = doc.addElement("journal-content");
156:
157: List content = root.content();
158:
159: if (!context.addPrimaryKey(JournalArticle.class, article
160: .getPrimaryKeyObj())) {
161:
162: JournalPortletDataHandlerImpl.exportArticle(context,
163: article);
164:
165: String xml = xStream.toXML(article);
166:
167: Document tempDoc = PortalUtil.readDocumentFromXML(xml);
168:
169: content.add(tempDoc.getRootElement().createCopy());
170: }
171:
172: String structureId = article.getStructureId();
173:
174: if (Validator.isNotNull(structureId)) {
175: JournalStructure structure = JournalStructureUtil
176: .findByG_S(article.getGroupId(), structureId);
177:
178: if (!context.addPrimaryKey(JournalStructure.class,
179: structure.getPrimaryKeyObj())) {
180:
181: JournalPortletDataHandlerImpl
182: .exportStructure(structure);
183:
184: String xml = xStream.toXML(structure);
185:
186: Document tempDoc = PortalUtil
187: .readDocumentFromXML(xml);
188:
189: content.add(tempDoc.getRootElement().createCopy());
190: }
191: }
192:
193: String templateId = article.getTemplateId();
194:
195: if (Validator.isNotNull(templateId)) {
196: JournalTemplate template = JournalTemplateUtil
197: .findByG_T(article.getGroupId(), templateId);
198:
199: if (!context.addPrimaryKey(JournalTemplate.class,
200: template.getPrimaryKeyObj())) {
201:
202: JournalPortletDataHandlerImpl.exportTemplate(
203: context, template);
204:
205: String xml = xStream.toXML(template);
206:
207: Document tempDoc = PortalUtil
208: .readDocumentFromXML(xml);
209:
210: content.add(tempDoc.getRootElement().createCopy());
211: }
212: }
213:
214: return doc.asXML();
215:
216: } catch (Exception e) {
217: throw new PortletDataException(e);
218: }
219: }
220:
221: public PortletDataHandlerControl[] getExportControls()
222: throws PortletDataException {
223:
224: return new PortletDataHandlerControl[] { _selectedArticles,
225: _images, _comments, _ratings, _tags };
226: }
227:
228: public PortletDataHandlerControl[] getImportControls()
229: throws PortletDataException {
230:
231: return new PortletDataHandlerControl[] { _selectedArticles,
232: _images, _comments, _ratings, _tags };
233: }
234:
235: public PortletPreferences importData(PortletDataContext context,
236: String portletId, PortletPreferences prefs, String data)
237: throws PortletDataException {
238:
239: try {
240: if (Validator.isNull(data)) {
241: return null;
242: }
243:
244: XStream xStream = new XStream();
245:
246: Document doc = PortalUtil.readDocumentFromXML(data);
247:
248: Element root = doc.getRootElement();
249:
250: Element el = root.element(JournalStructureImpl.class
251: .getName());
252:
253: Document tempDoc = DocumentHelper.createDocument();
254:
255: Map structurePKs = CollectionFactory.getHashMap();
256:
257: if (el != null) {
258: tempDoc.content().add(el.createCopy());
259:
260: JournalStructure structure = (JournalStructure) xStream
261: .fromXML(tempDoc.asXML());
262:
263: JournalPortletDataHandlerImpl.importStructure(context,
264: structurePKs, structure);
265: }
266:
267: el = root.element(JournalTemplateImpl.class.getName());
268:
269: Map templatePKs = CollectionFactory.getHashMap();
270:
271: if (el != null) {
272: tempDoc = DocumentHelper.createDocument();
273:
274: tempDoc.content().add(el.createCopy());
275:
276: JournalTemplate template = (JournalTemplate) xStream
277: .fromXML(tempDoc.asXML());
278:
279: JournalPortletDataHandlerImpl.importTemplate(context,
280: structurePKs, templatePKs, template);
281: }
282:
283: el = root.element(JournalArticleImpl.class.getName());
284:
285: if (el != null) {
286: tempDoc = DocumentHelper.createDocument();
287:
288: tempDoc.content().add(el.createCopy());
289:
290: JournalArticle article = (JournalArticle) xStream
291: .fromXML(tempDoc.asXML());
292:
293: article = JournalPortletDataHandlerImpl.importArticle(
294: context, structurePKs, templatePKs, article);
295:
296: prefs.setValue("group-id", String.valueOf(context
297: .getGroupId()));
298: prefs.setValue("article-id", article.getArticleId());
299: }
300:
301: return prefs;
302: } catch (Exception e) {
303: throw new PortletDataException(e);
304: }
305: }
306:
307: private static final String _NAMESPACE = "journal_content";
308:
309: private static final PortletDataHandlerBoolean _selectedArticles = new PortletDataHandlerBoolean(
310: _NAMESPACE, "selected-articles", true, true);
311:
312: private static final PortletDataHandlerBoolean _images = new PortletDataHandlerBoolean(
313: _NAMESPACE, "images");
314:
315: private static final PortletDataHandlerBoolean _comments = new PortletDataHandlerBoolean(
316: _NAMESPACE, "comments");
317:
318: private static final PortletDataHandlerBoolean _ratings = new PortletDataHandlerBoolean(
319: _NAMESPACE, "ratings");
320:
321: private static final PortletDataHandlerBoolean _tags = new PortletDataHandlerBoolean(
322: _NAMESPACE, "tags");
323:
324: private static Log _log = LogFactory
325: .getLog(JournalContentPortletDataHandlerImpl.class);
326:
327: }
|