Author: tchemit Date: 2012-08-17 15:58:26 +0200 (Fri, 17 Aug 2012) New Revision: 2628 Url: http://nuiton.org/repositories/revision/topia/2628 Log: refs #2266: Add some api about import / export in csv format (can import/export enum) Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/EntityCsvModel.java Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/EntityCsvModel.java =================================================================== --- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/EntityCsvModel.java 2012-08-17 09:17:17 UTC (rev 2627) +++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/csv/EntityCsvModel.java 2012-08-17 13:58:26 UTC (rev 2628) @@ -29,6 +29,7 @@ import org.nuiton.topia.persistence.TopiaEntityEnum; import org.nuiton.topia.persistence.TopiaId; import org.nuiton.topia.persistence.metadata.TableMeta; +import org.nuiton.util.csv.ValueParserFormatter; import org.nuiton.util.csv.ext.AbstractImportExportModel; import org.nuiton.util.decorator.Decorator; @@ -47,6 +48,8 @@ protected final TableMeta<T> tableMeta; + protected boolean useOrdinalForEnum; + public static <T extends TopiaEntityEnum, E extends TopiaEntity> EntityCsvModel<T, E> newModel( char separator, TableMeta<T> tableMeta) { @@ -65,16 +68,19 @@ return (E) tableMeta.newEntity(); } + public void setUseOrdinalForEnum(boolean useOrdinalForEnum) { + this.useOrdinalForEnum = useOrdinalForEnum; + } + public void addForeignKeyForExport(String propertyName, Class<TopiaEntity> entityType) { Map<String, TopiaEntity> universe = Collections.emptyMap(); - newColumnForExport( - propertyName, - TopiaCsvCommons.newForeignKeyValue(entityType, - propertyName, - universe) + newColumnForExport(propertyName, + TopiaCsvCommons.newForeignKeyValue(entityType, + propertyName, + universe) ); } @@ -95,12 +101,11 @@ Map<String, E> universe = Maps.uniqueIndex(entities, transform); - newMandatoryColumn( - headerName, - propertyName, - TopiaCsvCommons.newForeignKeyValue(entityType, - propertyName, - universe) + newMandatoryColumn(headerName, + propertyName, + TopiaCsvCommons.newForeignKeyValue(entityType, + propertyName, + universe) ); } @@ -128,11 +133,10 @@ Map<String, E> universe = Maps.uniqueIndex(entities, TopiaId.GET_TOPIA_ID); - newMandatoryColumn( - propertyName, - TopiaCsvCommons.newForeignKeyValue(entityType, - propertyName, - universe) + newMandatoryColumn(propertyName, + TopiaCsvCommons.newForeignKeyValue(entityType, + propertyName, + universe) ); } @@ -148,75 +152,82 @@ newColumnForImportExport( headerName, propertyName, - TopiaCsvCommons.DAY_TIME_SECOND_WITH_TIMESTAMP - ); + TopiaCsvCommons.DAY_TIME_SECOND_WITH_TIMESTAMP); } else if (double.class.equals(type)) { newColumnForImportExport( headerName, propertyName, - TopiaCsvCommons.DOUBLE_PRIMITIVE - ); + TopiaCsvCommons.DOUBLE_PRIMITIVE); } else if (Double.class.equals(type)) { newColumnForImportExport( headerName, propertyName, - TopiaCsvCommons.DOUBLE - ); + TopiaCsvCommons.DOUBLE); } else if (long.class.equals(type)) { newColumnForImportExport( headerName, propertyName, - TopiaCsvCommons.PRIMITIVE_LONG - ); + TopiaCsvCommons.PRIMITIVE_LONG); } else if (Long.class.equals(type)) { newColumnForImportExport( headerName, propertyName, - TopiaCsvCommons.LONG - ); + TopiaCsvCommons.LONG); } else if (float.class.equals(type)) { newColumnForImportExport( headerName, propertyName, - TopiaCsvCommons.PRIMITIVE_FLOAT - ); + TopiaCsvCommons.PRIMITIVE_FLOAT); } else if (Float.class.equals(type)) { newColumnForImportExport( headerName, propertyName, - TopiaCsvCommons.FLOAT - ); + TopiaCsvCommons.FLOAT); } else if (int.class.equals(type)) { newColumnForImportExport( headerName, propertyName, - TopiaCsvCommons.PRIMITIVE_INTEGER - ); + TopiaCsvCommons.PRIMITIVE_INTEGER); } else if (Integer.class.equals(type)) { newColumnForImportExport( headerName, propertyName, - TopiaCsvCommons.INTEGER - ); + TopiaCsvCommons.INTEGER); } else if (boolean.class.equals(type)) { newColumnForImportExport( headerName, propertyName, - TopiaCsvCommons.PRIMITIVE_BOOLEAN - ); + TopiaCsvCommons.PRIMITIVE_BOOLEAN); } else if (Boolean.class.equals(type)) { newColumnForImportExport( headerName, propertyName, - TopiaCsvCommons.BOOLEAN - ); + TopiaCsvCommons.BOOLEAN); + } else if (String.class.equals(type)) { + newColumnForImportExport( + headerName, + propertyName); + } else if (type.isEnum()) { + + Class<Enum> enumType = (Class<Enum>) type; + ValueParserFormatter<Enum> valueParserFormatter; + if (useOrdinalForEnum) { + valueParserFormatter = + TopiaCsvCommons.newEnumByOrdinalParserFormatter(enumType); + } else { + valueParserFormatter = + TopiaCsvCommons.newEnumByNameParserFormatter(enumType); + } + + newColumnForImportExport(headerName, + propertyName, + valueParserFormatter); + } else { - // string - newColumnForImportExport( - headerName, - propertyName - ); + throw new IllegalStateException(String.format( + "For header %s, property %s, no specific handler " + + "found for type %s", headerName, propertyName, type)); } }