Project

General

Profile

Download (3.14 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.UUID;
14

    
15
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
16
import org.joda.time.DateTime;
17

    
18
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
19
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
20
import eu.etaxonomy.cdm.model.common.Annotation;
21
import eu.etaxonomy.cdm.model.common.CdmBase;
22
import eu.etaxonomy.cdm.model.common.VersionableEntity;
23

    
24
/**
25
 * @author a.mueller
26
 * @since 12.05.2009
27
 */
28
public class DbLastActionMapper
29
        extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>>{
30

    
31
    @SuppressWarnings("unused")
32
	private static final Logger logger = LogManager.getLogger(DbLastActionMapper.class);
33

    
34
	public static final UUID uuidMarkerTypeHasNoLastAction = UUID.fromString("99652d5a-bc92-4251-b57d-0fec4d258ab7");
35
	public static final UUID uuidAnnotationTypeLastAction = UUID.fromString("d0423ffd-d1dc-4571-ba05-eb724eec3c77");
36
	public static final UUID uuidAnnotationTypeLastActionDate = UUID.fromString("f666fd60-b5dc-4950-9d6a-d3ded7d596d7");
37

    
38
	boolean isActionType;
39

    
40
	public static DbLastActionMapper NewInstance(String dbAttributeString, boolean isActionType){
41
		return new DbLastActionMapper(dbAttributeString, null, isActionType);
42
	}
43

    
44
	private DbLastActionMapper(String dbAttributeString, String defaultValue, boolean isActionType) {
45
		super("updated, created", dbAttributeString, defaultValue, false, false);
46
		this.isActionType = isActionType;
47
	}
48

    
49
	@Override
50
	protected Object getValue(CdmBase cdmBase) {
51
		if (cdmBase.isInstanceOf(VersionableEntity.class)){
52
			//exclude objects marked as 'hasNoM
53
			if (cdmBase.isInstanceOf(AnnotatableEntity.class)){
54
				AnnotatableEntity annotatableEntity = CdmBase.deproxy(cdmBase, AnnotatableEntity.class);
55
				UUID uuidType = isActionType ? uuidAnnotationTypeLastAction : uuidAnnotationTypeLastActionDate;
56
				for (Annotation annotation : annotatableEntity.getAnnotations(uuidType)){
57
				    return annotation.getText();
58
				}
59
				if (annotatableEntity.hasMarker(uuidMarkerTypeHasNoLastAction, true)){
60
					return null;
61
				}
62
			}
63
			//return updated or created
64
			VersionableEntity versionable = CdmBase.deproxy(cdmBase, VersionableEntity.class);
65
			if (versionable.getUpdated() != null){
66
				return makeChanged(versionable);
67
			}else{
68
				return makeCreated(cdmBase);
69
			}
70
		}else{
71
			//return created
72
			return makeCreated(cdmBase);
73
		}
74
	}
75

    
76
	private Object makeChanged(VersionableEntity versionable) {
77
		if (isActionType){
78
			return "changed";
79
		}else{
80
			return versionable.getUpdated();
81
		}
82
	}
83

    
84
	private Object makeCreated(CdmBase cdmBase) {
85
		if (isActionType){
86
			return "created";
87
		}else{
88
			return cdmBase.getCreated();
89
		}
90
	}
91

    
92
	@Override
93
	protected int getSqlType() {
94
		if (isActionType){
95
			return Types.VARCHAR;
96
		}else{
97
			return Types.DATE;
98
		}
99
	}
100

    
101
	@Override
102
	public Class<?> getTypeClass() {
103
		return DateTime.class;
104
	}
105

    
106
}
(23-23/44)