001
014
015 package com.liferay.portlet.wiki.lar;
016
017 import com.liferay.portal.kernel.lar.BasePortletDataHandler;
018 import com.liferay.portal.kernel.lar.PortletDataContext;
019 import com.liferay.portal.kernel.lar.PortletDataException;
020 import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
021 import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.MapUtil;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.kernel.xml.Document;
029 import com.liferay.portal.kernel.xml.Element;
030 import com.liferay.portal.kernel.xml.SAXReaderUtil;
031 import com.liferay.portlet.journal.lar.JournalPortletDataHandlerImpl;
032 import com.liferay.portlet.wiki.NoSuchNodeException;
033 import com.liferay.portlet.wiki.model.WikiNode;
034 import com.liferay.portlet.wiki.model.WikiPage;
035 import com.liferay.portlet.wiki.service.persistence.WikiNodeUtil;
036 import com.liferay.portlet.wiki.util.WikiCacheThreadLocal;
037 import com.liferay.portlet.wiki.util.WikiCacheUtil;
038
039 import java.util.Map;
040
041 import javax.portlet.PortletPreferences;
042
043
046 public class WikiDisplayPortletDataHandlerImpl extends BasePortletDataHandler {
047
048 @Override
049 public PortletDataHandlerControl[] getExportControls() {
050 return new PortletDataHandlerControl[] {
051 _nodesAndPages, _attachments, _categories, _comments, _tags
052 };
053 }
054
055 @Override
056 public PortletDataHandlerControl[] getImportControls() {
057 return new PortletDataHandlerControl[] {
058 _nodesAndPages, _attachments, _categories, _comments, _tags
059 };
060 }
061
062 @Override
063 public PortletPreferences importData(
064 PortletDataContext portletDataContext, String portletId,
065 PortletPreferences portletPreferences, String data)
066 throws PortletDataException {
067
068 WikiCacheThreadLocal.setClearCache(false);
069
070 try {
071 return super.importData(
072 portletDataContext, portletId, portletPreferences, data);
073 }
074 finally {
075 WikiCacheThreadLocal.setClearCache(true);
076 }
077 }
078
079 @Override
080 protected PortletPreferences doDeleteData(
081 PortletDataContext portletDataContext, String portletId,
082 PortletPreferences portletPreferences)
083 throws Exception {
084
085 portletPreferences.setValue("title", StringPool.BLANK);
086 portletPreferences.setValue("nodeId", StringPool.BLANK);
087
088 return portletPreferences;
089 }
090
091 @Override
092 protected String doExportData(
093 PortletDataContext portletDataContext, String portletId,
094 PortletPreferences portletPreferences)
095 throws Exception {
096
097 long nodeId = GetterUtil.getLong(
098 portletPreferences.getValue("nodeId", StringPool.BLANK));
099
100 if (nodeId <= 0) {
101 if (_log.isWarnEnabled()) {
102 _log.warn(
103 "No node id found in preferences of portlet " + portletId);
104 }
105
106 return StringPool.BLANK;
107 }
108
109 String title = portletPreferences.getValue("title", null);
110
111 if (title == null) {
112 if (_log.isWarnEnabled()) {
113 _log.warn(
114 "No title found in preferences of portlet " + portletId);
115 }
116
117 return StringPool.BLANK;
118 }
119
120 WikiNode node = null;
121
122 try {
123 node = WikiNodeUtil.findByPrimaryKey(nodeId);
124 }
125 catch (NoSuchNodeException nsne) {
126 if (_log.isWarnEnabled()) {
127 _log.warn(nsne, nsne);
128 }
129
130 return StringPool.BLANK;
131 }
132
133 portletDataContext.addPermissions(
134 "com.liferay.portlet.wiki", portletDataContext.getScopeGroupId());
135
136 Document document = SAXReaderUtil.createDocument();
137
138 Element rootElement = document.addElement("wiki-display-data");
139
140 rootElement.addAttribute(
141 "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
142
143 Element nodesElement = rootElement.addElement("nodes");
144 Element pagesElement = rootElement.addElement("pages");
145
146 WikiPortletDataHandlerImpl.exportNode(
147 portletDataContext, nodesElement, pagesElement, node);
148
149 return document.formattedString();
150 }
151
152 @Override
153 protected PortletPreferences doImportData(
154 PortletDataContext portletDataContext, String portletId,
155 PortletPreferences portletPreferences, String data)
156 throws Exception {
157
158 portletDataContext.importPermissions(
159 "com.liferay.portlet.wiki", portletDataContext.getSourceGroupId(),
160 portletDataContext.getScopeGroupId());
161
162 if (Validator.isNull(data)) {
163 return null;
164 }
165
166 Document document = SAXReaderUtil.read(data);
167
168 Element rootElement = document.getRootElement();
169
170 Element nodesElement = rootElement.element("nodes");
171
172 for (Element nodeElement : nodesElement.elements("node")) {
173 String path = nodeElement.attributeValue("path");
174
175 if (!portletDataContext.isPathNotProcessed(path)) {
176 continue;
177 }
178
179 WikiNode node = (WikiNode)portletDataContext.getZipEntryAsObject(
180 path);
181
182 WikiPortletDataHandlerImpl.importNode(portletDataContext, node);
183 }
184
185 Element pagesElement = rootElement.element("pages");
186
187 JournalPortletDataHandlerImpl.importReferencedData(
188 portletDataContext, pagesElement);
189
190 for (Element pageElement : pagesElement.elements("page")) {
191 String path = pageElement.attributeValue("path");
192
193 if (!portletDataContext.isPathNotProcessed(path)) {
194 continue;
195 }
196
197 WikiPage page = (WikiPage)portletDataContext.getZipEntryAsObject(
198 path);
199
200 WikiPortletDataHandlerImpl.importPage(
201 portletDataContext, pageElement, page);
202 }
203
204 Map<Long, Long> nodePKs =
205 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
206 WikiNode.class);
207
208 for (long nodeId : nodePKs.values()) {
209 WikiCacheUtil.clearCache(nodeId);
210 }
211
212 long nodeId = GetterUtil.getLong(
213 portletPreferences.getValue("nodeId", StringPool.BLANK));
214
215 if (nodeId > 0) {
216 nodeId = MapUtil.getLong(nodePKs, nodeId, nodeId);
217
218 portletPreferences.setValue("nodeId", String.valueOf(nodeId));
219 }
220
221 return portletPreferences;
222 }
223
224 private static final String _NAMESPACE = "wiki";
225
226 private static Log _log = LogFactoryUtil.getLog(
227 WikiDisplayPortletDataHandlerImpl.class);
228
229 private static PortletDataHandlerBoolean _attachments =
230 new PortletDataHandlerBoolean(_NAMESPACE, "attachments");
231
232 private static PortletDataHandlerBoolean _categories =
233 new PortletDataHandlerBoolean(_NAMESPACE, "categories");
234
235 private static PortletDataHandlerBoolean _comments =
236 new PortletDataHandlerBoolean(_NAMESPACE, "comments");
237
238 private static PortletDataHandlerBoolean _nodesAndPages =
239 new PortletDataHandlerBoolean(
240 _NAMESPACE, "wikis-and-pages", true, true);
241
242 private static PortletDataHandlerBoolean _tags =
243 new PortletDataHandlerBoolean(_NAMESPACE, "tags");
244
245 }