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

File QueryContext.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

16
50
17
4
234
153
26
0.52
2.94
4.25
1.53

Classes

Class Line # Actions
QueryContext 38 27 0% 15 6
0.866666786.7%
QueryContext.DocumentInfo 75 0 - 0 0
-1.0 -
QueryContext.ObjectInfo 86 13 0% 5 0
1.0100%
QueryContext.PropertyInfo 130 10 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 12 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.query.xwql.internal;
21   
22    import java.util.ArrayList;
23    import java.util.Collection;
24    import java.util.HashMap;
25    import java.util.LinkedHashMap;
26    import java.util.List;
27    import java.util.Map;
28    import java.util.Set;
29   
30    import org.xwiki.bridge.DocumentAccessBridge;
31    import org.xwiki.query.jpql.node.PPath;
32    import org.xwiki.query.jpql.node.PXObjectDecl;
33    import org.xwiki.query.jpql.node.Start;
34   
35    /**
36    * Contains information retrieved from query.
37    */
 
38    public class QueryContext
39    {
40    /**
41    * Parse tree of a query.
42    */
43    private Start tree;
44   
45    /**
46    * Map from document alias to its description.
47    */
48    private Map<String, DocumentInfo> documents = new HashMap<String, DocumentInfo>();
49   
50    /**
51    * Map from object alias to its description.
52    */
53    private Map<String, ObjectInfo> objects = new HashMap<String, ObjectInfo>();
54   
55    private AliasGenerator aliasGenerator = new AliasGenerator();
56   
57    private DocumentAccessBridge documentAccessBridge;
58   
 
59  1032 toggle public QueryContext(Start tree, DocumentAccessBridge documentAccessBridge)
60    {
61  1032 this.tree = tree;
62  1032 this.documentAccessBridge = documentAccessBridge;
63    }
64   
 
65  1032 toggle public Start getTree()
66    {
67  1032 return tree;
68    }
69   
 
70  113 toggle public AliasGenerator getAliasGenerator()
71    {
72  113 return aliasGenerator;
73    }
74   
 
75    public class DocumentInfo
76    {
77    public String alias;
78   
79    /**
80    * Map from unnamed object's class name to description. unnamed object is object declaration in where clause:
81    * "where doc.object('Class').prop=1"
82    */
83    public Map<String, ObjectInfo> unnamedObjects = new HashMap<String, ObjectInfo>();
84    }
85   
 
86    public class ObjectInfo
87    {
88    public String docAlias;
89   
90    public String className;
91   
92    public String alias;
93   
94    public String customMappingAlias;
95   
96    /**
97    * Properties appeared in query
98    */
99    public Map<String, PropertyInfo> properties = new LinkedHashMap<String, PropertyInfo>();
100   
 
101  397 toggle public ObjectInfo(String docAlias, String className, String objAlias)
102    {
103  397 this.className = className;
104  397 this.docAlias = docAlias;
105  397 this.alias = objAlias;
106    }
107   
 
108  172 toggle public PropertyInfo addProperty(String propname, PPath location)
109    {
110  172 PropertyInfo prop = properties.get(propname);
111  172 if (prop == null) {
112  149 prop = new PropertyInfo(propname, this);
113  149 properties.put(propname, prop);
114    }
115  172 prop.locations.add(location);
116  172 return prop;
117    }
118   
 
119  396 toggle public boolean isCustomMapped() throws Exception
120    {
121  396 for (PropertyInfo p : properties.values()) {
122  148 if (p.isCustomMapped()) {
123  2 return true;
124    }
125    }
126  394 return false;
127    }
128    }
129   
 
130    public class PropertyInfo
131    {
132    public ObjectInfo object;
133   
134    public String name;
135   
136    public String alias;
137   
 
138  149 toggle public PropertyInfo(String name, ObjectInfo object)
139    {
140  149 this.name = name;
141  149 this.object = object;
142    }
143   
144    public List<PPath> locations = new ArrayList<PPath>();
145   
 
146  279 toggle public String getType() throws Exception
147    {
148  279 return documentAccessBridge.getPropertyType(object.className, name);
149    }
150   
 
151  297 toggle public boolean isCustomMapped() throws Exception
152    {
153  297 return documentAccessBridge.isPropertyCustomMapped(object.className, name);
154    }
155   
 
156  132 toggle public String getValueField() throws Exception
157    {
158  132 String type = getType();
159  132 if (type.endsWith("DBStringListProperty")) {
160  2 return "list";
161  130 } else if (type.endsWith("StringListProperty")) {
162  1 return "textValue";
163    } else {
164  129 return "value";
165    }
166    }
167    }
168   
169    /**
170    * Map from tree node to object it represent.
171    */
172    private Map<PXObjectDecl, ObjectInfo> nodeToObject = new HashMap<PXObjectDecl, ObjectInfo>();
173   
 
174  397 toggle public ObjectInfo addObject(String docAlias, String className, String objAlias, PXObjectDecl node)
175    {
176  397 DocumentInfo di = getDocument(docAlias);
177  397 if (di == null) {
178  0 throw new InvalidQueryException("Can't find document alias [" + docAlias + "]");
179    }
180  397 ObjectInfo res = new ObjectInfo(docAlias, className, objAlias);
181  397 if (objAlias != null) {
182  394 objects.put(objAlias, res);
183  3 } else if (di.unnamedObjects.get(className) == null) {
184  2 di.unnamedObjects.put(className, res);
185    } else {
186  1 res = di.unnamedObjects.get(className);
187    }
188  397 nodeToObject.put(node, res);
189  397 return res;
190    }
191   
 
192  419 toggle public void addDocument(String alias)
193    {
194  419 if (documents.get(alias) != null && !"doc".equals(alias)) {
195  0 throw new InvalidQueryException("Redeclaration of document [" + alias + "]");
196    }
197  419 documents.put(alias, new DocumentInfo());
198    }
199   
 
200  1868 toggle public DocumentInfo getDocument(String alias)
201    {
202  1868 return documents.get(alias);
203    }
204   
 
205  6187 toggle public ObjectInfo getObject(String objAlias)
206    {
207  6187 return objects.get(objAlias);
208    }
209   
 
210  3 toggle public ObjectInfo getObject(PXObjectDecl node)
211    {
212  3 return nodeToObject.get(node);
213    }
214   
215    /**
216    * @return all objects used in query.
217    */
 
218  1032 toggle public Collection<ObjectInfo> getObjects()
219    {
220  1032 List<ObjectInfo> res = new ArrayList<ObjectInfo>();
221  1032 res.addAll(objects.values());
222  1032 for (DocumentInfo di : documents.values()) {
223  419 res.addAll(di.unnamedObjects.values());
224    }
225  1032 return res;
226    }
227   
 
228  0 toggle public Set<String> getDocuments()
229    {
230  0 return documents.keySet();
231    }
232   
233    String DocumentFromName = "Document";
234    }