Project

General

Profile

Download (5.45 KB) Statistics
| Branch: | Tag: | Revision:
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.net.URI;
14
import java.net.URISyntaxException;
15
import java.sql.ResultSet;
16
import java.sql.SQLException;
17
import java.util.Set;
18

    
19
import javax.mail.MethodNotSupportedException;
20

    
21
import org.apache.log4j.Logger;
22

    
23
import eu.etaxonomy.cdm.api.service.ITermService;
24
import eu.etaxonomy.cdm.common.CdmUtils;
25
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState;
26
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
29
import eu.etaxonomy.cdm.model.description.Feature;
30
import eu.etaxonomy.cdm.model.description.TaxonDescription;
31
import eu.etaxonomy.cdm.model.description.TextData;
32
import eu.etaxonomy.cdm.model.media.Media;
33
import eu.etaxonomy.cdm.model.taxon.Synonym;
34
import eu.etaxonomy.cdm.model.taxon.Taxon;
35
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
36

    
37
/**
38
 * This class maps a database attribute to CDM extension added to the target class
39
 * TODO maybe this class should not inherit from DbSingleAttributeImportMapperBase
40
 * as it does not map to a single attribute
41
 * @author a.mueller
42
 * @created 12.05.2009
43
 * @version 1.0
44
 */
45
public class DbImportImageGalleryMapper extends DbSingleAttributeImportMapperBase<DbImportStateBase<?,?>, Taxon> implements IDbImportMapper<DbImportStateBase<?,?>,Taxon>{
46
	private static final Logger logger = Logger.getLogger(DbImportImageGalleryMapper.class);
47
	
48
//************************** FACTORY METHODS ***************************************************************/
49
	
50
	/**
51
	 * @param dbAttributeString
52
	 * @param uuid
53
	 * @param label
54
	 * @param text
55
	 * @param labelAbbrev
56
	 * @return
57
	 */
58
	public static DbImportImageGalleryMapper NewInstance(String dbAttributeString){
59
		return new DbImportImageGalleryMapper(dbAttributeString);
60
	}
61
	
62
//***************** VARIABLES **********************************************************/
63
	
64

    
65
//******************************** CONSTRUCTOR *****************************************************************/
66
	
67
	/**
68
	 * @param dbAttributeString
69
	 * @param extensionType
70
	 */
71
	private DbImportImageGalleryMapper(String dbAttributeString) {
72
		super(dbAttributeString, dbAttributeString);
73
	}
74
	
75
//****************************** METHODS ***************************************************/
76
	
77
	/**
78
	 * @param service
79
	 * @param state
80
	 * @param tableName
81
	 */
82
	public void initialize(ITermService service, BerlinModelImportState state, Class<? extends CdmBase> destinationClass) {
83
		importMapperHelper.initialize(state, destinationClass);
84
		try {
85
			if (  checkDbColumnExists()){
86
				//do nothing
87
			}else{
88
				ignore = true;
89
			}
90
		} catch (MethodNotSupportedException e) {
91
			//do nothing
92
		}
93
	}
94
	
95

    
96
	/* (non-Javadoc)
97
	 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
98
	 */
99
	public TaxonBase invoke(ResultSet rs, TaxonBase taxonBase) throws SQLException {
100
		String dbValue = rs.getString(getSourceAttribute());
101
		return invoke(dbValue, taxonBase);
102
	}
103
	
104
	/**
105
	 * @param dbValue
106
	 * @param identifiableEntity
107
	 * @return
108
	 */
109
	private TaxonBase invoke(String dbValue, TaxonBase taxonBase){
110
		if (ignore || CdmUtils.isEmpty(dbValue)){
111
			return taxonBase;
112
		}
113
		boolean createNew = true;
114
		Taxon taxon;
115
		if (taxonBase.isInstanceOf(Synonym.class)){
116
			Synonym synonym = taxonBase.deproxy(taxonBase, Synonym.class);
117
			if (synonym.getAcceptedTaxa().size() > 0){
118
				logger.warn("Media will be added to a synonyms accepted taxon");
119
				taxon = synonym.getAcceptedTaxa().iterator().next();
120
			}else{
121
				throw new IllegalArgumentException("TaxonBase was of type synonym and does not belong to an accepted taxon");
122
			}
123
		}else{
124
			taxon = taxonBase.deproxy(taxonBase, Taxon.class);
125
		}
126
		
127
		TaxonDescription imageGallery = taxon.getImageGallery(createNew);
128
		Set<DescriptionElementBase> elements = imageGallery.getElements();
129
		DescriptionElementBase element = null;
130
		if (elements.size() != 1 ){
131
			element = TextData.NewInstance(Feature.IMAGE());
132
			imageGallery.addElement(element);
133
		}else {
134
			element = elements.iterator().next();
135
		}
136
		String uriString = dbValue;
137
		Integer size = null;
138
		String mimeType = null;
139
		String suffix = null;
140
		URI uri = null;
141
		try {
142
			uri = new URI(uriString);
143
		} catch (URISyntaxException e) {
144
			String warning = "URISyntaxException when trying to convert first uri string: " + uriString;
145
			logger.error(warning);
146
		}
147
		Media media = Media.NewInstance(uri, size, mimeType, suffix);
148
		element.addMedia(media);
149
		return taxon;
150
	}
151
	
152
	//not used
153
	/* (non-Javadoc)
154
	 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
155
	 */
156
	public Class<String> getTypeClass(){
157
		return String.class;
158
	}
159

    
160
	/* (non-Javadoc)
161
	 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
162
	 */
163
	@Override
164
	public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
165
		super.importMapperHelper.initialize(state, destinationClass);
166
	}
167
	
168

    
169
}
(18-18/50)