(no commit message)
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportTextDataCreationMapper.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.sql.ResultSet;
14 import java.sql.SQLException;
15
16 import org.apache.log4j.Logger;
17
18 import eu.etaxonomy.cdm.common.CdmUtils;
19 import eu.etaxonomy.cdm.io.common.DbImportStateBase;
20 import eu.etaxonomy.cdm.model.common.Language;
21 import eu.etaxonomy.cdm.model.description.Feature;
22 import eu.etaxonomy.cdm.model.description.TextData;
23 import eu.etaxonomy.cdm.model.description.TextFormat;
24
25 /**
26 * This Mapper creates a text data element and adds it to a taxon.
27 * @author a.mueller
28 * @created 11.03.2010
29 * @version 1.0
30 */
31 public class DbImportTextDataCreationMapper<STATE extends DbImportStateBase<?,?>> extends DbImportDescriptionElementCreationMapperBase<TextData, DbImportStateBase<?,?>> {
32 @SuppressWarnings("unused")
33 private static final Logger logger = Logger.getLogger(DbImportTextDataCreationMapper.class);
34
35 //******************************** FACTORY METHOD ***************************************************/
36
37
38 /**
39 * Creates a Distribution and adds it to the description of a taxon.
40 * @param dbIdAttribute
41 * @param objectToCreateNamespace
42 * @param dbTaxonFkAttribute
43 * @param taxonNamespace
44 * @return
45 */
46 public static DbImportTextDataCreationMapper<?> NewInstance(String dbIdAttribute, String objectToCreateNamespace, String dbTaxonFkAttribute, String taxonNamespace){
47 Feature feature = null;
48 Language language = null;
49 TextFormat format = null;
50 String dbTextAttribute = null;
51 return new DbImportTextDataCreationMapper(dbIdAttribute, objectToCreateNamespace, dbTaxonFkAttribute, taxonNamespace, dbTextAttribute, language, feature, format);
52 }
53
54
55 /**
56 * Creates a TextData, adds the the in the language and the format defined and then adds it to the description of a taxon.
57 * If language is <code>null</code> the default language is taken instead.
58 * @param dbIdAttribute
59 * @param objectToCreateNamespace
60 * @param dbTaxonFkAttribute
61 * @param taxonNamespace
62 * @param status
63 * @return
64 */
65 public static DbImportTextDataCreationMapper<?> NewInstance(String dbIdAttribute, String objectToCreateNamespace, String dbTaxonFkAttribute, String taxonNamespace, String dbTextAttribute, Language language, Feature feature, TextFormat format){
66 return new DbImportTextDataCreationMapper(dbIdAttribute, objectToCreateNamespace, dbTaxonFkAttribute, taxonNamespace, dbTextAttribute, language, feature, format);
67 }
68
69 //******************************* ATTRIBUTES ***************************************/
70
71 protected Feature defaultFeature;
72 protected Language defaultLanguage;
73 protected TextFormat defaultFormat;
74 protected String dbTextAttribute;
75
76 //********************************* CONSTRUCTOR ****************************************/
77 /**
78 * @param dbIdAttribute
79 * @param objectToCreateNamespace
80 * @param dbTaxonFkAttribute
81 * @param taxonNamespace
82 */
83 protected DbImportTextDataCreationMapper(String dbIdAttribute, String objectToCreateNamespace, String dbTaxonFkAttribute, String dbTextAttribute, String taxonNamespace, Language language, Feature feature, TextFormat format) {
84 super(dbIdAttribute, objectToCreateNamespace, dbTaxonFkAttribute, taxonNamespace);
85 this.defaultFeature = feature;
86 this.defaultLanguage = language;
87 this.defaultFormat = format;
88 }
89
90 //************************************ METHODS *******************************************/
91
92 /* (non-Javadoc)
93 * @see eu.etaxonomy.cdm.io.common.mapping.DbImportObjectCreationMapperBase#createObject(java.sql.ResultSet)
94 */
95 @Override
96 protected TextData createObject(ResultSet rs) throws SQLException {
97 TextData textData = TextData.NewInstance();
98 String text = null;
99 if (CdmUtils.isNotEmpty(dbTextAttribute)){
100 text = rs.getString(dbTextAttribute);
101 }
102 if (text != null){
103 Language language = this.defaultLanguage;
104 if (language == null){
105 language = Language.DEFAULT();
106 }
107 textData.putText(text, language);
108 }
109 TextFormat format = this.defaultFormat;
110 textData.setFormat(format);
111
112 Feature feature = this.defaultFeature;
113 textData.setFeature(feature);
114 return textData;
115 }
116
117 }