001
014
015 package com.liferay.portlet.wiki.service.impl;
016
017 import com.liferay.portal.kernel.configuration.Filter;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.search.Indexer;
023 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
024 import com.liferay.portal.kernel.util.InstancePool;
025 import com.liferay.portal.kernel.util.PropsKeys;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.model.Group;
029 import com.liferay.portal.model.ResourceConstants;
030 import com.liferay.portal.model.User;
031 import com.liferay.portal.service.ServiceContext;
032 import com.liferay.portal.util.PropsUtil;
033 import com.liferay.portal.util.PropsValues;
034 import com.liferay.portlet.wiki.DuplicateNodeNameException;
035 import com.liferay.portlet.wiki.NodeNameException;
036 import com.liferay.portlet.wiki.importers.WikiImporter;
037 import com.liferay.portlet.wiki.model.WikiNode;
038 import com.liferay.portlet.wiki.model.WikiPage;
039 import com.liferay.portlet.wiki.service.base.WikiNodeLocalServiceBaseImpl;
040
041 import java.io.InputStream;
042
043 import java.util.ArrayList;
044 import java.util.Date;
045 import java.util.HashMap;
046 import java.util.Iterator;
047 import java.util.List;
048 import java.util.Map;
049
050
055 public class WikiNodeLocalServiceImpl extends WikiNodeLocalServiceBaseImpl {
056
057 @Override
058 public WikiNode addDefaultNode(long userId, ServiceContext serviceContext)
059 throws PortalException, SystemException {
060
061 return addNode(
062 userId, PropsValues.WIKI_INITIAL_NODE_NAME, StringPool.BLANK,
063 serviceContext);
064 }
065
066 @Override
067 public WikiNode addNode(
068 long userId, String name, String description,
069 ServiceContext serviceContext)
070 throws PortalException, SystemException {
071
072
073
074 User user = userPersistence.findByPrimaryKey(userId);
075 long groupId = serviceContext.getScopeGroupId();
076 Date now = new Date();
077
078 validate(groupId, name);
079
080 long nodeId = counterLocalService.increment();
081
082 WikiNode node = wikiNodePersistence.create(nodeId);
083
084 node.setUuid(serviceContext.getUuid());
085 node.setGroupId(groupId);
086 node.setCompanyId(user.getCompanyId());
087 node.setUserId(user.getUserId());
088 node.setUserName(user.getFullName());
089 node.setCreateDate(serviceContext.getCreateDate(now));
090 node.setModifiedDate(serviceContext.getModifiedDate(now));
091 node.setName(name);
092 node.setDescription(description);
093
094 try {
095 wikiNodePersistence.update(node, false);
096 }
097 catch (SystemException se) {
098 if (_log.isWarnEnabled()) {
099 _log.warn(
100 "Add failed, fetch {groupId=" + groupId + ", name=" +
101 name + "}");
102 }
103
104 node = wikiNodePersistence.fetchByG_N(groupId, name, false);
105
106 if (node == null) {
107 throw se;
108 }
109
110 return node;
111 }
112
113
114
115 if (serviceContext.isAddGroupPermissions() ||
116 serviceContext.isAddGuestPermissions()) {
117
118 addNodeResources(
119 node, serviceContext.isAddGroupPermissions(),
120 serviceContext.isAddGuestPermissions());
121 }
122 else {
123 addNodeResources(
124 node, serviceContext.getGroupPermissions(),
125 serviceContext.getGuestPermissions());
126 }
127
128 return node;
129 }
130
131 @Override
132 public void addNodeResources(
133 long nodeId, boolean addGroupPermissions,
134 boolean addGuestPermissions)
135 throws PortalException, SystemException {
136
137 WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
138
139 addNodeResources(node, addGroupPermissions, addGuestPermissions);
140 }
141
142 @Override
143 public void addNodeResources(
144 long nodeId, String[] groupPermissions, String[] guestPermissions)
145 throws PortalException, SystemException {
146
147 WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
148
149 addNodeResources(node, groupPermissions, guestPermissions);
150 }
151
152 @Override
153 public void addNodeResources(
154 WikiNode node, boolean addGroupPermissions,
155 boolean addGuestPermissions)
156 throws PortalException, SystemException {
157
158 resourceLocalService.addResources(
159 node.getCompanyId(), node.getGroupId(), node.getUserId(),
160 WikiNode.class.getName(), node.getNodeId(), false,
161 addGroupPermissions, addGuestPermissions);
162 }
163
164 @Override
165 public void addNodeResources(
166 WikiNode node, String[] groupPermissions, String[] guestPermissions)
167 throws PortalException, SystemException {
168
169 resourceLocalService.addModelResources(
170 node.getCompanyId(), node.getGroupId(), node.getUserId(),
171 WikiNode.class.getName(), node.getNodeId(), groupPermissions,
172 guestPermissions);
173 }
174
175 @Override
176 public void deleteNode(long nodeId)
177 throws PortalException, SystemException {
178
179 WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
180
181 deleteNode(node);
182 }
183
184 @Override
185 public void deleteNode(WikiNode node)
186 throws PortalException, SystemException {
187
188
189
190 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
191 WikiPage.class);
192
193 indexer.delete(node);
194
195
196
197 subscriptionLocalService.deleteSubscriptions(
198 node.getCompanyId(), WikiNode.class.getName(), node.getNodeId());
199
200
201
202 wikiPageLocalService.deletePages(node.getNodeId());
203
204
205
206 resourceLocalService.deleteResource(
207 node.getCompanyId(), WikiNode.class.getName(),
208 ResourceConstants.SCOPE_INDIVIDUAL, node.getNodeId());
209
210
211
212 wikiNodePersistence.remove(node);
213 }
214
215 @Override
216 public void deleteNodes(long groupId)
217 throws PortalException, SystemException {
218
219 Iterator<WikiNode> itr = wikiNodePersistence.findByGroupId(
220 groupId).iterator();
221
222 while (itr.hasNext()) {
223 WikiNode node = itr.next();
224
225 deleteNode(node);
226 }
227 }
228
229 @Override
230 public List<WikiNode> getCompanyNodes(long companyId, int start, int end)
231 throws SystemException {
232
233 return wikiNodePersistence.findByCompanyId(companyId, start, end);
234 }
235
236 @Override
237 public int getCompanyNodesCount(long companyId) throws SystemException {
238 return wikiNodePersistence.countByCompanyId(companyId);
239 }
240
241 @Override
242 public WikiNode getNode(long nodeId)
243 throws PortalException, SystemException {
244
245 return wikiNodePersistence.findByPrimaryKey(nodeId);
246 }
247
248 @Override
249 public WikiNode getNode(long groupId, String nodeName)
250 throws PortalException, SystemException {
251
252 return wikiNodePersistence.findByG_N(groupId, nodeName);
253 }
254
255 @Override
256 public List<WikiNode> getNodes(long groupId)
257 throws PortalException, SystemException {
258
259 List<WikiNode> nodes = wikiNodePersistence.findByGroupId(groupId);
260
261 if (nodes.isEmpty()) {
262 nodes = addDefaultNode(groupId);
263 }
264
265 return nodes;
266 }
267
268 @Override
269 public List<WikiNode> getNodes(long groupId, int start, int end)
270 throws PortalException, SystemException {
271
272 List<WikiNode> nodes = wikiNodePersistence.findByGroupId(
273 groupId, start, end);
274
275 if (nodes.isEmpty()) {
276 nodes = addDefaultNode(groupId);
277 }
278
279 return nodes;
280 }
281
282 @Override
283 public int getNodesCount(long groupId) throws SystemException {
284 return wikiNodePersistence.countByGroupId(groupId);
285 }
286
287 @Override
288 public void importPages(
289 long userId, long nodeId, String importer,
290 InputStream[] inputStreams, Map<String, String[]> options)
291 throws PortalException, SystemException {
292
293 WikiNode node = getNode(nodeId);
294
295 WikiImporter wikiImporter = getWikiImporter(importer);
296
297 wikiImporter.importPages(userId, node, inputStreams, options);
298 }
299
300 @Override
301 public void subscribeNode(long userId, long nodeId)
302 throws PortalException, SystemException {
303
304 WikiNode node = getNode(nodeId);
305
306 subscriptionLocalService.addSubscription(
307 userId, node.getGroupId(), WikiNode.class.getName(), nodeId);
308 }
309
310 @Override
311 public void unsubscribeNode(long userId, long nodeId)
312 throws PortalException, SystemException {
313
314 subscriptionLocalService.deleteSubscription(
315 userId, WikiNode.class.getName(), nodeId);
316 }
317
318 @Override
319 public WikiNode updateNode(
320 long nodeId, String name, String description,
321 ServiceContext serviceContext)
322 throws PortalException, SystemException {
323
324 WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
325
326 validate(nodeId, node.getGroupId(), name);
327
328 node.setModifiedDate(serviceContext.getModifiedDate(null));
329 node.setName(name);
330 node.setDescription(description);
331
332 wikiNodePersistence.update(node, false);
333
334 return node;
335 }
336
337 protected List<WikiNode> addDefaultNode(long groupId)
338 throws PortalException, SystemException {
339
340 Group group = groupPersistence.findByPrimaryKey(groupId);
341
342 long defaultUserId = userLocalService.getDefaultUserId(
343 group.getCompanyId());
344
345 ServiceContext serviceContext = new ServiceContext();
346
347 serviceContext.setAddGroupPermissions(true);
348 serviceContext.setAddGuestPermissions(true);
349 serviceContext.setScopeGroupId(groupId);
350
351 WikiNode node = wikiNodeLocalService.addDefaultNode(
352 defaultUserId, serviceContext);
353
354 List<WikiNode> nodes = new ArrayList<WikiNode>(1);
355
356 nodes.add(node);
357
358 return nodes;
359 }
360
361 protected WikiImporter getWikiImporter(String importer)
362 throws SystemException {
363
364 WikiImporter wikiImporter = _wikiImporters.get(importer);
365
366 if (wikiImporter == null) {
367 String importerClass = PropsUtil.get(
368 PropsKeys.WIKI_IMPORTERS_CLASS, new Filter(importer));
369
370 if (importerClass != null) {
371 wikiImporter = (WikiImporter)InstancePool.get(importerClass);
372
373 _wikiImporters.put(importer, wikiImporter);
374 }
375
376 if (importer == null) {
377 throw new SystemException(
378 "Unable to instantiate wiki importer class " +
379 importerClass);
380 }
381 }
382
383 return wikiImporter;
384 }
385
386 protected void validate(long nodeId, long groupId, String name)
387 throws PortalException, SystemException {
388
389 if (name.equalsIgnoreCase("tag")) {
390 throw new NodeNameException(name + " is reserved");
391 }
392
393 if (Validator.isNull(name)) {
394 throw new NodeNameException();
395 }
396
397 WikiNode node = wikiNodePersistence.fetchByG_N(groupId, name);
398
399 if ((node != null) && (node.getNodeId() != nodeId)) {
400 throw new DuplicateNodeNameException();
401 }
402 }
403
404 protected void validate(long groupId, String name)
405 throws PortalException, SystemException {
406
407 validate(0, groupId, name);
408 }
409
410 private static Log _log = LogFactoryUtil.getLog(
411 WikiNodeLocalServiceImpl.class);
412
413 private Map<String, WikiImporter> _wikiImporters =
414 new HashMap<String, WikiImporter>();
415
416 }