added licence comment
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / berlinModel / CdmExtensionMapper.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.io.berlinModel;
11
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.util.Map;
15 import java.util.UUID;
16
17 import org.apache.log4j.Logger;
18
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;
28
29 /**
30 * @author a.mueller
31 * @created 20.03.2008
32 * @version 1.0
33 */
34 public class CdmExtensionMapper extends CdmSingleAttributeMapperBase {
35 @SuppressWarnings("unused")
36 private static Logger logger = Logger.getLogger(CdmExtensionMapper.class);
37
38 private boolean ignore = false;;
39 private ExtensionType extensionType;
40 private String label;
41 private String text;
42 private String labelAbbrev;
43 private UUID uuid;
44
45 private BerlinModelImportState state;
46 private String tableName;
47
48 /**
49 * @param dbValue
50 * @param cdmValue
51 */
52 public CdmExtensionMapper(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev) {
53 super(dbAttributeString, dbAttributeString);
54 this.uuid = uuid;
55 this.label = label;
56 this.text = text;
57 this.labelAbbrev = labelAbbrev;
58 }
59
60 /**
61 * @param dbValue
62 * @param cdmValue
63 */
64 public CdmExtensionMapper(String dbAttributeString, ExtensionType extensionType) {
65 super(dbAttributeString, dbAttributeString);
66 this.extensionType = extensionType;
67 }
68
69 public void initialize(ITermService service, BerlinModelImportState state, String tableName) {
70 this.state = state;
71 this.tableName = tableName;
72 if (checkSqlServerColumnExists()){
73 if (this.extensionType == null){
74 this.extensionType = getExtensionType(service, uuid, label, text, labelAbbrev);
75 }
76 }else{
77 ignore = true;
78 }
79 }
80
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);
85 }
86
87 public boolean invoke(ResultSet rs, CdmBase cdmBase) throws SQLException{
88 String dbValue = rs.getString(this.getSourceAttribute());
89 return invoke(dbValue, cdmBase);
90 }
91
92 private boolean invoke(String dbValue, CdmBase cdmBase){
93 if (ignore){
94 return true;
95 }
96 if (cdmBase.isInstanceOf(IdentifiableEntity.class)){
97 IdentifiableEntity<?> identEntity = (IdentifiableEntity<?>)cdmBase;
98 if (CdmUtils.isNotEmpty(dbValue)){
99 Extension.NewInstance(identEntity, dbValue, extensionType);
100 }
101 return true;
102 }else{
103 throw new IllegalArgumentException();
104 }
105 }
106
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);
113 }
114 return extensionType;
115 }
116
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) ;
126 int n;
127 try {
128 rs.next();
129 n = rs.getInt("n");
130 return n>0;
131 } catch (SQLException e) {
132 e.printStackTrace();
133 return false;
134 }
135
136 }
137
138 protected BerlinModelImportState getState(){
139 return this.state;
140 }
141
142 protected String getTableName(){
143 return this.tableName;
144 }
145
146
147 //not used
148 public Class<String> getTypeClass(){
149 return String.class;
150 }
151
152 }