1616 * along with this program. If not, see <https://www.gnu.org/licenses/>.
1717 */
1818
19- package de .symeda .sormas .backend .importexport ;
19+ package de .symeda .sormas .api .importexport ;
2020
21+ import de .symeda .sormas .api .Disease ;
22+ import de .symeda .sormas .api .ReferenceDto ;
2123import de .symeda .sormas .api .i18n .I18nProperties ;
24+ import de .symeda .sormas .api .i18n .Strings ;
2225import de .symeda .sormas .api .utils .DataHelper ;
2326import org .apache .commons .lang3 .StringUtils ;
2427
28+ import java .util .ArrayList ;
29+ import java .util .Date ;
30+ import java .util .List ;
31+
2532/**
2633 * Detailed import template column entity with information like entity name, column name, caption and data type.
2734 *
@@ -36,10 +43,13 @@ public class ImportColumn {
3643
3744 private String caption ;
3845
39- private ImportColumn (String entityName , String columnName , String caption ) {
46+ private String dataDescription ;
47+
48+ private ImportColumn (String entityName , String columnName , String caption , String dataDescription ) {
4049 this .entityName = entityName ;
4150 this .columnName = columnName ;
4251 this .caption = caption ;
52+ this .dataDescription = dataDescription ;
4353 }
4454
4555 public String getEntityName () {
@@ -54,10 +64,15 @@ public String getCaption() {
5464 return caption ;
5565 }
5666
57- public static ImportColumn from (Class <?> entityType , String columnName ) {
67+ public String getDataDescription () {
68+ return dataDescription ;
69+ }
70+
71+ public static ImportColumn from (Class <?> entityType , String columnName , Class <?> fieldType ) {
5872 String entityName = DataHelper .getHumanClassName (entityType );
59- String caption = calculateCaption (entityName , columnName );
60- return new ImportColumn (entityName , columnName , caption );
73+ String caption = computeCaption (entityName , columnName );
74+ String dataType = computeDataType (fieldType );
75+ return new ImportColumn (entityName , columnName , caption , dataType );
6176 }
6277
6378 /**
@@ -75,7 +90,7 @@ public static ImportColumn from(Class<?> entityType, String columnName) {
7590 * @param columnName column name from the CSV
7691 * @return list of captions for each column name.
7792 */
78- private static String calculateCaption (String entityName , String columnName ) {
93+ private static String computeCaption (String entityName , String columnName ) {
7994 if (StringUtils .contains (columnName , "." )) {
8095 String [] parts = columnName .split ("\\ ." );
8196 String fieldName = parts [parts .length - 1 ];
@@ -84,4 +99,32 @@ private static String calculateCaption(String entityName, String columnName) {
8499 return I18nProperties .getPrefixCaption (entityName , columnName );
85100 }
86101 }
102+
103+ /**
104+ * Computes the data type accepted for a certain field type. For values which cannot be determined at start a placeholder will be used (ex: <code>${activeDiseases}</code>.
105+ *
106+ * @param fieldType type of a CSV field (column)
107+ * @return a data type description, example or placeholder
108+ */
109+ private static String computeDataType (Class <?> fieldType ) {
110+ if (String .class .isAssignableFrom (fieldType )) {
111+ return I18nProperties .getString (Strings .text );
112+ } else if (Date .class .isAssignableFrom (fieldType )) {
113+ return I18nProperties .getString (Strings .date ) + ": dd/MM/yyyy" ;
114+ } else if (ReferenceDto .class .isAssignableFrom (fieldType )) {
115+ return String .format (I18nProperties .getString (Strings .nameOf ), DataHelper .getHumanClassName (fieldType ));
116+ } else if (Disease .class .isAssignableFrom (fieldType )) {
117+ return "${activeDiseases}" ;
118+ } else if (fieldType .isEnum ()) {
119+ List <String > enumNames = new ArrayList <>();
120+ for (Object enumConstant : fieldType .getEnumConstants ()) {
121+ enumNames .add (((Enum <?>) enumConstant ).name ());
122+ }
123+ return StringUtils .join (enumNames , "," );
124+ } else if (Number .class .isAssignableFrom (fieldType )) {
125+ return I18nProperties .getString (Strings .number );
126+ } else {
127+ return "" ;
128+ }
129+ }
87130}
0 commit comments