BrightSide Workbench Full Report + Source Code
AbstractImporterProvider.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero 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 Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.importer;
20 
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import org.turro.csv.CSVs;
31 import org.turro.ws.WsMember;
32 
37 public abstract class AbstractImporterProvider implements ImporterProvider {
38 
39  private final String name, service;
40 
41  private boolean fullImport;
42 
43  private ImporterDataType type = ImporterDataType.CSV;
44  private List<String> headers;
45  private List<ImporterRegister> data;
46 
47  public AbstractImporterProvider(String name, String service) {
48  this.name = name;
49  this.service = service;
50  }
51 
52  @Override
53  public String getName() {
54  return name;
55  }
56 
57  @Override
58  public String getService() {
59  return service;
60  }
61 
62  @Override
63  public boolean inRole() {
64  return isInRole();
65  }
66 
67  @Override
68  public void setType(ImporterDataType type) {
69  this.type = type;
70  }
71 
72  @Override
73  public void setType(String fileName) {
74  if(fileName.toLowerCase().endsWith("csv")) {
75  type = ImporterDataType.CSV;
76  } else if(fileName.toLowerCase().endsWith("tdf")) {
77  type = ImporterDataType.TDF;
78  }
79  }
80 
81  @Override
82  public void clear() {
83  data = new ArrayList<>();
84  headers = new ArrayList<>();
85  }
86 
87  @Override
88  public void setFullImport(boolean fullImport) {
89  this.fullImport = fullImport;
90  }
91 
92  @Override
93  public boolean isFullImport() {
94  return fullImport;
95  }
96 
97  @Override
98  public void importData(Reader reader) {
99  data.addAll(importDataFrom(reader));
100  }
101 
102  @Override
103  public List<String> getHeaders() {
104  return headers;
105  }
106 
107  @Override
108  public List<ImporterRegister> getData() {
109  return data;
110  }
111 
112  @Override
113  public void processData(WsMember member) {
114  process(member, data);
115  }
116 
117  protected List<ImporterRegister> importDataFrom(Reader reader) {
118  try {
119  if(type.equals(ImporterDataType.CSV) || type.equals(ImporterDataType.TDF)) {
120  CSVs csv = type.equals(ImporterDataType.CSV) ? CSVs.cvs().reader(reader) : CSVs.tdf().reader(reader);
121  List<Map<String, String>> values = csv.parse();
122  headers = csv.getRealHeaders();
123  return values.stream().map(register -> ImporterRegister.from(new HashMap<String, Object>(register))).toList();
124  }
125  } catch (IOException ex) {
126  Logger.getLogger(AbstractImporterProvider.class.getName()).log(Level.SEVERE, null, ex);
127  }
128  return Collections.EMPTY_LIST;
129  }
130 
131  protected abstract void process(WsMember member, List<ImporterRegister> data);
132  protected abstract boolean isInRole();
133 
134 }
List< ImporterRegister > importDataFrom(Reader reader)
abstract void process(WsMember member, List< ImporterRegister > data)