001package com.identityworksllc.iiq.common.reporting;
002
003import net.sf.jasperreports.engine.JRException;
004import net.sf.jasperreports.engine.JRField;
005import sailpoint.api.SailPointContext;
006import sailpoint.object.Attributes;
007import sailpoint.object.LiveReport;
008import sailpoint.object.QueryOptions;
009import sailpoint.object.Sort;
010import sailpoint.plugin.PluginsUtil;
011import sailpoint.reporting.datasource.JavaDataSource;
012import sailpoint.task.Monitor;
013import sailpoint.tools.GeneralException;
014
015import java.util.List;
016
017/**
018 * A data source that delegates to a 'real' data source loaded from a plugin.
019 * IIQ does not allow report data sources to be loaded from plugins by default.
020 *
021 * Your report would designate this class as its implementation. Then, specify the
022 * real data source class name in the TaskDefinition's attributes as 'pluginName'
023 * and 'pluginClass'. All other options will be delegated to your plugin class.
024 */
025public class PluginDataSource extends AbstractJavaDataSource {
026
027    /**
028     * The real data source, loaded from the plugin
029     */
030    private JavaDataSource realDataSource;
031
032    @Override
033    public void close() {
034        realDataSource.close();
035    }
036
037    @Override
038    public String getBaseHql() {
039        return realDataSource.getBaseHql();
040    }
041
042    @Override
043    public QueryOptions getBaseQueryOptions() {
044        return realDataSource.getBaseQueryOptions();
045    }
046
047    @Override
048    public Object getFieldValue(JRField jrField) throws JRException {
049        return realDataSource.getFieldValue(jrField);
050    }
051
052    @Override
053    public Object getFieldValue(String s) throws GeneralException {
054        return realDataSource.getFieldValue(s);
055    }
056
057    @Override
058    public int getSizeEstimate() throws GeneralException {
059        return realDataSource.getSizeEstimate();
060    }
061
062    /**
063     * Initializes the report by loading the real data source specified, then delegating
064     * immediately to that data source.
065     *
066     * {@inheritDoc}
067     */
068    @Override
069    public void initialize(SailPointContext sailPointContext, LiveReport liveReport, Attributes<String, Object> attributes, String s, List<Sort> list) throws GeneralException {
070        String pluginName = attributes.getString("pluginName");
071        String pluginClass = attributes.getString("pluginClass");
072        this.realDataSource = PluginsUtil.instantiate(pluginName, pluginClass);
073        if (realDataSource == null) {
074            throw new GeneralException("Plugin class " + pluginClass + " is not present in plugin " + pluginName);
075        }
076        this.realDataSource.initialize(sailPointContext, liveReport, attributes, s, list);
077    }
078
079    @Override
080    public boolean next() throws JRException {
081        return realDataSource.next();
082    }
083
084    @Override
085    public void setLimit(int i, int i1) {
086        realDataSource.setLimit(i, i1);
087    }
088
089    @Override
090    public void setMonitor(Monitor monitor) {
091        realDataSource.setMonitor(monitor);
092    }
093}