1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.wiki.lar;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.MapUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.kernel.xml.Document;
24  import com.liferay.portal.kernel.xml.Element;
25  import com.liferay.portal.kernel.xml.SAXReaderUtil;
26  import com.liferay.portal.lar.BasePortletDataHandler;
27  import com.liferay.portal.lar.PortletDataContext;
28  import com.liferay.portal.lar.PortletDataException;
29  import com.liferay.portal.lar.PortletDataHandlerBoolean;
30  import com.liferay.portal.lar.PortletDataHandlerControl;
31  import com.liferay.portlet.wiki.NoSuchNodeException;
32  import com.liferay.portlet.wiki.model.WikiNode;
33  import com.liferay.portlet.wiki.model.WikiPage;
34  import com.liferay.portlet.wiki.service.persistence.WikiNodeUtil;
35  
36  import java.util.List;
37  import java.util.Map;
38  
39  import javax.portlet.PortletPreferences;
40  
41  /**
42   * <a href="WikiDisplayPortletDataHandlerImpl.java.html"><b><i>View Source</i>
43   * </b></a>
44   *
45   * @author Marcellus Tavares
46   */
47  public class WikiDisplayPortletDataHandlerImpl extends BasePortletDataHandler {
48  
49      public PortletPreferences deleteData(
50              PortletDataContext context, String portletId,
51              PortletPreferences preferences)
52          throws PortletDataException {
53  
54          try {
55              preferences.setValue("title", StringPool.BLANK);
56              preferences.setValue("node-id", StringPool.BLANK);
57  
58              return preferences;
59          }
60          catch (Exception e) {
61              throw new PortletDataException(e);
62          }
63      }
64  
65      public String exportData(
66              PortletDataContext context, String portletId,
67              PortletPreferences preferences)
68          throws PortletDataException {
69  
70          try {
71              long nodeId = GetterUtil.getLong(
72                  preferences.getValue("node-id", StringPool.BLANK));
73  
74              if (nodeId <= 0) {
75                  if (_log.isWarnEnabled()) {
76                      _log.warn(
77                          "No node id found in preferences of portlet " +
78                              portletId);
79                  }
80  
81                  return StringPool.BLANK;
82              }
83  
84              String title = preferences.getValue("title", null);
85  
86              if (title == null) {
87                  if (_log.isWarnEnabled()) {
88                      _log.warn(
89                          "No title found in preferences of portlet " +
90                              portletId);
91                  }
92  
93                  return StringPool.BLANK;
94              }
95  
96              WikiNode node = null;
97  
98              try {
99                  node = WikiNodeUtil.findByPrimaryKey(nodeId);
100             }
101             catch (NoSuchNodeException nsne) {
102                 if (_log.isWarnEnabled()) {
103                     _log.warn(nsne);
104                 }
105             }
106 
107             if (node == null) {
108                 return StringPool.BLANK;
109             }
110 
111             Document doc = SAXReaderUtil.createDocument();
112 
113             Element root = doc.addElement("wiki-display-data");
114 
115             root.addAttribute("group-id", String.valueOf(context.getGroupId()));
116 
117             Element nodesEl = root.addElement("nodes");
118             Element pagesEl = root.addElement("pages");
119 
120             WikiPortletDataHandlerImpl.exportNode(
121                 context, nodesEl, pagesEl, node);
122 
123             return doc.formattedString();
124         }
125         catch (Exception e) {
126             throw new PortletDataException(e);
127         }
128     }
129 
130     public PortletDataHandlerControl[] getExportControls() {
131         return new PortletDataHandlerControl[] {
132             _nodesAndPages, _attachments, _categories, _comments, _tags
133         };
134     }
135 
136     public PortletDataHandlerControl[] getImportControls() {
137         return new PortletDataHandlerControl[] {
138             _nodesAndPages, _attachments, _categories, _comments, _tags
139         };
140     }
141 
142     public PortletPreferences importData(
143             PortletDataContext context, String portletId,
144             PortletPreferences preferences, String data)
145         throws PortletDataException {
146 
147         try {
148             if (Validator.isNull(data)) {
149                 return null;
150             }
151 
152             Document doc = SAXReaderUtil.read(data);
153 
154             Element root = doc.getRootElement();
155 
156             List<Element> nodeEls = root.element("nodes").elements("node");
157 
158             Map<Long, Long> nodePKs =
159                 (Map<Long, Long>)context.getNewPrimaryKeysMap(WikiNode.class);
160 
161             for (Element nodeEl : nodeEls) {
162                 String path = nodeEl.attributeValue("path");
163 
164                 if (!context.isPathNotProcessed(path)) {
165                     continue;
166                 }
167 
168                 WikiNode node = (WikiNode)context.getZipEntryAsObject(path);
169 
170                 WikiPortletDataHandlerImpl.importNode(context, nodePKs, node);
171             }
172 
173             List<Element> pageEls = root.element("pages").elements("page");
174 
175             for (Element pageEl : pageEls) {
176                 String path = pageEl.attributeValue("path");
177 
178                 if (!context.isPathNotProcessed(path)) {
179                     continue;
180                 }
181 
182                 WikiPage page = (WikiPage)context.getZipEntryAsObject(path);
183 
184                 WikiPortletDataHandlerImpl.importPage(
185                     context, nodePKs, pageEl, page);
186             }
187 
188             long nodeId = GetterUtil.getLong(
189                 preferences.getValue("node-id", StringPool.BLANK));
190 
191             if (nodeId > 0) {
192                 nodeId = MapUtil.getLong(nodePKs, nodeId, nodeId);
193 
194                 preferences.setValue("node-id", String.valueOf(nodeId));
195             }
196 
197             return preferences;
198         }
199         catch (Exception e) {
200             throw new PortletDataException(e);
201         }
202     }
203 
204     private static final String _NAMESPACE = "wiki";
205 
206     private static final PortletDataHandlerBoolean _nodesAndPages =
207         new PortletDataHandlerBoolean(
208             _NAMESPACE, "wikis-and-pages", true, true);
209 
210     private static final PortletDataHandlerBoolean _attachments =
211         new PortletDataHandlerBoolean(_NAMESPACE, "attachments");
212 
213     private static final PortletDataHandlerBoolean _categories =
214         new PortletDataHandlerBoolean(_NAMESPACE, "categories");
215 
216     private static final PortletDataHandlerBoolean _comments =
217         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
218 
219     private static final PortletDataHandlerBoolean _tags =
220         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
221 
222     private static Log _log = LogFactoryUtil.getLog(
223         WikiDisplayPortletDataHandlerImpl.class);
224 
225 }