2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.berlinModel
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
20 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
21 import eu
.etaxonomy
.cdm
.io
.berlinModel
.in
.BerlinModelImportState
;
22 import eu
.etaxonomy
.cdm
.io
.common
.CdmSingleAttributeMapperBase
;
23 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
24 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
25 import eu
.etaxonomy
.cdm
.model
.common
.Extension
;
26 import eu
.etaxonomy
.cdm
.model
.common
.ExtensionType
;
27 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
34 public class CdmExtensionMapper
extends CdmSingleAttributeMapperBase
{
35 @SuppressWarnings("unused")
36 private static Logger logger
= Logger
.getLogger(CdmExtensionMapper
.class);
38 private boolean ignore
= false;;
39 private ExtensionType extensionType
;
42 private String labelAbbrev
;
45 private BerlinModelImportState state
;
46 private String tableName
;
52 public CdmExtensionMapper(String dbAttributeString
, UUID uuid
, String label
, String text
, String labelAbbrev
) {
53 super(dbAttributeString
, dbAttributeString
);
57 this.labelAbbrev
= labelAbbrev
;
64 public CdmExtensionMapper(String dbAttributeString
, ExtensionType extensionType
) {
65 super(dbAttributeString
, dbAttributeString
);
66 this.extensionType
= extensionType
;
69 public void initialize(ITermService service
, BerlinModelImportState state
, String tableName
) {
71 this.tableName
= tableName
;
72 if (checkSqlServerColumnExists()){
73 if (this.extensionType
== null){
74 this.extensionType
= getExtensionType(service
, uuid
, label
, text
, labelAbbrev
);
81 public boolean invoke(Map
<String
, Object
> valueMap
, CdmBase cdmBase
){
82 Object dbValueObject
= valueMap
.get(this.getSourceAttribute().toLowerCase());
83 String dbValue
= dbValueObject
== null?
null: dbValueObject
.toString();
84 return invoke(dbValue
, cdmBase
);
87 public boolean invoke(ResultSet rs
, CdmBase cdmBase
) throws SQLException
{
88 String dbValue
= rs
.getString(this.getSourceAttribute());
89 return invoke(dbValue
, cdmBase
);
92 private boolean invoke(String dbValue
, CdmBase cdmBase
){
96 if (cdmBase
.isInstanceOf(IdentifiableEntity
.class)){
97 IdentifiableEntity
<?
> identEntity
= (IdentifiableEntity
<?
>)cdmBase
;
98 if (CdmUtils
.isNotEmpty(dbValue
)){
99 Extension
.NewInstance(identEntity
, dbValue
, extensionType
);
103 throw new IllegalArgumentException();
107 protected ExtensionType
getExtensionType(ITermService service
, UUID uuid
, String label
, String text
, String labelAbbrev
){
108 ExtensionType extensionType
= (ExtensionType
)service
.find(uuid
);
109 if (extensionType
== null){
110 extensionType
= new ExtensionType(label
, text
, labelAbbrev
);
111 extensionType
.setUuid(uuid
);
112 service
.save(extensionType
);
114 return extensionType
;
117 protected boolean checkSqlServerColumnExists(){
118 Source source
= getState().getConfig().getSource();
119 String strQuery
= "SELECT Count(t.id) as n " +
120 " FROM sysobjects AS t " +
121 " INNER JOIN syscolumns AS c ON t.id = c.id " +
122 " WHERE (t.xtype = 'U') AND " +
123 " (t.name = '" + getTableName() + "') AND " +
124 " (c.name = '" + getDestinationAttribute() + "')";
125 ResultSet rs
= source
.getResultSet(strQuery
) ;
131 } catch (SQLException e
) {
138 protected BerlinModelImportState
getState(){
142 protected String
getTableName(){
143 return this.tableName
;
148 public Class
<String
> getTypeClass(){