Project

General

Profile

Download (2.95 KB) Statistics
| Branch: | Tag: | Revision:
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.out;
11

    
12
import java.sql.Types;
13

    
14
import org.apache.log4j.Logger;
15

    
16
import eu.etaxonomy.cdm.common.CdmUtils;
17
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
18
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
19
import eu.etaxonomy.cdm.model.common.Annotation;
20
import eu.etaxonomy.cdm.model.common.CdmBase;
21

    
22
/**
23
 * Maps all Annotations belonging to an AnnotatableEntity to a DB field, using <code>separator</code>
24
 * as separator. If annotationPrefix is not <code>null</code>, only Annotations with the given prefix are used.
25
 * @author a.mueller
26
 * @since 12.05.2009
27
 */
28
public class DbAnnotationMapper extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>> implements IDbExportMapper<DbExportStateBase<?, IExportTransformer>, IExportTransformer>{
29
	@SuppressWarnings("unused")
30
	private static final Logger logger = Logger.getLogger(DbAnnotationMapper.class);
31

    
32
	private final String annotationPrefix;
33
	private String separator = ";";
34

    
35
	public static DbAnnotationMapper NewInstance(String annotationPrefix, String dbAttributeString){
36
		return new DbAnnotationMapper(annotationPrefix, dbAttributeString, null, null);
37
	}
38

    
39
	public static DbAnnotationMapper NewInstance(String annotationPrefix, String dbAttributeString, String defaultValue){
40
		return new DbAnnotationMapper(annotationPrefix, dbAttributeString, defaultValue, null);
41
	}
42

    
43
	/**
44
	 * @param dbAttributeString
45
	 * @param cdmAttributeString
46
	 */
47
	protected DbAnnotationMapper(String annotationPrefix, String dbAttributeString, Object defaultValue, String separator) {
48
		super("annotations", dbAttributeString, defaultValue);
49
		this.annotationPrefix  = annotationPrefix;
50
		if (separator != null){
51
			this.separator = separator;
52
		}
53
	}
54

    
55
	@Override
56
	protected Object getValue(CdmBase cdmBase) {
57
		String result = null;
58
		if (cdmBase.isInstanceOf(AnnotatableEntity.class)){
59
			AnnotatableEntity annotatableEntity = (AnnotatableEntity)cdmBase;
60
			for (Annotation annotation : annotatableEntity.getAnnotations()){
61
				String text = annotation.getText();
62
				if (text != null){
63
					if (this.annotationPrefix != null && text.startsWith(this.annotationPrefix) ){
64
						if (text.startsWith(this.annotationPrefix)){
65
							text = text.substring(annotationPrefix.length()).trim();
66
							result = CdmUtils.concat(separator, result, text);
67
						}
68
					}else{
69
						result = CdmUtils.concat(separator, result, text);
70
					}
71
				}
72
			}
73
		}else{
74
			throw new ClassCastException("CdmBase for DbAnnotationMapper must be of type AnnotatableEntity, but was " + cdmBase.getClass());
75
		}
76
		return result;
77
	}
78

    
79
	@Override
80
	protected int getSqlType() {
81
		return Types.VARCHAR;
82
	}
83

    
84
	@Override
85
	public Class<?> getTypeClass() {
86
		return String.class;
87
	}
88
}
(4-4/40)