add language mapper and single source mapper
[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.common.DescriptionElementSource;
24 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
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<?>> implements IDbExportMapper<DbExportStateBase<?>>{
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 DescriptionElementBase el = CdmBase.deproxy(cdmBase, DescriptionElementBase.class);
71 Set<DescriptionElementSource> sourceCandidates = el.getSources();
72 Set<DescriptionElementSource> filteredSources = new HashSet<DescriptionElementSource>();
73 for (DescriptionElementSource sourceCandidate : sourceCandidates){
74 if (isPesiSource(sourceCandidate)){
75 filteredSources.add(sourceCandidate);
76 }
77 }
78 if (filteredSources.size() == 0 ){
79 return null;
80 }else if (filteredSources.size() > 1){
81 logger.warn("There is more than 1 accepted source for description element " + el.getUuid() + ". Arbitrary first source is used.");
82 }
83 DescriptionElementSource source = filteredSources.iterator().next();
84 Reference<?> ref = source.getCitation();
85 if (ref == null){
86 logger.warn("Citation is missing for description element (" + el.getUuid() + ") source.");
87 return null;
88 }
89 if (isCache){
90 return ref.getTitleCache();
91 }else{
92 return getState().getDbId(ref);
93 }
94 }else{
95 throw new ClassCastException("CdmBase for "+this.getClass().getName() +" must be of type DescriptionElementBase, but was " + cdmBase.getClass());
96 }
97 }
98
99 private boolean isPesiSource(DescriptionElementSource source) {
100 boolean result = true;
101 if ( exclude.contains(EXCLUDE.NONE)){
102 return true;
103 }
104 if ( exclude.contains(EXCLUDE.WITH_ID)){
105 result &= StringUtils.isBlank(source.getIdInSource());
106 }else if ( exclude.contains(EXCLUDE.WITH_NAMESPACE)){
107 result &= StringUtils.isBlank(source.getIdNamespace());
108 }else{
109 logger.warn("isPesiSource case not yet handled for " + this.exclude);
110 }
111 return result;
112 }
113
114 /* (non-Javadoc)
115 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValueType()
116 */
117 @Override
118 protected int getSqlType() {
119 if (isCache){
120 return Types.VARCHAR;
121 }else{
122 return Types.INTEGER;
123 }
124 }
125
126
127 /* (non-Javadoc)
128 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
129 */
130 @Override
131 public Class<?> getTypeClass() {
132 if (isCache){
133 return String.class;
134 }else{
135 return Integer.class;
136 }
137 }
138 }