change parent pom version in 3.3
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / out / DbSingleSourceMapper.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.out;
12
13 import java.util.EnumSet;
14 import java.util.HashSet;
15 import java.util.Set;
16
17 import org.apache.commons.lang.StringUtils;
18 import org.apache.log4j.Logger;
19 import org.hsqldb.Types;
20
21 import eu.etaxonomy.cdm.io.common.DbExportStateBase;
22 import eu.etaxonomy.cdm.model.common.CdmBase;
23 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
24 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
25 import eu.etaxonomy.cdm.model.reference.Reference;
26
27
28 /**
29 * Maps distribution to a database foreign key or cache field.
30 * Requires according transformer implementation.
31 * @author a.mueller
32 * @created 06.02.2012
33 */
34 public class DbSingleSourceMapper extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>> implements IDbExportMapper<DbExportStateBase<?, IExportTransformer>, IExportTransformer>{
35 private static final Logger logger = Logger.getLogger(DbSingleSourceMapper.class);
36
37 public enum EXCLUDE{
38 NONE,
39 WITH_ID,
40 WITH_NAMESPACE
41 }
42 public static int EXCLUDE_NONE = 0;
43 public static int EXCLUDE_WITH_ID = 1;
44 public static int EXCLUDE_WITH_NAMESPACE = 2;
45
46 private boolean isCache = false;
47 private EnumSet<EXCLUDE> exclude;
48
49 public static DbSingleSourceMapper NewInstance(String dbAttributeString, EnumSet<EXCLUDE> exclude, boolean isCache){
50 return new DbSingleSourceMapper(dbAttributeString, exclude, isCache, null);
51 }
52
53 /**
54 * @param dbAttributeString
55 * @param cdmAttributeString
56 */
57 protected DbSingleSourceMapper(String dbAttributeString, EnumSet<EXCLUDE> exclude, boolean isCache, Object defaultValue) {
58 super("sources", dbAttributeString, defaultValue);
59 this.isCache = isCache;
60 this.exclude = exclude;
61 }
62
63 /* (non-Javadoc)
64 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValue(eu.etaxonomy.cdm.model.common.CdmBase)
65 */
66 @Override
67 protected Object getValue(CdmBase cdmBase) {
68 //TODO implement also for Identifiable sources
69 if (cdmBase.isInstanceOf(DescriptionElementBase.class)){
70 //find source candidates
71 DescriptionElementBase el = CdmBase.deproxy(cdmBase, DescriptionElementBase.class);
72 Set<DescriptionElementSource> sourceCandidates = el.getSources();
73 Set<DescriptionElementSource> filteredSources = new HashSet<DescriptionElementSource>();
74 for (DescriptionElementSource sourceCandidate : sourceCandidates){
75 if (isPesiSource(sourceCandidate)){ //TODO pesi should not appear here
76 filteredSources.add(sourceCandidate);
77 }
78 }
79 //filter
80 if (filteredSources.size() == 0 ){
81 return null;
82 }else if (filteredSources.size() > 1){
83 logger.warn("There is more than 1 accepted source for description element " + el.getUuid() + ". Arbitrary first source is used.");
84 }
85 DescriptionElementSource source = filteredSources.iterator().next();
86 Reference<?> ref = source.getCitation();
87 if (ref == null){
88 logger.warn("Citation is missing for description element (" + el.getUuid() + ") source.");
89 return null;
90 }
91 if (isCache){
92 return ref.getTitleCache();
93 }else{
94 return getState().getDbId(ref);
95 }
96 }else{
97 throw new ClassCastException("CdmBase for "+this.getClass().getName() +" must be of type DescriptionElementBase, but was " + cdmBase.getClass());
98 }
99 }
100
101 private boolean isPesiSource(DescriptionElementSource source) {
102 boolean result = true;
103 if ( exclude.contains(EXCLUDE.NONE)){
104 return true;
105 }
106 if ( exclude.contains(EXCLUDE.WITH_ID)){
107 result &= StringUtils.isBlank(source.getIdInSource());
108 }else if ( exclude.contains(EXCLUDE.WITH_NAMESPACE)){
109 result &= StringUtils.isBlank(source.getIdNamespace());
110 }else{
111 logger.warn("isPesiSource case not yet handled for " + this.exclude);
112 }
113 return result;
114 }
115
116 /* (non-Javadoc)
117 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValueType()
118 */
119 @Override
120 protected int getSqlType() {
121 if (isCache){
122 return Types.VARCHAR;
123 }else{
124 return Types.INTEGER;
125 }
126 }
127
128
129 /* (non-Javadoc)
130 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
131 */
132 @Override
133 public Class<?> getTypeClass() {
134 if (isCache){
135 return String.class;
136 }else{
137 return Integer.class;
138 }
139 }
140 }