ref #8162 adapt cdmlip to new term package structure
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportMarkerMapper.java
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 /* (non-Javadoc)
99 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
100 */
101 @Override
102 public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
103 importMapperHelper.initialize(state, destinationClass);
104 CdmImportBase<?, ?> currentImport = state.getCurrentIO();
105 if (currentImport == null){
106 throw new IllegalStateException("Current import is not available. Please make sure the the state knows about the current import (state.setCurrentImport())) !");
107 }
108
109 try {
110 if ( checkDbColumnExists()){
111 if (this.markerType == null){
112 this.markerType = getMarkerType(currentImport, uuid, label, text, labelAbbrev);
113 }
114 }else{
115 ignore = true;
116 }
117 } catch (DatabaseTypeNotSupportedException e) {
118 //do nothing - checkDbColumnExists is not possible
119 }
120 }
121
122
123 /**
124 * @param valueMap
125 * @param cdmBase
126 * @return
127 */
128 public boolean invoke(Map<String, Object> valueMap, CdmBase cdmBase){
129 Object dbValueObject = valueMap.get(this.getSourceAttribute().toLowerCase());
130 Boolean dbValue = (Boolean) (dbValueObject == null? null: dbValueObject);
131 return invoke(dbValue, cdmBase);
132 }
133
134 /**
135 * @param dbValue
136 * @param cdmBase
137 * @return
138 */
139 private boolean invoke(Boolean dbValue, CdmBase cdmBase){
140 if (ignore){
141 return true;
142 }
143 if (cdmBase instanceof AnnotatableEntity){
144 AnnotatableEntity annotatableEntity = (AnnotatableEntity) cdmBase;
145 invoke(dbValue, annotatableEntity);
146 return true;
147 }else{
148 throw new IllegalArgumentException("marked object must be of type annotatable entity.");
149 }
150
151 }
152
153 /* (non-Javadoc)
154 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
155 */
156 public AnnotatableEntity invoke(ResultSet rs, AnnotatableEntity annotatableEntity) throws SQLException {
157 Boolean dbValue = rs.getBoolean(getSourceAttribute());
158 return invoke(dbValue, annotatableEntity);
159 }
160
161 /**
162 * @param dbValue
163 * @param identifiableEntity
164 * @return
165 */
166 private AnnotatableEntity invoke(Boolean dbValue, AnnotatableEntity annotatableEntity){
167 if (ignore){
168 return annotatableEntity;
169 }
170 if (dbValue != null && ! dbValue.equals(this.ignoreValue)){
171 Marker.NewInstance(annotatableEntity, dbValue, this.markerType);
172 if (this.markerType == null){
173 logger.warn("No marker type available for marker");
174 }
175 }
176 return annotatableEntity;
177 }
178
179
180 /**
181 * @param currentImport
182 * @param uuid
183 * @param label
184 * @param text
185 * @param labelAbbrev
186 * @return
187 */
188 protected MarkerType getMarkerType(CdmImportBase<?, ?> currentImport, UUID uuid, String label, String text, String labelAbbrev){
189 ITermService termService = currentImport.getTermService();
190 MarkerType markerType = (MarkerType)termService.find(uuid);
191 if (markerType == null){
192 //create object
193 markerType = MarkerType.NewInstance(text, label, labelAbbrev);
194 markerType.setUuid(uuid);
195 //set vocabulary //TODO allow user defined vocabularies
196 UUID uuidMarkerTypeVocabulary = UUID.fromString("19dffff7-e142-429c-a420-5d28e4ebe305");
197 IVocabularyService vocService = currentImport.getVocabularyService();
198 TransactionStatus tx = currentImport.startTransaction();
199 TermVocabulary voc = vocService.find(uuidMarkerTypeVocabulary);
200 if (voc != null){
201 voc.addTerm(markerType);
202 }else{
203 logger.warn("Could not find default markerType vocabulary. Vocabulary not set for new marker type.");
204 }
205 //save
206 termService.save(markerType);
207 currentImport.commitTransaction(tx);
208 }
209 return markerType;
210 }
211
212
213 //not used
214 /* (non-Javadoc)
215 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
216 */
217 public Class<Boolean> getTypeClass(){
218 return Boolean.class;
219 }
220
221
222
223 }