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.Set;
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.common.CdmUtils;
23  import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState;
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   * @created 12.05.2009
41   * @version 1.0
42   */
43  public class DbImportImageGalleryMapper extends DbSingleAttributeImportMapperBase<DbImportStateBase<?,?>, Taxon> implements IDbImportMapper<DbImportStateBase<?,?>,Taxon>{
44  	private static final Logger logger = Logger.getLogger(DbImportImageGalleryMapper.class);
45  	
46  //************************** FACTORY METHODS ***************************************************************/
47  	
48  	/**
49  	 * @param dbAttributeString
50  	 * @param uuid
51  	 * @param label
52  	 * @param text
53  	 * @param labelAbbrev
54  	 * @return
55  	 */
56  	public static DbImportImageGalleryMapper NewInstance(String dbAttributeString){
57  		return new DbImportImageGalleryMapper(dbAttributeString);
58  	}
59  	
60  //***************** VARIABLES **********************************************************/
61  	
62  
63  //******************************** CONSTRUCTOR *****************************************************************/
64  	
65  	/**
66  	 * @param dbAttributeString
67  	 * @param extensionType
68  	 */
69  	private DbImportImageGalleryMapper(String dbAttributeString) {
70  		super(dbAttributeString, dbAttributeString);
71  	}
72  	
73  //****************************** METHODS ***************************************************/
74  	
75  	/**
76  	 * @param service
77  	 * @param state
78  	 * @param tableName
79  	 */
80  	public void initialize(ITermService service, BerlinModelImportState state, Class<? extends CdmBase> destinationClass) {
81  		importMapperHelper.initialize(state, destinationClass);
82  		try {
83  			if (  checkDbColumnExists()){
84  				//do nothing
85  			}else{
86  				ignore = true;
87  			}
88  		} catch (MethodNotSupportedException e) {
89  			//do nothing
90  		}
91  	}
92  	
93  
94  	/* (non-Javadoc)
95  	 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
96  	 */
97  	public TaxonBase invoke(ResultSet rs, TaxonBase taxonBase) throws SQLException {
98  		String dbValue = rs.getString(getSourceAttribute());
99  		return invoke(dbValue, taxonBase);
100 	}
101 	
102 	/**
103 	 * @param dbValue
104 	 * @param identifiableEntity
105 	 * @return
106 	 */
107 	private TaxonBase invoke(String dbValue, TaxonBase taxonBase){
108 		if (ignore || CdmUtils.isEmpty(dbValue)){
109 			return taxonBase;
110 		}
111 		boolean createNew = true;
112 		Taxon taxon;
113 		if (taxonBase.isInstanceOf(Synonym.class)){
114 			Synonym synonym = taxonBase.deproxy(taxonBase, Synonym.class);
115 			if (synonym.getAcceptedTaxa().size() > 0){
116 				logger.warn("Media will be added to a synonyms accepted taxon");
117 				taxon = synonym.getAcceptedTaxa().iterator().next();
118 			}else{
119 				throw new IllegalArgumentException("TaxonBase was of type synonym and does not belong to an accepted taxon");
120 			}
121 		}else{
122 			taxon = taxonBase.deproxy(taxonBase, Taxon.class);
123 		}
124 		
125 		TaxonDescription imageGallery = taxon.getImageGallery(createNew);
126 		Set<DescriptionElementBase> elements = imageGallery.getElements();
127 		DescriptionElementBase element = null;
128 		if (elements.size() != 1 ){
129 			element = TextData.NewInstance(Feature.IMAGE());
130 			imageGallery.addElement(element);
131 		}else {
132 			element = elements.iterator().next();
133 		}
134 		String uri = dbValue;
135 		Integer size = null;
136 		String mimeType = null;
137 		String suffix = null;
138 		Media media = Media.NewInstance(uri, size, mimeType, suffix);
139 		element.addMedia(media);
140 		return taxon;
141 	}
142 	
143 	//not used
144 	/* (non-Javadoc)
145 	 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
146 	 */
147 	public Class<String> getTypeClass(){
148 		return String.class;
149 	}
150 
151 	/* (non-Javadoc)
152 	 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
153 	 */
154 	@Override
155 	public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
156 		super.importMapperHelper.initialize(state, destinationClass);
157 	}
158 	
159 
160 }