Search in sources :

Example 1 with ScorecardParseException

use of org.drools.scorecards.parser.ScorecardParseException in project drools by kiegroup.

the class ScorecardCompiler method compileFromExcel.

public boolean compileFromExcel(final InputStream stream, final String worksheetName) {
    try {
        AbstractScorecardParser parser = new XLSScorecardParser();
        scorecardErrors = parser.parseFile(stream, worksheetName);
        if (scorecardErrors.isEmpty()) {
            pmmlDocument = parser.getPMMLDocument();
            return true;
        }
    } catch (ScorecardParseException e) {
        logger.error(e.getMessage(), e);
    } finally {
        closeStream(stream);
    }
    return false;
}
Also used : AbstractScorecardParser(org.drools.scorecards.parser.AbstractScorecardParser) XLSScorecardParser(org.drools.scorecards.parser.xls.XLSScorecardParser) ScorecardParseException(org.drools.scorecards.parser.ScorecardParseException)

Example 2 with ScorecardParseException

use of org.drools.scorecards.parser.ScorecardParseException in project drools by kiegroup.

the class XLSEventDataCollector method fulfillExpectation.

private void fulfillExpectation(int currentRowCtr, int currentColCtr, Object cellValue, Class expectedClass) throws ScorecardParseException {
    List<DataExpectation> dataExpectations = resolveExpectations(currentRowCtr, currentColCtr);
    CellReference cellRef = new CellReference(currentRowCtr, currentColCtr);
    Method method = null;
    for (DataExpectation dataExpectation : dataExpectations) {
        try {
            if (dataExpectation != null && dataExpectation.object != null) {
                if (cellValue == null || StringUtils.isEmpty(cellValue.toString())) {
                    if (dataExpectation.errorMessage != null && !StringUtils.isEmpty(dataExpectation.errorMessage)) {
                        parseErrors.add(new ScorecardError(cellRef.formatAsString(), dataExpectation.errorMessage));
                        return;
                    }
                }
                String setter = "set" + Character.toUpperCase(dataExpectation.property.charAt(0)) + dataExpectation.property.substring(1);
                method = getSuitableMethod(cellValue, expectedClass, dataExpectation, setter);
                if (method == null) {
                    if (cellValue != null && !StringUtils.isEmpty(cellValue.toString())) {
                        parseErrors.add(new ScorecardError(cellRef.formatAsString(), "Unexpected Value! Wrong Datatype?"));
                    }
                    return;
                }
                if (method.getParameterTypes()[0] == Double.class) {
                    cellValue = Double.parseDouble(cellValue.toString());
                }
                if (method.getParameterTypes()[0] == Boolean.class) {
                    cellValue = Boolean.valueOf(cellValue.toString());
                }
                if (method.getParameterTypes()[0] == String.class && !(cellValue instanceof String) && cellValue != null) {
                    cellValue = cellValue.toString();
                }
                method.invoke(dataExpectation.object, cellValue);
                if (dataExpectation.object instanceof Extension && ("cellRef".equals(((Extension) dataExpectation.object).getName()))) {
                    ((Extension) dataExpectation.object).setValue(cellRef.formatAsString());
                }
            // dataExpectations.remove(dataExpectation);
            }
        } catch (Exception e) {
            throw new ScorecardParseException(e);
        }
    }
}
Also used : ScorecardError(org.drools.scorecards.ScorecardError) Method(java.lang.reflect.Method) CellReference(org.apache.poi.hssf.util.CellReference) ScorecardParseException(org.drools.scorecards.parser.ScorecardParseException) ScorecardParseException(org.drools.scorecards.parser.ScorecardParseException)

Example 3 with ScorecardParseException

use of org.drools.scorecards.parser.ScorecardParseException in project drools by kiegroup.

the class XLSScorecardParser method parseFile.

@Override
public List<ScorecardError> parseFile(InputStream inStream, String worksheetName) throws ScorecardParseException {
    try {
        excelDataCollector = new XLSEventDataCollector();
        excelDataCollector.setParser(this);
        HSSFWorkbook workbook = new HSSFWorkbook(inStream);
        HSSFSheet worksheet = workbook.getSheet(worksheetName);
        if (worksheet != null) {
            currentWorksheet = worksheet;
            excelDataCollector.sheetStart(worksheetName);
            excelDataCollector.setMergedRegionsInSheet(getMergedCellRangeList(worksheet));
            processSheet(worksheet);
            excelDataCollector.sheetComplete();
            parseErrors = excelDataCollector.getParseErrors();
            scorecard = excelDataCollector.getScorecard();
        } else {
            throw new ScorecardParseException("No worksheet found with name '" + worksheetName + "'.");
        }
    } catch (IOException e) {
        throw new ScorecardParseException(e);
    }
    return parseErrors;
}
Also used : HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) IOException(java.io.IOException) ScorecardParseException(org.drools.scorecards.parser.ScorecardParseException) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Aggregations

ScorecardParseException (org.drools.scorecards.parser.ScorecardParseException)3 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 CellReference (org.apache.poi.hssf.util.CellReference)1 ScorecardError (org.drools.scorecards.ScorecardError)1 AbstractScorecardParser (org.drools.scorecards.parser.AbstractScorecardParser)1 XLSScorecardParser (org.drools.scorecards.parser.xls.XLSScorecardParser)1