Project

General

Profile

Download (4.91 KB) Statistics
| Branch: | Revision:
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
package eu.etaxonomy.cdm.io.pesi.faunaEuropaea;
10

    
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.util.Collection;
14
import java.util.Map;
15

    
16
import org.apache.log4j.Logger;
17
import org.springframework.stereotype.Component;
18
import org.springframework.transaction.TransactionStatus;
19

    
20
import eu.etaxonomy.cdm.io.common.ICdmIO;
21
import eu.etaxonomy.cdm.io.common.ImportHelper;
22
import eu.etaxonomy.cdm.io.common.MapWrapper;
23
import eu.etaxonomy.cdm.io.common.Source;
24
import eu.etaxonomy.cdm.model.agent.Team;
25
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27

    
28

    
29
/**
30
 * @author a.babadshanjan
31
 * @created 12.05.2009
32
 * @version 1.0
33
 */
34
@Component
35
public class FaunaEuropaeaAuthorImport extends FaunaEuropaeaImportBase {
36
	private static final Logger logger = Logger.getLogger(FaunaEuropaeaAuthorImport.class);
37

    
38
	private static int modCount = 1000;
39

    
40
	
41
	/* (non-Javadoc)
42
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
43
	 */
44
	@Override
45
	protected boolean doCheck(FaunaEuropaeaImportState state){
46
		boolean result = true;
47
		logger.warn("No checking for Authors not implemented");
48
		
49
		return result;
50
	}
51
	
52
	/* (non-Javadoc)
53
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
54
	 */
55
	@Override
56
	protected void doInvoke(FaunaEuropaeaImportState state){ 
57
		/*
58
		logger.warn("Start author doInvoke");
59
		ProfilerController.memorySnapshot();
60
		*/
61
		Map<String, MapWrapper<? extends CdmBase>> stores = state.getStores();
62
		MapWrapper<TeamOrPersonBase> authorStore = (MapWrapper<TeamOrPersonBase>)stores.get(ICdmIO.TEAM_STORE);
63
		TransactionStatus txStatus = null;
64
		
65
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
66
		Source source = fauEuConfig.getSource();
67
		
68
		String namespace = "AuthorTeam";
69
		
70
		if(logger.isInfoEnabled()) { logger.info("Start making Authors..."); }
71
		
72
		try {
73

    
74
			String strQuery = 
75
				" SELECT *  " +
76
				" FROM author " ;
77
			ResultSet rs = source.getResultSet(strQuery) ;
78

    
79
			int i = 0;
80
			while (rs.next()) {
81

    
82
				if ((i++ % modCount) == 0 && i!= 1 ) { 
83
					if(logger.isDebugEnabled()) {
84
						logger.debug("Authors retrieved: " + (i-1)); 
85
					}
86
				}
87

    
88
				int authorId = rs.getInt("aut_id");
89
				String authorName = rs.getString("aut_name");
90

    
91
				String auctWithNecRegEx = "\\bauct\\b\\.?.*\\bnec\\b\\.?.*";
92
				String necAuctRegEx = "\\bnec\\b\\.?.*\\bauct\\b\\.?.*";
93
				
94
				boolean auctWithNecFound = expressionMatches(auctWithNecRegEx, authorName);
95
				boolean necAuctFound = expressionMatches(necAuctRegEx, authorName);
96
				if (auctWithNecFound){
97
					logger.debug("authorName before auct nec string is removed" + authorName);
98
					authorName = authorName.substring(expressionEnd("nec\\.?", authorName)+1, authorName.length());
99
					logger.debug("authorName after auct nec string is removed" + authorName);
100
				}
101
				
102
				if (necAuctFound){
103
					logger.debug("authorName before nec auct string is removed" + authorName);
104
					authorName = authorName.substring(0, authorName.indexOf("nec")-1);
105
					logger.debug("authorName before nec auct string is removed" + authorName);
106
				}
107
				TeamOrPersonBase<Team> author = null;
108

    
109
				try {
110
					author = Team.NewInstance();
111
					author.setTitleCache(authorName, true);
112

    
113
					ImportHelper.setOriginalSource(author, fauEuConfig.getSourceReference(), authorId, namespace);
114

    
115
					if (!authorStore.containsId(authorId)) {
116
						authorStore.put(authorId, author);
117
						if (logger.isDebugEnabled()) { logger.debug("Stored author (" + authorId + ") " + authorName); }
118
					} else {
119
						logger.warn("Not imported author with duplicated aut_id (" + authorId + ") " + authorName);
120
					}
121
				} catch (Exception e) {
122
					logger.warn("An exception occurred when creating author with id " + authorId + ". Author could not be saved.");
123
				}
124
			}
125
			
126
			if(logger.isInfoEnabled()) { logger.info("Saving authors ..."); }
127

    
128
			txStatus = startTransaction();
129

    
130
			// save authors
131
			getAgentService().save((Collection)authorStore.objects());
132

    
133
			commitTransaction(txStatus);
134
			
135
			if(logger.isInfoEnabled()) { logger.info("End making authors ..."); }
136

    
137
			return;
138

    
139
		} catch (SQLException e) {
140
			logger.error("SQLException:" +  e);
141
			state.setUnsuccessfull();
142
		}
143
	}
144
	
145
	/* (non-Javadoc)
146
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
147
	 */
148
	protected boolean isIgnore(FaunaEuropaeaImportState state){
149
		return ! state.getConfig().isDoAuthors();
150
	}
151

    
152
}
(2-2/17)