Search in sources :

Example 1 with DataSourceException

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);
    }
}
Also used : DataSourceException(com.xpn.xwiki.plugin.charts.exceptions.DataSourceException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataSourceException(com.xpn.xwiki.plugin.charts.exceptions.DataSourceException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with DataSourceException

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);
    }
}
Also used : PlotFactory(com.xpn.xwiki.plugin.charts.plots.PlotFactory) Plot(org.jfree.chart.plot.Plot) Method(java.lang.reflect.Method) IOException(java.io.IOException) GenerateException(com.xpn.xwiki.plugin.charts.exceptions.GenerateException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JFreeChart(org.jfree.chart.JFreeChart) DataSource(com.xpn.xwiki.plugin.charts.source.DataSource) DataSourceException(com.xpn.xwiki.plugin.charts.exceptions.DataSourceException)

Example 3 with DataSourceException

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);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException) XWikiException(com.xpn.xwiki.XWikiException) DataSourceException(com.xpn.xwiki.plugin.charts.exceptions.DataSourceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DataSourceException(com.xpn.xwiki.plugin.charts.exceptions.DataSourceException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiException(com.xpn.xwiki.XWikiException)

Example 4 with DataSourceException

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);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException) XWikiException(com.xpn.xwiki.XWikiException) DataSourceException(com.xpn.xwiki.plugin.charts.exceptions.DataSourceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) BaseObject(com.xpn.xwiki.objects.BaseObject) DataSourceException(com.xpn.xwiki.plugin.charts.exceptions.DataSourceException) Iterator(java.util.Iterator) List(java.util.List) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseProperty(com.xpn.xwiki.objects.BaseProperty) XWikiException(com.xpn.xwiki.XWikiException)

Example 5 with DataSourceException

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;
}
Also used : TimeSeries(org.jfree.data.time.TimeSeries) Constructor(java.lang.reflect.Constructor) GenerateException(com.xpn.xwiki.plugin.charts.exceptions.GenerateException) Date(java.util.Date) GenerateException(com.xpn.xwiki.plugin.charts.exceptions.GenerateException) DataSourceException(com.xpn.xwiki.plugin.charts.exceptions.DataSourceException) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) RegularTimePeriod(org.jfree.data.time.RegularTimePeriod) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

DataSourceException (com.xpn.xwiki.plugin.charts.exceptions.DataSourceException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Constructor (java.lang.reflect.Constructor)3 XWikiException (com.xpn.xwiki.XWikiException)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2 GenerateException (com.xpn.xwiki.plugin.charts.exceptions.GenerateException)2 Method (java.lang.reflect.Method)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseProperty (com.xpn.xwiki.objects.BaseProperty)1 PlotFactory (com.xpn.xwiki.plugin.charts.plots.PlotFactory)1 DataSource (com.xpn.xwiki.plugin.charts.source.DataSource)1 IOException (java.io.IOException)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 List (java.util.List)1 JFreeChart (org.jfree.chart.JFreeChart)1 Plot (org.jfree.chart.plot.Plot)1 RegularTimePeriod (org.jfree.data.time.RegularTimePeriod)1