1
14
15 package com.liferay.portlet.wiki.lar;
16
17 import com.liferay.portal.kernel.lar.BasePortletDataHandler;
18 import com.liferay.portal.kernel.lar.PortletDataContext;
19 import com.liferay.portal.kernel.lar.PortletDataException;
20 import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
21 import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.util.GetterUtil;
25 import com.liferay.portal.kernel.util.MapUtil;
26 import com.liferay.portal.kernel.util.StringPool;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.kernel.xml.Document;
29 import com.liferay.portal.kernel.xml.Element;
30 import com.liferay.portal.kernel.xml.SAXReaderUtil;
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 import com.liferay.portlet.wiki.util.WikiCacheThreadLocal;
36 import com.liferay.portlet.wiki.util.WikiCacheUtil;
37
38 import java.util.List;
39 import java.util.Map;
40
41 import javax.portlet.PortletPreferences;
42
43
49 public class WikiDisplayPortletDataHandlerImpl extends BasePortletDataHandler {
50
51 public PortletPreferences deleteData(
52 PortletDataContext context, String portletId,
53 PortletPreferences preferences)
54 throws PortletDataException {
55
56 try {
57 preferences.setValue("title", StringPool.BLANK);
58 preferences.setValue("node-id", StringPool.BLANK);
59
60 return preferences;
61 }
62 catch (Exception e) {
63 throw new PortletDataException(e);
64 }
65 }
66
67 public String exportData(
68 PortletDataContext context, String portletId,
69 PortletPreferences preferences)
70 throws PortletDataException {
71
72 try {
73 long nodeId = GetterUtil.getLong(
74 preferences.getValue("node-id", StringPool.BLANK));
75
76 if (nodeId <= 0) {
77 if (_log.isWarnEnabled()) {
78 _log.warn(
79 "No node id found in preferences of portlet " +
80 portletId);
81 }
82
83 return StringPool.BLANK;
84 }
85
86 String title = preferences.getValue("title", null);
87
88 if (title == null) {
89 if (_log.isWarnEnabled()) {
90 _log.warn(
91 "No title found in preferences of portlet " +
92 portletId);
93 }
94
95 return StringPool.BLANK;
96 }
97
98 WikiNode node = null;
99
100 try {
101 node = WikiNodeUtil.findByPrimaryKey(nodeId);
102 }
103 catch (NoSuchNodeException nsne) {
104 if (_log.isWarnEnabled()) {
105 _log.warn(nsne);
106 }
107 }
108
109 if (node == null) {
110 return StringPool.BLANK;
111 }
112
113 context.addPermissions(
114 "com.liferay.portlet.wiki", context.getGroupId());
115
116 Document doc = SAXReaderUtil.createDocument();
117
118 Element root = doc.addElement("wiki-display-data");
119
120 root.addAttribute("group-id", String.valueOf(context.getGroupId()));
121
122 Element nodesEl = root.addElement("nodes");
123 Element pagesEl = root.addElement("pages");
124
125 WikiPortletDataHandlerImpl.exportNode(
126 context, nodesEl, pagesEl, node);
127
128 return doc.formattedString();
129 }
130 catch (Exception e) {
131 throw new PortletDataException(e);
132 }
133 }
134
135 public PortletDataHandlerControl[] getExportControls() {
136 return new PortletDataHandlerControl[] {
137 _nodesAndPages, _attachments, _categories, _comments, _tags
138 };
139 }
140
141 public PortletDataHandlerControl[] getImportControls() {
142 return new PortletDataHandlerControl[] {
143 _nodesAndPages, _attachments, _categories, _comments, _tags
144 };
145 }
146
147 public PortletPreferences importData(
148 PortletDataContext context, String portletId,
149 PortletPreferences preferences, String data)
150 throws PortletDataException {
151
152 WikiCacheThreadLocal.setClearCache(false);
153
154 try {
155 context.importPermissions(
156 "com.liferay.portlet.wiki", context.getSourceGroupId(),
157 context.getGroupId());
158
159 if (Validator.isNull(data)) {
160 return null;
161 }
162
163 Document doc = SAXReaderUtil.read(data);
164
165 Element root = doc.getRootElement();
166
167 List<Element> nodeEls = root.element("nodes").elements("node");
168
169 Map<Long, Long> nodePKs =
170 (Map<Long, Long>)context.getNewPrimaryKeysMap(WikiNode.class);
171
172 for (Element nodeEl : nodeEls) {
173 String path = nodeEl.attributeValue("path");
174
175 if (!context.isPathNotProcessed(path)) {
176 continue;
177 }
178
179 WikiNode node = (WikiNode)context.getZipEntryAsObject(path);
180
181 WikiPortletDataHandlerImpl.importNode(context, nodePKs, node);
182 }
183
184 List<Element> pageEls = root.element("pages").elements("page");
185
186 for (Element pageEl : pageEls) {
187 String path = pageEl.attributeValue("path");
188
189 if (!context.isPathNotProcessed(path)) {
190 continue;
191 }
192
193 WikiPage page = (WikiPage)context.getZipEntryAsObject(path);
194
195 WikiPortletDataHandlerImpl.importPage(
196 context, nodePKs, pageEl, page);
197 }
198
199 for (long nodeId : nodePKs.values()) {
200 WikiCacheUtil.clearCache(nodeId);
201 }
202
203 long nodeId = GetterUtil.getLong(
204 preferences.getValue("node-id", StringPool.BLANK));
205
206 if (nodeId > 0) {
207 nodeId = MapUtil.getLong(nodePKs, nodeId, nodeId);
208
209 preferences.setValue("node-id", String.valueOf(nodeId));
210 }
211
212 return preferences;
213 }
214 catch (Exception e) {
215 throw new PortletDataException(e);
216 }
217 finally {
218 WikiCacheThreadLocal.setClearCache(true);
219 }
220 }
221
222 private static final String _NAMESPACE = "wiki";
223
224 private static Log _log = LogFactoryUtil.getLog(
225 WikiDisplayPortletDataHandlerImpl.class);
226
227 private static PortletDataHandlerBoolean _attachments =
228 new PortletDataHandlerBoolean(_NAMESPACE, "attachments");
229
230 private static PortletDataHandlerBoolean _categories =
231 new PortletDataHandlerBoolean(_NAMESPACE, "categories");
232
233 private static PortletDataHandlerBoolean _comments =
234 new PortletDataHandlerBoolean(_NAMESPACE, "comments");
235
236 private static PortletDataHandlerBoolean _nodesAndPages =
237 new PortletDataHandlerBoolean(
238 _NAMESPACE, "wikis-and-pages", true, true);
239
240 private static PortletDataHandlerBoolean _tags =
241 new PortletDataHandlerBoolean(_NAMESPACE, "tags");
242
243 }