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

File AbstractXWikiPreferencesConfigurationSource.java

 

Coverage histogram

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

Code metrics

26
43
8
1
171
101
26
0.6
5.38
8
3.25

Classes

Class Line # Actions
AbstractXWikiPreferencesConfigurationSource 38 43 0% 26 13
0.8311688383.1%
 

Contributing tests

This file is covered by 7 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.configuration.internal;
21   
22    import org.xwiki.model.reference.DocumentReference;
23    import org.xwiki.model.reference.LocalDocumentReference;
24    import org.xwiki.model.reference.SpaceReference;
25   
26    import com.xpn.xwiki.XWikiContext;
27    import com.xpn.xwiki.XWikiException;
28    import com.xpn.xwiki.doc.XWikiDocument;
29    import com.xpn.xwiki.objects.BaseObject;
30    import com.xpn.xwiki.objects.BaseProperty;
31   
32    /**
33    * Base class for XWiki.XWikiPreferences xclass based configuration.
34    *
35    * @version $Id: c20cfd16de2461091e4ef3901a176ef4e0901da2 $
36    * @since 6.3M1
37    */
 
38    public abstract class AbstractXWikiPreferencesConfigurationSource extends AbstractDocumentConfigurationSource
39    {
40    /**
41    * The name of the space where wiki preferences are located.
42    */
43    protected static final String CLASS_SPACE_NAME = "XWiki";
44   
45    protected static final String CLASS_PAGE_NAME = "XWikiPreferences";
46   
47    /**
48    * The local reference of the class containing wiki preferences.
49    */
50    protected static final LocalDocumentReference CLASS_REFERENCE = new LocalDocumentReference(CLASS_SPACE_NAME,
51    CLASS_PAGE_NAME);
52   
 
53  0 toggle @Override
54    protected String getCacheKeyPrefix()
55    {
56  0 DocumentReference currentDocumentReference = getDocumentReference();
57  0 if (currentDocumentReference != null) {
58  0 return this.referenceSerializer.serialize(currentDocumentReference.getParent());
59    }
60   
61  0 return null;
62    }
63   
 
64  52127 toggle @Override
65    protected LocalDocumentReference getClassReference()
66    {
67  52132 return CLASS_REFERENCE;
68    }
69   
 
70  0 toggle @Override
71    protected DocumentReference getDocumentReference()
72    {
73  0 return new DocumentReference(CLASS_REFERENCE.getName(), new SpaceReference(CLASS_SPACE_NAME,
74    getCurrentWikiReference()));
75    }
76   
 
77  26065 toggle protected BaseObject getBaseObject(String language) throws XWikiException
78    {
79  26062 XWikiContext xcontext = this.xcontextProvider.get();
80   
81  26073 if (xcontext != null && xcontext.getWiki() != null) {
82  26071 DocumentReference documentReference = getFailsafeDocumentReference();
83  26075 LocalDocumentReference classReference = getFailsafeClassReference();
84   
85  26071 if (documentReference != null && classReference != null) {
86  25907 XWikiDocument document = xcontext.getWiki().getDocument(getDocumentReference(), xcontext);
87   
88  25913 return getBaseObject(document, language);
89    }
90    }
91   
92  162 return null;
93    }
94   
 
95  25915 toggle protected BaseObject getBaseObject(XWikiDocument document, String language)
96    {
97  25915 if (language != null) {
98  12668 BaseObject object = document.getXObject(getClassReference(), "default_language", language, true);
99   
100  12666 if (object != null) {
101  1174 return object;
102    }
103    } else {
104  13246 return document.getXObject(getClassReference());
105    }
106   
107  11495 return null;
108    }
109   
 
110  3 toggle @Override
111    protected BaseObject getBaseObject() throws XWikiException
112    {
113  3 XWikiContext xcontext = this.xcontextProvider.get();
114   
115  3 if (xcontext != null && xcontext.getWiki() != null) {
116  3 DocumentReference documentReference = getFailsafeDocumentReference();
117  3 LocalDocumentReference classReference = getFailsafeClassReference();
118   
119  3 if (documentReference != null && classReference != null) {
120  3 XWikiDocument document = xcontext.getWiki().getDocument(getDocumentReference(), xcontext);
121   
122    // First we try to get a translated preference object
123  3 BaseObject object = getBaseObject(document, xcontext.getLanguage());
124   
125  3 if (object != null) {
126  2 return object;
127    }
128   
129  1 return document.getXObject(classReference);
130    }
131    }
132   
133  0 return null;
134    }
135   
 
136  26064 toggle protected Object getBaseProperty(String propertyName, String language, boolean text) throws XWikiException
137    {
138    // First we try to get a translated preference object
139  26063 BaseObject baseObject = getBaseObject(language);
140   
141  26072 if (baseObject != null) {
142  2437 BaseProperty property = (BaseProperty) baseObject.getField(propertyName);
143   
144  403 return property != null ? (text ? property.toText() : property.getValue()) : null;
145    }
146   
147  23634 return null;
148    }
149   
 
150  13051 toggle @Override
151    protected Object getBaseProperty(String propertyName, boolean text) throws XWikiException
152    {
153  13047 XWikiContext xcontext = this.xcontextProvider.get();
154   
155    // First we try to get a translated preference object
156  13054 Object propertyValue = getBaseProperty(propertyName, xcontext.getLanguage(), text);
157   
158    // If empty we take it from the default pref object
159  13050 if (propertyValue == null || isEmpty(propertyValue)) {
160  13013 propertyValue = getBaseProperty(propertyName, null, text);
161    }
162   
163    // TODO: In the future we would need the notion of initialized/not-initialized property values in the wiki.
164    // When this is implemented modify the code below.
165  13054 if (isEmpty(propertyValue)) {
166  13019 propertyValue = null;
167    }
168   
169  13054 return propertyValue;
170    }
171    }