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

File WikiTemplatePropertyGroupProvider.java

 

Coverage histogram

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

Code metrics

8
22
2
1
116
71
9
0.41
11
2
4.5

Classes

Class Line # Actions
WikiTemplatePropertyGroupProvider 50 22 0% 9 6
0.812581.2%
 

Contributing tests

This file is covered by 3 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.template.internal;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Provider;
25    import javax.inject.Singleton;
26   
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.wiki.internal.descriptor.document.WikiDescriptorDocumentHelper;
29    import org.xwiki.wiki.manager.WikiManagerException;
30    import org.xwiki.wiki.properties.WikiPropertyGroup;
31    import org.xwiki.wiki.properties.WikiPropertyGroupException;
32    import org.xwiki.wiki.properties.WikiPropertyGroupProvider;
33    import org.xwiki.wiki.template.WikiTemplatePropertyGroup;
34   
35    import com.xpn.xwiki.XWiki;
36    import com.xpn.xwiki.XWikiContext;
37    import com.xpn.xwiki.XWikiException;
38    import com.xpn.xwiki.doc.XWikiDocument;
39    import com.xpn.xwiki.objects.BaseObject;
40   
41    /**
42    * Implementation of {@link WikiPropertyGroupProvider} for the property groups concerning the templates.
43    *
44    * @since 5.3M2
45    * @version $Id: 06e266f2f47537eac299efba9a1d3b913ebdc696 $
46    */
47    @Component
48    @Named(WikiTemplatePropertyGroupProvider.GROUP_NAME)
49    @Singleton
 
50    public class WikiTemplatePropertyGroupProvider implements WikiPropertyGroupProvider
51    {
52    /**
53    * Name of the property group.
54    */
55    public static final String GROUP_NAME = "template";
56   
57    private static final String ERROR_MESSAGE_NO_DESCRIPTOR_DOCUMENT = "Unable to load descriptor "
58    + "document for wiki [%s].";
59   
60    @Inject
61    private Provider<XWikiContext> xcontextProvider;
62   
63    @Inject
64    private WikiDescriptorDocumentHelper wikiDescriptorDocumentHelper;
65   
 
66  20 toggle @Override
67    public WikiPropertyGroup get(String wikiId) throws WikiPropertyGroupException
68    {
69  20 WikiTemplatePropertyGroup group = new WikiTemplatePropertyGroup(GROUP_NAME);
70   
71  20 try {
72  20 XWikiDocument descriptorDocument = wikiDescriptorDocumentHelper.getDocumentFromWikiId(wikiId);
73    // Get the object
74  19 BaseObject object = descriptorDocument.getXObject(WikiTemplateClassDocumentInitializer.SERVER_CLASS);
75  19 if (object != null) {
76  9 group.setTemplate(
77    object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, 0) != 0);
78    }
79    } catch (WikiManagerException e) {
80  1 throw new WikiPropertyGroupException(String.format(ERROR_MESSAGE_NO_DESCRIPTOR_DOCUMENT, wikiId), e);
81    }
82   
83  19 return group;
84    }
85   
 
86  7 toggle @Override
87    public void save(WikiPropertyGroup group, String wikiId) throws WikiPropertyGroupException
88    {
89  7 XWikiContext context = xcontextProvider.get();
90  7 XWiki xwiki = context.getWiki();
91   
92  7 WikiTemplatePropertyGroup templateGroup = (WikiTemplatePropertyGroup) group;
93   
94  7 try {
95  7 XWikiDocument descriptorDocument = wikiDescriptorDocumentHelper.getDocumentFromWikiId(wikiId);
96  7 BaseObject object = descriptorDocument.getXObject(WikiTemplateClassDocumentInitializer.SERVER_CLASS,
97    true, context);
98  7 object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE,
99  7 templateGroup.isTemplate() ? 1 : 0);
100    // The document must have a creator
101  7 if (descriptorDocument.getCreatorReference() == null) {
102  0 descriptorDocument.setCreatorReference(context.getUserReference());
103    }
104    // The document must have an author
105  7 if (descriptorDocument.getAuthorReference() == null) {
106  0 descriptorDocument.setAuthorReference(context.getUserReference());
107    }
108  7 xwiki.saveDocument(descriptorDocument, String.format("Changed property group [%s].", GROUP_NAME), context);
109    } catch (WikiManagerException e) {
110  0 throw new WikiPropertyGroupException(String.format(ERROR_MESSAGE_NO_DESCRIPTOR_DOCUMENT, wikiId), e);
111    } catch (XWikiException e) {
112  0 throw new WikiPropertyGroupException("Unable to save descriptor document.", e);
113    }
114    }
115   
116    }