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.lang.reflect.InvocationTargetException;
14  import java.lang.reflect.Method;
15  import java.sql.ResultSet;
16  import java.sql.SQLException;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.apache.log4j.Logger;
21  
22  import eu.etaxonomy.cdm.io.common.DbImportStateBase;
23  import eu.etaxonomy.cdm.io.common.ImportHelper;
24  import eu.etaxonomy.cdm.model.common.CdmBase;
25  import eu.etaxonomy.cdm.model.common.Language;
26  import eu.etaxonomy.cdm.model.common.LanguageString;
27  
28  /**
29   * @author a.mueller
30   * @created 15.03.2010
31   * @version 1.0
32   */
33  public class DbImportMultiLanguageTextMapper<CDMBASE extends CdmBase> extends DbImportMultiAttributeMapperBase<CDMBASE, DbImportStateBase> {
34  	@SuppressWarnings("unused")
35  	private static final Logger logger = Logger.getLogger(DbImportMultiLanguageTextMapper.class);
36  
37  //****************************** FACTORY METHOD ********************************************/
38  	
39  	public static DbImportMultiLanguageTextMapper NewInstance(String dbTextAttribute, String dbLanguageAttribute, String languageNamespace, String cdmMultiLanguageTextAttribute){
40  		return new DbImportMultiLanguageTextMapper(dbTextAttribute, dbLanguageAttribute, languageNamespace, cdmMultiLanguageTextAttribute);
41  	}
42  	
43  	
44  //***************************** VARIABLES *************************************************/
45  
46  	private String dbTextAttribute;
47  	private String dbLanguageAttribute;
48  	private String languageNamespace;
49  	private String cdmMultiLanguageTextAttribute;
50  	private Method destinationMethod;
51  	
52  //***************************** CONSTRUCTOR *************************************************/
53  
54  	protected DbImportMultiLanguageTextMapper(String dbTextAttribute, String dbLanguageAttribute, String languageNamespace, String cdmMultiLanguageTextAttribute){
55  		super();
56  		this.dbTextAttribute = dbTextAttribute;
57  		this.dbLanguageAttribute = dbLanguageAttribute;
58  		this.languageNamespace = languageNamespace;
59  		this.cdmMultiLanguageTextAttribute = cdmMultiLanguageTextAttribute;
60  	}
61  	
62  	/* (non-Javadoc)
63  	 * @see eu.etaxonomy.cdm.io.common.mapping.DbImportMultiAttributeMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
64  	 */
65  	@Override
66  	public void initialize(DbImportStateBase state, Class<? extends CdmBase> destinationClass) {
67  		super.initialize(state, destinationClass);
68  		try {
69  			Class targetClass = getTargetClass(destinationClass);
70  			Class<?> parameterType = getTypeClass();
71  			String methodName = ImportHelper.getSetterMethodName(targetClass, cdmMultiLanguageTextAttribute);
72  			destinationMethod = targetClass.getMethod(methodName, parameterType);
73  		} catch (SecurityException e) {
74  			throw new RuntimeException(e);
75  		} catch (NoSuchMethodException e) {
76  			throw new RuntimeException(e);
77  		}
78  	}
79  
80  	/**
81  	 * This method is used to be compliant with DbSingleAttributeImport
82  	 * @return
83  	 */
84  	protected Class<?> getTypeClass() {
85  		return Map.class;
86  	}
87  
88  	/**
89  	 * This method is used to be compliant with DbSingleAttributeImport
90  	 * @param destinationClass
91  	 * @return
92  	 * @throws SecurityException
93  	 * @throws NoSuchMethodException
94  	 */
95  	protected Class getTargetClass(Class<?> destinationClass) throws SecurityException, NoSuchMethodException{
96  		return destinationClass;
97  	}
98  	
99  	/* (non-Javadoc)
100 	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
101 	 */
102 	public CDMBASE invoke(ResultSet rs, CDMBASE cdmBase) throws SQLException {
103 		//TODO make this a definedTermMapper
104 		Language language = (Language)getRelatedObject(rs, languageNamespace, dbLanguageAttribute);
105 		String text = getStringDbValue(rs, dbTextAttribute);
106 		
107 		Map<Language, LanguageString> multilanguageText = new HashMap<Language, LanguageString>();
108 		LanguageString languageString = LanguageString.NewInstance(text, language);
109 		multilanguageText.put(language, languageString);
110 		try {
111 			destinationMethod.invoke(cdmBase, multilanguageText);
112 		} catch (IllegalArgumentException e) {
113 			throw e;
114 		} catch (IllegalAccessException e) {
115 			throw new RuntimeException(e);
116 		} catch (InvocationTargetException e) {
117 			throw new RuntimeException (e);
118 		}
119 		return cdmBase;
120 	}
121 	
122 
123 }