1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.wiki.service.impl;
16  
17  import com.liferay.portal.kernel.configuration.Filter;
18  import com.liferay.portal.kernel.exception.PortalException;
19  import com.liferay.portal.kernel.exception.SystemException;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.search.Indexer;
23  import com.liferay.portal.kernel.search.IndexerRegistryUtil;
24  import com.liferay.portal.kernel.util.InstancePool;
25  import com.liferay.portal.kernel.util.PropsKeys;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.ResourceConstants;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.service.ServiceContext;
30  import com.liferay.portal.util.PropsUtil;
31  import com.liferay.portlet.wiki.DuplicateNodeNameException;
32  import com.liferay.portlet.wiki.NodeNameException;
33  import com.liferay.portlet.wiki.importers.WikiImporter;
34  import com.liferay.portlet.wiki.model.WikiNode;
35  import com.liferay.portlet.wiki.model.WikiPage;
36  import com.liferay.portlet.wiki.service.base.WikiNodeLocalServiceBaseImpl;
37  
38  import java.io.File;
39  
40  import java.util.Date;
41  import java.util.HashMap;
42  import java.util.Iterator;
43  import java.util.List;
44  import java.util.Map;
45  
46  /**
47   * <a href="WikiNodeLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Charles May
51   * @author Raymond Augé
52   */
53  public class WikiNodeLocalServiceImpl extends WikiNodeLocalServiceBaseImpl {
54  
55      public WikiNode addNode(
56              long userId, String name, String description,
57              ServiceContext serviceContext)
58          throws PortalException, SystemException {
59  
60          return addNode(null, userId, name, description, serviceContext);
61      }
62  
63      public WikiNode addNode(
64              String uuid, long userId, String name, String description,
65              ServiceContext serviceContext)
66          throws PortalException, SystemException {
67  
68          // Node
69  
70          User user = userPersistence.findByPrimaryKey(userId);
71          long groupId = serviceContext.getScopeGroupId();
72          Date now = new Date();
73  
74          validate(groupId, name);
75  
76          long nodeId = counterLocalService.increment();
77  
78          WikiNode node = wikiNodePersistence.create(nodeId);
79  
80          node.setUuid(uuid);
81          node.setGroupId(groupId);
82          node.setCompanyId(user.getCompanyId());
83          node.setUserId(user.getUserId());
84          node.setUserName(user.getFullName());
85          node.setCreateDate(now);
86          node.setModifiedDate(now);
87          node.setName(name);
88          node.setDescription(description);
89  
90          try {
91              wikiNodePersistence.update(node, false);
92          }
93          catch (SystemException se) {
94              if (_log.isWarnEnabled()) {
95                  _log.warn(
96                      "Add failed, fetch {groupId=" + groupId + ", name=" +
97                          name + "}");
98              }
99  
100             node = wikiNodePersistence.fetchByG_N(groupId, name, false);
101 
102             if (node == null) {
103                 throw se;
104             }
105 
106             return node;
107         }
108 
109         // Resources
110 
111         if (serviceContext.getAddCommunityPermissions() ||
112             serviceContext.getAddGuestPermissions()) {
113 
114             addNodeResources(
115                 node, serviceContext.getAddCommunityPermissions(),
116                 serviceContext.getAddGuestPermissions());
117         }
118         else {
119             addNodeResources(
120                 node, serviceContext.getCommunityPermissions(),
121                 serviceContext.getGuestPermissions());
122         }
123 
124         return node;
125     }
126 
127     public void addNodeResources(
128             long nodeId, boolean addCommunityPermissions,
129             boolean addGuestPermissions)
130         throws PortalException, SystemException {
131 
132         WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
133 
134         addNodeResources(node, addCommunityPermissions, addGuestPermissions);
135     }
136 
137     public void addNodeResources(
138             WikiNode node, boolean addCommunityPermissions,
139             boolean addGuestPermissions)
140         throws PortalException, SystemException {
141 
142         resourceLocalService.addResources(
143             node.getCompanyId(), node.getGroupId(), node.getUserId(),
144             WikiNode.class.getName(), node.getNodeId(), false,
145             addCommunityPermissions, addGuestPermissions);
146     }
147 
148     public void addNodeResources(
149             long nodeId, String[] communityPermissions,
150             String[] guestPermissions)
151         throws PortalException, SystemException {
152 
153         WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
154 
155         addNodeResources(node, communityPermissions, guestPermissions);
156     }
157 
158     public void addNodeResources(
159             WikiNode node, String[] communityPermissions,
160             String[] guestPermissions)
161         throws PortalException, SystemException {
162 
163         resourceLocalService.addModelResources(
164             node.getCompanyId(), node.getGroupId(), node.getUserId(),
165             WikiNode.class.getName(), node.getNodeId(), communityPermissions,
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         // Indexer
181 
182         Indexer indexer = IndexerRegistryUtil.getIndexer(WikiPage.class);
183 
184         indexer.delete(node);
185 
186         // Subscriptions
187 
188         subscriptionLocalService.deleteSubscriptions(
189             node.getCompanyId(), WikiNode.class.getName(), node.getNodeId());
190 
191         // Pages
192 
193         wikiPageLocalService.deletePages(node.getNodeId());
194 
195         // Resources
196 
197         resourceLocalService.deleteResource(
198             node.getCompanyId(), WikiNode.class.getName(),
199             ResourceConstants.SCOPE_INDIVIDUAL, node.getNodeId());
200 
201         // Node
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) throws SystemException {
242         return wikiNodePersistence.findByGroupId(groupId);
243     }
244 
245     public List<WikiNode> getNodes(long groupId, int start, int end)
246         throws SystemException {
247 
248         return wikiNodePersistence.findByGroupId(groupId, start, end);
249     }
250 
251     public int getNodesCount(long groupId) throws SystemException {
252         return wikiNodePersistence.countByGroupId(groupId);
253     }
254 
255     public void importPages(
256             long userId, long nodeId, String importer, File[] files,
257             Map<String, String[]> options)
258         throws PortalException, SystemException {
259 
260         WikiNode node = getNode(nodeId);
261 
262         getWikiImporter(importer).importPages(userId, node, files, options);
263     }
264 
265     public void subscribeNode(long userId, long nodeId)
266         throws PortalException, SystemException {
267 
268         subscriptionLocalService.addSubscription(
269             userId, WikiNode.class.getName(), nodeId);
270     }
271 
272     public void unsubscribeNode(long userId, long nodeId)
273         throws PortalException, SystemException {
274 
275         subscriptionLocalService.deleteSubscription(
276             userId, WikiNode.class.getName(), nodeId);
277     }
278 
279     public WikiNode updateNode(long nodeId, String name, String description)
280         throws PortalException, SystemException {
281 
282         WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
283 
284         validate(nodeId, node.getGroupId(), name);
285 
286         node.setModifiedDate(new Date());
287         node.setName(name);
288         node.setDescription(description);
289 
290         wikiNodePersistence.update(node, false);
291 
292         return node;
293     }
294 
295     protected WikiImporter getWikiImporter(String importer)
296         throws SystemException {
297 
298         WikiImporter wikiImporter = _wikiImporters.get(importer);
299 
300         if (wikiImporter == null) {
301             String importerClass = PropsUtil.get(
302                 PropsKeys.WIKI_IMPORTERS_CLASS, new Filter(importer));
303 
304             if (importerClass != null) {
305                 wikiImporter = (WikiImporter)InstancePool.get(importerClass);
306 
307                 _wikiImporters.put(importer, wikiImporter);
308             }
309 
310             if (importer == null) {
311                 throw new SystemException(
312                     "Unable to instantiate wiki importer class " +
313                         importerClass);
314             }
315         }
316 
317         return wikiImporter;
318     }
319 
320     protected void validate(long groupId, String name)
321         throws PortalException, SystemException {
322 
323         validate(0, groupId, name);
324     }
325 
326     protected void validate(long nodeId, long groupId, String name)
327         throws PortalException, SystemException {
328 
329         if (name.equalsIgnoreCase("tag")) {
330             throw new NodeNameException(name + " is reserved");
331         }
332 
333         if (!Validator.isName(name)) {
334             throw new NodeNameException();
335         }
336 
337         WikiNode node = wikiNodePersistence.fetchByG_N(groupId, name);
338 
339         if ((node != null) && (node.getNodeId() != nodeId)) {
340             throw new DuplicateNodeNameException();
341         }
342     }
343 
344     private static Log _log = LogFactoryUtil.getLog(
345         WikiNodeLocalServiceImpl.class);
346 
347     private Map<String, WikiImporter> _wikiImporters =
348         new HashMap<String, WikiImporter>();
349 
350 }