Project

General

Profile

Download (4.13 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
import java.util.Collection;
14
import java.util.HashSet;
15
import java.util.UUID;
16

    
17
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
18

    
19
import eu.etaxonomy.cdm.common.CdmUtils;
20
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
21
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
22
import eu.etaxonomy.cdm.model.common.Annotation;
23
import eu.etaxonomy.cdm.model.common.AnnotationType;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25

    
26
/**
27
 * Maps all Annotations belonging to an AnnotatableEntity to a DB field, using <code>separator</code>
28
 * as separator. If annotationPrefix is not <code>null</code>, only Annotations with the given prefix are used.
29
 *
30
 * @author a.mueller
31
 * @since 12.05.2009
32
 */
33
public class DbAnnotationMapper
34
            extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>>{
35

    
36
    @SuppressWarnings("unused")
37
	private static final Logger logger = LogManager.getLogger(DbAnnotationMapper.class);
38

    
39
	private final String annotationPrefix;
40
	private String separator = ";";
41
	//TODO switch to UUID
42
	private Collection<AnnotationType> excludedTypes = new HashSet<>();
43
	private Collection<UUID> includedTypes = new HashSet<>();
44

    
45
	public static DbAnnotationMapper NewInstance(String annotationPrefix, String dbAttributeString){
46
		return new DbAnnotationMapper(dbAttributeString, annotationPrefix, null, null, null, null);
47
	}
48

    
49
	public static DbAnnotationMapper NewInstance(String annotationPrefix, String dbAttributeString, String defaultValue){
50
		return new DbAnnotationMapper(dbAttributeString, annotationPrefix, null, null, defaultValue, null);
51
	}
52

    
53
    public static DbAnnotationMapper NewExludedInstance(Collection<AnnotationType> excludedTypes, String dbAttributeString){
54
        return new DbAnnotationMapper(dbAttributeString, null, null, excludedTypes, null, null);
55
    }
56

    
57
    public static DbAnnotationMapper NewIncludedInstance(Collection<UUID> includedTypes, String dbAttributeString){
58
        return new DbAnnotationMapper(dbAttributeString, null, includedTypes, null, null, null);
59
    }
60

    
61
	protected DbAnnotationMapper(String dbAttributeString, String annotationPrefix, Collection<UUID> includedTypes,
62
	        Collection<AnnotationType> excludedTypes, Object defaultValue, String separator) {
63

    
64
	    super("annotations", dbAttributeString, defaultValue);
65
		this.annotationPrefix  = annotationPrefix;
66
		if (separator != null){
67
			this.separator = separator;
68
		}
69
		this.includedTypes = includedTypes;
70
		this.excludedTypes = excludedTypes == null? new HashSet<>(): excludedTypes;
71
	}
72

    
73
	@Override
74
	protected Object getValue(CdmBase cdmBase) {
75
		String result = null;
76
		if (cdmBase.isInstanceOf(AnnotatableEntity.class)){
77
			AnnotatableEntity annotatableEntity = (AnnotatableEntity)cdmBase;
78
			for (Annotation annotation : annotatableEntity.getAnnotations()){
79
			    //include + exclude
80
			    if (includedTypes != null &&
81
			            (annotation.getAnnotationType() == null || !includedTypes.contains(annotation.getAnnotationType().getUuid()))){
82
                    continue;
83
                }
84
			    if (excludedTypes.contains(annotation.getAnnotationType())){
85
				    continue;
86
				}
87

    
88
			    String text = annotation.getText();
89
				if (text != null){
90
					if (this.annotationPrefix != null && text.startsWith(this.annotationPrefix) ){
91
						if (text.startsWith(this.annotationPrefix)){
92
							text = text.substring(annotationPrefix.length()).trim();
93
							result = CdmUtils.concat(separator, result, text);
94
						}
95
					}else{
96
						result = CdmUtils.concat(separator, result, text);
97
					}
98
				}
99
			}
100
		}else{
101
			throw new ClassCastException("CdmBase for DbAnnotationMapper must be of type AnnotatableEntity, but was " + cdmBase.getClass());
102
		}
103
		return result;
104
	}
105

    
106
	@Override
107
	protected int getSqlType() {
108
		return Types.VARCHAR;
109
	}
110

    
111
	@Override
112
	public Class<?> getTypeClass() {
113
		return String.class;
114
	}
115
}
(4-4/44)