1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.strategy.match;
|
12
|
|
13
|
import java.lang.reflect.Field;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.Arrays;
|
16
|
import java.util.List;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
|
20
|
import eu.etaxonomy.cdm.common.DoubleResult;
|
21
|
import eu.etaxonomy.cdm.strategy.match.Match.ReplaceMode;
|
22
|
|
23
|
/**
|
24
|
* @author a.mueller
|
25
|
* @created 07.08.2009
|
26
|
* @version 1.0
|
27
|
*/
|
28
|
public class CacheMatcher extends FieldMatcherBase {
|
29
|
@SuppressWarnings("unused")
|
30
|
private static final Logger logger = Logger.getLogger(CacheMatcher.class);
|
31
|
|
32
|
private ReplaceMode cacheReplaceMode;
|
33
|
private List<String> replacedProperties;
|
34
|
private MatchMode replaceMatchMode;
|
35
|
|
36
|
public static CacheMatcher NewInstance(Field field, ReplaceMode replaceMode, String[] replacedProperties, MatchMode replaceMatchMode){
|
37
|
return new CacheMatcher(field.getName(), field, MatchMode.CACHE, replaceMode, Arrays.asList(replacedProperties), replaceMatchMode);
|
38
|
}
|
39
|
|
40
|
private CacheMatcher (String propertyname, Field field, MatchMode matchMode, ReplaceMode replaceMode, List<String> replacedProperties, MatchMode replaceMatchMode){
|
41
|
super(propertyname, field, matchMode);
|
42
|
this.replacedProperties = replacedProperties;
|
43
|
this.cacheReplaceMode = replaceMode;
|
44
|
this.replaceMatchMode = replaceMatchMode;
|
45
|
}
|
46
|
|
47
|
public Field getProtectedField(Matching matching){
|
48
|
String protectedPropertyName = getProtectedPropertyName();
|
49
|
FieldMatcher fieldMatcher = matching.getFieldMatcher(protectedPropertyName);
|
50
|
return fieldMatcher.getField();
|
51
|
}
|
52
|
|
53
|
/**
|
54
|
* @return
|
55
|
*/
|
56
|
public String getProtectedPropertyName() {
|
57
|
String protectedPropertyName = "protected" + this.getPropertyName().substring(0, 1).toUpperCase() + this.getPropertyName().substring(1);
|
58
|
return protectedPropertyName;
|
59
|
}
|
60
|
|
61
|
|
62
|
|
63
|
public List<DoubleResult<String, MatchMode>> getReplaceMatchModes(Matching matching) throws MatchException{
|
64
|
List<DoubleResult<String, MatchMode>> result = new ArrayList<DoubleResult<String,MatchMode>>();
|
65
|
List<FieldMatcher> fieldMatchers = matching.getFieldMatchers(true);
|
66
|
for (FieldMatcher fieldMatcher : fieldMatchers){
|
67
|
String propertyName = fieldMatcher.getPropertyName();
|
68
|
if (isReplaceProperty(propertyName)){
|
69
|
result.add(new DoubleResult<String, MatchMode>(propertyName, replaceMatchMode));
|
70
|
}
|
71
|
}
|
72
|
return result;
|
73
|
}
|
74
|
|
75
|
|
76
|
/**
|
77
|
* @param fieldMatcher
|
78
|
* @return
|
79
|
* @throws MatchException
|
80
|
*/
|
81
|
private boolean isReplaceProperty(String propertyName) throws MatchException {
|
82
|
if (this.cacheReplaceMode == ReplaceMode.NONE){
|
83
|
return false;
|
84
|
}else if (this.cacheReplaceMode == ReplaceMode.ALL){
|
85
|
return true;
|
86
|
}else if (this.cacheReplaceMode == ReplaceMode.DEFINED){
|
87
|
return this.replacedProperties.contains(propertyName);
|
88
|
}else if (this.cacheReplaceMode == ReplaceMode.DEFINED_REVERSE){
|
89
|
return ! this.replacedProperties.contains(propertyName);
|
90
|
}else{
|
91
|
throw new MatchException("ReplaceMode not supported: " + this.cacheReplaceMode);
|
92
|
}
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* @return the cacheReplaceMode
|
97
|
*/
|
98
|
public ReplaceMode getCacheReplaceMode() {
|
99
|
return cacheReplaceMode;
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* @param cacheReplaceMode the cacheReplaceMode to set
|
104
|
*/
|
105
|
public void setCacheReplaceMode(ReplaceMode cacheReplaceMode) {
|
106
|
this.cacheReplaceMode = cacheReplaceMode;
|
107
|
}
|
108
|
|
109
|
|
110
|
|
111
|
/**
|
112
|
* @return the replacedProperties
|
113
|
*/
|
114
|
public List<String> getReplacedProperties() {
|
115
|
return replacedProperties;
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* @param replacedProperties the replacedProperties to set
|
120
|
*/
|
121
|
public void setReplacedProperties(List<String> replacedProperties) {
|
122
|
this.replacedProperties = replacedProperties;
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* @return the replaceMatchMode
|
127
|
*/
|
128
|
public MatchMode getReplaceMatchMode() {
|
129
|
return replaceMatchMode;
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* @param replaceMatchMode the replaceMatchMode to set
|
134
|
*/
|
135
|
public void setReplaceMatchMode(MatchMode replaceMatchMode) {
|
136
|
this.replaceMatchMode = replaceMatchMode;
|
137
|
}
|
138
|
|
139
|
|
140
|
|
141
|
}
|