View Javadoc
1   package net.sumaris.core.extraction.vo;
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 com.google.common.collect.Maps;
27  import lombok.AccessLevel;
28  import lombok.Data;
29  import lombok.experimental.FieldDefaults;
30  import lombok.experimental.FieldNameConstants;
31  import org.apache.commons.collections4.CollectionUtils;
32  
33  import java.util.*;
34  
35  /**
36   * @author Ludovic Pecquot <ludovic.pecquot>
37   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
38   */
39  @Data
40  @FieldDefaults(level = AccessLevel.PRIVATE)
41  public class ExtractionContextVO {
42  
43      long id;
44  
45      String label;
46      String formatName;
47      String formatVersion;
48      ExtractionFilterVO filter;
49  
50      @FieldNameConstants.Exclude
51      Map<String, String> tableNames = new LinkedHashMap<>();
52  
53      @FieldNameConstants.Exclude
54      Map<String, Set<String>> hiddenColumnNames = new LinkedHashMap<>();
55  
56  
57      @FieldNameConstants.Exclude
58      Set<String> tableNameWithDistinct = new HashSet<>();
59  
60      public ExtractionContextVO() {
61  
62      }
63  
64      protected ExtractionContextVO../../../../net/sumaris/core/extraction/vo/ExtractionContextVO.html#ExtractionContextVO">ExtractionContextVO(ExtractionContextVO source) {
65  
66          this.id = source.id;
67          this.label = source.label;
68          this.formatName = source.formatName;
69          this.formatVersion = source.formatVersion;
70          this.tableNames.putAll(source.tableNames);
71          this.hiddenColumnNames.putAll(source.hiddenColumnNames);
72          this.tableNameWithDistinct.addAll(source.tableNameWithDistinct);
73      }
74  
75  
76      public String getLabel() {
77          return label != null ? label : (this.formatName != null ? this.formatName.toLowerCase() : null);
78      }
79  
80      /**
81       * Register a table (with rows inside)
82       * @param tableName
83       * @param sheetName
84       */
85      public void addTableName(String tableName, String sheetName) {
86          addTableName(tableName, sheetName, null, false);
87      }
88  
89      /**
90       * Register a table (with rows inside)
91       * @param tableName
92       * @param sheetName
93       * @param hiddenColumnNames
94       */
95      public void addTableName(String tableName, String sheetName,
96                               Set<String> hiddenColumnNames,
97                               boolean enableDistinct) {
98          tableNames.put(tableName, sheetName);
99          if (CollectionUtils.isNotEmpty(hiddenColumnNames)) {
100             this.hiddenColumnNames.put(tableName, hiddenColumnNames);
101         }
102         if (enableDistinct) {
103             this.tableNameWithDistinct.add(tableName);
104         }
105     }
106 
107     public String getSheetName(String tableName) {
108         String otherName = tableNames.get(tableName);
109         return (otherName!=null) ? otherName : tableName;
110     }
111 
112     public Set<String> getTableNames() {
113         return tableNames.keySet();
114     }
115 
116     public String getTableNameBySheetName(String sheetName) {
117         Preconditions.checkNotNull(sheetName);
118         return tableNames.entrySet().stream()
119                 .filter(e -> sheetName.equalsIgnoreCase(e.getValue()))
120                 .map(e -> e.getKey())
121                 .findFirst()
122                 .orElse(null);
123     }
124 
125     public boolean hasSheet(String sheetName) {
126         Preconditions.checkNotNull(sheetName);
127         return tableNames.containsValue(sheetName);
128     }
129 
130     /**
131      * Return the hidden columns of the given table
132      * @param tableName
133      */
134     public Set<String> getHiddenColumns(String tableName) {
135         return hiddenColumnNames.get(tableName);
136     }
137 
138 
139     /**
140      * Return is distinct is enable on the table
141      * @param tableName
142      */
143     public boolean isDistinctEnable(String tableName) {
144         return tableNameWithDistinct.contains(tableName);
145     }
146 }