|
| 1 | +/* |
| 2 | + * SORMAS® - Surveillance Outbreak Response Management & Analysis System |
| 3 | + * Copyright © 2016-2020 Helmholtz-Zentrum für Infektionsforschung GmbH (HZI) |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package de.symeda.sormas.backend.importexport; |
| 20 | + |
| 21 | +import de.symeda.sormas.api.i18n.I18nProperties; |
| 22 | +import de.symeda.sormas.api.utils.DataHelper; |
| 23 | +import org.apache.commons.lang3.StringUtils; |
| 24 | + |
| 25 | +/** |
| 26 | + * Detailed import template column entity with information like entity name, column name, caption and data type. |
| 27 | + * |
| 28 | + * @author Alex Vidrean |
| 29 | + * @since 18-Sep-20 |
| 30 | + */ |
| 31 | +public class ImportColumn { |
| 32 | + |
| 33 | + private String entityName; |
| 34 | + |
| 35 | + private String columnName; |
| 36 | + |
| 37 | + private String caption; |
| 38 | + |
| 39 | + private ImportColumn(String entityName, String columnName, String caption) { |
| 40 | + this.entityName = entityName; |
| 41 | + this.columnName = columnName; |
| 42 | + this.caption = caption; |
| 43 | + } |
| 44 | + |
| 45 | + public String getEntityName() { |
| 46 | + return entityName; |
| 47 | + } |
| 48 | + |
| 49 | + public String getColumnName() { |
| 50 | + return columnName; |
| 51 | + } |
| 52 | + |
| 53 | + public String getCaption() { |
| 54 | + return caption; |
| 55 | + } |
| 56 | + |
| 57 | + public static ImportColumn from(Class<?> entityType, String columnName) { |
| 58 | + String entityName = DataHelper.getHumanClassName(entityType); |
| 59 | + String caption = calculateCaption(entityName, columnName); |
| 60 | + return new ImportColumn(entityName, columnName, caption); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Computes the captions based on the entity name and column name. |
| 65 | + * <p/> |
| 66 | + * Column name is composed (ex: <code>person.firstName</code> or <code>person.address.city</code>) |
| 67 | + * -> Split it in parts and use the last part as field name |
| 68 | + * -> Combine the entity name with the field name |
| 69 | + * <p/> |
| 70 | + * Column name is simple (ex: <code>diseaseDetails</code>) -> Combine the entity name with the column name |
| 71 | + * <p/> |
| 72 | + * The result is a list with the same length as the <code>columnNames</code> |
| 73 | + * |
| 74 | + * @param entityName name of the entity from which the field is part of |
| 75 | + * @param columnName column name from the CSV |
| 76 | + * @return list of captions for each column name. |
| 77 | + */ |
| 78 | + private static String calculateCaption(String entityName, String columnName) { |
| 79 | + if (StringUtils.contains(columnName, ".")) { |
| 80 | + String[] parts = columnName.split("\\."); |
| 81 | + String fieldName = parts[parts.length - 1]; |
| 82 | + return I18nProperties.getPrefixCaption(entityName, fieldName); |
| 83 | + } else { |
| 84 | + return I18nProperties.getPrefixCaption(entityName, columnName); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments