1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.wiki.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.lar.PortletDataHandlerKeys;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portlet.wiki.NoSuchNodeException;
33  import com.liferay.portlet.wiki.NoSuchPageException;
34  import com.liferay.portlet.wiki.model.WikiNode;
35  import com.liferay.portlet.wiki.model.WikiPage;
36  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
37  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
38  import com.liferay.portlet.wiki.service.persistence.WikiNodeUtil;
39  import com.liferay.portlet.wiki.service.persistence.WikiPageFinderUtil;
40  import com.liferay.portlet.wiki.service.persistence.WikiPageUtil;
41  import com.liferay.util.CollectionFactory;
42  import com.liferay.util.MapUtil;
43  
44  import com.thoughtworks.xstream.XStream;
45  
46  import java.util.ArrayList;
47  import java.util.Iterator;
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  /**
61   * <a href="WikiPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Bruno Farache
64   *
65   */
66  public class WikiPortletDataHandlerImpl implements PortletDataHandler {
67  
68      public PortletPreferences deleteData(
69              PortletDataContext context, String portletId,
70              PortletPreferences prefs)
71          throws PortletDataException {
72  
73          try {
74  
75              // Nodes
76  
77              if (!context.addPrimaryKey(
78                      WikiPortletDataHandlerImpl.class, "deleteData")) {
79  
80                  WikiNodeLocalServiceUtil.deleteNodes(context.getGroupId());
81              }
82              return null;
83          }
84          catch (Exception e) {
85              throw new PortletDataException(e);
86          }
87      }
88  
89      public String exportData(
90              PortletDataContext context, String portletId,
91              PortletPreferences prefs)
92          throws PortletDataException {
93  
94          try {
95              XStream xStream = new XStream();
96  
97              Document doc = DocumentHelper.createDocument();
98  
99              Element root = doc.addElement("wiki-data");
100 
101             root.addAttribute("group-id", String.valueOf(context.getGroupId()));
102 
103             // Nodes
104 
105             List nodes = WikiNodeUtil.findByGroupId(context.getGroupId());
106 
107             List pages = new ArrayList();
108 
109             Iterator itr = nodes.iterator();
110 
111             while (itr.hasNext()) {
112                 WikiNode node = (WikiNode)itr.next();
113 
114                 if (context.addPrimaryKey(
115                         WikiNode.class, node.getPrimaryKeyObj())) {
116 
117                     itr.remove();
118                 }
119                 else {
120                     node.setUserUuid(node.getUserUuid());
121 
122                     List nodePages = WikiPageUtil.findByNodeId(
123                         node.getNodeId());
124 
125                     pages.addAll(nodePages);
126                 }
127             }
128 
129             String xml = xStream.toXML(nodes);
130 
131             Element el = root.addElement("wiki-nodes");
132 
133             Document tempDoc = PortalUtil.readDocumentFromXML(xml);
134 
135             el.content().add(tempDoc.getRootElement().createCopy());
136 
137             // Pages
138 
139             itr = pages.iterator();
140 
141             while (itr.hasNext()) {
142                 WikiPage page = (WikiPage)itr.next();
143 
144                 if (context.addPrimaryKey(
145                         WikiPage.class, page.getPrimaryKeyObj())) {
146 
147                     itr.remove();
148                 }
149                 else {
150                     page.setUserUuid(page.getUserUuid());
151 
152                     if (context.getBooleanParameter(_NAMESPACE, "comments")) {
153                         context.addComments(
154                             WikiPage.class,
155                             new Long(page.getResourcePrimKey()));
156                     }
157 
158                     if (context.getBooleanParameter(_NAMESPACE, "tags")) {
159                         context.addTagsEntries(
160                             WikiPage.class,
161                             new Long(page.getResourcePrimKey()));
162                     }
163                 }
164             }
165 
166             xml = xStream.toXML(pages);
167 
168             el = root.addElement("wiki-pages");
169 
170             tempDoc = PortalUtil.readDocumentFromXML(xml);
171 
172             el.content().add(tempDoc.getRootElement().createCopy());
173 
174             return doc.asXML();
175         }
176         catch (Exception e) {
177             throw new PortletDataException(e);
178         }
179     }
180 
181     public PortletDataHandlerControl[] getExportControls()
182         throws PortletDataException {
183 
184         return new PortletDataHandlerControl[] {
185             _nodesAndPages, _comments, _tags
186         };
187     }
188 
189     public PortletDataHandlerControl[] getImportControls()
190         throws PortletDataException {
191 
192         return new PortletDataHandlerControl[] {
193             _nodesAndPages, _comments, _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             XStream xStream = new XStream();
204 
205             Document doc = PortalUtil.readDocumentFromXML(data);
206 
207             Element root = doc.getRootElement();
208 
209             // Nodes
210 
211             Element el = root.element("wiki-nodes").element("list");
212 
213             Document tempDoc = DocumentHelper.createDocument();
214 
215             tempDoc.content().add(el.createCopy());
216 
217             Map nodePKs = CollectionFactory.getHashMap();
218 
219             List nodes = (List)xStream.fromXML(tempDoc.asXML());
220 
221             Iterator itr = nodes.iterator();
222 
223             while (itr.hasNext()) {
224                 WikiNode node = (WikiNode)itr.next();
225 
226                 importNode(context, nodePKs, node);
227             }
228 
229             // Pages
230 
231             el = root.element("wiki-pages").element("list");
232 
233             tempDoc = DocumentHelper.createDocument();
234 
235             tempDoc.content().add(el.createCopy());
236 
237             List pages = (List)xStream.fromXML(tempDoc.asXML());
238 
239             itr = pages.iterator();
240 
241             while (itr.hasNext()) {
242                 WikiPage page = (WikiPage)itr.next();
243 
244                 importPage(context, nodePKs, page);
245             }
246 
247             return null;
248         }
249         catch (Exception e) {
250             throw new PortletDataException(e);
251         }
252     }
253 
254     protected void importNode(
255             PortletDataContext context, Map nodePKs, WikiNode node)
256         throws Exception {
257 
258         long userId = context.getUserId(node.getUserUuid());
259         long plid = context.getPlid();
260 
261         boolean addCommunityPermissions = true;
262         boolean addGuestPermissions = true;
263 
264         WikiNode existingNode = null;
265 
266         if (context.getDataStrategy().equals(
267                 PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
268 
269             existingNode = WikiNodeUtil.fetchByUUID_G(
270                 node.getUuid(), context.getGroupId());
271 
272             if (existingNode == null) {
273                 existingNode = WikiNodeLocalServiceUtil.addNode(
274                     node.getUuid(), userId, plid, node.getName(),
275                     node.getDescription(), addCommunityPermissions,
276                     addGuestPermissions);
277             }
278             else {
279                 existingNode = WikiNodeLocalServiceUtil.updateNode(
280                     existingNode.getNodeId(), node.getName(),
281                     node.getDescription());
282             }
283         }
284         else {
285             existingNode = WikiNodeLocalServiceUtil.addNode(
286                 userId, plid, node.getName(), node.getDescription(),
287                 addCommunityPermissions, addGuestPermissions);
288         }
289 
290         nodePKs.put(node.getPrimaryKeyObj(), existingNode.getPrimaryKeyObj());
291     }
292 
293     protected void importPage(
294             PortletDataContext context, Map nodePKs, WikiPage page)
295         throws Exception {
296 
297         long userId = context.getUserId(page.getUserUuid());
298         long nodeId = MapUtil.getLong(
299             nodePKs, page.getNodeId(), page.getNodeId());
300 
301         String[] tagsEntries = null;
302 
303         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
304             tagsEntries = context.getTagsEntries(
305                 WikiPage.class, page.getPrimaryKeyObj());
306         }
307 
308         WikiPage existingPage = null;
309 
310         try {
311             WikiNodeUtil.findByPrimaryKey(nodeId);
312 
313             if (context.getDataStrategy().equals(
314                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
315 
316                 try {
317                     existingPage = WikiPageFinderUtil.findByUuid_G(
318                         page.getUuid(), context.getGroupId());
319 
320                     existingPage = WikiPageLocalServiceUtil.updatePage(
321                         userId, nodeId, existingPage.getTitle(),
322                         page.getContent(), page.getFormat(), tagsEntries);
323                 }
324                 catch (NoSuchPageException nspe) {
325                     existingPage = WikiPageLocalServiceUtil.addPage(
326                         page.getUuid(), userId, nodeId, page.getTitle(),
327                         page.getVersion(), page.getContent(), page.getFormat(),
328                         page.getHead(), tagsEntries);
329                 }
330             }
331             else {
332                 existingPage = WikiPageLocalServiceUtil.addPage(
333                     userId, nodeId, page.getTitle(), page.getVersion(),
334                     page.getContent(), page.getFormat(), page.getHead(),
335                     tagsEntries);
336             }
337 
338             if (context.getBooleanParameter(_NAMESPACE, "comments")) {
339                 context.importComments(
340                     WikiPage.class, new Long(page.getResourcePrimKey()),
341                     new Long(existingPage.getResourcePrimKey()),
342                     context.getGroupId());
343             }
344 
345             if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
346                 context.importRatingsEntries(
347                     WikiPage.class, new Long(page.getResourcePrimKey()),
348                     new Long(existingPage.getResourcePrimKey()));
349             }
350         }
351         catch (NoSuchNodeException nsne) {
352             _log.error(
353                 "Could not find the node for page " +
354                     page.getPageId());
355         }
356     }
357 
358     private static final String _NAMESPACE = "wiki";
359 
360     private static final PortletDataHandlerBoolean _nodesAndPages =
361         new PortletDataHandlerBoolean(
362             _NAMESPACE, "nodes-and-pages", true, true);
363 
364     private static final PortletDataHandlerBoolean _comments =
365         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
366 
367     private static final PortletDataHandlerBoolean _tags =
368         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
369 
370     private static Log _log =
371         LogFactory.getLog(WikiPortletDataHandlerImpl.class);
372 
373 }