Project

General

Profile

Download (4.91 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.net.URI;
13
import java.net.URISyntaxException;
14
import java.sql.ResultSet;
15
import java.sql.SQLException;
16
import java.util.Set;
17

    
18
import org.apache.commons.lang3.StringUtils;
19
import org.apache.log4j.Logger;
20

    
21
import eu.etaxonomy.cdm.api.service.ITermService;
22
import eu.etaxonomy.cdm.common.CdmUtils;
23
import eu.etaxonomy.cdm.database.update.DatabaseTypeNotSupportedException;
24
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
27
import eu.etaxonomy.cdm.model.description.Feature;
28
import eu.etaxonomy.cdm.model.description.TaxonDescription;
29
import eu.etaxonomy.cdm.model.description.TextData;
30
import eu.etaxonomy.cdm.model.media.Media;
31
import eu.etaxonomy.cdm.model.taxon.Synonym;
32
import eu.etaxonomy.cdm.model.taxon.Taxon;
33
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
34

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

    
45
//************************** FACTORY METHODS ***************************************************************/
46

    
47
	public static DbImportImageGalleryMapper NewInstance(String dbAttributeString){
48
		return new DbImportImageGalleryMapper(dbAttributeString);
49
	}
50

    
51
//***************** VARIABLES **********************************************************/
52

    
53

    
54
//******************************** CONSTRUCTOR *****************************************************************/
55

    
56
	private DbImportImageGalleryMapper(String dbAttributeString) {
57
		super(dbAttributeString, dbAttributeString);
58
	}
59

    
60
//****************************** METHODS ***************************************************/
61

    
62
	public void initialize(ITermService service, DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
63
		importMapperHelper.initialize(state, destinationClass);
64
		try {
65
			if (  checkDbColumnExists()){
66
				//do nothing
67
			}else{
68
				ignore = true;
69
			}
70
		} catch (DatabaseTypeNotSupportedException e) {
71
			//do nothing
72
		}
73
	}
74

    
75
	public TaxonBase invoke(ResultSet rs, TaxonBase taxonBase) throws SQLException {
76
		String dbValue = rs.getString(getSourceAttribute());
77
		return invoke(dbValue, taxonBase);
78
	}
79

    
80
	private TaxonBase invoke(String dbValue, TaxonBase taxonBase){
81
		if (ignore || StringUtils.isBlank(dbValue)){
82
			return taxonBase;
83
		}
84
		boolean createNew = true;
85
		Taxon taxon;
86
		if (taxonBase.isInstanceOf(Synonym.class)){
87
			Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class);
88
			if (synonym.getAcceptedTaxon() != null){
89
				logger.warn("Media will be added to a synonyms accepted taxon");
90
				taxon = synonym.getAcceptedTaxon();
91
			}else{
92
				throw new IllegalArgumentException("TaxonBase was of type synonym and does not belong to an accepted taxon");
93
			}
94
		}else{
95
			taxon = CdmBase.deproxy(taxonBase, Taxon.class);
96
		}
97

    
98
		TaxonDescription imageGallery = taxon.getImageGallery(createNew);
99
		Set<DescriptionElementBase> elements = imageGallery.getElements();
100
		DescriptionElementBase element = null;
101
		if (elements.size() != 1 ){
102
			element = TextData.NewInstance(Feature.IMAGE());
103
			imageGallery.addElement(element);
104
		}else {
105
			element = elements.iterator().next();
106
		}
107
		String uriString = dbValue;
108
		Integer size = null;
109
		String mimeType = null;
110
		String suffix = null;
111
		URI uri = null;
112
		try {
113
			uri = new URI(uriString);
114
		} catch (URISyntaxException e) {
115
			String warning = "URISyntaxException when trying to convert first uri string: " + uriString;
116
			logger.error(warning);
117
		}
118
		Media media = Media.NewInstance(uri, size, mimeType, suffix);
119
		element.addMedia(media);
120
		return taxon;
121
	}
122

    
123
	//not used
124
	/* (non-Javadoc)
125
	 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
126
	 */
127
	@Override
128
    public Class<String> getTypeClass(){
129
		return String.class;
130
	}
131

    
132
	/* (non-Javadoc)
133
	 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
134
	 */
135
	@Override
136
	public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
137
		super.importMapperHelper.initialize(state, destinationClass);
138
	}
139

    
140

    
141
}
(19-19/53)