1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wiki.internal.manager

File DefaultWikiManager.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

4
26
5
1
147
91
10
0.38
5.2
5
2

Classes

Class Line # Actions
DefaultWikiManager 51 26 0% 10 4
0.885714388.6%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.wiki.internal.manager;
21   
22    import javax.inject.Inject;
23    import javax.inject.Provider;
24    import javax.inject.Singleton;
25   
26    import org.xwiki.bridge.event.WikiCopiedEvent;
27    import org.xwiki.bridge.event.WikiCreateFailedEvent;
28    import org.xwiki.bridge.event.WikiCreatedEvent;
29    import org.xwiki.bridge.event.WikiCreatingEvent;
30    import org.xwiki.bridge.event.WikiDeletedEvent;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.observation.ObservationManager;
33    import org.xwiki.wiki.descriptor.WikiDescriptor;
34    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
35    import org.xwiki.wiki.manager.WikiManager;
36    import org.xwiki.wiki.manager.WikiManagerException;
37    import org.xwiki.wiki.provisioning.WikiCopier;
38   
39    import com.xpn.xwiki.XWiki;
40    import com.xpn.xwiki.XWikiContext;
41    import com.xpn.xwiki.XWikiException;
42    import com.xpn.xwiki.util.Util;
43   
44    /**
45    * Default implementation for {@link WikiManager}.
46    * @version $Id: b1fe283d98a162383c7f55d226f7d16f47baa556 $
47    * @since 5.3M2
48    */
49    @Component
50    @Singleton
 
51    public class DefaultWikiManager implements WikiManager
52    {
53    @Inject
54    private Provider<XWikiContext> xcontextProvider;
55   
56    @Inject
57    private WikiDescriptorManager wikiDescriptorManager;
58   
59    @Inject
60    private ObservationManager observationManager;
61   
62    @Inject
63    private WikiCreator wikiCreator;
64   
65    @Inject
66    private WikiCopier wikiCopier;
67   
68    @Inject
69    private WikiDeleter wikiDeleter;
70   
 
71  10 toggle @Override
72    public WikiDescriptor create(String wikiId, String wikiAlias, boolean failOnExist) throws WikiManagerException
73    {
74    // Check that the wiki Id is available
75  10 if (failOnExist && !idAvailable(wikiId)) {
76  3 throw new WikiManagerException(String.format("wiki id [%s] is already used and is thus not available",
77    wikiId));
78    }
79   
80  7 XWikiContext context = xcontextProvider.get();
81  7 WikiDescriptor descriptor;
82   
83  7 try {
84    // Send the begin event
85  7 observationManager.notify(new WikiCreatingEvent(wikiId), wikiId, context);
86   
87    // Create the wiki
88  7 descriptor = wikiCreator.create(wikiId, wikiAlias);
89   
90    // Send the end event
91  6 observationManager.notify(new WikiCreatedEvent(wikiId), wikiId, context);
92   
93    } catch (WikiManagerException e) {
94    // Send the failed event
95  1 observationManager.notify(new WikiCreateFailedEvent(wikiId), wikiId, context);
96   
97    // Throw the exception
98  1 throw e;
99    }
100   
101  6 return descriptor;
102    }
103   
 
104  2 toggle @Override
105    public WikiDescriptor copy(String fromWikiId, String newWikiId, String newWikiAlias, boolean copyHistory,
106    boolean copyRecycleBin, boolean failOnExist) throws WikiManagerException
107    {
108  2 WikiDescriptor newWiki = create(newWikiId, newWikiAlias, failOnExist);
109  1 wikiCopier.copyDocuments(fromWikiId, newWikiId, copyHistory);
110  1 if (copyRecycleBin) {
111  1 wikiCopier.copyDeletedDocuments(fromWikiId, newWikiId);
112    }
113  1 observationManager.notify(new WikiCopiedEvent(fromWikiId, newWikiId), fromWikiId, xcontextProvider.get());
114  1 return newWiki;
115    }
116   
 
117  0 toggle @Override
118    public WikiDescriptor rename(String wikiId, String newWikiId) throws WikiManagerException
119    {
120  0 throw new WikiManagerException("This method is not implemented yet");
121    }
122   
 
123  4 toggle @Override
124    public void delete(String wikiId) throws WikiManagerException
125    {
126    // Delete the wiki
127  4 wikiDeleter.delete(wikiId);
128   
129    // Send an event
130  4 observationManager.notify(new WikiDeletedEvent(wikiId), wikiId);
131    }
132   
 
133  44 toggle @Override
134    public boolean idAvailable(String wikiId) throws WikiManagerException {
135    // Get the store
136  44 XWikiContext xcontext = xcontextProvider.get();
137  44 XWiki xwiki = xcontext.getWiki();
138    // Get the forbidden list
139  43 String wikiForbiddenList = xcontextProvider.get().getWiki().Param("xwiki.virtual.reserved_wikis");
140  44 try {
141  44 return !wikiDescriptorManager.exists(wikiId) && !Util.contains(wikiId, wikiForbiddenList, ", ")
142    && xwiki.getStore().isWikiNameAvailable(wikiId, xcontext);
143    } catch (XWikiException e) {
144  0 throw new WikiManagerException("Fail to look at the databases.");
145    }
146    }
147    }