1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.charts.source

File ObjectidDataSourceFactory.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

8
30
3
1
101
71
11
0.37
10
3
3.67

Classes

Class Line # Actions
ObjectidDataSourceFactory 34 30 0% 11 41
0.00%
 

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.plugin.charts.source;
21   
22    import java.lang.reflect.Constructor;
23    import java.lang.reflect.InvocationTargetException;
24    import java.util.Iterator;
25    import java.util.List;
26    import java.util.Map;
27   
28    import com.xpn.xwiki.XWikiContext;
29    import com.xpn.xwiki.XWikiException;
30    import com.xpn.xwiki.objects.BaseObject;
31    import com.xpn.xwiki.objects.BaseProperty;
32    import com.xpn.xwiki.plugin.charts.exceptions.DataSourceException;
33   
 
34    public class ObjectidDataSourceFactory implements DataSourceFactory
35    {
36    private static DataSourceFactory uniqueInstance = new ObjectidDataSourceFactory();
37   
 
38  0 toggle private ObjectidDataSourceFactory()
39    {
40    // empty
41    }
42   
 
43  0 toggle public static DataSourceFactory getInstance()
44    {
45  0 return uniqueInstance;
46    }
47   
 
48  0 toggle @Override
49    public DataSource create(Map params, XWikiContext context) throws DataSourceException
50    {
51  0 int objectid;
52  0 try {
53  0 String id = (String) params.get("id");
54  0 if (id != null) {
55  0 objectid = Integer.parseInt(id);
56    } else {
57  0 throw new DataSourceException("source=type:objectid implies the presence of an id argument");
58    }
59    } catch (NumberFormatException e) {
60  0 throw new DataSourceException(e);
61    }
62   
63  0 BaseObject xobj;
64  0 try {
65  0 List list =
66    context.getWiki().getStore().search(
67    "from " + BaseObject.class.getName() + " as obj where obj.id='" + objectid + "'", 0, 0, context);
68  0 if (list.size() == 0) {
69  0 throw new DataSourceException("Object ID not found");
70    }
71  0 xobj = (BaseObject) list.get(0);
72  0 List propertyList =
73    context.getWiki().getStore().search(
74    "from " + BaseProperty.class.getName() + " as p where p.id.id='" + objectid + "'", 0, 0, context);
75  0 Iterator it = propertyList.iterator();
76  0 while (it.hasNext()) {
77  0 BaseProperty prop = (BaseProperty) it.next();
78  0 xobj.addField(prop.getName(), prop);
79    }
80    } catch (XWikiException e) {
81  0 throw new DataSourceException(e);
82    }
83   
84  0 String xclass = xobj.getClassName();
85  0 if (!xclass.startsWith("XWiki.")) {
86  0 throw new DataSourceException("XWiki prefix missing in object class name " + xclass);
87    }
88   
89  0 String className = DataSource.class.getPackage().getName() + "." + xclass.substring("XWiki.".length());
90   
91  0 try {
92  0 Class class_ = Class.forName(className);
93  0 Constructor ctor = class_.getConstructor(new Class[] {BaseObject.class, XWikiContext.class});
94  0 return (DataSource) ctor.newInstance(new Object[] {xobj, context});
95    } catch (InvocationTargetException e) {
96  0 throw new DataSourceException(e.getTargetException());
97    } catch (Exception e) {
98  0 throw new DataSourceException(e);
99    }
100    }
101    }