Project

General

Profile

« Previous | Next » 

Revision 16932594

Added by Katja Luther over 8 years ago

adapt fauna europaea userimport

View differences:

cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/faunaEuropaea/FaunaEuropaeaUsersImport.java
25 25
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
26 26
import eu.etaxonomy.cdm.io.common.ImportHelper;
27 27
import eu.etaxonomy.cdm.io.common.Source;
28
import eu.etaxonomy.cdm.model.agent.AgentBase;
29
import eu.etaxonomy.cdm.model.agent.Person;
28 30
import eu.etaxonomy.cdm.model.agent.Team;
29 31
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
30 32
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
33
import eu.etaxonomy.cdm.model.common.User;
31 34
import eu.etaxonomy.cdm.model.reference.Reference;
32 35
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
33 36

  
......
79 82
		ProfilerController.memorySnapshot();
80 83
		*/
81 84
		TransactionStatus txStatus = null;
82
		Map<Integer, Reference> references = null;
83
		Map<String,TeamOrPersonBase> authors = null;
84
		Map<Integer, UUID> referenceUuids = new HashMap<Integer, UUID>();
85
		Map<String, AgentBase<?>> persons = null;
86
		Map<Integer, User> users= null;
87
		Map<Integer, UUID> userUuids = new HashMap<Integer, UUID>();
85 88
		int limit = state.getConfig().getLimitSave();
86 89

  
87 90
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
88 91
		Source source = fauEuConfig.getSource();
89 92

  
90
		String namespace = "Reference";
93
		String namespace = "User";
91 94
		int i = 0;
92 95

  
93 96
		String selectCountUsers = 
94 97
			" SELECT count(*) FROM Users";
95 98

  
96 99
		String selectColumnsUsers = 
97
			" SELECT usr_id, usr_title, usr_firstname, usr_lastname, usr_createdat FROM Users";
100
			" SELECT usr_id, usr_title, usr_firstname, usr_lastname, usr_createdat, usr_password FROM Users";
98 101

  
99 102
		int count;
100 103
		if(logger.isInfoEnabled()) { logger.info("Start making References (Users)..."); }
101 104

  
102 105
		try {
103
			ResultSet rsRefs = source.getResultSet(selectCountUsers);
104
			rsRefs.next();
105
			count = rsRefs.getInt(1);
106
			ResultSet rsUser = source.getResultSet(selectCountUsers);
107
			rsUser.next();
108
			count = rsUser.getInt(1);
106 109
			
107
			rsRefs = source.getResultSet(selectColumnsUsers);
110
			rsUser= source.getResultSet(selectColumnsUsers);
108 111

  
109 112
	        if (logger.isInfoEnabled()) {
110 113
	        	logger.info("Get all References..."); 
......
113 116
				logger.info("Select Query: " + selectColumnsUsers);
114 117
			}
115 118
	        
116
	        while (rsRefs.next()){
117
	        	int refId = rsRefs.getInt("usr_id");
118
				String refTitle = rsRefs.getString("usr_title");
119
				String refFirstname = rsRefs.getString("usr_firstname");
120
				String refLastname = rsRefs.getString("usr_lastname");
121
				String createdDate = rsRefs.getString("usr_createdat");
119
	        while (rsUser.next()){
120
	        	int userId = rsUser.getInt("usr_id");
121
				String userTitle = rsUser.getString("usr_title");
122
				String userFirstname = rsUser.getString("usr_firstname");
123
				String userLastname = rsUser.getString("usr_lastname");
124
				String createdDate = rsUser.getString("usr_createdat");
125
				String userPwd = rsUser.getString("usr_password");
122 126

  
123
				// build author
124
				String refAuthor = "";
125
				if (refTitle != null) {
126
					refAuthor = refTitle;
127
					if (! refTitle.endsWith(".")) {
128
						refAuthor += ".";
127
				// build person
128
				String userPerson = "";
129
				if (userTitle != null) {
130
					userPerson = userTitle;
131
					if (! userTitle.endsWith(".")) {
132
						userPerson += ".";
129 133
					}
130 134
				}
131
				refAuthor += refTitle == null ? NullToEmpty(refFirstname) : " " + NullToEmpty(refFirstname);
132
				if ((refTitle != null || refFirstname != null) && refLastname != null) {
133
					refAuthor += " " + refLastname;
135
				userPerson += userTitle == null ? NullToEmpty(userFirstname) : " " + NullToEmpty(userFirstname);
136
				if ((userTitle != null || userFirstname != null) && userLastname != null) {
137
					userPerson += " " + userLastname;
134 138
				}
135 139

  
136 140
				// build year
......
142 146
				if ((i++ % limit) == 0) {
143 147

  
144 148
					txStatus = startTransaction();
145
					references = new HashMap<Integer,Reference>(limit);
146
					authors = new HashMap<String,TeamOrPersonBase>(limit);
149
					persons= new HashMap<String,AgentBase<?>>(limit);
150
					users = new HashMap<Integer,User>(limit);
147 151
					
148 152
					if(logger.isInfoEnabled()) {
149
						logger.info("i = " + i + " - Reference import transaction started"); 
153
						logger.info("i = " + i + " - User import transaction started"); 
150 154
					}
151 155
				}
152 156
				
153
				Reference<?> reference = null;
154
				TeamOrPersonBase<Team> author = null;
155
				reference = ReferenceFactory.newGeneric();
156

  
157
				reference.setTitle("" + refId); // This unique key is needed to get a hand on this Reference in PesiTaxonExport
158
				reference.setDatePublished(ImportHelper.getDatePublished(year));
157
				AgentBase<?> person = null;
158
				User user = null;
159
				person= Person.NewTitledInstance(userTitle);
160
				user = User.NewInstance(userPerson, userPwd);
161
				//reference.setTitle("" + refId); // This unique key is needed to get a hand on this Reference in PesiTaxonExport
162
				//reference.setDatePublished(ImportHelper.getDatePublished(year));
159 163
				
160
				if (!authors.containsKey(refAuthor)) {
161
					if (refAuthor == null) {
162
						logger.warn("Reference author is null");
164
				if (!persons.containsKey(userPerson)) {
165
					if (userPerson == null) {
166
						logger.warn("User is null");
163 167
					}
164
					author = Team.NewInstance();
165
					author.setTitleCache(refAuthor, true);
166
					authors.put(refAuthor,author); 
168
									
169
					persons.put(userPerson, person); 
167 170
					if (logger.isTraceEnabled()) { 
168
						logger.trace("Stored author (" + refAuthor + ")");
171
						logger.trace("Stored user (" + userPerson + ")");
169 172
					}
170 173
				//}
171 174

  
172 175
				} else {
173
					author = authors.get(refAuthor);
176
					person = persons.get(userPerson);
174 177
					if (logger.isDebugEnabled()) { 
175
						logger.debug("Not imported author with duplicated aut_id (" + refId + 
176
							") " + refAuthor);
178
						logger.debug("Not imported user with duplicated user_id (" + userId + 
179
							") " + userPerson);
177 180
					}
178 181
				}
179 182
				
180 183
				// set protected titleCache
181
				StringBuilder referenceTitleCache = new StringBuilder(author.getTitleCache() + ".");
184
				/*StringBuilder referenceTitleCache = new StringBuilder(user.getTitleCache() + ".");
182 185
				if (year != null) {
183 186
					referenceTitleCache.append(" " + year);
184 187
				}
185 188
				reference.setTitleCache(referenceTitleCache.toString(), true);
186 189
				
187
				reference.setAuthorTeam(author);
190
				reference.setAuthorTeam(author);*/
188 191
				
189
				ImportHelper.setOriginalSource(reference, fauEuConfig.getSourceReference(), refId, namespace);
190
				ImportHelper.setOriginalSource(author, fauEuConfig.getSourceReference(), refId, namespace);
192
				//ImportHelper.setOriginalSource(user, fauEuConfig.getSourceReference(), userId, namespace);
193
				ImportHelper.setOriginalSource(person, fauEuConfig.getSourceReference(), userId, namespace);
191 194

  
192 195
				
193
				// Store reference
194
				if (!references.containsKey(refId)) {
196
				// Store persons
197
				if (!users.containsKey(userId)) {
195 198

  
196
					if (reference == null) {
197
						logger.warn("Reference is null");
199
					if (user == null) {
200
						logger.warn("User is null");
198 201
					}
199
					references.put(refId, reference);
202
					users.put(userId, user);
200 203
					if (logger.isTraceEnabled()) { 
201
						logger.trace("Stored reference (" + refAuthor + ")"); 
204
						logger.trace("Stored user (" + userTitle + ")"); 
202 205
					}
203 206
				} else {
204 207
					if (logger.isDebugEnabled()) { 
205
						logger.debug("Duplicated reference (" + refId + ", " + refAuthor + ")");
208
						logger.debug("Duplicated user(" + userId + ", " + userTitle+ ")");
206 209
					}
207 210
					//continue;
208 211
				}
209 212
				
210 213
				if (((i % limit) == 0 && i > 1 ) || i == count ) { 
211 214
					
212
					commitReferences(txStatus, references, authors,
213
							referenceUuids, i);
215
					commitUsers(txStatus, persons, users,
216
							userUuids, i);
214 217
					
215
					authors = null;					
216
					references= null;
218
					users = null;					
219
					persons= null;
217 220
				}
218 221
	        	
219 222
	        }
220
	        if (references != null){
221
	        	commitReferences(txStatus, references, authors, referenceUuids, i);
222
	        	authors = null;					
223
				references= null;
223
	        if (users != null){
224
	        	commitUsers(txStatus, persons, users, userUuids, i);
225
	        	users = null;					
226
				persons= null;
224 227
	        }
225 228
		}catch(SQLException e) {
226 229
			logger.error("SQLException:" +  e);
......
235 238
		return;
236 239
	}
237 240

  
238
	private void commitReferences(TransactionStatus txStatus,
239
			Map<Integer, Reference> references,
240
			Map<String, TeamOrPersonBase> authors,
241
			Map<Integer, UUID> referenceUuids, int i) {
242
		Map <UUID, Reference> referenceMap =getReferenceService().save(references.values());
243
		logger.info("i = " + i + " - references saved"); 
241
	private void commitUsers(TransactionStatus txStatus,
242
			Map<String, AgentBase<?>> persons,
243
			Map<Integer, User> users,
244
			Map<Integer, UUID> userUuids, int i) {
245
		Map<UUID, AgentBase> userMap =getAgentService().save((Collection)persons.values());
246
		logger.info("i = " + i + " - users saved"); 
244 247

  
245
		Iterator<Entry<UUID, Reference>> it = referenceMap.entrySet().iterator();
248
		Iterator<Entry<UUID, AgentBase>> it = userMap.entrySet().iterator();
246 249
		while (it.hasNext()){
247
			Reference ref = it.next().getValue();
248
			int refID = Integer.valueOf(((OriginalSourceBase)ref.getSources().iterator().next()).getIdInSource());
249
			UUID uuid = ref.getUuid();
250
			referenceUuids.put(refID, uuid);
250
			AgentBase person = it.next().getValue();
251
			int userID = Integer.valueOf(((OriginalSourceBase)person.getSources().iterator().next()).getIdInSource());
252
			UUID uuid = person.getUuid();
253
			userUuids.put(userID, uuid);
251 254
		}
252 255
		
253
		getAgentService().save((Collection)authors.values());
256
		getUserService().save((Collection)users.values());
254 257
		commitTransaction(txStatus);
255 258
	}
256 259

  

Also available in: Unified diff