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