| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
|
| |
|
| 0% |
Uncovered Elements: 41 (41) |
Complexity: 11 |
Complexity Density: 0.37 |
|
| 34 |
|
public class ObjectidDataSourceFactory implements DataSourceFactory |
| 35 |
|
{ |
| 36 |
|
private static DataSourceFactory uniqueInstance = new ObjectidDataSourceFactory(); |
| 37 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 38 |
0 |
private ObjectidDataSourceFactory()... |
| 39 |
|
{ |
| 40 |
|
|
| 41 |
|
} |
| 42 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 43 |
0 |
public static DataSourceFactory getInstance()... |
| 44 |
|
{ |
| 45 |
0 |
return uniqueInstance; |
| 46 |
|
} |
| 47 |
|
|
| |
|
| 0% |
Uncovered Elements: 37 (37) |
Complexity: 9 |
Complexity Density: 0.31 |
|
| 48 |
0 |
@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 |
|
} |