implement source reference for partitions
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportMultiLanguageTextMapper.java
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 String message = "NoSuchMethod in MultiLanguageTextMapper for attribute %s and destination method %s";
77 message = String.format(message, this.dbTextAttribute, this.destinationMethod);
78 throw new RuntimeException(message, e);
79 }
80 }
81
82 /**
83 * This method is used to be compliant with DbSingleAttributeImport
84 * @return
85 */
86 protected Class<?> getTypeClass() {
87 return Map.class;
88 }
89
90 /**
91 * This method is used to be compliant with DbSingleAttributeImport
92 * @param destinationClass
93 * @return
94 * @throws SecurityException
95 * @throws NoSuchMethodException
96 */
97 protected Class getTargetClass(Class<?> destinationClass) throws SecurityException, NoSuchMethodException{
98 return destinationClass;
99 }
100
101 /* (non-Javadoc)
102 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
103 */
104 public CDMBASE invoke(ResultSet rs, CDMBASE cdmBase) throws SQLException {
105 //TODO make this a definedTermMapper
106 Language language = (Language)getRelatedObject(rs, languageNamespace, dbLanguageAttribute);
107 String text = getStringDbValue(rs, dbTextAttribute);
108
109 Map<Language, LanguageString> multilanguageText = new HashMap<Language, LanguageString>();
110 LanguageString languageString = LanguageString.NewInstance(text, language);
111 multilanguageText.put(language, languageString);
112 try {
113 destinationMethod.invoke(cdmBase, multilanguageText);
114 } catch (IllegalArgumentException e) {
115 throw e;
116 } catch (IllegalAccessException e) {
117 throw new RuntimeException(e);
118 } catch (InvocationTargetException e) {
119 throw new RuntimeException (e);
120 }
121 return cdmBase;
122 }
123
124
125 }