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.portlet.wiki.DuplicateNodeNameException;
034 import com.liferay.portlet.wiki.NodeNameException;
035 import com.liferay.portlet.wiki.importers.WikiImporter;
036 import com.liferay.portlet.wiki.model.WikiNode;
037 import com.liferay.portlet.wiki.model.WikiPage;
038 import com.liferay.portlet.wiki.service.base.WikiNodeLocalServiceBaseImpl;
039
040 import java.io.File;
041
042 import java.util.ArrayList;
043 import java.util.Date;
044 import java.util.HashMap;
045 import java.util.Iterator;
046 import java.util.List;
047 import java.util.Map;
048
049
054 public class WikiNodeLocalServiceImpl extends WikiNodeLocalServiceBaseImpl {
055
056 public WikiNode addDefaultNode(long userId, ServiceContext serviceContext)
057 throws PortalException, SystemException {
058
059 String nodeName = PropsUtil.get(PropsKeys.WIKI_INITIAL_NODE_NAME);
060
061 return addNode(
062 userId, nodeName, StringPool.BLANK, 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.getAddCommunityPermissions() ||
114 serviceContext.getAddGuestPermissions()) {
115
116 addNodeResources(
117 node, serviceContext.getAddCommunityPermissions(),
118 serviceContext.getAddGuestPermissions());
119 }
120 else {
121 addNodeResources(
122 node, serviceContext.getCommunityPermissions(),
123 serviceContext.getGuestPermissions());
124 }
125
126 return node;
127 }
128
129 public void addNodeResources(
130 long nodeId, boolean addCommunityPermissions,
131 boolean addGuestPermissions)
132 throws PortalException, SystemException {
133
134 WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
135
136 addNodeResources(node, addCommunityPermissions, addGuestPermissions);
137 }
138
139 public void addNodeResources(
140 WikiNode node, boolean addCommunityPermissions,
141 boolean addGuestPermissions)
142 throws PortalException, SystemException {
143
144 resourceLocalService.addResources(
145 node.getCompanyId(), node.getGroupId(), node.getUserId(),
146 WikiNode.class.getName(), node.getNodeId(), false,
147 addCommunityPermissions, addGuestPermissions);
148 }
149
150 public void addNodeResources(
151 long nodeId, String[] communityPermissions,
152 String[] guestPermissions)
153 throws PortalException, SystemException {
154
155 WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
156
157 addNodeResources(node, communityPermissions, guestPermissions);
158 }
159
160 public void addNodeResources(
161 WikiNode node, String[] communityPermissions,
162 String[] guestPermissions)
163 throws PortalException, SystemException {
164
165 resourceLocalService.addModelResources(
166 node.getCompanyId(), node.getGroupId(), node.getUserId(),
167 WikiNode.class.getName(), node.getNodeId(), communityPermissions,
168 guestPermissions);
169 }
170
171 public void deleteNode(long nodeId)
172 throws PortalException, SystemException {
173
174 WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
175
176 deleteNode(node);
177 }
178
179 public void deleteNode(WikiNode node)
180 throws PortalException, SystemException {
181
182
183
184 Indexer indexer = IndexerRegistryUtil.getIndexer(WikiPage.class);
185
186 indexer.delete(node);
187
188
189
190 subscriptionLocalService.deleteSubscriptions(
191 node.getCompanyId(), WikiNode.class.getName(), node.getNodeId());
192
193
194
195 wikiPageLocalService.deletePages(node.getNodeId());
196
197
198
199 resourceLocalService.deleteResource(
200 node.getCompanyId(), WikiNode.class.getName(),
201 ResourceConstants.SCOPE_INDIVIDUAL, node.getNodeId());
202
203
204
205 wikiNodePersistence.remove(node);
206 }
207
208 public void deleteNodes(long groupId)
209 throws PortalException, SystemException {
210
211 Iterator<WikiNode> itr = wikiNodePersistence.findByGroupId(
212 groupId).iterator();
213
214 while (itr.hasNext()) {
215 WikiNode node = itr.next();
216
217 deleteNode(node);
218 }
219 }
220
221 public List<WikiNode> getCompanyNodes(long companyId, int start, int end)
222 throws SystemException {
223
224 return wikiNodePersistence.findByCompanyId(companyId, start, end);
225 }
226
227 public int getCompanyNodesCount(long companyId) throws SystemException {
228 return wikiNodePersistence.countByCompanyId(companyId);
229 }
230
231 public WikiNode getNode(long nodeId)
232 throws PortalException, SystemException {
233
234 return wikiNodePersistence.findByPrimaryKey(nodeId);
235 }
236
237 public WikiNode getNode(long groupId, String nodeName)
238 throws PortalException, SystemException {
239
240 return wikiNodePersistence.findByG_N(groupId, nodeName);
241 }
242
243 public List<WikiNode> getNodes(long groupId)
244 throws PortalException, SystemException {
245
246 List<WikiNode> nodes = wikiNodePersistence.findByGroupId(groupId);
247
248 if (nodes.isEmpty()) {
249 nodes = addDefaultNode(groupId);
250 }
251
252 return nodes;
253 }
254
255 public List<WikiNode> getNodes(long groupId, int start, int end)
256 throws PortalException, SystemException {
257
258 List<WikiNode> nodes = wikiNodePersistence.findByGroupId(
259 groupId, start, end);
260
261 if (nodes.isEmpty()) {
262 nodes = addDefaultNode(groupId);
263 }
264
265 return nodes;
266 }
267
268 public int getNodesCount(long groupId) throws SystemException {
269 return wikiNodePersistence.countByGroupId(groupId);
270 }
271
272 public void importPages(
273 long userId, long nodeId, String importer, File[] files,
274 Map<String, String[]> options)
275 throws PortalException, SystemException {
276
277 WikiNode node = getNode(nodeId);
278
279 WikiImporter wikiImporter = getWikiImporter(importer);
280
281 wikiImporter.importPages(userId, node, files, options);
282 }
283
284 public void subscribeNode(long userId, long nodeId)
285 throws PortalException, SystemException {
286
287 subscriptionLocalService.addSubscription(
288 userId, 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.setAddCommunityPermissions(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 groupId, String name)
365 throws PortalException, SystemException {
366
367 validate(0, groupId, name);
368 }
369
370 protected void validate(long nodeId, long groupId, String name)
371 throws PortalException, SystemException {
372
373 if (name.equalsIgnoreCase("tag")) {
374 throw new NodeNameException(name + " is reserved");
375 }
376
377 if (!Validator.isVariableName(name)) {
378 throw new NodeNameException();
379 }
380
381 WikiNode node = wikiNodePersistence.fetchByG_N(groupId, name);
382
383 if ((node != null) && (node.getNodeId() != nodeId)) {
384 throw new DuplicateNodeNameException();
385 }
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 }