1 package net.sumaris.core.extraction.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.google.common.base.Preconditions;
26 import net.sumaris.core.exception.SumarisTechnicalException;
27 import net.sumaris.core.extraction.vo.AggregationContextVO;
28 import net.sumaris.core.extraction.vo.ExtractionContextVO;
29 import net.sumaris.core.extraction.vo.ExtractionRawFormatEnum;
30 import net.sumaris.core.extraction.vo.ExtractionTypeVO;
31 import net.sumaris.core.util.StringUtils;
32 import net.sumaris.core.vo.technical.extraction.ExtractionProductVO;
33 import org.apache.commons.collections4.CollectionUtils;
34 import org.apache.commons.collections4.ListUtils;
35 import org.apache.commons.collections4.MapUtils;
36
37 import java.util.Collection;
38 import java.util.Map;
39 import java.util.Optional;
40
41
42
43
44 public class ExtractionBeans extends net.sumaris.core.util.ExtractionBeans {
45
46 protected ExtractionBeans() {
47
48 }
49
50 public static <C extends ExtractionTypeVO> C checkAndFindType(Collection<C> availableTypes, C type) throws IllegalArgumentException {
51 Preconditions.checkNotNull(type, "Missing argument 'type' ");
52 Preconditions.checkNotNull(type.getLabel(), "Missing argument 'type.label'");
53
54
55 final String label = type.getLabel();
56 if (type.getCategory() == null) {
57 type = availableTypes.stream()
58 .filter(aType -> label.equalsIgnoreCase(aType.getLabel()))
59 .findFirst()
60 .orElseThrow(() -> new IllegalArgumentException(String.format("Unknown extraction type label {%s}", label)));
61 } else {
62 final String extractionCategory = type.getCategory();
63 type = availableTypes.stream()
64 .filter(aType -> label.equalsIgnoreCase(aType.getLabel()) && aType.getCategory().equalsIgnoreCase(extractionCategory))
65 .findFirst()
66 .orElseThrow(() -> new IllegalArgumentException(String.format("Unknown extraction type category/label {%s/%s}", extractionCategory, label)));
67 }
68 return type;
69 }
70
71 public static String getTableName(ExtractionContextVO context, String sheetName) {
72
73 String tableName = null;
74 if (StringUtils.isNotBlank(sheetName)) {
75 tableName = context.getTableNameBySheetName(sheetName);
76 Preconditions.checkArgument(tableName != null,
77 String.format("Invalid sheet {%s}: not exists on product {%s}", sheetName, context.getLabel()));
78 }
79
80
81 else if (CollectionUtils.isNotEmpty(context.getTableNames())) {
82 tableName = context.getTableNames().iterator().next();
83 }
84
85
86 if (tableName == null) {
87 if (StringUtils.isBlank(sheetName)) {
88 tableName = "P_" + context.getLabel() + "_" + sheetName.toUpperCase();
89 }
90 else {
91 tableName = "P_" + context.getLabel();
92 }
93 }
94
95 return tableName;
96 }
97
98 public static String getTableName(ExtractionProductVO product, String sheetName) {
99 Preconditions.checkNotNull(product);
100 Preconditions.checkArgument(product.getLabel() != null || sheetName != null);
101
102 String tableName = null;
103 if (StringUtils.isNotBlank(sheetName)) {
104 tableName = product.getTableNameBySheetName(sheetName)
105 .orElseThrow(() -> new SumarisTechnicalException(String.format("Invalid sheet {%s}: not exists on product {%s}", sheetName, product.getLabel())));
106 }
107
108
109 else if (CollectionUtils.isNotEmpty(product.getTables())) {
110 tableName = product.getTableNames().get(0);
111 }
112
113 if (StringUtils.isNotBlank(tableName)) return tableName;
114
115
116 return "p_" + product.getLabel().toLowerCase();
117 }
118
119 public static ExtractionRawFormatEnum getFormat(ExtractionProductVO source) {
120 return getFormatFromLabel(source.getLabel());
121 }
122
123 public static ExtractionRawFormatEnum getFormat(AggregationContextVO source) {
124 return getFormatFromLabel(source.getLabel());
125 }
126
127 public static ExtractionRawFormatEnum getFormatFromLabel(String label) {
128
129 final String formatStr = label == null ? null :
130 StringUtils.changeCaseToUnderscore(label.split("-")[0]).toUpperCase();
131
132 return ExtractionRawFormatEnum.fromString(formatStr)
133 .orElseGet(() -> {
134 if (formatStr != null && formatStr.contains(ExtractionRawFormatEnum.RDB.name())) {
135 return ExtractionRawFormatEnum.RDB;
136 }
137 throw new SumarisTechnicalException(String.format("Data aggregation on format '%s' not implemented !", formatStr));
138 });
139 }
140 }