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.wiki.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.util.PortalUtil;
030: import com.liferay.portlet.wiki.NoSuchNodeException;
031: import com.liferay.portlet.wiki.NoSuchPageException;
032: import com.liferay.portlet.wiki.model.WikiNode;
033: import com.liferay.portlet.wiki.model.WikiPage;
034: import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
035: import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
036: import com.liferay.portlet.wiki.service.persistence.WikiNodeUtil;
037: import com.liferay.portlet.wiki.service.persistence.WikiPageFinderUtil;
038: import com.liferay.portlet.wiki.service.persistence.WikiPageUtil;
039: import com.liferay.util.CollectionFactory;
040: import com.liferay.util.MapUtil;
041:
042: import com.thoughtworks.xstream.XStream;
043:
044: import java.util.ArrayList;
045: import java.util.Iterator;
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="WikiPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
060: *
061: * @author Bruno Farache
062: *
063: */
064: public class WikiPortletDataHandlerImpl implements PortletDataHandler {
065:
066: public PortletPreferences deleteData(PortletDataContext context,
067: String portletId, PortletPreferences prefs)
068: throws PortletDataException {
069:
070: try {
071:
072: // Nodes
073:
074: if (!context.addPrimaryKey(
075: WikiPortletDataHandlerImpl.class, "deleteData")) {
076:
077: WikiNodeLocalServiceUtil.deleteNodes(context
078: .getGroupId());
079: }
080: return null;
081: } catch (Exception e) {
082: throw new PortletDataException(e);
083: }
084: }
085:
086: public String exportData(PortletDataContext context,
087: String portletId, PortletPreferences prefs)
088: throws PortletDataException {
089:
090: try {
091: XStream xStream = new XStream();
092:
093: Document doc = DocumentHelper.createDocument();
094:
095: Element root = doc.addElement("wiki-data");
096:
097: root.addAttribute("group-id", String.valueOf(context
098: .getGroupId()));
099:
100: // Nodes
101:
102: List nodes = WikiNodeUtil.findByGroupId(context
103: .getGroupId());
104:
105: List pages = new ArrayList();
106:
107: Iterator itr = nodes.iterator();
108:
109: while (itr.hasNext()) {
110: WikiNode node = (WikiNode) itr.next();
111:
112: if (context.addPrimaryKey(WikiNode.class, node
113: .getPrimaryKeyObj())) {
114:
115: itr.remove();
116: } else {
117: node.setUserUuid(node.getUserUuid());
118:
119: List nodePages = WikiPageUtil.findByNodeId(node
120: .getNodeId());
121:
122: pages.addAll(nodePages);
123: }
124: }
125:
126: String xml = xStream.toXML(nodes);
127:
128: Element el = root.addElement("wiki-nodes");
129:
130: Document tempDoc = PortalUtil.readDocumentFromXML(xml);
131:
132: el.content().add(tempDoc.getRootElement().createCopy());
133:
134: // Pages
135:
136: itr = pages.iterator();
137:
138: while (itr.hasNext()) {
139: WikiPage page = (WikiPage) itr.next();
140:
141: if (context.addPrimaryKey(WikiPage.class, page
142: .getPrimaryKeyObj())) {
143:
144: itr.remove();
145: } else {
146: page.setUserUuid(page.getUserUuid());
147:
148: if (context.getBooleanParameter(_NAMESPACE,
149: "comments")) {
150: context.addComments(WikiPage.class, new Long(
151: page.getResourcePrimKey()));
152: }
153:
154: if (context.getBooleanParameter(_NAMESPACE, "tags")) {
155: context.addTagsEntries(WikiPage.class,
156: new Long(page.getResourcePrimKey()));
157: }
158: }
159: }
160:
161: xml = xStream.toXML(pages);
162:
163: el = root.addElement("wiki-pages");
164:
165: tempDoc = PortalUtil.readDocumentFromXML(xml);
166:
167: el.content().add(tempDoc.getRootElement().createCopy());
168:
169: return doc.asXML();
170: } catch (Exception e) {
171: throw new PortletDataException(e);
172: }
173: }
174:
175: public PortletDataHandlerControl[] getExportControls()
176: throws PortletDataException {
177:
178: return new PortletDataHandlerControl[] { _nodesAndPages,
179: _comments, _tags };
180: }
181:
182: public PortletDataHandlerControl[] getImportControls()
183: throws PortletDataException {
184:
185: return new PortletDataHandlerControl[] { _nodesAndPages,
186: _comments, _tags };
187: }
188:
189: public PortletPreferences importData(PortletDataContext context,
190: String portletId, PortletPreferences prefs, String data)
191: throws PortletDataException {
192:
193: try {
194: XStream xStream = new XStream();
195:
196: Document doc = PortalUtil.readDocumentFromXML(data);
197:
198: Element root = doc.getRootElement();
199:
200: // Nodes
201:
202: Element el = root.element("wiki-nodes").element("list");
203:
204: Document tempDoc = DocumentHelper.createDocument();
205:
206: tempDoc.content().add(el.createCopy());
207:
208: Map nodePKs = CollectionFactory.getHashMap();
209:
210: List nodes = (List) xStream.fromXML(tempDoc.asXML());
211:
212: Iterator itr = nodes.iterator();
213:
214: while (itr.hasNext()) {
215: WikiNode node = (WikiNode) itr.next();
216:
217: importNode(context, nodePKs, node);
218: }
219:
220: // Pages
221:
222: el = root.element("wiki-pages").element("list");
223:
224: tempDoc = DocumentHelper.createDocument();
225:
226: tempDoc.content().add(el.createCopy());
227:
228: List pages = (List) xStream.fromXML(tempDoc.asXML());
229:
230: itr = pages.iterator();
231:
232: while (itr.hasNext()) {
233: WikiPage page = (WikiPage) itr.next();
234:
235: importPage(context, nodePKs, page);
236: }
237:
238: return null;
239: } catch (Exception e) {
240: throw new PortletDataException(e);
241: }
242: }
243:
244: protected void importNode(PortletDataContext context, Map nodePKs,
245: WikiNode node) throws Exception {
246:
247: long userId = context.getUserId(node.getUserUuid());
248: long plid = context.getPlid();
249:
250: boolean addCommunityPermissions = true;
251: boolean addGuestPermissions = true;
252:
253: WikiNode existingNode = null;
254:
255: if (context.getDataStrategy().equals(
256: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
257:
258: existingNode = WikiNodeUtil.fetchByUUID_G(node.getUuid(),
259: context.getGroupId());
260:
261: if (existingNode == null) {
262: existingNode = WikiNodeLocalServiceUtil.addNode(node
263: .getUuid(), userId, plid, node.getName(), node
264: .getDescription(), addCommunityPermissions,
265: addGuestPermissions);
266: } else {
267: existingNode = WikiNodeLocalServiceUtil.updateNode(
268: existingNode.getNodeId(), node.getName(), node
269: .getDescription());
270: }
271: } else {
272: existingNode = WikiNodeLocalServiceUtil.addNode(userId,
273: plid, node.getName(), node.getDescription(),
274: addCommunityPermissions, addGuestPermissions);
275: }
276:
277: nodePKs.put(node.getPrimaryKeyObj(), existingNode
278: .getPrimaryKeyObj());
279: }
280:
281: protected void importPage(PortletDataContext context, Map nodePKs,
282: WikiPage page) throws Exception {
283:
284: long userId = context.getUserId(page.getUserUuid());
285: long nodeId = MapUtil.getLong(nodePKs, page.getNodeId(), page
286: .getNodeId());
287:
288: String[] tagsEntries = null;
289:
290: if (context.getBooleanParameter(_NAMESPACE, "tags")) {
291: tagsEntries = context.getTagsEntries(WikiPage.class, page
292: .getPrimaryKeyObj());
293: }
294:
295: WikiPage existingPage = null;
296:
297: try {
298: WikiNodeUtil.findByPrimaryKey(nodeId);
299:
300: if (context.getDataStrategy().equals(
301: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
302:
303: try {
304: existingPage = WikiPageFinderUtil.findByUuid_G(page
305: .getUuid(), context.getGroupId());
306:
307: existingPage = WikiPageLocalServiceUtil.updatePage(
308: userId, nodeId, existingPage.getTitle(),
309: page.getContent(), page.getFormat(),
310: tagsEntries);
311: } catch (NoSuchPageException nspe) {
312: existingPage = WikiPageLocalServiceUtil.addPage(
313: page.getUuid(), userId, nodeId, page
314: .getTitle(), page.getVersion(),
315: page.getContent(), page.getFormat(), page
316: .getHead(), tagsEntries);
317: }
318: } else {
319: existingPage = WikiPageLocalServiceUtil.addPage(userId,
320: nodeId, page.getTitle(), page.getVersion(),
321: page.getContent(), page.getFormat(), page
322: .getHead(), tagsEntries);
323: }
324:
325: if (context.getBooleanParameter(_NAMESPACE, "comments")) {
326: context.importComments(WikiPage.class, new Long(page
327: .getResourcePrimKey()), new Long(existingPage
328: .getResourcePrimKey()), context.getGroupId());
329: }
330:
331: if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
332: context.importRatingsEntries(WikiPage.class, new Long(
333: page.getResourcePrimKey()), new Long(
334: existingPage.getResourcePrimKey()));
335: }
336: } catch (NoSuchNodeException nsne) {
337: _log.error("Could not find the node for page "
338: + page.getPageId());
339: }
340: }
341:
342: private static final String _NAMESPACE = "wiki";
343:
344: private static final PortletDataHandlerBoolean _nodesAndPages = new PortletDataHandlerBoolean(
345: _NAMESPACE, "nodes-and-pages", true, true);
346:
347: private static final PortletDataHandlerBoolean _comments = new PortletDataHandlerBoolean(
348: _NAMESPACE, "comments");
349:
350: private static final PortletDataHandlerBoolean _tags = new PortletDataHandlerBoolean(
351: _NAMESPACE, "tags");
352:
353: private static Log _log = LogFactory
354: .getLog(WikiPortletDataHandlerImpl.class);
355:
356: }
|