View Javadoc
1   package net.sumaris.core.extraction.utils;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core Extraction
6    * %%
7    * Copyright (C) 2018 - 2019 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>*
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          // Retrieve the extraction type, from list
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          // Or use first table
81          else if (CollectionUtils.isNotEmpty(context.getTableNames())) {
82              tableName = context.getTableNames().iterator().next();
83          }
84  
85          // Table name not found: compute it from context label
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         // Or use first table
109         else if (CollectionUtils.isNotEmpty(product.getTables())) {
110             tableName = product.getTableNames().get(0);
111         }
112 
113         if (StringUtils.isNotBlank(tableName)) return tableName;
114 
115         // Table name not found: compute it (from the context label)
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 }