(no commit message)
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportDescriptionElementSourceCreationMapper.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.DescriptionElementSource;
21 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
22 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
23
24 /**
25 * This mapper creates an description element source that is added to the according description element.
26 * It adds the reference and the microReference.
27 * @author a.mueller
28 * @created 11.03.2010
29 * @version 1.0
30 */
31 public class DbImportDescriptionElementSourceCreationMapper extends DbImportObjectCreationMapperBase<DescriptionElementSource, DbImportStateBase<?,?>> {
32 @SuppressWarnings("unused")
33 private static final Logger logger = Logger.getLogger(DbImportDescriptionElementSourceCreationMapper.class);
34
35 //**************************** FACTORY METHOD ***********************************************/
36
37 /**
38 * Returns an description element source creation mapper.
39 * @param dbDescriptionElementFkAttribute
40 * @param descriptionElementNamespace
41 * @param dbReferenceFkAttribute
42 * @param referenceNamespace
43 * @return
44 */
45 public static DbImportDescriptionElementSourceCreationMapper NewInstance(String dbDescriptionElementFkAttribute, String descriptionElementNamespace, String dbReferenceFkAttribute, String referenceNamespace){
46 String dbMicroReferenceAttribute = null;
47 return new DbImportDescriptionElementSourceCreationMapper(dbDescriptionElementFkAttribute, descriptionElementNamespace, dbReferenceFkAttribute, referenceNamespace, dbMicroReferenceAttribute);
48 }
49
50
51 /**
52 * Returns an description element source creation mapper.
53 * @param dbDescriptionElementFkAttribute
54 * @param descriptionElementNamespace
55 * @param dbReferenceFkAttribute
56 * @param referenceNamespace
57 * @param dbMicroReferenceAttribute
58 * @return
59 */
60 public static DbImportDescriptionElementSourceCreationMapper NewInstance(String dbDescriptionElementFkAttribute, String descriptionElementNamespace, String dbReferenceFkAttribute, String referenceNamespace, String dbMicroReferenceAttribute){
61 return new DbImportDescriptionElementSourceCreationMapper(dbDescriptionElementFkAttribute, descriptionElementNamespace, dbReferenceFkAttribute, referenceNamespace, dbMicroReferenceAttribute);
62 }
63
64
65 //******************************* VARIABLES ***********************************************/
66
67 protected String descriptionElementNamespace;
68 protected String dbDescriptionElementFkAttribute;
69 protected String referenceNamespace;
70 protected String dbReferenceFkAttribute;
71 protected String dbMicroReferenceAttribute;
72
73
74 //******************************* CONSTRUCTOR ***********************************************/
75
76 /**
77 * @param dbIdAttribute
78 * @param objectToCreateNamespace
79 */
80 protected DbImportDescriptionElementSourceCreationMapper(String dbDescriptionElementFkAttribute, String descriptionElementNamespace, String dbReferenceFkAttribute, String referenceNamespace, String dbMicroReferenceAttribute) {
81 super(null, null); // idAttribute and objectToCreateNamespace not needed
82 this.descriptionElementNamespace = descriptionElementNamespace;
83 this.dbDescriptionElementFkAttribute = dbDescriptionElementFkAttribute;
84 this.dbReferenceFkAttribute = dbReferenceFkAttribute;
85 this.referenceNamespace = referenceNamespace;
86 this.dbMicroReferenceAttribute = dbMicroReferenceAttribute;
87 }
88
89 //******************************* METHODS ***********************************************/
90
91 /* (non-Javadoc)
92 * @see eu.etaxonomy.cdm.io.common.mapping.DbImportObjectCreationMapperBase#createObject(java.sql.ResultSet)
93 */
94 @Override
95 protected DescriptionElementSource createObject(ResultSet rs)throws SQLException {
96 DescriptionElementSource source = DescriptionElementSource.NewInstance();
97 return source;
98 }
99
100 /* (non-Javadoc)
101 * @see eu.etaxonomy.cdm.io.common.mapping.DbImportObjectCreationMapperBase#doInvoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.VersionableEntity)
102 */
103 @Override
104 protected DescriptionElementSource doInvoke(ResultSet rs, DescriptionElementSource source) throws SQLException {
105 addCitation(rs, source);
106 addMicroCitation(rs, source);
107 setDescriptionElement(rs, source);
108 return source;
109 }
110
111 /**
112 * @param rs
113 * @param source
114 * @throws SQLException
115 */
116 private void setDescriptionElement(ResultSet rs,
117 DescriptionElementSource source) throws SQLException {
118 DescriptionElementBase descriptionElement = (DescriptionElementBase)getRelatedObject(rs, descriptionElementNamespace, dbDescriptionElementFkAttribute);
119 source.setSourcedObj(descriptionElement);
120 }
121
122 /**
123 * @param rs
124 * @param source
125 * @throws SQLException
126 */
127 private void addMicroCitation(ResultSet rs, DescriptionElementSource source) throws SQLException {
128 String microReference = null;
129 if (CdmUtils.isNotEmpty(dbMicroReferenceAttribute)){
130 microReference = rs.getString(dbMicroReferenceAttribute);
131 }
132 source.setCitationMicroReference(microReference);
133 }
134
135 /**
136 * @param rs
137 * @param source
138 * @throws SQLException
139 */
140 private void addCitation(ResultSet rs, DescriptionElementSource source) throws SQLException {
141 ReferenceBase citation = (ReferenceBase)getRelatedObject(rs, referenceNamespace, dbReferenceFkAttribute);
142 source.setCitation(citation);
143 }
144
145 }