Project

General

Profile

Download (8.1 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

    
10
package eu.etaxonomy.cdm.io.algaterra;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.Map;
17
import java.util.Set;
18

    
19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21

    
22
import eu.etaxonomy.cdm.io.algaterra.validation.AlgaTerraCollectionImportValidator;
23
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase;
24
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator;
25
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState;
26
import eu.etaxonomy.cdm.io.common.IOValidator;
27
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.occurrence.Collection;
30
import eu.etaxonomy.cdm.model.reference.Reference;
31

    
32

    
33
/**
34
 * @author a.mueller
35
 * @created 01.09.2012
36
 */
37
@Component
38
public class AlgaTerraCollectionImport  extends BerlinModelImportBase {
39
	private static final Logger logger = Logger.getLogger(AlgaTerraCollectionImport.class);
40

    
41
	
42
	private static int modCount = 5000;
43
	private static final String pluralString = "collections";
44
	private static final String dbTableName = "Collection";  //??  
45
	
46
	public static final String NAMESPACE_COLLECTION = "Collection"; 
47
	public static final String NAMESPACE_SUBCOLLECTION = "Collection (Subcollection)"; 
48

    
49

    
50
	public AlgaTerraCollectionImport(){
51
		super(dbTableName, pluralString);
52
	}
53

    
54
	
55
	/* (non-Javadoc)
56
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getIdQuery()
57
	 */
58
	@Override
59
	protected String getIdQuery(BerlinModelImportState state) {
60
		String result = " SELECT CollectionId " + 
61
				" FROM Collection c "
62
				+ " ORDER BY partOfFk, c.CollectionId ";
63
		return result;
64
	}
65

    
66
	/* (non-Javadoc)
67
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
68
	 */
69
	@Override
70
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
71
			String strQuery =   
72
            " SELECT CollectionId, Name, Town, IHCode, Subcollection, partOfFk, TDWGGazetteerFk, Address, CultCollFlag, " +
73
            		" Created_When, Updated_When, Created_Who, Updated_Who, Notes " +
74
            " FROM Collection c " + 
75
            " WHERE c.CollectionId IN (" + ID_LIST_TOKEN + ") "  
76
            + " ORDER BY partOfFk, c.CollectionId "
77
            ;
78
		return strQuery;
79
	}
80

    
81
	/* (non-Javadoc)
82
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#doPartition(eu.etaxonomy.cdm.io.berlinModel.in.ResultSetPartitioner, eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
83
	 */
84
	public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState bmState) {
85
		boolean success = true;
86
		
87
		AlgaTerraImportState state = (AlgaTerraImportState)bmState;
88
		Set<Collection> collectionsToSave = new HashSet<Collection>();
89

    
90
		
91
		Map<String, Collection> collectionMap = (Map<String, Collection>) partitioner.getObjectMap(NAMESPACE_COLLECTION);
92
		
93
		
94
		ResultSet rs = partitioner.getResultSet();
95

    
96
		try {
97
			
98
			int i = 0;
99

    
100
			//for each reference
101
            while (rs.next()){
102
                
103
        		if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
104
				
105
        		int collectionId = rs.getInt("CollectionId");
106
        		String name = rs.getString("Name");
107
        		String town = rs.getString("Town");
108
        		String ihCode = rs.getString("IHCode");
109
        		String subCollectionStr = rs.getString("Subcollection");
110
        		Integer partOfFk = nullSafeInt(rs, "PartOfFk");
111
        		
112
//        		Integer tdwgArea = nullSafeInt(rs, "TDWGGazetteerFk");  //somehow redundant with town
113
//        		String address = rs.getString("Address");           //only available for BGBM
114
//        		Boolean cultCollFlag = rs.getBoolean("CultCollFlag");  //?? not really needed according to Henning
115
        		
116
        		//TODO createdUpdates, NOtes		
117
      
118
        		try {
119
					
120
					//source ref
121
					Reference<?> sourceRef = state.getTransactionalSourceReference();
122
				
123
					
124
					//collection
125
					Collection collection;
126
					if (partOfFk == null){
127
						collection = makeCollection(collectionsToSave,
128
								collectionId, name, town, ihCode, sourceRef, NAMESPACE_COLLECTION, collectionMap);
129
					}else{
130
						collection = collectionMap.get(String.valueOf(partOfFk));
131
						if (collection == null){
132
							logger.warn("PartOf collection not found");
133
						}
134
					}
135
					
136

    
137
					//subcollection
138
					if (isNotBlank(subCollectionStr)){
139
						Collection subCollection = makeCollection(collectionsToSave, collectionId, subCollectionStr, town, ihCode, sourceRef, NAMESPACE_SUBCOLLECTION, collectionMap);
140
						subCollection.setSuperCollection(collection);
141
					}
142
					
143
					
144
					//TODO movedToFk (extension ??)
145
					
146

    
147
				} catch (Exception e) {
148
					logger.warn("Exception in collection: CollectionId " + collectionId + ". " + e.getMessage());
149
//					e.printStackTrace();
150
				} 
151
                
152
            }
153
           
154
			logger.warn(pluralString + " to save: " + collectionsToSave.size());
155
			getCollectionService().save(collectionsToSave);	
156
			
157
			return success;
158
		} catch (SQLException e) {
159
			logger.error("SQLException:" +  e);
160
			return false;
161
		}
162
	}
163

    
164

    
165

    
166
	/**
167
	 * @param collectionsToSave
168
	 * @param collectionId
169
	 * @param name
170
	 * @param town
171
	 * @param ihCode
172
	 * @param sourceRef
173
	 * @param collectionMap 
174
	 * @return
175
	 */
176
	private Collection makeCollection(Set<Collection> collectionsToSave, int collectionId, 
177
			String name, String town, String ihCode, Reference<?> sourceRef, String namespace, Map<String, Collection> collectionMap) {
178
		Collection collection = Collection.NewInstance();
179
		collection.setName(name);
180
		if (isNotBlank(ihCode) && ! "--".equals(ihCode)){
181
			collection.setCode(ihCode);
182
			collection.setCodeStandard("Index Herbariorum");
183
		}
184
		
185
		collection.setTownOrLocation(town);
186
		collection.addSource(String.valueOf(collectionId), namespace, sourceRef, null);
187
		
188
		collectionMap.put(String.valueOf(collectionId), collection);
189
		collectionsToSave.add(collection);  //or subcollection ? 
190
		return collection;
191
	}
192

    
193

    
194

    
195
	/* (non-Javadoc)
196
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
197
	 */
198
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
199
		String nameSpace;
200
		Class cdmClass;
201
		Set<String> idSet;
202
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
203

    
204
		try{
205
			Set<String> collectionIdSet = new HashSet<String>();
206
			
207
			while (rs.next()){
208
				handleForeignKey(rs, collectionIdSet, "partOfFk");
209
			}
210
			
211
			//type specimen map
212
			nameSpace = NAMESPACE_COLLECTION;
213
			cdmClass = Collection.class;
214
			idSet = collectionIdSet;
215
			Map<String, Collection> collectionMap = (Map<String, Collection>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
216
			result.put(nameSpace, collectionMap);
217

    
218
			
219
		} catch (SQLException e) {
220
			throw new RuntimeException(e);
221
		}
222
		return result;
223
		
224
	}
225

    
226

    
227
	/* (non-Javadoc)
228
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
229
	 */
230
	@Override
231
	protected boolean doCheck(BerlinModelImportState state){
232
		IOValidator<BerlinModelImportState> validator = new AlgaTerraCollectionImportValidator();
233
		return validator.validate(state);
234
	}
235

    
236
	/* (non-Javadoc)
237
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
238
	 */
239
	protected boolean isIgnore(BerlinModelImportState bmState){
240
		AlgaTerraImportState state = (AlgaTerraImportState)bmState;
241
		return ! ( state.getAlgaTerraConfigurator().isDoEcoFacts() ||  state.getAlgaTerraConfigurator().isDoTypes() );
242
	}
243
	
244
}
(1-1/11)