252315587d1ee243cf03f763963c60ece0815cf4
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportMarkerMapper.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.cdm.io.common.mapping;
12
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.util.UUID;
16
17 import javax.mail.MethodNotSupportedException;
18
19 import org.apache.log4j.Logger;
20
21 import eu.etaxonomy.cdm.api.service.ITermService;
22 import eu.etaxonomy.cdm.api.service.IVocabularyService;
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.common.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 * @created 12.05.2009
37 * @version 1.0
38 */
39 public class DbImportMarkerMapper extends DbSingleAttributeImportMapperBase<DbImportStateBase<?,?>, AnnotatableEntity> implements IDbImportMapper<DbImportStateBase<?,?>,AnnotatableEntity>{
40 @SuppressWarnings("unused")
41 private static final Logger logger = Logger.getLogger(DbImportMarkerMapper.class);
42
43 //************************** FACTORY METHODS ***************************************************************/
44
45 /**
46 * @param dbAttributeString
47 * @param uuid
48 * @param label
49 * @param text
50 * @param labelAbbrev
51 * @return
52 */
53 public static DbImportMarkerMapper NewInstance(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev){
54 return new DbImportMarkerMapper(dbAttributeString, uuid, label, text, labelAbbrev);
55 }
56
57 public static DbImportMarkerMapper NewInstance(String dbAttributeString, MarkerType markerType){
58 return new DbImportMarkerMapper(dbAttributeString, markerType);
59 }
60
61 //***************** VARIABLES **********************************************************/
62
63 private MarkerType markerType;
64 private String label;
65 private String text;
66 private String labelAbbrev;
67 private UUID uuid;
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) {
78 super(dbAttributeString, dbAttributeString);
79 this.uuid = uuid;
80 this.label = label;
81 this.text = text;
82 this.labelAbbrev = labelAbbrev;
83 }
84
85 /**
86 * @param dbAttributeString
87 * @param extensionType
88 */
89 private DbImportMarkerMapper(String dbAttributeString, MarkerType markerType) {
90 super(dbAttributeString, dbAttributeString);
91 this.markerType = markerType;
92 }
93
94 //****************************** METHODS ***************************************************/
95
96 /* (non-Javadoc)
97 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
98 */
99 @Override
100 public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
101 importMapperHelper.initialize(state, destinationClass);
102 CdmImportBase<?, ?> currentImport = state.getCurrentIO();
103 if (currentImport == null){
104 throw new IllegalStateException("Current import is not available. Please make sure the the state knows about the current import (state.setCurrentImport())) !");
105 }
106
107 try {
108 if ( checkDbColumnExists()){
109 if (this.markerType == null){
110 this.markerType = getMarkerType(currentImport, uuid, label, text, labelAbbrev);
111 }
112 }else{
113 ignore = true;
114 }
115 } catch (MethodNotSupportedException e) {
116 //do nothing - checkDbColumnExists is not possible
117 }
118 }
119
120
121 /**
122 * @param dbValue
123 * @param cdmBase
124 * @return
125 */
126 private boolean invoke(Boolean dbValue, CdmBase cdmBase){
127 if (ignore){
128 return true;
129 }
130 if (cdmBase instanceof AnnotatableEntity){
131 AnnotatableEntity annotatableEntity = (AnnotatableEntity) cdmBase;
132 invoke(dbValue, annotatableEntity);
133 return true;
134 }else{
135 throw new IllegalArgumentException("marked object must be of type annotatable entity.");
136 }
137
138 }
139
140 /* (non-Javadoc)
141 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
142 */
143 public AnnotatableEntity invoke(ResultSet rs, AnnotatableEntity annotatableEntity) throws SQLException {
144 Boolean dbValue = rs.getBoolean(getSourceAttribute());
145 return invoke(dbValue, annotatableEntity);
146 }
147
148 /**
149 * @param dbValue
150 * @param identifiableEntity
151 * @return
152 */
153 private AnnotatableEntity invoke(Boolean dbValue, AnnotatableEntity annotatableEntity){
154 if (ignore){
155 return annotatableEntity;
156 }
157 if (dbValue != null){
158 Marker.NewInstance(annotatableEntity, dbValue, this.markerType);
159 }
160 return annotatableEntity;
161 }
162
163
164 /**
165 * @param currentImport
166 * @param uuid
167 * @param label
168 * @param text
169 * @param labelAbbrev
170 * @return
171 */
172 protected MarkerType getMarkerType(CdmImportBase<?, ?> currentImport, UUID uuid, String label, String text, String labelAbbrev){
173 ITermService termService = currentImport.getTermService();
174 MarkerType markerType = (MarkerType)termService.find(uuid);
175 if (markerType == null){
176 //create object
177 markerType = MarkerType.NewInstance(text, label, labelAbbrev);
178 markerType.setUuid(uuid);
179 //set vocabulary //TODO allow user defined vocabularies
180 UUID uuidMarkerTypeVocabulary = UUID.fromString("19dffff7-e142-429c-a420-5d28e4ebe305");
181 IVocabularyService vocService = currentImport.getVocabularyService();
182 TermVocabulary voc = vocService.find(uuidMarkerTypeVocabulary);
183 if (voc != null){
184 voc.addTerm(markerType);
185 }else{
186 logger.warn("Could not find default markerType vocabulary. Vocabulary not set for new marker type.");
187 }
188 //save
189 termService.save(markerType);
190 }
191 return markerType;
192 }
193
194
195 //not used
196 /* (non-Javadoc)
197 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
198 */
199 public Class<Boolean> getTypeClass(){
200 return Boolean.class;
201 }
202
203
204
205 }