use of com.xpn.xwiki.plugin.charts.exceptions.DataSourceException in project xwiki-platform by xwiki.
the class MainDataSourceFactory method create.
@Override
public DataSource create(Map params, XWikiContext context) throws DataSourceException {
String type = (String) params.get("type");
if (type == null || "".equals(type)) {
throw new DataSourceException("Empty datasource type");
}
String factoryClassName = DataSource.class.getPackage().getName() + "." + Character.toUpperCase(type.charAt(0)) + type.toLowerCase().substring(1) + "DataSourceFactory";
try {
Class class_ = Class.forName(factoryClassName);
Method method = class_.getMethod("getInstance", new Class[] {});
DataSourceFactory factory = (DataSourceFactory) method.invoke(null, new Object[] {});
return factory.create(params, context);
} catch (InvocationTargetException e) {
throw new DataSourceException(e.getTargetException());
} catch (Exception e) {
throw new DataSourceException(e);
}
}
use of com.xpn.xwiki.plugin.charts.exceptions.DataSourceException in project xwiki-platform by xwiki.
the class ChartingPlugin method generateChart.
public Chart generateChart(ChartParams params, XWikiContext context) throws GenerateException {
try {
// Obtain the corresponding data source and wrap it into a data source object
DataSource dataSource = MainDataSourceFactory.getInstance().create(params.getMap(ChartParams.SOURCE), context);
String type = params.getString(ChartParams.TYPE);
Plot plot;
try {
String factoryClassName = ChartingPlugin.class.getPackage().getName() + ".plots." + Character.toUpperCase(type.charAt(0)) + type.toLowerCase().substring(1) + "PlotFactory";
Class factoryClass = Class.forName(factoryClassName);
Method method = factoryClass.getMethod("getInstance", new Class[] {});
PlotFactory factory = (PlotFactory) method.invoke(null, new Object[] {});
plot = factory.create(dataSource, params);
} catch (InvocationTargetException e) {
throw new GenerateException(e.getTargetException());
} catch (Throwable e) {
throw new GenerateException(e);
}
ChartCustomizer.customizePlot(plot, params);
JFreeChart jfchart = new JFreeChart(plot);
ChartCustomizer.customizeChart(jfchart, params);
return generatePngChart(jfchart, params, context);
} catch (IOException ioe) {
throw new GenerateException(ioe);
} catch (DataSourceException dse) {
throw new GenerateException(dse);
}
}
use of com.xpn.xwiki.plugin.charts.exceptions.DataSourceException in project xwiki-platform by xwiki.
the class ObjectDataSourceFactory method create.
@Override
public DataSource create(Map params, XWikiContext context) throws DataSourceException {
String docName = (String) params.get("doc");
if (docName == null) {
throw new DataSourceException("source=type:object implies the presence of a doc argument");
}
XWikiDocument doc;
try {
doc = context.getWiki().getDocument(docName, context);
} catch (XWikiException e) {
throw new DataSourceException(e);
}
String className = (String) params.get("class");
if (className == null) {
throw new DataSourceException("source=type:object implies the presence of a class argument");
}
int number;
try {
String s = (String) params.get("object_number");
try {
number = Integer.parseInt(s);
} catch (NumberFormatException e) {
throw new DataSourceException(e);
}
} catch (NumberFormatException e) {
throw new DataSourceException(e);
}
BaseObject xobj = doc.getObject("XWiki." + className, number);
if (xobj == null) {
throw new DataSourceException("XWiki." + className + "#" + number + " object not found");
}
try {
Class class_ = Class.forName(getClass().getPackage().getName() + "." + className);
Constructor ctor = class_.getConstructor(new Class[] { BaseObject.class, XWikiContext.class });
return (DataSource) ctor.newInstance(new Object[] { xobj, context });
} catch (InvocationTargetException e) {
throw new DataSourceException(e.getTargetException());
} catch (Exception e) {
throw new DataSourceException(e);
}
}
use of com.xpn.xwiki.plugin.charts.exceptions.DataSourceException in project xwiki-platform by xwiki.
the class ObjectidDataSourceFactory method create.
@Override
public DataSource create(Map params, XWikiContext context) throws DataSourceException {
int objectid;
try {
String id = (String) params.get("id");
if (id != null) {
objectid = Integer.parseInt(id);
} else {
throw new DataSourceException("source=type:objectid implies the presence of an id argument");
}
} catch (NumberFormatException e) {
throw new DataSourceException(e);
}
BaseObject xobj;
try {
List list = context.getWiki().getStore().search("from " + BaseObject.class.getName() + " as obj where obj.id='" + objectid + "'", 0, 0, context);
if (list.size() == 0) {
throw new DataSourceException("Object ID not found");
}
xobj = (BaseObject) list.get(0);
List propertyList = context.getWiki().getStore().search("from " + BaseProperty.class.getName() + " as p where p.id.id='" + objectid + "'", 0, 0, context);
Iterator it = propertyList.iterator();
while (it.hasNext()) {
BaseProperty prop = (BaseProperty) it.next();
xobj.addField(prop.getName(), prop);
}
} catch (XWikiException e) {
throw new DataSourceException(e);
}
String xclass = xobj.getClassName();
if (!xclass.startsWith("XWiki.")) {
throw new DataSourceException("XWiki prefix missing in object class name " + xclass);
}
String className = DataSource.class.getPackage().getName() + "." + xclass.substring("XWiki.".length());
try {
Class class_ = Class.forName(className);
Constructor ctor = class_.getConstructor(new Class[] { BaseObject.class, XWikiContext.class });
return (DataSource) ctor.newInstance(new Object[] { xobj, context });
} catch (InvocationTargetException e) {
throw new DataSourceException(e.getTargetException());
} catch (Exception e) {
throw new DataSourceException(e);
}
}
use of com.xpn.xwiki.plugin.charts.exceptions.DataSourceException in project xwiki-platform by xwiki.
the class TimeSeriesCollectionFactory method create.
public XYDataset create(DataSource dataSource, ChartParams params) throws GenerateException, DataSourceException {
String dataSeries = params.getString(ChartParams.SERIES);
TimeSeriesCollection dataset = new TimeSeriesCollection();
Class timePeriodClass = params.getClass(ChartParams.TIME_PERIOD_CLASS);
if (timePeriodClass == null) {
timePeriodClass = Day.class;
}
DateFormat format = params.getDateFormat(ChartParams.DATE_FORMAT);
if (format == null) {
format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
}
if (dataSeries.equals("columns")) {
if (!dataSource.hasHeaderColumn()) {
throw new GenerateException("Header column required");
}
for (int column = 0; column < dataSource.getColumnCount(); column++) {
String seriesName;
if (dataSource.hasHeaderRow()) {
seriesName = dataSource.getHeaderRowValue(column);
} else {
seriesName = "Series " + (column + 1);
}
TimeSeries series = new TimeSeries(seriesName, timePeriodClass);
for (int row = 0; row < dataSource.getRowCount(); row++) {
RegularTimePeriod period;
try {
Date date = format.parse(dataSource.getHeaderColumnValue(row));
Constructor ctor = timePeriodClass.getConstructor(new Class[] { Date.class });
period = (RegularTimePeriod) ctor.newInstance(new Object[] { date });
} catch (Exception e) {
throw new GenerateException(e);
}
series.add(period, dataSource.getCell(row, column));
}
dataset.addSeries(series);
}
} else if (dataSeries.equals("rows")) {
if (!dataSource.hasHeaderRow()) {
throw new GenerateException("Header row required");
}
for (int row = 0; row < dataSource.getRowCount(); row++) {
String seriesName;
if (dataSource.hasHeaderColumn()) {
seriesName = dataSource.getHeaderColumnValue(row);
} else {
seriesName = "Series " + (row + 1);
}
TimeSeries series = new TimeSeries(seriesName, timePeriodClass);
for (int column = 0; column < dataSource.getColumnCount(); column++) {
RegularTimePeriod period;
try {
Date date = format.parse(dataSource.getHeaderRowValue(column));
Constructor ctor = timePeriodClass.getConstructor(new Class[] { Date.class });
period = (RegularTimePeriod) ctor.newInstance(new Object[] { date });
} catch (Exception e) {
throw new GenerateException(e);
}
series.add(period, dataSource.getCell(row, column));
}
dataset.addSeries(series);
}
} else {
throw new GenerateException("Invalid series parameter:" + dataSeries);
}
return dataset;
}
Aggregations