Project

General

Profile

Download (6.99 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.mapping;
11

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

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

    
20
import eu.etaxonomy.cdm.api.service.ITermService;
21
import eu.etaxonomy.cdm.api.service.IVocabularyService;
22
import eu.etaxonomy.cdm.database.update.DatabaseTypeNotSupportedException;
23
import eu.etaxonomy.cdm.io.common.CdmImportBase;
24
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
25
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.common.Marker;
28
import eu.etaxonomy.cdm.model.common.MarkerType;
29
import eu.etaxonomy.cdm.model.term.TermVocabulary;
30

    
31
/**
32
 * This class maps a database attribute to CDM extension added to the target class
33
 * TODO maybe this class should not inherit from DbSingleAttributeImportMapperBase
34
 * as it does not map to a single attribute
35
 * @author a.mueller
36
 * @since 12.05.2009
37
 * @version 1.0
38
 */
39
public class DbImportMarkerMapper extends DbSingleAttributeImportMapperBase<DbImportStateBase<?,?>, AnnotatableEntity> implements IDbImportMapper<DbImportStateBase<?,?>,AnnotatableEntity>{
40
	private static final Logger logger = Logger.getLogger(DbImportMarkerMapper.class);
41

    
42
//************************** FACTORY METHODS ***************************************************************/
43

    
44
	/**
45
	 * @param dbAttributeString
46
	 * @param uuid
47
	 * @param label
48
	 * @param text
49
	 * @param labelAbbrev
50
	 * @return
51
	 */
52
	public static DbImportMarkerMapper NewInstance(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev, Boolean ignoreValue){
53
		return new DbImportMarkerMapper(dbAttributeString, uuid, label, text, labelAbbrev, ignoreValue);
54
	}
55

    
56
	public static DbImportMarkerMapper NewInstance(String dbAttributeString, MarkerType markerType, Boolean ignoreValue){
57
		return new DbImportMarkerMapper(dbAttributeString, markerType, ignoreValue);
58
	}
59

    
60
//***************** VARIABLES **********************************************************/
61

    
62
	private MarkerType markerType;
63
	private String label;
64
	private String text;
65
	private String labelAbbrev;
66
	private UUID uuid;
67
	private Boolean ignoreValue;
68

    
69
//******************************** CONSTRUCTOR *****************************************************************/
70
	/**
71
	 * @param dbAttributeString
72
	 * @param uuid
73
	 * @param label
74
	 * @param text
75
	 * @param labelAbbrev
76
	 */
77
	private DbImportMarkerMapper(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev, Boolean ignoreValue) {
78
		super(dbAttributeString, dbAttributeString);
79
		this.uuid = uuid;
80
		this.label = label;
81
		this.text = text;
82
		this.labelAbbrev = labelAbbrev;
83
		this.ignoreValue = ignoreValue;
84
	}
85

    
86
	/**
87
	 * @param dbAttributeString
88
	 * @param extensionType
89
	 */
90
	private DbImportMarkerMapper(String dbAttributeString, MarkerType markerType, Boolean ignoreValue) {
91
		super(dbAttributeString, dbAttributeString);
92
		this.markerType = markerType;
93
		this.ignoreValue = ignoreValue;
94
	}
95

    
96
//****************************** METHODS ***************************************************/
97

    
98
	@Override
99
	public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
100
		importMapperHelper.initialize(state, destinationClass);
101
		CdmImportBase<?, ?> currentImport = state.getCurrentIO();
102
		if (currentImport == null){
103
			throw new IllegalStateException("Current import is not available. Please make sure the the state knows about the current import (state.setCurrentImport())) !");
104
		}
105

    
106
		try {
107
			if (  checkDbColumnExists()){
108
				if (this.markerType == null){
109
					this.markerType = getMarkerType(currentImport, uuid, label, text, labelAbbrev);
110
				}
111
			}else{
112
				ignore = true;
113
			}
114
		} catch (DatabaseTypeNotSupportedException e) {
115
			//do nothing  - checkDbColumnExists is not possible
116
		}
117
	}
118

    
119
	public boolean invoke(Map<String, Object> valueMap, CdmBase cdmBase){
120
		Object dbValueObject = valueMap.get(this.getSourceAttribute().toLowerCase());
121
		Boolean dbValue = (Boolean) (dbValueObject == null? null: dbValueObject);
122
		return invoke(dbValue, cdmBase);
123
	}
124

    
125
	private boolean invoke(Boolean dbValue, CdmBase cdmBase){
126
		if (ignore){
127
			return true;
128
		}
129
		if (cdmBase instanceof AnnotatableEntity){
130
			AnnotatableEntity annotatableEntity = (AnnotatableEntity) cdmBase;
131
			invoke(dbValue, annotatableEntity);
132
			return true;
133
		}else{
134
			throw new IllegalArgumentException("marked object must be of type annotatable entity.");
135
		}
136

    
137
	}
138
	
139
	/* (non-Javadoc)
140
	 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
141
	 */
142
	public AnnotatableEntity invoke(ResultSet rs, AnnotatableEntity annotatableEntity) throws SQLException {
143
		Boolean dbValue = rs.getBoolean(getSourceAttribute());
144
		return invoke(dbValue, annotatableEntity);
145
	}
146

    
147
	private AnnotatableEntity invoke(Boolean dbValue, AnnotatableEntity annotatableEntity){
148
		if (ignore){
149
			return annotatableEntity;
150
		}
151
		if (dbValue != null && ! dbValue.equals(this.ignoreValue)){
152
			Marker.NewInstance(annotatableEntity, dbValue, this.markerType);
153
			if (this.markerType == null){
154
				logger.warn("No marker type available for marker");
155
			}
156
		}
157
		return annotatableEntity;
158
	}
159

    
160

    
161
	/**
162
	 * @param currentImport
163
	 * @param uuid
164
	 * @param label
165
	 * @param text
166
	 * @param labelAbbrev
167
	 * @return
168
	 */
169
	protected MarkerType getMarkerType(CdmImportBase<?, ?> currentImport, UUID uuid, String label, String text, String labelAbbrev){
170
		ITermService termService = currentImport.getTermService();
171
		MarkerType markerType = (MarkerType)termService.find(uuid);
172
		if (markerType == null){
173
			//create object
174
			markerType = MarkerType.NewInstance(text, label, labelAbbrev);
175
			markerType.setUuid(uuid);
176
			//set vocabulary //TODO allow user defined vocabularies
177
			UUID uuidMarkerTypeVocabulary = UUID.fromString("19dffff7-e142-429c-a420-5d28e4ebe305");
178
			IVocabularyService vocService = currentImport.getVocabularyService();
179
			TransactionStatus tx = currentImport.startTransaction();
180
			TermVocabulary voc = vocService.find(uuidMarkerTypeVocabulary);
181
			if (voc != null){
182
				voc.addTerm(markerType);
183
			}else{
184
				logger.warn("Could not find default markerType vocabulary. Vocabulary not set for new marker type.");
185
			}
186
			//save
187
			termService.save(markerType);
188
			currentImport.commitTransaction(tx);
189
		}
190
		return markerType;
191
	}
192

    
193

    
194
	//not used
195
	/* (non-Javadoc)
196
	 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
197
	 */
198
	@Override
199
    public Class<Boolean> getTypeClass(){
200
		return Boolean.class;
201
	}
202

    
203

    
204

    
205
}
(24-24/53)