Skip to content

Commit

Permalink
small report imrpovements
Browse files Browse the repository at this point in the history
  • Loading branch information
katkav committed Mar 15, 2019
1 parent 4c36d23 commit fd256fa
Show file tree
Hide file tree
Showing 6 changed files with 385 additions and 1 deletion.
Expand Up @@ -46,6 +46,7 @@ Collection<PrismObject<? extends ObjectType>> searchObjects(ObjectQuery query,
Collection<SelectorOptions<GetOperationOptions>> options) throws SchemaException, ObjectNotFoundException, SecurityViolationException, CommunicationException, ConfigurationException, ExpressionEvaluationException;

Collection<PrismContainerValue<? extends Containerable>> evaluateScript(String script, Map<QName, Object> parameters) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException;
Object evaluate(String script, Map<QName, Object> parameters) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException;

Collection<AuditEventRecord> evaluateAuditScript(String script, Map<QName, Object> parameters) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException;

Expand Down
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2010-2018 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.report.impl;

import java.io.File;
import java.io.Serializable;

import net.sf.jasperreports.crosstabs.JRCrosstab;
import net.sf.jasperreports.engine.JRDataset;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperReportsContext;
import net.sf.jasperreports.engine.design.JRAbstractCompiler;
import net.sf.jasperreports.engine.design.JRCompilationSourceCode;
import net.sf.jasperreports.engine.design.JRCompilationUnit;
import net.sf.jasperreports.engine.design.JRCompiler;
import net.sf.jasperreports.engine.design.JRSourceCompileTask;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.fill.JREvaluator;

/**
* @author katka
*
*/
public class JRMidpointCompiler extends JRAbstractCompiler {


/**
* @param jasperReportsContext
* @param needsSourceFiles
*/
public JRMidpointCompiler(JasperReportsContext jasperReportsContext) {
super(jasperReportsContext, false);
// TODO Auto-generated constructor stub
}

/* (non-Javadoc)
* @see net.sf.jasperreports.engine.design.JRCompiler#loadEvaluator(net.sf.jasperreports.engine.JasperReport)
*/
@Override
public JREvaluator loadEvaluator(JasperReport jasperReport) throws JRException {
return new JRMidpointEvaluator(jasperReport);
}

/* (non-Javadoc)
* @see net.sf.jasperreports.engine.design.JRCompiler#loadEvaluator(net.sf.jasperreports.engine.JasperReport, net.sf.jasperreports.crosstabs.JRCrosstab)
*/
@Override
public JREvaluator loadEvaluator(JasperReport jasperReport, JRCrosstab crosstab) throws JRException {
return new JRMidpointEvaluator(jasperReport);
}

/* (non-Javadoc)
* @see net.sf.jasperreports.engine.design.JRCompiler#loadEvaluator(net.sf.jasperreports.engine.JasperReport, net.sf.jasperreports.engine.JRDataset)
*/
@Override
public JREvaluator loadEvaluator(JasperReport jasperReport, JRDataset dataset) throws JRException {
return new JRMidpointEvaluator(jasperReport, dataset);
}

/* (non-Javadoc)
* @see net.sf.jasperreports.engine.design.JRAbstractCompiler#loadEvaluator(java.io.Serializable, java.lang.String)
*/
@Override
protected JREvaluator loadEvaluator(Serializable compileData, String unitName) throws JRException {
return new JRMidpointEvaluator(compileData, unitName);

}

/* (non-Javadoc)
* @see net.sf.jasperreports.engine.design.JRAbstractCompiler#checkLanguage(java.lang.String)
*/
@Override
protected void checkLanguage(String language) throws JRException {
if (!"midPoint".equals(language)) {
throw new JRException("asdasd");
}

}

/* (non-Javadoc)
* @see net.sf.jasperreports.engine.design.JRAbstractCompiler#generateSourceCode(net.sf.jasperreports.engine.design.JRSourceCompileTask)
*/
@Override
protected JRCompilationSourceCode generateSourceCode(JRSourceCompileTask sourceTask) throws JRException {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see net.sf.jasperreports.engine.design.JRAbstractCompiler#compileUnits(net.sf.jasperreports.engine.design.JRCompilationUnit[], java.lang.String, java.io.File)
*/
@Override
protected String compileUnits(JRCompilationUnit[] units, String classpath, File tempDirFile) throws JRException {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see net.sf.jasperreports.engine.design.JRAbstractCompiler#getSourceFileName(java.lang.String)
*/
@Override
protected String getSourceFileName(String unitName) {
// TODO Auto-generated method stub
return unitName;
}

}
@@ -0,0 +1,188 @@
/*
* Copyright (c) 2010-2018 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.report.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.xml.namespace.QName;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluator;
import com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluatorFactory;
import com.evolveum.midpoint.model.common.expression.script.ScriptExpressionUtil;
import com.evolveum.midpoint.repo.common.expression.ExpressionEvaluatorFactory;
import com.evolveum.midpoint.repo.common.expression.ExpressionUtil;
import com.evolveum.midpoint.report.api.ReportService;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ExpressionEvaluationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.ibm.icu.text.ChineseDateFormat.Field;

import net.sf.jasperreports.engine.JRDataset;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExpression;
import net.sf.jasperreports.engine.JRExpressionChunk;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.fill.ExpressionValues;
import net.sf.jasperreports.engine.fill.FillExpressionDefaultValues;
import net.sf.jasperreports.engine.fill.FillExpressionEstimatedValues;
import net.sf.jasperreports.engine.fill.FillExpressionOldValues;
import net.sf.jasperreports.engine.fill.JREvaluator;
import net.sf.jasperreports.engine.fill.JRExpressionEvalException;
import net.sf.jasperreports.engine.fill.JRFillField;
import net.sf.jasperreports.engine.fill.JRFillParameter;
import net.sf.jasperreports.engine.fill.JRFillVariable;

/**
* @author katka
*
*/
public class JRMidpointEvaluator extends JREvaluator {

private static final transient Trace LOGGER = TraceManager.getTrace(JRMidpointEvaluator.class);

private Serializable compileData = null;
private String unitName = null;

private ReportService reportService;

private JasperReport jasperReport;
private JRDataset dataset;

private Map<String, JRFillParameter> parametersMap;
private Map<String, JRFillField> fieldsMap;
private Map<String, JRFillVariable> variablesMap;


public JRMidpointEvaluator(Serializable compileData, String unitName) {
this.compileData = compileData;
this.unitName = unitName;
}

public JRMidpointEvaluator(JasperReport jasperReprot, JRDataset dataset) {
this.jasperReport = jasperReprot;
this.dataset = dataset;
}

public JRMidpointEvaluator(JasperReport jasperReprot) {
this.jasperReport = jasperReprot;
}

@Override
protected void customizedInit(Map<String, JRFillParameter> parametersMap, Map<String, JRFillField> fieldsMap,
Map<String, JRFillVariable> variablesMap) throws JRException {
LOGGER.info("cutomized init: ");
LOGGER.info("parametersMap : {}", parametersMap);
LOGGER.info("fieldsMap : {}", fieldsMap);
LOGGER.info("variablesMap : {}", variablesMap);

this.parametersMap = parametersMap;
this.fieldsMap = fieldsMap;
this.variablesMap = variablesMap;

reportService = SpringApplicationContext.getBean(ReportService.class);
}

@Override
public Object evaluate(JRExpression expression) throws JRExpressionEvalException {
if (expression == null) {
return null;
}
JRExpressionChunk[] ch = expression.getChunks();

Map<QName, Object> parameters = new HashMap<>();

String groovyCode = "";

for (JRExpressionChunk chunk : expression.getChunks()) {
if (chunk == null) {
break;
}

switch (chunk.getType()){
case JRExpressionChunk.TYPE_FIELD:
groovyCode += chunk.getText();
JRFillField field = fieldsMap.get(chunk.getText());
parameters.put(new QName(field.getName()), field.getValue());
break;
case JRExpressionChunk.TYPE_PARAMETER:
groovyCode += chunk.getText();
JRFillParameter param = parametersMap.get(chunk.getText());
parameters.put(new QName(param.getName()), param.getValue());
break;
case JRExpressionChunk.TYPE_VARIABLE:
groovyCode += chunk.getText();
JRFillVariable var = variablesMap.get(chunk.getText());
parameters.put(new QName(var.getName()), var.getValue());
break;
case JRExpressionChunk.TYPE_TEXT:
groovyCode += chunk.getText();
break;
default :
groovyCode += chunk.getText();

}

}


if (reportService != null) {
try {
return reportService.evaluate(groovyCode, parameters);
} catch (SchemaException | ExpressionEvaluationException | ObjectNotFoundException | CommunicationException
| ConfigurationException | SecurityViolationException e) {
throw new JRRuntimeException(e.getMessage(), e);
}
}

byte type = ch[0].getType();
return "tralalal";
}

@Override
protected Object evaluate(int id) throws Throwable {
LOGGER.info("evaluate: {}", id);
return null;

}

@Override
protected Object evaluateOld(int id) throws Throwable {
LOGGER.info("evaluateOld: {}", id);
return null;
}

@Override
protected Object evaluateEstimated(int id) throws Throwable {
LOGGER.info("evaluateEstimated: {}", id);
return null;
}

}
Expand Up @@ -223,6 +223,36 @@ public Collection<PrismContainerValue<? extends Containerable>> evaluateScript(S

return results;
}

@Override
public Object evaluate(String script,
Map<QName, Object> parameters) throws SchemaException, ExpressionEvaluationException,
ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {

ExpressionVariables variables = new ExpressionVariables();
variables.addVariableDefinitions(parameters);

// special variable for audit report
variables.addVariableDefinition(new QName("auditParams"), getConvertedParams(parameters));

Task task = taskManager.createTaskInstance(ReportService.class.getName() + ".evaluateScript");
OperationResult parentResult = task.getResult();

Collection<FunctionLibrary> functions = createFunctionLibraries();

Jsr223ScriptEvaluator scripts = new Jsr223ScriptEvaluator("Groovy", prismContext,
prismContext.getDefaultProtector(), localizationService);
ModelExpressionThreadLocalHolder.pushExpressionEnvironment(new ExpressionEnvironment<>(task, task.getResult()));
Object o;
try{
o = scripts.evaluateReportScript(script, variables, objectResolver, functions, "desc",
parentResult);
} finally{
ModelExpressionThreadLocalHolder.popExpressionEnvironment();
}
return o;

}

protected PrismContainerValue convertResultingObject(Object obj) {
if (obj instanceof PrismObject) {
Expand Down
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2010-2018 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.report.impl;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
* @author katka
*
*/
@Component
public class SpringApplicationContext implements ApplicationContextAware {

private static ApplicationContext appCtx;


@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringApplicationContext.appCtx = applicationContext;
}

public static <T extends Object> T getBean(Class<T> beanClass) {
return appCtx.getBean(beanClass);
}

}

0 comments on commit fd256fa

Please sign in to comment.