use of net.sf.jasperreports.engine.JRException in project midpoint by Evolveum.
the class ReportManagerImpl method invoke.
/**
* Transforms change:
* 1/ ReportOutputType DELETE to MODIFY some attribute to mark it for deletion.
* 2/ ReportType ADD and MODIFY should compute jasper design and styles if necessary
*
* @param context
* @param task
* @param result
* @return
* @throws UnsupportedEncodingException
*/
@Override
public HookOperationMode invoke(@NotNull ModelContext context, @NotNull Task task, @NotNull OperationResult parentResult) {
ModelState state = context.getState();
if (state != ModelState.FINAL) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("report manager called in state = " + state + ", exiting.");
}
return HookOperationMode.FOREGROUND;
} else {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("report manager called in state = " + state + ", proceeding.");
}
}
boolean relatesToReport = false;
boolean isDeletion = false;
PrismObject<?> object = null;
for (Object o : context.getProjectionContexts()) {
boolean deletion = false;
object = ((ModelElementContext<?>) o).getObjectNew();
if (object == null) {
deletion = true;
object = ((ModelElementContext<?>) o).getObjectOld();
}
if (object == null) {
LOGGER.warn("Probably invalid projection context: both old and new objects are null");
} else if (object.getCompileTimeClass().isAssignableFrom(ReportType.class)) {
relatesToReport = true;
isDeletion = deletion;
}
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("change relates to report: " + relatesToReport + ", is deletion: " + isDeletion);
}
if (!relatesToReport) {
LOGGER.trace("invoke() EXITING: Changes not related to report");
return HookOperationMode.FOREGROUND;
}
if (isDeletion) {
LOGGER.trace("invoke() EXITING because operation is DELETION");
return HookOperationMode.FOREGROUND;
}
OperationResult result = parentResult.createSubresult(CLASS_NAME_WITH_DOT + "invoke");
try {
ReportType reportType = (ReportType) object.asObjectable();
JasperDesign jasperDesign = null;
if (reportType.getTemplate() == null) {
String message = "Report template must not be null";
LOGGER.error(message);
result.recordFatalError(message, new SystemException());
} else // {
// PrismSchema reportSchema = null;
// PrismContainer<ReportConfigurationType> parameterConfiguration = null;
// try
// {
// reportSchema = ReportUtils.getParametersSchema(reportType, prismContext);
// parameterConfiguration = ReportUtils.getParametersContainer(reportType, reportSchema);
//
// } catch (Exception ex){
// String message = "Cannot create parameter configuration: " + ex.getMessage();
// LOGGER.error(message);
// result.recordFatalError(message, ex);
// }
//
// jasperDesign = ReportUtils.createJasperDesign(reportType, parameterConfiguration, reportSchema) ;
// LOGGER.trace("create jasper design : {}", jasperDesign);
// }
{
byte[] reportTemplateBase64 = reportType.getTemplate();
byte[] reportTemplate = Base64.decodeBase64(reportTemplateBase64);
InputStream inputStreamJRXML = new ByteArrayInputStream(reportTemplate);
jasperDesign = JRXmlLoader.load(inputStreamJRXML);
LOGGER.trace("load jasper design : {}", jasperDesign);
}
// Compile template
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
LOGGER.trace("compile jasper design, create jasper report : {}", jasperReport);
//result.computeStatus();
result.recordSuccessIfUnknown();
} catch (JRException ex) {
String message = "Cannot load or compile jasper report: " + ex.getMessage();
LOGGER.error(message);
result.recordFatalError(message, ex);
}
return HookOperationMode.FOREGROUND;
}
use of net.sf.jasperreports.engine.JRException in project midpoint by Evolveum.
the class MidPointDataSource method getFieldValue.
@Override
public Object getFieldValue(JRField jrField) throws JRException {
String fieldName = jrField.getName();
if (fieldName.equals("oid")) {
if (currentObject.getParent() instanceof PrismObject) {
return ((PrismObject) currentObject.getParent()).getOid();
} else {
throw new IllegalStateException("oid property is not supported for " + currentObject.getClass());
}
} else if (PARENT_NAME.equals(fieldName)) {
PrismContainerable parent1 = currentObject.getParent();
if (!(parent1 instanceof PrismContainer)) {
return null;
}
return ((PrismContainer) parent1).getParent();
} else if (THIS_NAME.equals(fieldName)) {
return currentObject;
}
ItemPathType itemPathType = new ItemPathType(fieldName);
ItemPath path = itemPathType.getItemPath();
Item i = currentObject.findItem(path);
if (i == null) {
return null;
}
if (i instanceof PrismProperty) {
if (i.isSingleValue()) {
return normalize(((PrismProperty) i).getRealValue(), jrField.getValueClass());
}
List normalized = new ArrayList<>();
for (Object real : ((PrismProperty) i).getRealValues()) {
normalized.add(normalize(real, jrField.getValueClass()));
}
return ((PrismProperty) i).getRealValues();
} else if (i instanceof PrismReference) {
if (i.isSingleValue()) {
return ObjectTypeUtil.createObjectRef(((PrismReference) i).getValue());
}
List<Referencable> refs = new ArrayList<Referencable>();
for (PrismReferenceValue refVal : ((PrismReference) i).getValues()) {
refs.add(ObjectTypeUtil.createObjectRef(refVal));
}
return refs;
} else if (i instanceof PrismContainer) {
if (i.isSingleValue()) {
return ((PrismContainer) i).getValue().asContainerable();
}
List<Containerable> containers = new ArrayList<Containerable>();
for (Object pcv : i.getValues()) {
if (pcv instanceof PrismContainerValue) {
containers.add(((PrismContainerValue) pcv).asContainerable());
}
}
return containers;
} else
throw new JRException("Could not get value of the fileld: " + fieldName);
// return
// throw new UnsupportedOperationException("dataSource.getFiledValue() not supported");
}
use of net.sf.jasperreports.engine.JRException in project midpoint by Evolveum.
the class MidPointQueryExecutor method createDatasource.
@Override
public JRDataSource createDatasource() throws JRException {
try {
if (query == null && script == null) {
throw new JRException("Neither query, nor script defined in the report.");
}
if (query != null) {
Collection<PrismObject<? extends ObjectType>> results;
results = searchObjects(query, SelectorOptions.createCollection(GetOperationOptions.createRaw()));
return createDataSourceFromObjects(results);
} else {
if (script.contains("AuditEventRecord")) {
Collection<AuditEventRecord> audtiEventRecords = searchAuditRecords(script, getPromptingParameters());
Collection<AuditEventRecordType> auditEventRecordsType = new ArrayList<>();
for (AuditEventRecord aer : audtiEventRecords) {
AuditEventRecordType aerType = aer.createAuditEventRecordType(true);
auditEventRecordsType.add(aerType);
}
return new JRBeanCollectionDataSource(auditEventRecordsType);
} else {
Collection<PrismContainerValue<? extends Containerable>> results;
results = evaluateScript(script, getParameters());
return createDataSourceFromContainerValues(results);
}
}
} catch (SchemaException | ObjectNotFoundException | SecurityViolationException | CommunicationException | ConfigurationException | ExpressionEvaluationException e) {
// TODO Auto-generated catch block
throw new JRException(e);
}
}
use of net.sf.jasperreports.engine.JRException in project midpoint by Evolveum.
the class ReportTypeUtil method loadJasperDesign.
public static JasperDesign loadJasperDesign(byte[] template) throws SchemaException {
try {
byte[] reportTemplate = Base64.decodeBase64(template);
InputStream inputStreamJRXML = new ByteArrayInputStream(reportTemplate);
JasperDesign jasperDesign = JRXmlLoader.load(inputStreamJRXML);
// LOGGER.trace("load jasper design : {}", jasperDesign);
return jasperDesign;
} catch (JRException ex) {
throw new SchemaException(ex.getMessage(), ex.getCause());
}
}
use of net.sf.jasperreports.engine.JRException in project adempiere by adempiere.
the class ReportStarter method compileReport.
/**
* @author rlemeill
* @param reportFile
* @param jasperFile
* @return compiled JasperReport
*/
private JasperReport compileReport(File reportFile, File jasperFile) {
JWScorrectClassPath();
JasperReport compiledJasperReport = null;
try {
JasperCompileManager.compileReportToFile(reportFile.getAbsolutePath(), jasperFile.getAbsolutePath());
//Synchronize Dates
jasperFile.setLastModified(reportFile.lastModified());
compiledJasperReport = (JasperReport) JRLoader.loadObject(jasperFile);
} catch (JRException e) {
log.log(Level.SEVERE, "Error", e);
}
return compiledJasperReport;
}
Aggregations