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