1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.model.internal

File DefaultModelContext.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
41% 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 6
0.837837883.8%
 

Contributing tests

This file is covered by 59 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  4488834 toggle @Override
63    public EntityReference getCurrentEntityReference()
64    {
65  4488863 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  4488878 ExecutionContext econtext = this.execution.getContext();
70   
71  4488887 if (econtext != null) {
72  4488853 Map<String, Object> xcontext = (Map<String, Object>) econtext.getProperty(XCONTEXT_KEY);
73   
74  4488864 if (xcontext != null) {
75  4488878 String wikiName = (String) xcontext.get(WIKINAME_KEY);
76   
77  4488856 if (wikiName != null) {
78  4488858 result = new WikiReference(wikiName);
79    }
80    }
81    }
82   
83  4488871 return result;
84    }
85   
 
86  423420 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  423495 ExecutionContext econtext = this.execution.getContext();
92   
93  423634 if (econtext != null) {
94  423615 Map<String, Object> xcontext = (Map<String, Object>) econtext.getProperty(XCONTEXT_KEY);
95   
96  423628 if (xcontext != null) {
97  423618 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  423603 toggle private String extractWikiName(EntityReference entityReference)
109    {
110  423618 String wikiName = null;
111  423610 if (entityReference != null) {
112  423619 EntityReference wikiReference = entityReference.extractReference(EntityType.WIKI);
113  423562 if (wikiReference != null) {
114  423588 wikiName = wikiReference.getName();
115    }
116    }
117   
118  423572 return wikiName;
119    }
120    }