Project

General

Profile

Download (5.08 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.ArrayList;
14
import java.util.Arrays;
15
import java.util.List;
16

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

    
19
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
20
import eu.etaxonomy.cdm.model.common.CdmBase;
21
import eu.etaxonomy.cdm.model.common.Language;
22
import eu.etaxonomy.cdm.model.common.LanguageString;
23
import eu.etaxonomy.cdm.model.description.TextData;
24

    
25
/**
26
 * Maps text data to a database string field. (Only handles one language)
27
 * @author a.mueller
28
 * @since 06.02.2012
29
 */
30
public class DbTextDataMapper extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>> implements IDbExportMapper<DbExportStateBase<?, IExportTransformer>, IExportTransformer>{
31
	@SuppressWarnings("unused")
32
	private static final Logger logger = LogManager.getLogger(DbTextDataMapper.class);
33

    
34
	private final List<Language> languages;
35
	private boolean restrictToGivenLanguages;
36

    
37
    /**
38
     * Returns a mapper that uses the default language representation first.
39
     * If it does not exist it uses any existing representation.
40
     * @param dbAttributeString the target column name
41
     * @return the DbTextDataMapper
42
     */
43
    public static DbTextDataMapper NewDefaultInstance(String dbAttributeString){
44
        return new DbTextDataMapper(null, dbAttributeString, false, null);
45
    }
46

    
47
    /**
48
     * Returns a mapper that uses the given language representation first.
49
     * If it does not exist it uses the default language or, if it also does not exist,
50
     * any existing representation.
51
     * @param dbAttributeString the target column name
52
     * @param language the preferred language of the representation to use
53
     * @return the DbTextDataMapper
54
     */
55
	public static DbTextDataMapper NewInstance(String dbAttributeString, Language language){
56
		return new DbTextDataMapper(toList(language), dbAttributeString, false, null);
57
	}
58

    
59
    /**
60
     * Returns a mapper that uses the given language representation.
61
     * If a representation in the given language does not exist the default value is returned.
62
     * Representations in other languages are NOT considered.
63
     * @param dbAttributeString the target column name
64
     * @param language the ONLY language of the representation to use
65
     * @return the DbTextDataMapper
66
     */
67
	public static DbTextDataMapper NewInstance(String dbAttributeString, Language language, String defaultValue){
68
	    return new DbTextDataMapper(toList(language), dbAttributeString, true, defaultValue);
69
	}
70

    
71
    /**
72
     * Returns a mapper that returns a representation according to the
73
     * language priorisation of the *languages* attribute.
74
     * If no representation in any of the given languages exists
75
     * and *restrictToGivenLanguages* is <code>false</code> first the
76
     * any other representation is returned with the applications default
77
     * language having priority.
78
     * If still no representation exists or if *restrictToGivenLanguages*
79
     * is <code>true</code> the *defaultValue* is returned.
80

    
81
     * @param dbAttributeString the target column name
82
     * @param languages the sorted list of (preferred) languages
83
     * @param restrictToGivenLanguages flag wether to restrict to given languages or not
84
     * @param defaultValue the default value
85
     * @return the DbTextDataMapper
86
     */
87
    public static DbTextDataMapper NewInstance(String dbAttributeString, List<Language> languages,
88
            boolean restrictToGivenLanguages, String defaultValue){
89
        return new DbTextDataMapper(languages, dbAttributeString, restrictToGivenLanguages, defaultValue);
90
    }
91

    
92
    private static List<Language> toList(Language language) {
93
        return Arrays.asList(new Language[]{language});
94
    }
95

    
96
//************************* CONSTRUCTOR ********************************************/
97

    
98
	protected DbTextDataMapper(List<Language> languages, String dbAttributeString,
99
	            boolean restrictToGivenLanguages, Object defaultValue) {
100

    
101
	    super("multiLanguageText", dbAttributeString, defaultValue);
102
	    if (languages == null){
103
	        languages = new ArrayList<>();
104
	    }
105
		this.languages  = languages;
106
		this.restrictToGivenLanguages = restrictToGivenLanguages;
107
	}
108

    
109
	@Override
110
	protected Object getValue(CdmBase cdmBase) {
111
		if (cdmBase.isInstanceOf(TextData.class)){
112
			TextData textData = CdmBase.deproxy(cdmBase, TextData.class);
113
			LanguageString langString = textData.getPreferredLanguageString(languages, restrictToGivenLanguages);
114
			if (langString != null){
115
				return langString.getText();
116
			}else{
117
				return null;
118
			}
119
		}else{
120
			throw new ClassCastException("CdmBase for "+this.getClass().getName() +" must be of type TextData, but was " + cdmBase.getClass());
121
		}
122
	}
123

    
124
	@Override
125
	protected int getSqlType() {
126
		return Types.VARCHAR;
127
	}
128

    
129
	@Override
130
	public Class<?> getTypeClass() {
131
		return String.class;
132
	}
133
}
(33-33/44)