3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.io
.common
.mapping
;
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
;
20 import org
.apache
.log4j
.Logger
;
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
;
33 public class DbImportMultiLanguageTextMapper
<CDMBASE
extends CdmBase
> extends DbImportMultiAttributeMapperBase
<CDMBASE
, DbImportStateBase
<?
,?
>> {
34 @SuppressWarnings("unused")
35 private static final Logger logger
= Logger
.getLogger(DbImportMultiLanguageTextMapper
.class);
37 //****************************** FACTORY METHOD ********************************************/
39 public static DbImportMultiLanguageTextMapper
NewInstance(String dbTextAttribute
, String dbLanguageAttribute
, String languageNamespace
, String cdmMultiLanguageTextAttribute
){
40 return new DbImportMultiLanguageTextMapper(dbTextAttribute
, dbLanguageAttribute
, languageNamespace
, cdmMultiLanguageTextAttribute
);
44 //***************************** VARIABLES *************************************************/
46 private String dbTextAttribute
;
47 private String dbLanguageAttribute
;
48 private String languageNamespace
;
49 private String cdmMultiLanguageTextAttribute
;
50 private Method destinationMethod
;
52 //***************************** CONSTRUCTOR *************************************************/
54 protected DbImportMultiLanguageTextMapper(String dbTextAttribute
, String dbLanguageAttribute
, String languageNamespace
, String cdmMultiLanguageTextAttribute
){
56 this.dbTextAttribute
= dbTextAttribute
;
57 this.dbLanguageAttribute
= dbLanguageAttribute
;
58 this.languageNamespace
= languageNamespace
;
59 this.cdmMultiLanguageTextAttribute
= cdmMultiLanguageTextAttribute
;
63 * @see eu.etaxonomy.cdm.io.common.mapping.DbImportMultiAttributeMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
66 public void initialize(DbImportStateBase
<?
,?
> state
, Class
<?
extends CdmBase
> destinationClass
) {
67 super.initialize(state
, destinationClass
);
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
);
83 * This method is used to be compliant with DbSingleAttributeImport
86 protected Class
<?
> getTypeClass() {
91 * This method is used to be compliant with DbSingleAttributeImport
92 * @param destinationClass
94 * @throws SecurityException
95 * @throws NoSuchMethodException
97 protected Class
getTargetClass(Class
<?
> destinationClass
) throws SecurityException
, NoSuchMethodException
{
98 return destinationClass
;
102 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
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
);
109 Map
<Language
, LanguageString
> multilanguageText
= new HashMap
<Language
, LanguageString
>();
110 LanguageString languageString
= LanguageString
.NewInstance(text
, language
);
111 multilanguageText
.put(language
, languageString
);
113 destinationMethod
.invoke(cdmBase
, multilanguageText
);
114 } catch (IllegalArgumentException e
) {
116 } catch (IllegalAccessException e
) {
117 throw new RuntimeException(e
);
118 } catch (InvocationTargetException e
) {
119 throw new RuntimeException (e
);