minor
[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 @Override
64 protected Object getValue(CdmBase cdmBase) {
65 //TODO implement also for Identifiable sources
66 if (cdmBase.isInstanceOf(DescriptionElementBase.class)){
67 //find source candidates
68 DescriptionElementBase el = CdmBase.deproxy(cdmBase, DescriptionElementBase.class);
69 Set<DescriptionElementSource> sourceCandidates = el.getSources();
70 Set<DescriptionElementSource> filteredSources = new HashSet<DescriptionElementSource>();
71 for (DescriptionElementSource sourceCandidate : sourceCandidates){
72 if (isPesiSource(sourceCandidate)){ //TODO pesi should not appear here
73 filteredSources.add(sourceCandidate);
74 }
75 }
76 //filter
77 if (filteredSources.size() == 0 ){
78 return null;
79 }else if (filteredSources.size() > 1){
80 logger.warn("There is more than 1 accepted source for description element " + el.getUuid() + ". Arbitrary first source is used.");
81 }
82 DescriptionElementSource source = filteredSources.iterator().next();
83 Reference<?> ref = source.getCitation();
84 if (ref == null){
85 logger.warn("Citation is missing for description element (" + el.getUuid() + ") source.");
86 return null;
87 }
88 if (isCache){
89 return ref.getTitleCache();
90 }else{
91 return getState().getDbId(ref);
92 }
93 }else{
94 throw new ClassCastException("CdmBase for "+this.getClass().getName() +" must be of type DescriptionElementBase, but was " + cdmBase.getClass());
95 }
96 }
97
98 private boolean isPesiSource(DescriptionElementSource source) {
99 boolean result = true;
100 if ( exclude.contains(EXCLUDE.NONE)){
101 return true;
102 }
103 if ( exclude.contains(EXCLUDE.WITH_ID)){
104 result &= StringUtils.isBlank(source.getIdInSource());
105 }else if ( exclude.contains(EXCLUDE.WITH_NAMESPACE)){
106 result &= StringUtils.isBlank(source.getIdNamespace());
107 }else{
108 logger.warn("isPesiSource case not yet handled for " + this.exclude);
109 }
110 return result;
111 }
112
113 @Override
114 protected int getSqlType() {
115 if (isCache){
116 return Types.VARCHAR;
117 }else{
118 return Types.INTEGER;
119 }
120 }
121
122 @Override
123 public Class<?> getTypeClass() {
124 if (isCache){
125 return String.class;
126 }else{
127 return Integer.class;
128 }
129 }
130 }