refId instead of refAuthor is stored now in title
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / faunaEuropaea / FaunaEuropaeaUsersImport.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.faunaEuropaea;
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.Iterator;
17 import java.util.Map;
18 import java.util.UUID;
19 import java.util.Map.Entry;
20
21 import org.apache.log4j.Logger;
22 import org.springframework.stereotype.Component;
23 import org.springframework.transaction.TransactionStatus;
24
25 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
26 import eu.etaxonomy.cdm.io.common.ImportHelper;
27 import eu.etaxonomy.cdm.io.common.Source;
28 import eu.etaxonomy.cdm.model.agent.Team;
29 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
30 import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
31 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
32 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
33
34
35 /**
36 * @author a.babadshanjan
37 * @created 23.08.2010
38 * @version 1.0
39 */
40 @Component
41 public class FaunaEuropaeaUsersImport extends FaunaEuropaeaImportBase {
42 private static final Logger logger = Logger.getLogger(FaunaEuropaeaUsersImport.class);
43
44 /* Interval for progress info message when retrieving taxa */
45 private int modCount = 10000;
46
47 /* (non-Javadoc)
48 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
49 */
50 @Override
51 protected boolean doCheck(FaunaEuropaeaImportState state) {
52 boolean result = true;
53 FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
54 logger.warn("Checking for References not yet fully implemented");
55 result &= checkReferenceStatus(fauEuConfig);
56
57 return result;
58 }
59
60 private boolean checkReferenceStatus(FaunaEuropaeaImportConfigurator fauEuConfig) {
61 boolean result = true;
62 // try {
63 Source source = fauEuConfig.getSource();
64 String sqlStr = "";
65 // ResultSet rs = source.getResultSet(sqlStr);
66 return result;
67 // } catch (SQLException e) {
68 // e.printStackTrace();
69 // return false;
70 // }
71 }
72
73 /* (non-Javadoc)
74 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
75 */
76 @Override
77 protected boolean doInvoke(FaunaEuropaeaImportState state) {
78
79 TransactionStatus txStatus = null;
80 Map<Integer, ReferenceBase> references = null;
81 Map<String,TeamOrPersonBase> authors = null;
82 Map<Integer, UUID> referenceUuids = new HashMap<Integer, UUID>();
83 int limit = state.getConfig().getLimitSave();
84
85 FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
86 Source source = fauEuConfig.getSource();
87
88 String namespace = "Reference";
89 boolean success = true;
90 int i = 0;
91
92 String selectCountUsers =
93 " SELECT count(*) FROM Users";
94
95 String selectColumnsUsers =
96 " SELECT * FROM Users";
97
98 int count;
99 if(logger.isInfoEnabled()) { logger.info("Start making References (Users)..."); }
100
101 try {
102 ResultSet rsRefs = source.getResultSet(selectCountUsers);
103 rsRefs.next();
104 count = rsRefs.getInt(1);
105
106 rsRefs = source.getResultSet(selectColumnsUsers);
107
108 if (logger.isInfoEnabled()) {
109 logger.info("Get all References...");
110 logger.info("Number of rows: " + count);
111 logger.info("Count Query: " + selectCountUsers);
112 logger.info("Select Query: " + selectColumnsUsers);
113 }
114
115 while (rsRefs.next()){
116 int refId = rsRefs.getInt("usr_id");
117 String refTitle = rsRefs.getString("usr_title");
118 String refFirstname = rsRefs.getString("usr_firstname");
119 String refLastname = rsRefs.getString("usr_lastname");
120 String createdDate = rsRefs.getString("usr_createdat");
121
122 // build author
123 String refAuthor = "";
124 if (refTitle != null) {
125 refAuthor = refTitle;
126 if (! refTitle.endsWith(".")) {
127 refAuthor += ".";
128 }
129 }
130 refAuthor += refTitle == null ? NullToEmpty(refFirstname) : " " + NullToEmpty(refFirstname);
131 if ((refTitle != null || refFirstname != null) && refLastname != null) {
132 refAuthor += " " + refLastname;
133 }
134
135 // build year
136 String year = null;
137 if (createdDate != null) {
138 year = createdDate.substring(0, createdDate.indexOf("-"));
139 }
140
141 if ((i++ % limit) == 0) {
142
143 txStatus = startTransaction();
144 references = new HashMap<Integer,ReferenceBase>(limit);
145 authors = new HashMap<String,TeamOrPersonBase>(limit);
146
147 if(logger.isInfoEnabled()) {
148 logger.info("i = " + i + " - Reference import transaction started");
149 }
150 }
151
152 ReferenceBase<?> reference = null;
153 TeamOrPersonBase<Team> author = null;
154 reference = ReferenceFactory.newGeneric();
155
156 reference.setTitle("" + refId); // This unique key is needed to get a hand on this Reference in PesiTaxonExport
157 reference.setDatePublished(ImportHelper.getDatePublished(year));
158
159 if (!authors.containsKey(refAuthor)) {
160 if (refAuthor == null) {
161 logger.warn("Reference author is null");
162 }
163 author = Team.NewInstance();
164 author.setTitleCache(refAuthor, true);
165 authors.put(refAuthor,author);
166 if (logger.isTraceEnabled()) {
167 logger.trace("Stored author (" + refAuthor + ")");
168 }
169 //}
170
171 } else {
172 author = authors.get(refAuthor);
173 if (logger.isDebugEnabled()) {
174 logger.debug("Not imported author with duplicated aut_id (" + refId +
175 ") " + refAuthor);
176 }
177 }
178
179 reference.setAuthorTeam(author);
180
181 ImportHelper.setOriginalSource(reference, fauEuConfig.getSourceReference(), refId, namespace);
182 ImportHelper.setOriginalSource(author, fauEuConfig.getSourceReference(), refId, namespace);
183
184
185 // Store reference
186 if (!references.containsKey(refId)) {
187
188 if (reference == null) {
189 logger.warn("Reference is null");
190 }
191 references.put(refId, reference);
192 if (logger.isTraceEnabled()) {
193 logger.trace("Stored reference (" + refAuthor + ")");
194 }
195 } else {
196 if (logger.isDebugEnabled()) {
197 logger.debug("Duplicated reference (" + refId + ", " + refAuthor + ")");
198 }
199 //continue;
200 }
201
202 if (((i % limit) == 0 && i > 1 ) || i == count) {
203
204 Map <UUID, ReferenceBase> referenceMap =getReferenceService().save(references.values());
205 logger.info("i = " + i + " - references saved");
206
207 Iterator<Entry<UUID, ReferenceBase>> it = referenceMap.entrySet().iterator();
208 while (it.hasNext()){
209 ReferenceBase ref = it.next().getValue();
210 int refID = Integer.valueOf(((OriginalSourceBase)ref.getSources().iterator().next()).getIdInSource());
211 UUID uuid = ref.getUuid();
212 referenceUuids.put(refID, uuid);
213 }
214 references= null;
215 getAgentService().save((Collection)authors.values());
216
217 authors = null;
218 commitTransaction(txStatus);
219 }
220
221 }
222 }catch(SQLException e) {
223 logger.error("SQLException:" + e);
224 success = false;
225 }
226
227 if(logger.isInfoEnabled()) { logger.info("End making References (Users) ..."); }
228
229 return success;
230 }
231
232 /**
233 * Returns an empty string in case of a null string.
234 * This avoids having the string "null" when using StringBuilder.append(null);
235 * @param string
236 * @return
237 */
238 private String NullToEmpty(String string) {
239 if (string == null) {
240 return "";
241 } else {
242 return string;
243 }
244 }
245
246 /* (non-Javadoc)
247 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
248 */
249 protected boolean isIgnore(FaunaEuropaeaImportState state){
250 return (state.getConfig().getDoReferences() == IImportConfigurator.DO_REFERENCES.NONE);
251 }
252
253 }