1
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 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.getScopeGroupId());
115
116 Document doc = SAXReaderUtil.createDocument();
117
118 Element root = doc.addElement("wiki-display-data");
119
120 root.addAttribute(
121 "group-id", String.valueOf(context.getScopeGroupId()));
122
123 Element nodesEl = root.addElement("nodes");
124 Element pagesEl = root.addElement("pages");
125
126 WikiPortletDataHandlerImpl.exportNode(
127 context, nodesEl, pagesEl, node);
128
129 return doc.formattedString();
130 }
131 catch (Exception e) {
132 throw new PortletDataException(e);
133 }
134 }
135
136 public PortletDataHandlerControl[] getExportControls() {
137 return new PortletDataHandlerControl[] {
138 _nodesAndPages, _attachments, _categories, _comments, _tags
139 };
140 }
141
142 public PortletDataHandlerControl[] getImportControls() {
143 return new PortletDataHandlerControl[] {
144 _nodesAndPages, _attachments, _categories, _comments, _tags
145 };
146 }
147
148 public PortletPreferences importData(
149 PortletDataContext context, String portletId,
150 PortletPreferences preferences, String data)
151 throws PortletDataException {
152
153 WikiCacheThreadLocal.setClearCache(false);
154
155 try {
156 context.importPermissions(
157 "com.liferay.portlet.wiki", context.getSourceGroupId(),
158 context.getScopeGroupId());
159
160 if (Validator.isNull(data)) {
161 return null;
162 }
163
164 Document doc = SAXReaderUtil.read(data);
165
166 Element root = doc.getRootElement();
167
168 List<Element> nodeEls = root.element("nodes").elements("node");
169
170 Map<Long, Long> nodePKs =
171 (Map<Long, Long>)context.getNewPrimaryKeysMap(WikiNode.class);
172
173 for (Element nodeEl : nodeEls) {
174 String path = nodeEl.attributeValue("path");
175
176 if (!context.isPathNotProcessed(path)) {
177 continue;
178 }
179
180 WikiNode node = (WikiNode)context.getZipEntryAsObject(path);
181
182 WikiPortletDataHandlerImpl.importNode(context, nodePKs, node);
183 }
184
185 List<Element> pageEls = root.element("pages").elements("page");
186
187 for (Element pageEl : pageEls) {
188 String path = pageEl.attributeValue("path");
189
190 if (!context.isPathNotProcessed(path)) {
191 continue;
192 }
193
194 WikiPage page = (WikiPage)context.getZipEntryAsObject(path);
195
196 WikiPortletDataHandlerImpl.importPage(
197 context, nodePKs, pageEl, page);
198 }
199
200 for (long nodeId : nodePKs.values()) {
201 WikiCacheUtil.clearCache(nodeId);
202 }
203
204 long nodeId = GetterUtil.getLong(
205 preferences.getValue("node-id", StringPool.BLANK));
206
207 if (nodeId > 0) {
208 nodeId = MapUtil.getLong(nodePKs, nodeId, nodeId);
209
210 preferences.setValue("node-id", String.valueOf(nodeId));
211 }
212
213 return preferences;
214 }
215 catch (Exception e) {
216 throw new PortletDataException(e);
217 }
218 finally {
219 WikiCacheThreadLocal.setClearCache(true);
220 }
221 }
222
223 private static final String _NAMESPACE = "wiki";
224
225 private static final PortletDataHandlerBoolean _nodesAndPages =
226 new PortletDataHandlerBoolean(
227 _NAMESPACE, "wikis-and-pages", true, true);
228
229 private static final PortletDataHandlerBoolean _attachments =
230 new PortletDataHandlerBoolean(_NAMESPACE, "attachments");
231
232 private static final PortletDataHandlerBoolean _categories =
233 new PortletDataHandlerBoolean(_NAMESPACE, "categories");
234
235 private static final PortletDataHandlerBoolean _comments =
236 new PortletDataHandlerBoolean(_NAMESPACE, "comments");
237
238 private static final PortletDataHandlerBoolean _tags =
239 new PortletDataHandlerBoolean(_NAMESPACE, "tags");
240
241 private static Log _log = LogFactoryUtil.getLog(
242 WikiDisplayPortletDataHandlerImpl.class);
243
244 }