ref #8162 adapt cdmlip to new term package structure
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportSupplementCreationMapperBase.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.common.mapping;
11
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14
15 import org.apache.log4j.Logger;
16
17 import eu.etaxonomy.cdm.common.CdmUtils;
18 import eu.etaxonomy.cdm.io.common.DbImportStateBase;
19 import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
20 import eu.etaxonomy.cdm.model.common.Annotation;
21 import eu.etaxonomy.cdm.model.common.AnnotationType;
22 import eu.etaxonomy.cdm.model.common.Language;
23 import eu.etaxonomy.cdm.model.common.VersionableEntity;
24 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
25
26 /**
27 * @author a.mueller
28 * @since 12.05.2009
29 * @version 1.0
30 */
31 public abstract class DbImportSupplementCreationMapperBase<SUPPLEMENT extends VersionableEntity, SUPPLEMENTABLE extends AnnotatableEntity, STATE extends DbImportStateBase<?,?>, TYPE extends DefinedTermBase> extends DbImportObjectCreationMapperBase<SUPPLEMENT, STATE> {
32 @SuppressWarnings("unused")
33 private static final Logger logger = Logger.getLogger(DbImportSupplementCreationMapperBase.class);
34
35
36 //******************************* ATTRIBUTES ***************************************/
37 protected String dbSupplementValueAttribute;
38 protected String dbSupplementedObjectAttribute;
39 protected TYPE supplementType;
40 protected boolean addOriginalSourceId = false;
41
42
43 //********************************* CONSTRUCTOR ****************************************/
44 /**
45 * @param mappingImport
46 */
47 protected DbImportSupplementCreationMapperBase(String dbSupplementValueAttribute, String dbSupplementedObjectAttribute, String dbIdAttribute, String supplementedObjectNamespace, TYPE supplementType) {
48 super(dbIdAttribute, supplementedObjectNamespace);
49 this.dbSupplementValueAttribute = dbSupplementValueAttribute;
50 this.dbSupplementedObjectAttribute = dbSupplementedObjectAttribute;
51 this.supplementType = supplementType;
52 //FIXME make it a real Multiple attribute mapper
53 // this.singleMappers.add(DbImportAnnotationMapper.NewInstance(dbAnnotationAttribute, annotationType, language));
54 // String relatedObjectNamespace = "yyy";
55 // this.singleMappers.add(DbImportObjectMapper.NewInstance(dbAnnotatedObjectAttribute, "xxx",relatedObjectNamespace ));
56 }
57
58 //************************************ METHODS *******************************************/
59
60 /* (non-Javadoc)
61 * @see eu.etaxonomy.cdm.io.common.mapping.DbImportObjectCreationMapperBase#doInvoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.VersionableEntity)
62 */
63 @Override
64 protected SUPPLEMENT doInvoke(ResultSet rs, SUPPLEMENT supplement) throws SQLException {
65 //add object to supplementable object
66 String strId = getStringDbValue(rs, dbSupplementedObjectAttribute);
67 SUPPLEMENTABLE supplementableEntity = (SUPPLEMENTABLE)getRelatedObject(objectToCreateNamespace, strId);
68 addSupplement(supplement, supplementableEntity, strId);
69
70 //set Value
71 if (CdmUtils.isNotEmpty(dbSupplementValueAttribute)){
72 setSupplementValue(rs, supplement);
73 }
74
75 //return
76 return supplement;
77 }
78
79 /**
80 * @param value
81 *
82 */
83 protected abstract void setSupplementValue(ResultSet rs, SUPPLEMENT supplement) throws SQLException;
84
85 /**
86 * Adds the supplement to the supplementable entity. Returns falls if supplementable entity is
87 * <code>null</code>
88 * @param supplement the supplement (e.g. an instance of class Extension)
89 * @param supplementableEntity the supplementable entity (e.g. an <code>IdentifiableEntity</code> in case of a
90 * supplement of type <code>Extension</code>
91 * @param id the supplementableEntity original source id (needed for verbose logging)
92 */
93 protected abstract boolean addSupplement(SUPPLEMENT supplement, SUPPLEMENTABLE supplementableEntity, String id) ;
94
95
96
97 /* (non-Javadoc)
98 * @see eu.etaxonomy.cdm.io.common.mapping.DbImportObjectCreationMapperBase#addOriginalSource(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.VersionableEntity)
99 */
100 @Override
101 public void addOriginalSource(ResultSet rs, SUPPLEMENT supplement) throws SQLException {
102 if (addOriginalSourceId && supplement.isInstanceOf(AnnotatableEntity.class)){
103 AnnotatableEntity annotatableEntity = (AnnotatableEntity)supplement;
104 Language orginalSourceLanguage = null;
105 AnnotationType annotationType = AnnotationType.TECHNICAL();
106 String originalId = getStringDbValue(rs, dbIdAttribute);
107 Annotation idAnnotation = Annotation.NewInstance(originalId, annotationType, orginalSourceLanguage);
108 annotatableEntity.addAnnotation(idAnnotation);
109 }
110 }
111
112
113
114 }