Skip to content

Commit

Permalink
GUVNOR-2030: Guided Decision Table: Empty field results in all Condit…
Browse files Browse the repository at this point in the history
…ions being cancelled
  • Loading branch information
manstis committed Feb 22, 2016
1 parent 8985947 commit 0f6c62a
Show file tree
Hide file tree
Showing 17 changed files with 1,546 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<groupId>org.drools</groupId>
<artifactId>drools-workbench-models-datamodel-api</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-workbench-models-commons</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-workbench-models-guided-dtable</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* 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 org.drools.workbench.screens.dtablexls.backend.server.conversion;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.drools.workbench.models.datamodel.oracle.DataType;
import org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52;
import org.kie.workbench.common.services.shared.preferences.ApplicationPreferences;

/**
* Utilities relating to the use of DTCellValue's
*/
public class DTCellValueUtilities {

private static final String DATE_FORMAT = ApplicationPreferences.getDroolsDateFormat();

private static final SimpleDateFormat FORMATTER = new SimpleDateFormat( DATE_FORMAT );

/**
* The column-data type is looked up from the SuggestionCompletionEngine and
* represents the *true* data-type that the column represents. The data-type
* associated with the Cell Value can be incorrect for legacy models. For
* pre-5.2 they will always be String and for pre-5.4 numerical fields are
* always Numeric
* @param type
* @param dcv
*/
public static void assertDTCellValue( final String type,
final DTCellValue52 dcv ) {
if ( dcv == null ) {
return;
}

//If already converted exit
final DataType.DataTypes dataType = convertToTypeSafeType( type );
if ( dataType.equals( dcv.getDataType() ) ) {
return;
}

switch ( dcv.getDataType() ) {
case NUMERIC:
convertDTCellValueFromNumeric( dataType,
dcv );
break;
default:
convertDTCellValueFromString( dataType,
dcv );
}
}

private static DataType.DataTypes convertToTypeSafeType( final String type ) {
if ( type.equals( DataType.TYPE_NUMERIC ) ) {
return DataType.DataTypes.NUMERIC;
} else if ( type.equals( DataType.TYPE_NUMERIC_BIGDECIMAL ) ) {
return DataType.DataTypes.NUMERIC_BIGDECIMAL;
} else if ( type.equals( DataType.TYPE_NUMERIC_BIGINTEGER ) ) {
return DataType.DataTypes.NUMERIC_BIGINTEGER;
} else if ( type.equals( DataType.TYPE_NUMERIC_BYTE ) ) {
return DataType.DataTypes.NUMERIC_BYTE;
} else if ( type.equals( DataType.TYPE_NUMERIC_DOUBLE ) ) {
return DataType.DataTypes.NUMERIC_DOUBLE;
} else if ( type.equals( DataType.TYPE_NUMERIC_FLOAT ) ) {
return DataType.DataTypes.NUMERIC_FLOAT;
} else if ( type.equals( DataType.TYPE_NUMERIC_INTEGER ) ) {
return DataType.DataTypes.NUMERIC_INTEGER;
} else if ( type.equals( DataType.TYPE_NUMERIC_LONG ) ) {
return DataType.DataTypes.NUMERIC_LONG;
} else if ( type.equals( DataType.TYPE_NUMERIC_SHORT ) ) {
return DataType.DataTypes.NUMERIC_SHORT;
} else if ( type.equals( DataType.TYPE_BOOLEAN ) ) {
return DataType.DataTypes.BOOLEAN;
} else if ( type.equals( DataType.TYPE_DATE ) ) {
return DataType.DataTypes.DATE;
}
return DataType.DataTypes.STRING;
}

//If the Decision Table model has been converted from the legacy text based
//class then all values are held in the DTCellValue's StringValue. This
//function attempts to set the correct DTCellValue property based on
//the DTCellValue's data type.
private static void convertDTCellValueFromString( final DataType.DataTypes dataType,
final DTCellValue52 dcv ) {
String text = dcv.getStringValue();
switch ( dataType ) {
case BOOLEAN:
dcv.setBooleanValue( ( text == null ? Boolean.FALSE : Boolean.valueOf( text ) ) );
break;
case DATE:
Date d = null;
try {
if ( text != null ) {
d = FORMATTER.parse( text );
}
} catch ( ParseException e ) {
}
dcv.setDateValue( d );
break;
case NUMERIC:
BigDecimal numericValue = null;
try {
if ( text != null ) {
numericValue = new BigDecimal( text );
}
} catch ( Exception e ) {
}
dcv.setNumericValue( numericValue );
break;
case NUMERIC_BIGDECIMAL:
BigDecimal bigDecimalValue = null;
try {
if ( text != null ) {
bigDecimalValue = new BigDecimal( text );
}
} catch ( Exception e ) {
}
dcv.setNumericValue( bigDecimalValue );
break;
case NUMERIC_BIGINTEGER:
BigInteger bigIntegerValue = null;
try {
if ( text != null ) {
bigIntegerValue = new BigInteger( text );
}
} catch ( Exception e ) {
}
dcv.setNumericValue( bigIntegerValue );
break;
case NUMERIC_BYTE:
Byte byteValue = null;
try {
if ( text != null ) {
byteValue = Byte.valueOf( text );
}
} catch ( Exception e ) {
}
dcv.setNumericValue( byteValue );
break;
case NUMERIC_DOUBLE:
Double doubleValue = null;
try {
if ( text != null ) {
doubleValue = Double.valueOf( text );
}
} catch ( Exception e ) {
}
dcv.setNumericValue( doubleValue );
break;
case NUMERIC_FLOAT:
Float floatValue = null;
try {
if ( text != null ) {
floatValue = Float.valueOf( text );
}
} catch ( Exception e ) {
}
dcv.setNumericValue( floatValue );
break;
case NUMERIC_INTEGER:
Integer integerValue = null;
try {
if ( text != null ) {
integerValue = Integer.valueOf( text );
}
} catch ( Exception e ) {
}
dcv.setNumericValue( integerValue );
break;
case NUMERIC_LONG:
Long longValue = null;
try {
if ( text != null ) {
longValue = Long.valueOf( text );
}
} catch ( Exception e ) {
}
dcv.setNumericValue( longValue );
break;
case NUMERIC_SHORT:
Short shortValue = null;
try {
if ( text != null ) {
shortValue = Short.valueOf( text );
}
} catch ( Exception e ) {
}
dcv.setNumericValue( shortValue );
break;
}

}

//If the Decision Table model was pre-5.4 Numeric data-types were always stored as
//BigDecimals. This function attempts to set the correct DTCellValue property based
//on the *true* data type.
private static void convertDTCellValueFromNumeric( final DataType.DataTypes dataType,
final DTCellValue52 dcv ) {
//Generic type NUMERIC was always stored as a BigDecimal
final BigDecimal value = (BigDecimal) dcv.getNumericValue();
switch ( dataType ) {
case NUMERIC_BIGDECIMAL:
dcv.setNumericValue( value == null ? null : value );
break;
case NUMERIC_BIGINTEGER:
dcv.setNumericValue( value == null ? null : value.toBigInteger() );
break;
case NUMERIC_BYTE:
dcv.setNumericValue( value == null ? null : value.byteValue() );
break;
case NUMERIC_DOUBLE:
dcv.setNumericValue( value == null ? null : value.doubleValue() );
break;
case NUMERIC_FLOAT:
dcv.setNumericValue( value == null ? null : value.floatValue() );
break;
case NUMERIC_INTEGER:
dcv.setNumericValue( value == null ? null : value.intValue() );
break;
case NUMERIC_LONG:
dcv.setNumericValue( value == null ? null : value.longValue() );
break;
case NUMERIC_SHORT:
dcv.setNumericValue( value == null ? null : value.shortValue() );
break;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.drools.template.model.Global;
import org.drools.template.model.Import;
import org.drools.template.parser.DataListener;
import org.drools.workbench.models.datamodel.oracle.PackageDataModelOracle;
import org.drools.workbench.models.guided.dtable.shared.conversion.ConversionMessageType;
import org.drools.workbench.models.guided.dtable.shared.conversion.ConversionResult;
import org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52;
Expand All @@ -50,11 +51,13 @@
import org.drools.workbench.screens.guided.dtable.service.GuidedDecisionTableEditorService;
import org.drools.workbench.screens.guided.dtable.type.GuidedDTableResourceTypeDefinition;
import org.guvnor.common.services.project.model.ProjectImports;
import org.guvnor.common.services.shared.config.AppConfigService;
import org.guvnor.common.services.shared.metadata.MetadataService;
import org.guvnor.common.services.shared.metadata.model.Metadata;
import org.jboss.errai.security.shared.api.identity.User;
import org.kie.workbench.common.screens.datamodeller.model.droolsdomain.DroolsDomainAnnotations;
import org.kie.workbench.common.screens.datamodeller.service.DataModelerService;
import org.kie.workbench.common.services.datamodel.backend.server.service.DataModelService;
import org.kie.workbench.common.services.datamodeller.core.Annotation;
import org.kie.workbench.common.services.datamodeller.core.AnnotationDefinition;
import org.kie.workbench.common.services.datamodeller.core.DataModel;
Expand All @@ -65,6 +68,7 @@
import org.kie.workbench.common.services.datamodeller.core.impl.DataModelImpl;
import org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl;
import org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl;
import org.kie.workbench.common.services.shared.preferences.ApplicationPreferences;
import org.kie.workbench.common.services.shared.project.KieProject;
import org.kie.workbench.common.services.shared.project.KieProjectService;
import org.kie.workbench.common.services.shared.project.ProjectImportsService;
Expand Down Expand Up @@ -120,15 +124,26 @@ public class DecisionTableXLSToDecisionTableGuidedConverter implements DecisionT
@Inject
private DataModelerService modellerService;

@Inject
private DataModelService dataModelService;

@Inject
//Type Definition to ensure new files have correct extension
private GlobalResourceTypeDefinition globalsType;

@Inject
private AppConfigService appConfigService;

private Map<String, String> orderedBaseTypes = new TreeMap<String, String>();
private Map<String, AnnotationDefinition> annotationDefinitions;

@PostConstruct
public void initialiseTypeConversionMetaData() {
public void setup() {
initialiseTypeConversionMetaData();
initialiseApplicationPreferences();
}

private void initialiseTypeConversionMetaData() {
final List<PropertyType> baseTypes = modellerService.getBasePropertyTypes();
if ( baseTypes != null ) {
for ( PropertyType type : baseTypes ) {
Expand All @@ -139,6 +154,10 @@ public void initialiseTypeConversionMetaData() {
annotationDefinitions = modellerService.getAnnotationDefinitions();
}

private void initialiseApplicationPreferences() {
ApplicationPreferences.setUp( appConfigService.loadPreferences() );
}

@Override
public ConversionResult convert( final Path path ) {

Expand All @@ -151,9 +170,12 @@ public ConversionResult convert( final Path path ) {
return result;
}

final PackageDataModelOracle dmo = dataModelService.getDataModel( path );

//Perform conversion!
final GuidedDecisionTableGeneratorListener listener = parseAssets( path,
result );
result,
dmo );

//Root path for new resources is the same folder as the XLS file
final Path context = Paths.convert( Paths.convert( path ).getParent() );
Expand Down Expand Up @@ -187,10 +209,12 @@ public ConversionResult convert( final Path path ) {
}

private GuidedDecisionTableGeneratorListener parseAssets( final Path path,
final ConversionResult result ) {
final ConversionResult result,
final PackageDataModelOracle dmo ) {

final List<DataListener> listeners = new ArrayList<DataListener>();
final GuidedDecisionTableGeneratorListener listener = new GuidedDecisionTableGeneratorListener( result );
final GuidedDecisionTableGeneratorListener listener = new GuidedDecisionTableGeneratorListener( result,
dmo );
listeners.add( listener );

final ExcelParser parser = new ExcelParser( listeners );
Expand Down Expand Up @@ -275,7 +299,7 @@ private void makeNewJavaTypes( final Path context,

for ( FactMetaModel factMetaModel : factModels.getModels() ) {
final DataObject dataObject = new DataObjectImpl( packageName,
factMetaModel.getName() );
factMetaModel.getName() );
dataObject.setSuperClassName( factMetaModel.getSuperType() );
final List<AnnotationMetaModel> annotationMetaModel = factMetaModel.getAnnotations();
addAnnotations( dataObject,
Expand All @@ -291,7 +315,7 @@ private void makeNewJavaTypes( final Path context,
boolean isBaseType = orderedBaseTypes.containsValue( fieldType );
ObjectProperty property = new ObjectPropertyImpl( fieldName,
fieldType,
isMultiple);
isMultiple );
property.setBaseType( isBaseType );

//field has no annotation in Guvnor 5.5 (and earlier)
Expand Down
Loading

0 comments on commit 0f6c62a

Please sign in to comment.