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

File DefaultWikiTemplateManager.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

2
35
6
1
184
124
15
0.43
5.83
6
2.5

Classes

Class Line # Actions
DefaultWikiTemplateManager 57 35 0% 15 24
0.4418604744.2%
 

Contributing tests

No tests hitting this source file were found.

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.template.internal;
21   
22    import java.util.ArrayList;
23    import java.util.Collection;
24    import java.util.List;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Provider;
29    import javax.inject.Singleton;
30   
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.query.Query;
33    import org.xwiki.query.QueryManager;
34    import org.xwiki.wiki.descriptor.WikiDescriptor;
35    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
36    import org.xwiki.wiki.manager.WikiManager;
37    import org.xwiki.wiki.manager.WikiManagerException;
38    import org.xwiki.wiki.properties.WikiPropertyGroupException;
39    import org.xwiki.wiki.properties.WikiPropertyGroupProvider;
40    import org.xwiki.wiki.provisioning.WikiProvisioningJob;
41    import org.xwiki.wiki.provisioning.WikiProvisioningJobException;
42    import org.xwiki.wiki.provisioning.WikiProvisioningJobExecutor;
43    import org.xwiki.wiki.template.WikiTemplateManager;
44    import org.xwiki.wiki.template.WikiTemplateManagerException;
45    import org.xwiki.wiki.template.WikiTemplatePropertyGroup;
46   
47    import com.xpn.xwiki.XWikiContext;
48   
49    /**
50    * Default implementation for {@link WikiTemplateManager}.
51    *
52    * @version $Id: a3d43ba0e669b4f4914bc483e62387df79fc3bd6 $
53    * @since 5.3M2
54    */
55    @Component
56    @Singleton
 
57    public class DefaultWikiTemplateManager implements WikiTemplateManager
58    {
59    /**
60    * Used to access current {@link XWikiContext}.
61    */
62    @Inject
63    private Provider<XWikiContext> xcontextProvider;
64   
65    @Inject
66    @Named("default")
67    private WikiManager wikiManager;
68   
69    @Inject
70    private WikiDescriptorManager wikiDescriptorManager;
71   
72    @Inject
73    private QueryManager queryManager;
74   
75    @Inject
76    private WikiProvisioningJobExecutor wikiProvisionerExecutor;
77   
78    @Inject
79    @Named("template")
80    private WikiPropertyGroupProvider templateWikiPropertyGroupProvider;
81   
82    private String errorMessageNoDescriptor = "Failed to get the descriptor for [%s].";
83   
 
84  4 toggle @Override
85    public Collection<WikiDescriptor> getTemplates() throws WikiTemplateManagerException
86    {
87  4 List<WikiDescriptor> result = new ArrayList<WikiDescriptor>();
88   
89  4 try {
90  4 Query query = this.queryManager.createQuery(
91    "from doc.object(WikiManager.WikiTemplateClass) as descriptor where doc.name like 'XWikiServer%' "
92    + "and descriptor.iswikitemplate = 1", Query.XWQL);
93  4 query.setWiki(xcontextProvider.get().getMainXWiki());
94  4 List<String> documentNames = query.execute();
95   
96  4 if (documentNames != null && !documentNames.isEmpty()) {
97  3 for (String documentName : documentNames) {
98  3 String id = documentName.substring("XWiki.XWikiServer".length()).toLowerCase();
99  3 result.add(wikiDescriptorManager.getById(id));
100    }
101    }
102    } catch (Exception e) {
103  0 throw new WikiTemplateManagerException("Failed to locate XWiki.XWikiServerClass documents", e);
104    }
105   
106  4 return result;
107    }
108   
 
109  3 toggle @Override
110    public void setTemplate(String wikiId, boolean value) throws WikiTemplateManagerException
111    {
112  3 try {
113    // Get the descriptor
114  3 WikiDescriptor descriptor = wikiDescriptorManager.getById(wikiId);
115    // Get the property group
116  3 WikiTemplatePropertyGroup group = (WikiTemplatePropertyGroup) descriptor.getPropertyGroup(
117    WikiTemplatePropertyGroupProvider.GROUP_NAME);
118    // Set the value
119  3 group.setTemplate(value);
120    // Save the property groups
121  3 templateWikiPropertyGroupProvider.save(group, wikiId);
122    } catch (WikiPropertyGroupException e) {
123  0 throw new WikiTemplateManagerException(String.format("Failed to save the property group [%s]",
124    WikiTemplatePropertyGroupProvider.GROUP_NAME), e);
125    } catch (WikiManagerException e) {
126  0 throw new WikiTemplateManagerException(String.format(errorMessageNoDescriptor, wikiId), e);
127    }
128    }
129   
 
130  0 toggle @Override
131    public boolean isTemplate(String wikiId) throws WikiTemplateManagerException
132    {
133  0 try {
134    // Get the descriptor
135  0 WikiDescriptor descriptor = wikiDescriptorManager.getById(wikiId);
136    // Get the property group
137  0 WikiTemplatePropertyGroup group = (WikiTemplatePropertyGroup) descriptor.getPropertyGroup(
138    WikiTemplatePropertyGroupProvider.GROUP_NAME);
139    // Return the value
140  0 return group.isTemplate();
141    } catch (WikiManagerException e) {
142  0 throw new WikiTemplateManagerException(String.format(errorMessageNoDescriptor, wikiId), e);
143    }
144    }
145   
 
146  0 toggle @Override
147    public WikiProvisioningJob createWikiFromTemplate(String newWikiId, String newWikiAlias, String templateId,
148    String ownerId, boolean failOnExist) throws WikiTemplateManagerException
149    {
150  0 try {
151    // First, create the new wiki
152  0 WikiDescriptor descriptor = wikiManager.create(newWikiId, newWikiAlias, failOnExist);
153   
154    // Set the owner
155  0 descriptor.setOwnerId(ownerId);
156  0 wikiDescriptorManager.saveDescriptor(descriptor);
157   
158    // finally, we apply the template to the new wiki
159  0 return applyTemplate(newWikiId, templateId);
160    } catch (WikiManagerException e) {
161  0 throw new WikiTemplateManagerException(e.getMessage(), e);
162    }
163    }
164   
 
165  0 toggle @Override
166    public WikiProvisioningJob applyTemplate(String wikiId, String templateId) throws WikiTemplateManagerException
167    {
168  0 try {
169  0 return wikiProvisionerExecutor.createAndExecuteJob(wikiId, TemplateWikiProvisioningJob.JOBTYPE, templateId);
170    } catch (WikiProvisioningJobException e) {
171  0 throw new WikiTemplateManagerException(e.getMessage(), e);
172    }
173    }
174   
 
175  0 toggle @Override
176    public WikiProvisioningJob getWikiProvisioningJob(List<String> jobId) throws WikiTemplateManagerException
177    {
178  0 try {
179  0 return wikiProvisionerExecutor.getJob(jobId);
180    } catch (WikiProvisioningJobException e) {
181  0 throw new WikiTemplateManagerException(e.getMessage(), e);
182    }
183    }
184    }