Search in sources :

Example 21 with JRException

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;
}
Also used : JasperDesign(net.sf.jasperreports.engine.design.JasperDesign) JRException(net.sf.jasperreports.engine.JRException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ModelState(com.evolveum.midpoint.model.api.context.ModelState) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) JasperReport(net.sf.jasperreports.engine.JasperReport) ByteArrayInputStream(java.io.ByteArrayInputStream) PrismObject(com.evolveum.midpoint.prism.PrismObject)

Example 22 with JRException

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");
}
Also used : Referencable(com.evolveum.midpoint.prism.Referencable) PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) JRException(net.sf.jasperreports.engine.JRException) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) ArrayList(java.util.ArrayList) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) PrismContainerable(com.evolveum.midpoint.prism.PrismContainerable) PrismObject(com.evolveum.midpoint.prism.PrismObject) Item(com.evolveum.midpoint.prism.Item) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) PrismReferenceValue(com.evolveum.midpoint.prism.PrismReferenceValue) PrismContainer(com.evolveum.midpoint.prism.PrismContainer) PrismReference(com.evolveum.midpoint.prism.PrismReference) ArrayList(java.util.ArrayList) List(java.util.List) PrismObject(com.evolveum.midpoint.prism.PrismObject) PrismContainerable(com.evolveum.midpoint.prism.PrismContainerable) Containerable(com.evolveum.midpoint.prism.Containerable) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 23 with JRException

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);
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) JRException(net.sf.jasperreports.engine.JRException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) ArrayList(java.util.ArrayList) JRBeanCollectionDataSource(net.sf.jasperreports.engine.data.JRBeanCollectionDataSource) PrismObject(com.evolveum.midpoint.prism.PrismObject) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) AuditEventRecordType(com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordType) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Containerable(com.evolveum.midpoint.prism.Containerable) AuditEventRecord(com.evolveum.midpoint.audit.api.AuditEventRecord)

Example 24 with JRException

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());
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) JasperDesign(net.sf.jasperreports.engine.design.JasperDesign) JRException(net.sf.jasperreports.engine.JRException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 25 with JRException

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;
}
Also used : JRException(net.sf.jasperreports.engine.JRException) JasperReport(net.sf.jasperreports.engine.JasperReport)

Aggregations

JRException (net.sf.jasperreports.engine.JRException)33 JasperReport (net.sf.jasperreports.engine.JasperReport)8 File (java.io.File)7 InputStream (java.io.InputStream)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 JasperPrint (net.sf.jasperreports.engine.JasperPrint)6 PrismObject (com.evolveum.midpoint.prism.PrismObject)5 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)5 ArrayList (java.util.ArrayList)4 JRBeanCollectionDataSource (net.sf.jasperreports.engine.data.JRBeanCollectionDataSource)4 Containerable (com.evolveum.midpoint.prism.Containerable)3 PrismContainerValue (com.evolveum.midpoint.prism.PrismContainerValue)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Map (java.util.Map)3 JasperDesign (net.sf.jasperreports.engine.design.JasperDesign)3 BusinessServiceException (sic.service.BusinessServiceException)3 ServiceException (sic.service.ServiceException)3 Item (com.evolveum.midpoint.prism.Item)2 PrismContainer (com.evolveum.midpoint.prism.PrismContainer)2