3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.io
.common
.mapping
.out
;
13 import java
.util
.EnumSet
;
14 import java
.util
.HashSet
;
17 import org
.apache
.commons
.lang
.StringUtils
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.hsqldb
.Types
;
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
;
29 * Maps distribution to a database foreign key or cache field.
30 * Requires according transformer implementation.
34 public class DbSingleSourceMapper
extends DbSingleAttributeExportMapperBase
<DbExportStateBase
<?
, IExportTransformer
>> implements IDbExportMapper
<DbExportStateBase
<?
, IExportTransformer
>, IExportTransformer
>{
35 private static final Logger logger
= Logger
.getLogger(DbSingleSourceMapper
.class);
42 public static int EXCLUDE_NONE
= 0;
43 public static int EXCLUDE_WITH_ID
= 1;
44 public static int EXCLUDE_WITH_NAMESPACE
= 2;
46 private boolean isCache
= false;
47 private EnumSet
<EXCLUDE
> exclude
;
49 public static DbSingleSourceMapper
NewInstance(String dbAttributeString
, EnumSet
<EXCLUDE
> exclude
, boolean isCache
){
50 return new DbSingleSourceMapper(dbAttributeString
, exclude
, isCache
, null);
54 * @param dbAttributeString
55 * @param cdmAttributeString
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
;
64 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValue(eu.etaxonomy.cdm.model.common.CdmBase)
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
);
80 if (filteredSources
.size() == 0 ){
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.");
85 DescriptionElementSource source
= filteredSources
.iterator().next();
86 Reference
<?
> ref
= source
.getCitation();
88 logger
.warn("Citation is missing for description element (" + el
.getUuid() + ") source.");
92 return ref
.getTitleCache();
94 return getState().getDbId(ref
);
97 throw new ClassCastException("CdmBase for "+this.getClass().getName() +" must be of type DescriptionElementBase, but was " + cdmBase
.getClass());
101 private boolean isPesiSource(DescriptionElementSource source
) {
102 boolean result
= true;
103 if ( exclude
.contains(EXCLUDE
.NONE
)){
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());
111 logger
.warn("isPesiSource case not yet handled for " + this.exclude
);
117 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValueType()
120 protected int getSqlType() {
122 return Types
.VARCHAR
;
124 return Types
.INTEGER
;
130 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
133 public Class
<?
> getTypeClass() {
137 return Integer
.class;