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

File CurrentGetDocumentReferenceDocumentReferenceResolver.java

 

Coverage histogram

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

Code metrics

6
12
1
1
96
52
5
0.42
12
1
5

Classes

Class Line # Actions
CurrentGetDocumentReferenceDocumentReferenceResolver 49 12 0% 5 2
0.894736889.5%
 

Contributing tests

This file is covered by 4 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 com.xpn.xwiki.internal.model.reference;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Singleton;
25   
26    import org.xwiki.component.annotation.Component;
27    import org.xwiki.model.EntityType;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.model.reference.DocumentReferenceResolver;
30    import org.xwiki.model.reference.EntityReference;
31    import org.xwiki.model.reference.EntityReferenceResolver;
32    import org.xwiki.wiki.descriptor.WikiDescriptor;
33    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
34   
35    /**
36    * Specialized version of {@link org.xwiki.model.reference.EntityReferenceResolver} which can be considered a helper
37    * component to resolve {@link org.xwiki.model.reference.DocumentReference} objects from Entity Reference (when they
38    * miss some parent references or have NULL values).
39    * <p>
40    * The goal is to have the document that correspond the best to the passed reference (if a space then get the space home
41    * page, if a wiki then get the wiki home page, if an attachment then get the document where it's located, etc).
42    *
43    * @version $Id: b9a2470e1d00b284bf164a748d4f288de3bd9ac6 $
44    * @since 7.2M1
45    */
46    @Component
47    @Named("currentgetdocument")
48    @Singleton
 
49    public class CurrentGetDocumentReferenceDocumentReferenceResolver implements DocumentReferenceResolver<EntityReference>
50    {
51    @Inject
52    @Named("current")
53    private DocumentReferenceResolver<EntityReference> currentDocumentResolver;
54   
55    @Inject
56    @Named("current")
57    private EntityReferenceResolver<EntityReference> currentResolver;
58   
59    @Inject
60    private DocumentReferenceResolver<EntityReference> defaultResolver;
61   
62    @Inject
63    private WikiDescriptorManager wikiDescriptorManager;
64   
 
65  4039 toggle @Override
66    public DocumentReference resolve(EntityReference initialReference, Object... parameters)
67    {
68  4039 if (initialReference.getType().ordinal() < EntityType.DOCUMENT.ordinal()) {
69  3398 if (EntityType.WIKI.equals(initialReference.getType())) {
70    // Use whatever document reference that is configured as wiki homepage.
71  997 String wikiId = initialReference.getName();
72  997 try {
73  997 WikiDescriptor wikiDescriptor = wikiDescriptorManager.getById(wikiId);
74  997 if (wikiDescriptor != null) {
75  997 DocumentReference wikiHomepageReference = wikiDescriptor.getMainPageReference();
76  997 return wikiHomepageReference;
77    }
78    } catch (Exception e) {
79    // It would not be safe to do any assumptions a this level since we risk affecting the wrong
80    // document.
81  0 throw new RuntimeException(String.format(
82    "Failed to get wiki descriptor while resolving reference [%s]", initialReference), e);
83    }
84    }
85   
86    // Get the complete initial reference
87  2401 EntityReference currentParentReference =
88    this.currentResolver.resolve(initialReference, initialReference.getType(), parameters);
89   
90    // Resolve missing lower part as default (to get space home page and wiki home page)
91  2401 return this.defaultResolver.resolve(currentParentReference, parameters);
92    } else {
93  641 return this.currentDocumentResolver.resolve(initialReference, parameters);
94    }
95    }
96    }