View Javadoc

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.Map;
16  import java.util.UUID;
17  
18  import javax.mail.MethodNotSupportedException;
19  
20  import org.apache.log4j.Logger;
21  
22  import eu.etaxonomy.cdm.api.service.ITermService;
23  import eu.etaxonomy.cdm.api.service.IVocabularyService;
24  import eu.etaxonomy.cdm.common.CdmUtils;
25  import eu.etaxonomy.cdm.io.common.CdmImportBase;
26  import eu.etaxonomy.cdm.io.common.DbImportStateBase;
27  import eu.etaxonomy.cdm.model.common.CdmBase;
28  import eu.etaxonomy.cdm.model.common.Extension;
29  import eu.etaxonomy.cdm.model.common.ExtensionType;
30  import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
31  import eu.etaxonomy.cdm.model.common.TermVocabulary;
32  
33  /**
34   * This class maps a database attribute to CDM extension added to the target class
35   * TODO maybe this class should not inherit from DbSingleAttributeImportMapperBase
36   * as it does not map to a single attribute
37   * @author a.mueller
38   * @created 12.05.2009
39   * @version 1.0
40   */
41  public class DbImportExtensionMapper extends DbSingleAttributeImportMapperBase<DbImportStateBase<?,?>, IdentifiableEntity> implements IDbImportMapper<DbImportStateBase<?,?>,IdentifiableEntity>{
42  	@SuppressWarnings("unused")
43  	private static final Logger logger = Logger.getLogger(DbImportExtensionMapper.class);
44  	
45  //************************** FACTORY METHODS ***************************************************************/
46  	
47  	/**
48  	 * @param dbAttributeString
49  	 * @param uuid
50  	 * @param label
51  	 * @param text
52  	 * @param labelAbbrev
53  	 * @return
54  	 */
55  	public static DbImportExtensionMapper NewInstance(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev){
56  		return new DbImportExtensionMapper(dbAttributeString, uuid, label, text, labelAbbrev);
57  	}
58  	
59  	public static DbImportExtensionMapper NewInstance(String dbAttributeString, ExtensionType extensionType){
60  		return new DbImportExtensionMapper(dbAttributeString, extensionType);
61  	}
62  	
63  //***************** VARIABLES **********************************************************/
64  	
65  	private ExtensionType extensionType;
66  	private String label;
67  	private String text;
68  	private String labelAbbrev;
69  	private UUID uuid;
70  
71  //******************************** CONSTRUCTOR *****************************************************************/
72  	/**
73  	 * @param dbAttributeString
74  	 * @param uuid
75  	 * @param label
76  	 * @param text
77  	 * @param labelAbbrev
78  	 */
79  	private DbImportExtensionMapper(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev) {
80  		super(dbAttributeString, dbAttributeString);
81  		this.uuid = uuid;
82  		this.label = label;
83  		this.text = text;
84  		this.labelAbbrev = labelAbbrev;
85  	}
86  	
87  	/**
88  	 * @param dbAttributeString
89  	 * @param extensionType
90  	 */
91  	private DbImportExtensionMapper(String dbAttributeString, ExtensionType extensionType) {
92  		super(dbAttributeString, dbAttributeString);
93  		this.extensionType = extensionType;
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.extensionType == null){
112 					this.extensionType = getExtensionType(currentImport, uuid, label, text, labelAbbrev);
113 				}
114 			}else{
115 				ignore = true;
116 			}
117 		} catch (MethodNotSupportedException 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 		String dbValue = dbValueObject == null? null: dbValueObject.toString();
131 		return invoke(dbValue, cdmBase);
132 	}
133 	
134 	/**
135 	 * @param dbValue
136 	 * @param cdmBase
137 	 * @return
138 	 */
139 	private boolean invoke(String dbValue, CdmBase cdmBase){
140 		if (ignore){
141 			return true;
142 		}
143 		if (cdmBase instanceof IdentifiableEntity){
144 			IdentifiableEntity identifiableEntity = (IdentifiableEntity) cdmBase;
145 			invoke(dbValue, identifiableEntity);
146 			return true;
147 		}else{
148 			throw new IllegalArgumentException("extended object must be of type identifiable 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 IdentifiableEntity invoke(ResultSet rs, IdentifiableEntity identifiableEntity) throws SQLException {
157 		String dbValue = rs.getString(getSourceAttribute());
158 		return invoke(dbValue, identifiableEntity);
159 	}
160 	
161 	/**
162 	 * @param dbValue
163 	 * @param identifiableEntity
164 	 * @return
165 	 */
166 	private IdentifiableEntity invoke(String dbValue, IdentifiableEntity identifiableEntity){
167 		if (ignore){
168 			return identifiableEntity;
169 		}
170 		if (CdmUtils.isNotEmpty(dbValue)){
171 			Extension.NewInstance(identifiableEntity, dbValue, extensionType);
172 		}
173 		return identifiableEntity;
174 	}
175 	
176 	
177 	/**
178 	 * @param service
179 	 * @param uuid
180 	 * @param label
181 	 * @param text
182 	 * @param labelAbbrev
183 	 * @return
184 	 */
185 	protected ExtensionType getExtensionType(CdmImportBase<?, ?> currentImport, UUID uuid, String label, String text, String labelAbbrev){
186 		ITermService termService = currentImport.getTermService();
187 		ExtensionType extensionType = (ExtensionType)termService.find(uuid);
188 		if (extensionType == null){
189 			extensionType = ExtensionType.NewInstance(text, label, labelAbbrev);
190 			extensionType.setUuid(uuid);
191 			//set vocabulary //TODO allow user defined vocabularies
192 			UUID uuidExtensionTypeVocabulary = UUID.fromString("117cc307-5bd4-4b10-9b2f-2e14051b3b20");
193 			IVocabularyService vocService = currentImport.getVocabularyService();
194 			TermVocabulary voc = vocService.find(uuidExtensionTypeVocabulary);
195 			if (voc != null){
196 				voc.addTerm(extensionType);
197 			}else{
198 				logger.warn("Could not find default extensionType vocabulary. Vocabulary not set for new extension type.");
199 			}
200 			//save
201 			termService.save(extensionType);
202 		}
203 		return extensionType;
204 	}
205 
206 
207 	//not used
208 	/* (non-Javadoc)
209 	 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
210 	 */
211 	public Class<String> getTypeClass(){
212 		return String.class;
213 	}
214 
215 	
216 
217 }