1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.internal.filter

File DefaultInstanceModel.java

 

Coverage histogram

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

Code metrics

0
29
3
1
138
92
6
0.21
9.67
3
2

Classes

Class Line # Actions
DefaultInstanceModel 54 29 0% 6 3
0.9062590.6%
 

Contributing tests

No tests hitting this source file were found.

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.filter;
21   
22    import java.util.ArrayList;
23    import java.util.Collections;
24    import java.util.List;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Provider;
29    import javax.inject.Singleton;
30   
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.filter.FilterException;
33    import org.xwiki.filter.instance.internal.InstanceModel;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.model.reference.EntityReferenceSerializer;
36    import org.xwiki.model.reference.EntityReferenceTree;
37    import org.xwiki.model.reference.EntityReferenceTreeNode;
38    import org.xwiki.model.reference.SpaceReference;
39    import org.xwiki.model.reference.SpaceReferenceResolver;
40    import org.xwiki.model.reference.WikiReference;
41    import org.xwiki.query.Query;
42    import org.xwiki.query.QueryException;
43    import org.xwiki.query.QueryManager;
44   
45    import com.xpn.xwiki.XWikiContext;
46    import com.xpn.xwiki.XWikiException;
47   
48    /**
49    * @version $Id: b01635ff7f7ee3a2e8f47976a0c224740d105364 $
50    * @since 6.2M1
51    */
52    @Component
53    @Singleton
 
54    public class DefaultInstanceModel implements InstanceModel
55    {
56    @Inject
57    private Provider<XWikiContext> xcontextProvider;
58   
59    @Inject
60    private QueryManager queryManager;
61   
62    @Inject
63    private SpaceReferenceResolver<String> spaceResolver;
64   
65    @Inject
66    @Named("local")
67    private EntityReferenceSerializer<String> localSerializer;
68   
 
69  3 toggle @Override
70    public List<WikiReference> getWikiReferences() throws FilterException
71    {
72  3 XWikiContext xcontext = this.xcontextProvider.get();
73   
74  3 try {
75  3 List<String> wikis = xcontext.getWiki().getVirtualWikisDatabaseNames(xcontext);
76   
77  3 List<WikiReference> wikiReferences = new ArrayList<>(wikis.size());
78  3 for (String wikiName : wikis) {
79  3 wikiReferences.add(new WikiReference(new WikiReference(wikiName)));
80    }
81  3 Collections.sort(wikis);
82   
83  3 return wikiReferences;
84    } catch (XWikiException e) {
85  0 throw new FilterException("Failed to get the list of wikis", e);
86    }
87    }
88   
 
89  3 toggle @Override
90    public EntityReferenceTreeNode getSpaceReferences(WikiReference wikiReference) throws FilterException
91    {
92    // Get the spaces
93  3 List<String> spaceReferenceStrings;
94  3 try {
95  3 spaceReferenceStrings =
96    this.queryManager.getNamedQuery("getSpaces").setWiki(wikiReference.getName()).execute();
97    } catch (QueryException e) {
98  0 throw new FilterException(String.format("Failed to get the list of spaces in wiki [%s]", wikiReference), e);
99    }
100   
101    // Get references
102  3 List<SpaceReference> spaceReferences = new ArrayList<>(spaceReferenceStrings.size());
103  3 for (String spaceReferenceString : spaceReferenceStrings) {
104  288 spaceReferences.add(this.spaceResolver.resolve(spaceReferenceString, wikiReference));
105    }
106   
107    // Create the tree
108  3 EntityReferenceTree tree = new EntityReferenceTree(spaceReferences);
109   
110  3 return tree.getChildren().iterator().next();
111    }
112   
 
113  213 toggle @Override
114    public List<DocumentReference> getDocumentReferences(SpaceReference spaceReference) throws FilterException
115    {
116  213 List<String> documentNames;
117  213 try {
118  213 Query query =
119    this.queryManager.createQuery(
120    "select distinct doc.name from Document doc where doc.space = :space order by doc.name asc",
121    Query.XWQL);
122  213 query.bindValue("space", localSerializer.serialize(spaceReference));
123  213 query.setWiki(spaceReference.getWikiReference().getName());
124   
125  213 documentNames = query.execute();
126    } catch (QueryException e) {
127  0 throw new FilterException(
128    String.format("Failed to get the list of documents in space [%s]", spaceReference), e);
129    }
130   
131  213 List<DocumentReference> documentReferences = new ArrayList<>(documentNames.size());
132  213 for (String documentName : documentNames) {
133  213 documentReferences.add(new DocumentReference(documentName, spaceReference));
134    }
135   
136  213 return documentReferences;
137    }
138    }