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.blogs.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.lar.PortletDataHandlerKeys;
029: import com.liferay.portal.kernel.util.CalendarFactoryUtil;
030: import com.liferay.portal.theme.ThemeDisplay;
031: import com.liferay.portal.util.PortalUtil;
032: import com.liferay.portlet.blogs.model.BlogsEntry;
033: import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
034: import com.liferay.portlet.blogs.service.persistence.BlogsEntryUtil;
035:
036: import com.thoughtworks.xstream.XStream;
037:
038: import java.util.Calendar;
039: import java.util.Iterator;
040: import java.util.List;
041:
042: import javax.portlet.PortletPreferences;
043:
044: import org.dom4j.Document;
045: import org.dom4j.DocumentHelper;
046: import org.dom4j.Element;
047:
048: /**
049: * <a href="BlogsPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
050: *
051: * @author Bruno Farache
052: *
053: */
054: public class BlogsPortletDataHandlerImpl implements PortletDataHandler {
055:
056: public PortletPreferences deleteData(PortletDataContext context,
057: String portletId, PortletPreferences prefs)
058: throws PortletDataException {
059:
060: try {
061:
062: // Entries
063:
064: if (!context.addPrimaryKey(
065: BlogsPortletDataHandlerImpl.class, "deleteData")) {
066:
067: BlogsEntryLocalServiceUtil.deleteEntries(context
068: .getGroupId());
069: }
070:
071: return null;
072: } catch (Exception e) {
073: throw new PortletDataException(e);
074: }
075: }
076:
077: public String exportData(PortletDataContext context,
078: String portletId, PortletPreferences prefs)
079: throws PortletDataException {
080:
081: try {
082: XStream xStream = new XStream();
083:
084: Document doc = DocumentHelper.createDocument();
085:
086: Element root = doc.addElement("blogs-data");
087:
088: root.addAttribute("group-id", String.valueOf(context
089: .getGroupId()));
090:
091: // Entries
092:
093: List entries = BlogsEntryUtil.findByGroupId(context
094: .getGroupId());
095:
096: Iterator itr = entries.iterator();
097:
098: while (itr.hasNext()) {
099: BlogsEntry entry = (BlogsEntry) itr.next();
100:
101: if (context.addPrimaryKey(BlogsEntry.class, entry
102: .getPrimaryKeyObj())) {
103:
104: itr.remove();
105: } else {
106: entry.setUserUuid(entry.getUserUuid());
107:
108: if (context.getBooleanParameter(_NAMESPACE,
109: "comments")) {
110: context.addComments(BlogsEntry.class, entry
111: .getPrimaryKeyObj());
112: }
113:
114: if (context.getBooleanParameter(_NAMESPACE,
115: "ratings")) {
116: context.addRatingsEntries(BlogsEntry.class,
117: entry.getPrimaryKeyObj());
118: }
119:
120: if (context.getBooleanParameter(_NAMESPACE, "tags")) {
121: context.addTagsEntries(BlogsEntry.class, entry
122: .getPrimaryKeyObj());
123: }
124: }
125: }
126:
127: String xml = xStream.toXML(entries);
128:
129: Element el = root.addElement("blog-entries");
130:
131: Document tempDoc = PortalUtil.readDocumentFromXML(xml);
132:
133: el.content().add(tempDoc.getRootElement().createCopy());
134:
135: return doc.asXML();
136: } catch (Exception e) {
137: throw new PortletDataException(e);
138: }
139: }
140:
141: public PortletDataHandlerControl[] getExportControls()
142: throws PortletDataException {
143:
144: return new PortletDataHandlerControl[] { _entries, _comments,
145: _ratings, _tags };
146: }
147:
148: public PortletDataHandlerControl[] getImportControls()
149: throws PortletDataException {
150:
151: return new PortletDataHandlerControl[] { _entries, _comments,
152: _ratings, _tags };
153: }
154:
155: public PortletPreferences importData(PortletDataContext context,
156: String portletId, PortletPreferences prefs, String data)
157: throws PortletDataException {
158:
159: try {
160: XStream xStream = new XStream();
161:
162: Document doc = PortalUtil.readDocumentFromXML(data);
163:
164: Element root = doc.getRootElement();
165:
166: // Entries
167:
168: Element el = root.element("blog-entries").element("list");
169:
170: Document tempDoc = DocumentHelper.createDocument();
171:
172: tempDoc.content().add(el.createCopy());
173:
174: List entries = (List) xStream.fromXML(tempDoc.asXML());
175:
176: Iterator itr = entries.iterator();
177:
178: while (itr.hasNext()) {
179: BlogsEntry entry = (BlogsEntry) itr.next();
180:
181: importEntry(context, entry);
182: }
183:
184: return null;
185: } catch (Exception e) {
186: throw new PortletDataException(e);
187: }
188: }
189:
190: protected void importEntry(PortletDataContext context,
191: BlogsEntry entry) throws Exception {
192:
193: long userId = context.getUserId(entry.getUserUuid());
194: long plid = context.getPlid();
195:
196: Calendar displayDateCal = CalendarFactoryUtil.getCalendar();
197:
198: displayDateCal.setTime(entry.getDisplayDate());
199:
200: int displayDateMonth = displayDateCal.get(Calendar.MONTH);
201: int displayDateDay = displayDateCal.get(Calendar.DATE);
202: int displayDateYear = displayDateCal.get(Calendar.YEAR);
203: int displayDateHour = displayDateCal.get(Calendar.HOUR);
204: int displayDateMinute = displayDateCal.get(Calendar.MINUTE);
205:
206: String[] tagsEntries = null;
207:
208: if (context.getBooleanParameter(_NAMESPACE, "tags")) {
209: tagsEntries = context.getTagsEntries(BlogsEntry.class,
210: entry.getPrimaryKeyObj());
211: }
212:
213: boolean addCommunityPermissions = true;
214: boolean addGuestPermissions = true;
215:
216: ThemeDisplay themeDisplay = null;
217:
218: BlogsEntry existingEntry = null;
219:
220: if (context.getDataStrategy().equals(
221: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
222:
223: existingEntry = BlogsEntryUtil.fetchByUUID_G(entry
224: .getUuid(), context.getGroupId());
225:
226: if (existingEntry == null) {
227: existingEntry = BlogsEntryLocalServiceUtil.addEntry(
228: entry.getUuid(), userId, plid,
229: entry.getTitle(), entry.getContent(),
230: displayDateMonth, displayDateDay,
231: displayDateYear, displayDateHour,
232: displayDateMinute, tagsEntries,
233: addCommunityPermissions, addGuestPermissions,
234: themeDisplay);
235: } else {
236: existingEntry = BlogsEntryLocalServiceUtil.updateEntry(
237: userId, existingEntry.getEntryId(), entry
238: .getTitle(), entry.getContent(),
239: displayDateMonth, displayDateDay,
240: displayDateYear, displayDateHour,
241: displayDateMinute, tagsEntries, themeDisplay);
242: }
243: } else {
244: existingEntry = BlogsEntryLocalServiceUtil.addEntry(userId,
245: plid, entry.getTitle(), entry.getContent(),
246: displayDateMonth, displayDateDay, displayDateYear,
247: displayDateHour, displayDateMinute, tagsEntries,
248: addCommunityPermissions, addGuestPermissions,
249: themeDisplay);
250: }
251:
252: if (context.getBooleanParameter(_NAMESPACE, "comments")) {
253: context.importComments(BlogsEntry.class, entry
254: .getPrimaryKeyObj(), existingEntry
255: .getPrimaryKeyObj(), context.getGroupId());
256: }
257:
258: if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
259: context.importRatingsEntries(BlogsEntry.class, entry
260: .getPrimaryKeyObj(), existingEntry
261: .getPrimaryKeyObj());
262: }
263: }
264:
265: private static final String _NAMESPACE = "blogs";
266:
267: private static final PortletDataHandlerBoolean _entries = new PortletDataHandlerBoolean(
268: _NAMESPACE, "entries", true, true);
269:
270: private static final PortletDataHandlerBoolean _comments = new PortletDataHandlerBoolean(
271: _NAMESPACE, "comments");
272:
273: private static final PortletDataHandlerBoolean _ratings = new PortletDataHandlerBoolean(
274: _NAMESPACE, "ratings");
275:
276: private static final PortletDataHandlerBoolean _tags = new PortletDataHandlerBoolean(
277: _NAMESPACE, "tags");
278:
279: }
|