Project

General

Profile

Download (6.97 KB) Statistics
| Branch: | Tag: | 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.common;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.UUID;
15

    
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.common.CdmUtils;
19
import eu.etaxonomy.cdm.model.common.AnnotationType;
20
import eu.etaxonomy.cdm.model.common.CdmBase;
21
import eu.etaxonomy.cdm.model.common.DescriptionElementSource;
22
import eu.etaxonomy.cdm.model.common.ExtensionType;
23
import eu.etaxonomy.cdm.model.common.IOriginalSource;
24
import eu.etaxonomy.cdm.model.common.ISourceable;
25
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
26
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
27
import eu.etaxonomy.cdm.model.common.MarkerType;
28
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
29
import eu.etaxonomy.cdm.model.location.NamedArea;
30
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
31
import eu.etaxonomy.cdm.model.location.NamedAreaType;
32
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
33
import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
34

    
35
/**
36
 * @author a.mueller
37
 * @created 01.07.2008
38
 * @version 1.0
39
 */
40
public abstract class CdmImportBase<CONFIG extends IImportConfigurator, STATE extends ImportStateBase> extends CdmIoBase<STATE> implements ICdmImport<CONFIG, STATE>{
41
	private static Logger logger = Logger.getLogger(CdmImportBase.class);
42

    
43
	protected TaxonomicTree makeTree(STATE state, ReferenceBase ref){
44
		String treeName = "TaxonTree (Import)";
45
		if (ref != null && CdmUtils.isNotEmpty(ref.getTitleCache())){
46
			treeName = ref.getTitleCache();
47
		}
48
		TaxonomicTree tree = TaxonomicTree.NewInstance(treeName);
49
		tree.setReference(ref);
50
		
51

    
52
		// use defined uuid for first tree
53
		CONFIG config = (CONFIG)state.getConfig();
54
		if (state.countTrees() < 1 ){
55
			tree.setUuid(config.getTaxonomicTreeUuid());
56
		}
57
		getTaxonTreeService().save(tree);
58
		state.putTree(ref, tree);
59
		return tree;
60
	}
61
	
62
	
63
	/**
64
	 * Alternative memory saving method variant of
65
	 * {@link #makeTree(STATE state, ReferenceBase ref)} which stores only the
66
	 * UUID instead of the full tree in the <code>ImportStateBase</code> by 
67
	 * using <code>state.putTreeUuid(ref, tree);</code>
68
	 * 
69
	 * @param state
70
	 * @param ref
71
	 * @return
72
	 */
73
	protected TaxonomicTree makeTreeMemSave(STATE state, ReferenceBase ref){
74
		String treeName = "TaxonTree (Import)";
75
		if (ref != null && CdmUtils.isNotEmpty(ref.getTitleCache())){
76
			treeName = ref.getTitleCache();
77
		}
78
		TaxonomicTree tree = TaxonomicTree.NewInstance(treeName);
79
		tree.setReference(ref);
80
		
81

    
82
		// use defined uuid for first tree
83
		CONFIG config = (CONFIG)state.getConfig();
84
		if (state.countTrees() < 1 ){
85
			tree.setUuid(config.getTaxonomicTreeUuid());
86
		}
87
		getTaxonTreeService().save(tree);
88
		state.putTreeUuid(ref, tree);
89
		return tree;
90
	}
91
	
92
	
93
	protected ExtensionType getExtensionType(STATE state, UUID uuid, String label, String text, String labelAbbrev){
94
		ExtensionType extensionType = state.getExtensionType(uuid);
95
		if (extensionType == null){
96
			extensionType = (ExtensionType)getTermService().find(uuid);
97
			if (extensionType == null){
98
				extensionType = ExtensionType.NewInstance(text, label, labelAbbrev);
99
				extensionType.setUuid(uuid);
100
				extensionType.setVocabulary(ExtensionType.DOI().getVocabulary());
101
				getTermService().save(extensionType);
102
			}
103
			state.putExtensionType(extensionType);
104
		}
105
		return extensionType;
106
	}
107
	
108
	protected MarkerType getMarkerType(STATE state, UUID uuid, String label, String text, String labelAbbrev){
109
		MarkerType markerType = state.getMarkerType(uuid);
110
		if (markerType == null){
111
			markerType = (MarkerType)getTermService().find(uuid);
112
			if (markerType == null){
113
				markerType = MarkerType.NewInstance(label, text, labelAbbrev);
114
				markerType.setUuid(uuid);
115
				markerType.setVocabulary(MarkerType.COMPLETE().getVocabulary());
116
				getTermService().save(markerType);
117
			}
118
			state.putMarkerType(markerType);
119
		}
120
		return markerType;
121
	}
122
	
123
	protected AnnotationType getAnnotationType(STATE state, UUID uuid, String label, String text, String labelAbbrev){
124
		AnnotationType annotationType = state.getAnnotationType(uuid);
125
		if (annotationType == null){
126
			annotationType = (AnnotationType)getTermService().find(uuid);
127
			if (annotationType == null){
128
				annotationType = AnnotationType.NewInstance(label, text, labelAbbrev);
129
				annotationType.setUuid(uuid);
130
				annotationType.setVocabulary(AnnotationType.EDITORIAL().getVocabulary());
131
				getTermService().save(annotationType);
132
			}
133
			state.putAnnotationType(annotationType);
134
		}
135
		return annotationType;
136
	}
137
	
138
	/**
139
	 * Returns a named area for a given uuid by first . If the named area does not
140
	 * @param state
141
	 * @param uuid
142
	 * @param label
143
	 * @param text
144
	 * @param labelAbbrev
145
	 * @param areaType
146
	 * @param level
147
	 * @return
148
	 */
149
	protected NamedArea getNamedArea(STATE state, UUID uuid, String label, String text, String labelAbbrev, NamedAreaType areaType, NamedAreaLevel level){
150
		NamedArea namedArea = state.getNamedArea(uuid);
151
		if (namedArea == null){
152
			namedArea = (NamedArea)getTermService().find(uuid);
153
			if (namedArea == null){
154
				namedArea = NamedArea.NewInstance(text, label, labelAbbrev);
155
				namedArea.setType(areaType);
156
				namedArea.setLevel(level);
157
				namedArea.setUuid(uuid);
158
				getTermService().save(namedArea);
159
			}
160
			state.putNamedArea(namedArea);
161
		}
162
		return namedArea;
163
	}
164
	
165
	
166
	
167
	/**
168
	 * Adds an orginal source to a sourceable objects (implemented for Identifiable entity and description element.
169
	 * If cdmBase is not sourceable nothing happens.
170
	 * TODO Move to DbImportBase once this exists.
171
	 * TODO also implemented in DbImportObjectCreationMapper (reduce redundance)
172
	 * @param rs
173
	 * @param cdmBase
174
	 * @param dbIdAttribute
175
	 * @param namespace
176
	 * @param citation
177
	 * @throws SQLException
178
	 */
179
	public void addOriginalSource(ResultSet rs, CdmBase cdmBase, String dbIdAttribute, String namespace, ReferenceBase citation) throws SQLException {
180
		if (cdmBase instanceof ISourceable ){
181
			IOriginalSource source;
182
			ISourceable sourceable = (ISourceable)cdmBase;
183
			Object id = rs.getObject(dbIdAttribute);
184
			String strId = String.valueOf(id);
185
			String microCitation = null;
186
			if (cdmBase instanceof IdentifiableEntity){
187
				source = IdentifiableSource.NewInstance(strId, namespace, citation, microCitation);
188
			}else if (cdmBase instanceof DescriptionElementBase){
189
				source = DescriptionElementSource.NewInstance(strId, namespace, citation, microCitation);
190
			}else{
191
				logger.warn("ISourceable not beeing identifiable entities or description element base are not yet supported. CdmBase is of type " + cdmBase.getClass().getName() + ". Original source not added.");
192
				return;
193
			}
194
			sourceable.addSource(source);
195
		}
196
	}
197
	
198
}
(7-7/41)