001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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    /**
051     * @author Brian Wing Shun Chan
052     * @author Charles May
053     * @author Raymond Augé
054     */
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                    // Node
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                    // Resources
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                    // Indexer
181    
182                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
183                            WikiPage.class);
184    
185                    indexer.delete(node);
186    
187                    // Subscriptions
188    
189                    subscriptionLocalService.deleteSubscriptions(
190                            node.getCompanyId(), WikiNode.class.getName(), node.getNodeId());
191    
192                    // Pages
193    
194                    wikiPageLocalService.deletePages(node.getNodeId());
195    
196                    // Resources
197    
198                    resourceLocalService.deleteResource(
199                            node.getCompanyId(), WikiNode.class.getName(),
200                            ResourceConstants.SCOPE_INDIVIDUAL, node.getNodeId());
201    
202                    // Node
203    
204                    wikiNodePersistence.remove(node);
205            }
206    
207            public void deleteNodes(long groupId)
208                    throws PortalException, SystemException {
209    
210                    Iterator<WikiNode> itr = wikiNodePersistence.findByGroupId(
211                            groupId).iterator();
212    
213                    while (itr.hasNext()) {
214                            WikiNode node = itr.next();
215    
216                            deleteNode(node);
217                    }
218            }
219    
220            public List<WikiNode> getCompanyNodes(long companyId, int start, int end)
221                    throws SystemException {
222    
223                    return wikiNodePersistence.findByCompanyId(companyId, start, end);
224            }
225    
226            public int getCompanyNodesCount(long companyId) throws SystemException {
227                    return wikiNodePersistence.countByCompanyId(companyId);
228            }
229    
230            public WikiNode getNode(long nodeId)
231                    throws PortalException, SystemException {
232    
233                    return wikiNodePersistence.findByPrimaryKey(nodeId);
234            }
235    
236            public WikiNode getNode(long groupId, String nodeName)
237                    throws PortalException, SystemException {
238    
239                    return wikiNodePersistence.findByG_N(groupId, nodeName);
240            }
241    
242            public List<WikiNode> getNodes(long groupId)
243                    throws PortalException, SystemException {
244    
245                    List<WikiNode> nodes = wikiNodePersistence.findByGroupId(groupId);
246    
247                    if (nodes.isEmpty()) {
248                            nodes = addDefaultNode(groupId);
249                    }
250    
251                    return nodes;
252            }
253    
254            public List<WikiNode> getNodes(long groupId, int start, int end)
255                    throws PortalException, SystemException {
256    
257                    List<WikiNode> nodes = wikiNodePersistence.findByGroupId(
258                            groupId, start, end);
259    
260                    if (nodes.isEmpty()) {
261                            nodes = addDefaultNode(groupId);
262                    }
263    
264                    return nodes;
265            }
266    
267            public int getNodesCount(long groupId) throws SystemException {
268                    return wikiNodePersistence.countByGroupId(groupId);
269            }
270    
271            public void importPages(
272                            long userId, long nodeId, String importer,
273                            InputStream[] inputStreams, Map<String, String[]> options)
274                    throws PortalException, SystemException {
275    
276                    WikiNode node = getNode(nodeId);
277    
278                    WikiImporter wikiImporter = getWikiImporter(importer);
279    
280                    wikiImporter.importPages(userId, node, inputStreams, options);
281            }
282    
283            public void subscribeNode(long userId, long nodeId)
284                    throws PortalException, SystemException {
285    
286                    WikiNode node = getNode(nodeId);
287    
288                    subscriptionLocalService.addSubscription(
289                            userId, node.getGroupId(), WikiNode.class.getName(), nodeId);
290            }
291    
292            public void unsubscribeNode(long userId, long nodeId)
293                    throws PortalException, SystemException {
294    
295                    subscriptionLocalService.deleteSubscription(
296                            userId, WikiNode.class.getName(), nodeId);
297            }
298    
299            public WikiNode updateNode(
300                            long nodeId, String name, String description,
301                            ServiceContext serviceContext)
302                    throws PortalException, SystemException {
303    
304                    WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
305    
306                    validate(nodeId, node.getGroupId(), name);
307    
308                    node.setModifiedDate(serviceContext.getModifiedDate(null));
309                    node.setName(name);
310                    node.setDescription(description);
311    
312                    wikiNodePersistence.update(node, false);
313    
314                    return node;
315            }
316    
317            protected List<WikiNode> addDefaultNode(long groupId)
318                    throws PortalException, SystemException {
319    
320                    Group group = groupPersistence.findByPrimaryKey(groupId);
321    
322                    long defaultUserId = userLocalService.getDefaultUserId(
323                            group.getCompanyId());
324    
325                    ServiceContext serviceContext = new ServiceContext();
326    
327                    serviceContext.setAddGroupPermissions(true);
328                    serviceContext.setAddGuestPermissions(true);
329                    serviceContext.setScopeGroupId(groupId);
330    
331                    WikiNode node = addDefaultNode(defaultUserId, serviceContext);
332    
333                    List<WikiNode> nodes = new ArrayList<WikiNode>(1);
334    
335                    nodes.add(node);
336    
337                    return nodes;
338            }
339    
340            protected WikiImporter getWikiImporter(String importer)
341                    throws SystemException {
342    
343                    WikiImporter wikiImporter = _wikiImporters.get(importer);
344    
345                    if (wikiImporter == null) {
346                            String importerClass = PropsUtil.get(
347                                    PropsKeys.WIKI_IMPORTERS_CLASS, new Filter(importer));
348    
349                            if (importerClass != null) {
350                                    wikiImporter = (WikiImporter)InstancePool.get(importerClass);
351    
352                                    _wikiImporters.put(importer, wikiImporter);
353                            }
354    
355                            if (importer == null) {
356                                    throw new SystemException(
357                                            "Unable to instantiate wiki importer class " +
358                                                    importerClass);
359                            }
360                    }
361    
362                    return wikiImporter;
363            }
364    
365            protected void validate(long nodeId, long groupId, String name)
366                    throws PortalException, SystemException {
367    
368                    if (name.equalsIgnoreCase("tag")) {
369                            throw new NodeNameException(name + " is reserved");
370                    }
371    
372                    if (Validator.isNull(name)) {
373                            throw new NodeNameException();
374                    }
375    
376                    WikiNode node = wikiNodePersistence.fetchByG_N(groupId, name);
377    
378                    if ((node != null) && (node.getNodeId() != nodeId)) {
379                            throw new DuplicateNodeNameException();
380                    }
381            }
382    
383            protected void validate(long groupId, String name)
384                    throws PortalException, SystemException {
385    
386                    validate(0, groupId, name);
387            }
388    
389            private static Log _log = LogFactoryUtil.getLog(
390                    WikiNodeLocalServiceImpl.class);
391    
392            private Map<String, WikiImporter> _wikiImporters =
393                    new HashMap<String, WikiImporter>();
394    
395    }