dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #39407
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19946: add utility method to ValueType for getting ValueType from type/numberType/textType
------------------------------------------------------------
revno: 19946
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-09-03 09:58:03 +0700
message:
add utility method to ValueType for getting ValueType from type/numberType/textType
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/ValueType.java
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/csv/DefaultCsvImportService.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/ValueType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/ValueType.java 2015-09-02 05:15:31 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/ValueType.java 2015-09-03 02:58:03 +0000
@@ -112,9 +112,14 @@
*/
public static ValueType getFromDataElement( DataElement dataElement )
{
- if ( DataElement.VALUE_TYPE_STRING.equals( dataElement.getType() ) )
+ return getFromDataElementTypes( dataElement.getType(), dataElement.getNumberType(), dataElement.getTextType() );
+ }
+
+ public static ValueType getFromDataElementTypes( String type, String numberType, String textType )
+ {
+ if ( DataElement.VALUE_TYPE_STRING.equals( type ) )
{
- if ( DataElement.VALUE_TYPE_LONG_TEXT.equals( dataElement.getTextType() ) )
+ if ( DataElement.VALUE_TYPE_LONG_TEXT.equals( textType ) )
{
return ValueType.LONG_TEXT;
}
@@ -123,29 +128,29 @@
return ValueType.TEXT;
}
}
- else if ( DataElement.VALUE_TYPE_INT.equals( dataElement.getType() ) )
+ else if ( DataElement.VALUE_TYPE_INT.equals( type ) )
{
- if ( DataElement.VALUE_TYPE_UNIT_INTERVAL.equals( dataElement.getNumberType() ) )
+ if ( DataElement.VALUE_TYPE_UNIT_INTERVAL.equals( numberType ) )
{
return ValueType.UNIT_INTERVAL;
}
- else if ( DataElement.VALUE_TYPE_PERCENTAGE.equals( dataElement.getNumberType() ) )
+ else if ( DataElement.VALUE_TYPE_PERCENTAGE.equals( numberType ) )
{
return ValueType.PERCENTAGE;
}
- else if ( DataElement.VALUE_TYPE_INT.equals( dataElement.getNumberType() ) )
+ else if ( DataElement.VALUE_TYPE_INT.equals( numberType ) )
{
return ValueType.INTEGER;
}
- else if ( DataElement.VALUE_TYPE_POSITIVE_INT.equals( dataElement.getNumberType() ) )
+ else if ( DataElement.VALUE_TYPE_POSITIVE_INT.equals( numberType ) )
{
return ValueType.INTEGER_POSITIVE;
}
- else if ( DataElement.VALUE_TYPE_ZERO_OR_POSITIVE_INT.equals( dataElement.getNumberType() ) )
+ else if ( DataElement.VALUE_TYPE_ZERO_OR_POSITIVE_INT.equals( numberType ) )
{
return ValueType.INTEGER_ZERO_OR_POSITIVE;
}
- else if ( DataElement.VALUE_TYPE_NEGATIVE_INT.equals( dataElement.getNumberType() ) )
+ else if ( DataElement.VALUE_TYPE_NEGATIVE_INT.equals( numberType ) )
{
return ValueType.INTEGER_NEGATIVE;
}
@@ -154,23 +159,23 @@
return ValueType.NUMBER;
}
}
- else if ( DataElement.VALUE_TYPE_BOOL.equals( dataElement.getType() ) )
+ else if ( DataElement.VALUE_TYPE_BOOL.equals( numberType ) )
{
return ValueType.BOOLEAN;
}
- else if ( DataElement.VALUE_TYPE_TRUE_ONLY.equals( dataElement.getType() ) )
+ else if ( DataElement.VALUE_TYPE_TRUE_ONLY.equals( numberType ) )
{
return ValueType.TRUE_ONLY;
}
- else if ( DataElement.VALUE_TYPE_DATE.equals( dataElement.getType() ) )
+ else if ( DataElement.VALUE_TYPE_DATE.equals( numberType ) )
{
return ValueType.DATE;
}
- else if ( DataElement.VALUE_TYPE_DATETIME.equals( dataElement.getType() ) )
+ else if ( DataElement.VALUE_TYPE_DATETIME.equals( numberType ) )
{
return ValueType.DATETIME;
}
- else if ( DataElement.VALUE_TYPE_USER_NAME.equals( dataElement.getType() ) )
+ else if ( DataElement.VALUE_TYPE_USER_NAME.equals( numberType ) )
{
return ValueType.USERNAME;
}
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/csv/DefaultCsvImportService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/csv/DefaultCsvImportService.java 2015-03-27 11:01:54 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/csv/DefaultCsvImportService.java 2015-09-03 02:58:03 +0000
@@ -28,21 +28,13 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-import static org.hisp.dhis.system.util.DateUtils.getMediumDate;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.Charset;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
+import com.csvreader.CsvReader;
import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.common.BaseIdentifiableObject;
import org.hisp.dhis.common.CodeGenerator;
import org.hisp.dhis.common.IdentifiableObject;
import org.hisp.dhis.common.ListMap;
+import org.hisp.dhis.common.ValueType;
import org.hisp.dhis.dataelement.CategoryOptionGroup;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
@@ -64,7 +56,15 @@
import org.hisp.dhis.validation.ValidationRule;
import org.springframework.beans.factory.annotation.Autowired;
-import com.csvreader.CsvReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.hisp.dhis.system.util.DateUtils.getMediumDate;
/**
* @author Lars Helge Overland
@@ -74,7 +74,7 @@
{
@Autowired
private DataElementCategoryService categoryService;
-
+
@Autowired
private ExpressionService expressionService;
@@ -175,7 +175,7 @@
throws IOException
{
DataElementCategoryCombo categoryCombo = categoryService.getDefaultDataElementCategoryCombo();
-
+
List<DataElement> list = new ArrayList<>();
while ( reader.readRecord() )
@@ -190,14 +190,20 @@
object.setShortName( getSafe( values, 3, object.getName(), 50 ) );
object.setDescription( getSafe( values, 4, null, null ) );
object.setFormName( getSafe( values, 5, null, 230 ) );
-
+
String domainType = getSafe( values, 6, DataElementDomain.AGGREGATE.getValue(), 16 );
object.setDomainType( DataElementDomain.fromValue( domainType ) );
object.setType( getSafe( values, 7, DataElement.VALUE_TYPE_INT, 16 ) );
object.setNumberType( getSafe( values, 8, DataElement.VALUE_TYPE_NUMBER, 16 ) );
object.setTextType( getSafe( values, 9, null, 16 ) );
- object.setAggregationOperator( getSafe( values, 10, DataElement.AGGREGATION_OPERATOR_SUM, 16 ) );
+ object.setValueType( ValueType.getFromDataElementTypes(
+ getSafe( values, 7, DataElement.VALUE_TYPE_INT, 16 ),
+ getSafe( values, 8, DataElement.VALUE_TYPE_NUMBER, 16 ),
+ getSafe( values, 9, null, 16 )
+ ) );
+
+ object.setAggregationOperator( getSafe( values, 10, DataElement.AGGREGATION_OPERATOR_SUM, 16 ) );
String categoryComboUid = getSafe( values, 11, null, 11 );
object.setUrl( getSafe( values, 12, null, 255 ) );
object.setZeroIsSignificant( Boolean.valueOf( getSafe( values, 13, "false", null ) ) );
@@ -214,7 +220,7 @@
{
object.setCategoryCombo( categoryCombo );
}
-
+
if ( optionSetUid != null )
{
OptionSet optionSet = new OptionSet();
@@ -228,7 +234,7 @@
optionSet.setUid( commentOptionSetUid );
object.setCommentOptionSet( optionSet );
}
-
+
list.add( object );
}
}
@@ -255,7 +261,7 @@
return list;
}
-
+
private List<ValidationRule> validationRulesFromCsv( CsvReader reader )
throws IOException
{
@@ -266,10 +272,10 @@
String[] values = reader.getValues();
if ( values != null && values.length > 0 )
- {
+ {
Expression leftSide = new Expression();
Expression rightSide = new Expression();
-
+
ValidationRule object = new ValidationRule();
setIdentifiableObject( object, values );
object.setDescription( getSafe( values, 3, null, 255 ) );
@@ -278,27 +284,27 @@
object.setRuleType( getSafe( values, 6, ValidationRule.RULE_TYPE_VALIDATION, 255 ) );
object.setOperator( Operator.safeValueOf( getSafe( values, 7, Operator.equal_to.toString(), 255 ) ) );
object.setPeriodType( PeriodType.getByNameIgnoreCase( getSafe( values, 8, MonthlyPeriodType.NAME, 255 ) ) );
-
+
leftSide.setExpression( getSafe( values, 9, null, 255 ) );
leftSide.setDescription( getSafe( values, 10, null, 255 ) );
leftSide.setMissingValueStrategy( MissingValueStrategy.safeValueOf( getSafe( values, 11, MissingValueStrategy.NEVER_SKIP.toString(), 50 ) ) );
leftSide.setDataElementsInExpression( expressionService.getDataElementsInExpression( leftSide.getExpression() ) );
-
+
rightSide.setExpression( getSafe( values, 12, null, 255 ) );
rightSide.setDescription( getSafe( values, 13, null, 255 ) );
- rightSide.setMissingValueStrategy( MissingValueStrategy.safeValueOf( getSafe( values, 14, MissingValueStrategy.NEVER_SKIP.toString(), 50 ) ) );
+ rightSide.setMissingValueStrategy( MissingValueStrategy.safeValueOf( getSafe( values, 14, MissingValueStrategy.NEVER_SKIP.toString(), 50 ) ) );
rightSide.setDataElementsInExpression( expressionService.getDataElementsInExpression( rightSide.getExpression() ) );
-
+
object.setLeftSide( leftSide );
object.setRightSide( rightSide );
-
+
list.add( object );
}
}
-
+
return list;
}
-
+
private List<OrganisationUnit> organisationUnitsFromCsv( CsvReader reader )
throws IOException
{
@@ -326,14 +332,14 @@
object.setAddress( getSafe( values, 14, null, 255 ) );
object.setEmail( getSafe( values, 15, null, 150 ) );
object.setPhoneNumber( getSafe( values, 16, null, 150 ) );
-
+
if ( parentUid != null )
{
OrganisationUnit parent = new OrganisationUnit();
parent.setUid( parentUid );
parent.setCode( parentUid );
parent.setName( parentUid );
-
+
object.setParent( parent );
}
@@ -366,7 +372,7 @@
/**
* Option set format:
- *
+ * <p>
* <ul>
* <li>option set name</li>
* <li>option set uid</li>
@@ -383,7 +389,7 @@
Map<String, OptionSet> nameOptionSetMap = new HashMap<>();
// Read option sets and options and put in maps
-
+
while ( reader.readRecord() )
{
String[] values = reader.getValues();
@@ -392,7 +398,7 @@
{
OptionSet optionSet = new OptionSet();
setIdentifiableObject( optionSet, values );
-
+
Option option = new Option();
option.setName( getSafe( values, 3, null, 230 ) );
option.setUid( getSafe( values, 4, CodeGenerator.generateCode(), 11 ) );
@@ -402,25 +408,25 @@
{
continue;
}
-
+
nameOptionSetMap.put( optionSet.getName(), optionSet );
-
+
nameOptionMap.putValue( optionSet.getName(), option );
-
+
metaData.getOptions().add( option );
}
}
// Read option sets from map and set in meta data
-
+
for ( String optionSetName : nameOptionSetMap.keySet() )
{
OptionSet optionSet = nameOptionSetMap.get( optionSetName );
-
+
List<Option> options = new ArrayList<>( nameOptionMap.get( optionSetName ) );
-
+
optionSet.setOptions( options );
-
+
metaData.getOptionSets().add( optionSet );
}
}
@@ -431,7 +437,7 @@
/**
* Sets the name, uid and code properties on the given object.
- *
+ *
* @param object the object to set identifiable properties.
* @param values the array of property values.
*/
@@ -444,11 +450,11 @@
/**
* Returns a string from the given array avoiding exceptions.
- *
- * @param values the string array.
- * @param index the array index of the string to get.
+ *
+ * @param values the string array.
+ * @param index the array index of the string to get.
* @param defaultValue the default value in case index is out of bounds.
- * @param maxChars the max number of characters to return for the string.
+ * @param maxChars the max number of characters to return for the string.
*/
private static String getSafe( String[] values, int index, String defaultValue, Integer maxChars )
{