Project

General

Profile

Download (1.9 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.logging.log4j.LogManager;
15
import org.apache.logging.log4j.Logger;
16

    
17
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
18
import eu.etaxonomy.cdm.model.common.CdmBase;
19

    
20
/**
21
 * @author a.mueller
22
 * @since 28.09.2019
23
 */
24
public class DbFixedStringMapper
25
        extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>>
26
        implements IDbExportMapper<DbExportStateBase<?,IExportTransformer>, IExportTransformer> {
27

    
28
    private static final Logger logger = LogManager.getLogger();
29

    
30
	private static final int MAX_PRECISION = -1;  //precision for datatype nvarchar(max) == clob (SQL Server 2008)
31

    
32
	private String fixString;
33

    
34
	public static DbFixedStringMapper NewInstance(String fixString, String dbAttributeString){
35
		return new DbFixedStringMapper(fixString, dbAttributeString);
36
	}
37

    
38
	private DbFixedStringMapper(String fixString, String dbAttributeString) {
39
		super(null, dbAttributeString, fixString, true, false);
40
		this.fixString = fixString;
41
	}
42

    
43
	@Override
44
	protected Object getValue(CdmBase cdmBase) {
45
		String result = fixString;
46

    
47
    	//truncate if needed
48
    	if (result != null && result.length() > getPrecision() && getPrecision() != MAX_PRECISION && getPrecision() > 0){
49
    		logger.warn("Truncation (" + result.length() + "->" + getPrecision() + ") needed for Attribute " + getDestinationAttribute() + " in " +  cdmBase + "." );
50
    		result = result.substring(0, getPrecision());
51
    	}
52
		return result;
53
	}
54

    
55
	@Override
56
	protected int getSqlType() {
57
		return Types.VARCHAR;
58
	}
59

    
60
	@Override
61
	public Class<?> getTypeClass() {
62
		return String.class;
63
	}
64
}
(18-18/44)