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

File DefaultModelContext.java

 

Coverage histogram

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

Code metrics

14
20
3
1
120
58
10
0.5
6.67
3
3.33

Classes

Class Line # Actions
DefaultModelContext 43 20 0% 10 4
0.891891989.2%
 

Contributing tests

This file is covered by 57 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.model.internal;
21   
22    import java.util.Map;
23   
24    import javax.inject.Inject;
25    import javax.inject.Singleton;
26   
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.context.Execution;
29    import org.xwiki.context.ExecutionContext;
30    import org.xwiki.model.EntityType;
31    import org.xwiki.model.ModelContext;
32    import org.xwiki.model.reference.EntityReference;
33    import org.xwiki.model.reference.WikiReference;
34   
35    /**
36    * Default implementation bridging to the old XWiki Context to get current Model Reference Objects.
37    *
38    * @version $Id: 8627074601c05619b7fa3229ff922b3eb76eecfd $
39    * @since 2.2M1
40    */
41    @Component
42    @Singleton
 
43    public class DefaultModelContext implements ModelContext
44    {
45    /**
46    * Key of the XWikiContext located in the {@link ExecutionContext}.
47    */
48    public static final String XCONTEXT_KEY = "xwikicontext";
49   
50    /**
51    * Key of the wiki name located in the XWikiContext.
52    */
53    private static final String WIKINAME_KEY = "wiki";
54   
55    /**
56    * The Execution Context from which we get the old XWiki Context from which we get the current Model Reference
57    * Objects.
58    */
59    @Inject
60    private Execution execution;
61   
 
62  263096 toggle @Override
63    public EntityReference getCurrentEntityReference()
64    {
65  263096 WikiReference result = null;
66   
67    // TODO: This is bridge to the old XWiki Context since we currently don't store the current entity in the
68    // new Execution Context yet. Remove when we do so.
69  263100 ExecutionContext econtext = this.execution.getContext();
70   
71  263103 if (econtext != null) {
72  263083 Map<String, Object> xcontext = (Map<String, Object>) econtext.getProperty(XCONTEXT_KEY);
73   
74  263082 if (xcontext != null) {
75  263086 String wikiName = (String) xcontext.get(WIKINAME_KEY);
76   
77  263089 if (wikiName != null) {
78  263081 result = new WikiReference(wikiName);
79    }
80    }
81    }
82   
83  263111 return result;
84    }
85   
 
86  32337 toggle @Override
87    public void setCurrentEntityReference(EntityReference entityReference)
88    {
89    // TODO: This is bridge to the old XWiki Context since we currently don't store the current entity in the
90    // new Execution Context yet. Remove when we do so.
91  32339 ExecutionContext econtext = this.execution.getContext();
92   
93  32341 if (econtext != null) {
94  32341 Map<String, Object> xcontext = (Map<String, Object>) econtext.getProperty(XCONTEXT_KEY);
95   
96  32345 if (xcontext != null) {
97  32341 xcontext.put(WIKINAME_KEY, extractWikiName(entityReference));
98    }
99    }
100    }
101   
102    /**
103    * Extract wiki name from provided entity reference.
104    *
105    * @param entityReference the entity reference
106    * @return the wiki name, null if no wiki name can be found.
107    */
 
108  32338 toggle private String extractWikiName(EntityReference entityReference)
109    {
110  32340 String wikiName = null;
111  32341 if (entityReference != null) {
112  32333 EntityReference wikiReference = entityReference.extractReference(EntityType.WIKI);
113  32337 if (wikiReference != null) {
114  32336 wikiName = wikiReference.getName();
115    }
116    }
117   
118  32343 return wikiName;
119    }
120    }