ref #5448 Fix saving of imported families
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / redlist / gefaesspflanzen / RedListGefaesspflanzenImportFamily.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.io.redlist.gefaesspflanzen;
11
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.UUID;
19
20 import org.apache.log4j.Logger;
21 import org.springframework.stereotype.Component;
22
23 import eu.etaxonomy.cdm.io.common.DbImportBase;
24 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
25 import eu.etaxonomy.cdm.model.common.CdmBase;
26 import eu.etaxonomy.cdm.model.name.BotanicalName;
27 import eu.etaxonomy.cdm.model.name.Rank;
28 import eu.etaxonomy.cdm.model.taxon.Taxon;
29 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
30
31 /**
32 *
33 * @author pplitzner
34 * @date Mar 1, 2016
35 *
36 */
37
38 @Component
39 @SuppressWarnings("serial")
40 public class RedListGefaesspflanzenImportFamily extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
41
42 private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportFamily.class);
43
44 private static final String tableName = "Rote Liste Gefäßpflanzen";
45
46 private static final String pluralString = "families";
47
48
49 public RedListGefaesspflanzenImportFamily() {
50 super(tableName, pluralString);
51 }
52
53 @Override
54 protected String getIdQuery(RedListGefaesspflanzenImportState state) {
55 return null;
56 }
57
58 @Override
59 protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
60 return null;
61 }
62
63 @Override
64 protected void doInvoke(RedListGefaesspflanzenImportState state) {
65 try {
66 Collection<TaxonBase> families = importFamilies(state);
67 getTaxonService().saveOrUpdate(families);
68 } catch (SQLException e) {
69 e.printStackTrace();
70 }
71 }
72
73
74 private Collection<TaxonBase> importFamilies(RedListGefaesspflanzenImportState state) throws SQLException {
75 Collection<TaxonBase> families = new HashSet<TaxonBase>();
76 Map<String, UUID> familyMapGL = new HashMap<>();
77 Map<String, UUID> familyMapCL = new HashMap<>();
78
79 String query = "SELECT DISTINCT f.FAMILIE "
80 + " FROM GATTUNG_FAMILIE f";
81
82 ResultSet rs = state.getConfig().getSource().getResultSet(query);
83 while(rs.next()){
84 String familieStr = rs.getString("FAMILIE");
85 BotanicalName name = BotanicalName.NewInstance(Rank.FAMILY());
86 name.setGenusOrUninomial(familieStr);
87 Taxon familyGL = Taxon.NewInstance(name, null);
88 familyMapGL.put(familieStr, familyGL.getUuid());
89 //clone for checkliste
90 Taxon familyCL = Taxon.NewInstance(name, null);
91 familyMapCL.put(familieStr, familyCL.getUuid());
92
93 families.add(familyGL);
94 families.add(familyCL);
95 }
96 state.setFamilyMapGesamtListe(familyMapGL);
97 state.setFamilyMapCheckliste(familyMapCL);
98 return families;
99 }
100
101 @Override
102 public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
103 return true;
104 }
105
106 @Override
107 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
108 RedListGefaesspflanzenImportState state) {
109 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
110 return result;
111 }
112
113 @Override
114 protected boolean doCheck(RedListGefaesspflanzenImportState state) {
115 return false;
116 }
117
118 @Override
119 protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
120 return false;
121 }
122
123 }