Project

General

Profile

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

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

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

    
27
    private static final Logger logger = LogManager.getLogger(DbFixedStringMapper.class);
28

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

    
31
	private String fixString;
32

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

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

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

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

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

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