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