Project

General

Profile

Download (5.05 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
package eu.etaxonomy.cdm.io.markup;
10

    
11
import java.util.HashMap;
12
import java.util.HashSet;
13
import java.util.Map;
14
import java.util.Set;
15

    
16
import org.apache.commons.lang.StringUtils;
17
import org.apache.log4j.Logger;
18

    
19
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
20
import eu.etaxonomy.cdm.common.CdmUtils;
21
import eu.etaxonomy.cdm.model.description.PolytomousKey;
22
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
23

    
24
public class UnmatchedLeads {
25
	private static final Logger logger = Logger.getLogger(UnmatchedLeads.class);
26

    
27
	
28
	private Map<UnmatchedLeadsKey, Set<PolytomousKeyNode>> map = new HashMap<UnmatchedLeadsKey, Set<PolytomousKeyNode>>();
29
	
30
	public static class UnmatchedLeadsKey{
31
		public static UnmatchedLeadsKey NewInstance(PolytomousKey key, String num){
32
			UnmatchedLeadsKey result = new UnmatchedLeadsKey();
33
			result.key = key;
34
			result.num = num;
35
			return result;
36
		}
37
		
38
		public static UnmatchedLeadsKey NewInstance(String num, String taxon){
39
			num = (StringUtils.isBlank(num) ? "" : num + " " );
40
			return NewInstance(num + taxon);
41
		}
42
		
43
		public static UnmatchedLeadsKey NewInstance(String numAndTaxon){
44
			UnmatchedLeadsKey result = new UnmatchedLeadsKey();
45
			result.numAndTaxon = numAndTaxon;
46
			return result;
47
		}
48
		
49
		//firstPart
50
		PolytomousKey key;
51
		//secondPart
52
		String num;
53
		
54
		//taxonKey
55
		String numAndTaxon;
56

    
57
		
58
		public boolean isInnerLead(){
59
			return (key != null);
60
		}
61
		
62
		public boolean isTaxonLead(){
63
			return (numAndTaxon != null);
64
		}
65
		
66
		@Override
67
		public boolean equals(Object object){
68
			if (object == null ||   ! (object instanceof UnmatchedLeadsKey)){
69
				return false;
70
			}
71
			UnmatchedLeadsKey unmatchedLeadsKey = (UnmatchedLeadsKey)object;
72
			if (! CdmUtils.nullSafeEqual(this.num, unmatchedLeadsKey.num)){
73
				return false;
74
			}
75
			if (! CdmUtils.nullSafeEqual(this.numAndTaxon, unmatchedLeadsKey.numAndTaxon)){
76
				return false;
77
			}
78
			if (! CdmUtils.nullSafeEqual(this.key, unmatchedLeadsKey.key)){
79
				return false;
80
			}
81
			return true;
82
		}
83
		
84
		@Override
85
		public int hashCode() {
86
			   int hashCode = 29 * 7;
87
			   if(this.key != null && this.key.getUuid() != null) {
88
				   hashCode = hashCode + this.key.getUuid().hashCode();
89
			   }
90
			   if(this.numAndTaxon != null ) {
91
				   hashCode = hashCode + this.numAndTaxon.hashCode();
92
			   }
93
			   if(this.num != null) {
94
				   hashCode = hashCode + this.num.hashCode();
95
			   }
96
			   return hashCode;
97
		}
98
		
99
		@Override
100
		public String toString(){
101
			String result = "";
102
			if (this.num != null){
103
				result += num;
104
			}
105
			if (this.key != null){
106
				result += ":" + this.key.getUuid();
107
			}
108
			if (this.numAndTaxon != null){
109
				result += this.numAndTaxon;
110
			}
111
			return result;
112
		}
113
	}
114
	
115
	
116
//************************* FACTORY METHODS ********************************/
117
	
118
	public static UnmatchedLeads NewInstance(){
119
		return new UnmatchedLeads();
120
	}
121
	
122
//************************* METHODS ********************************/
123
	
124
	
125
	/**
126
	 * Adds a polytomous key node to the 
127
	 * @param key
128
	 * @param node
129
	 */
130
	public void addKey(UnmatchedLeadsKey key, PolytomousKeyNode node){
131
		Set<PolytomousKeyNode> nodes = map.get(key);
132
		if (nodes == null){
133
			nodes = new HashSet<PolytomousKeyNode>();
134
			map.put(key, nodes);
135
		}else{
136
			String message = "A feature node for this key does already exist: %s";
137
			message = String.format(message, key.toString());
138
			logger.info(message);
139
		}
140
		nodes.add(node);
141
	}
142
	
143
	public Set<PolytomousKeyNode> getNodes(UnmatchedLeadsKey key){
144
		Set<PolytomousKeyNode> result = new HashSet<PolytomousKeyNode>();
145
		Set<PolytomousKeyNode> nodes = map.get(key);
146
		if (nodes != null){
147
			result.addAll(nodes);
148
		}
149
		return result;
150
	}
151

    
152
	public boolean removeNode(UnmatchedLeadsKey key, PolytomousKeyNode matchingNode) {
153
		Set<PolytomousKeyNode> nodes = map.get(key);
154
		if (nodes != null){
155
			boolean result = nodes.remove(matchingNode);
156
			if (nodes.isEmpty()){
157
				map.remove(key);
158
			}
159
			return result;
160
		}
161
		return false;
162
	}
163
	
164

    
165
	public int size(){
166
		return map.size();
167
	}
168
	
169
	/**
170
	 * SaveOrUpdates all polytomousKeyNodes in the unmatchedLeadsKey map.
171
	 * Used to move nodes from one transaction to another.
172
	 * @param service
173
	 */
174
	public void saveToSession(IPolytomousKeyNodeService service){
175
		Set<PolytomousKeyNode> allNodes = new HashSet<PolytomousKeyNode>();
176
		for (Set<PolytomousKeyNode> set :map.values()){
177
			allNodes.addAll(set);
178
		}
179
		service.saveOrUpdate(allNodes);
180
	}
181
	
182
//********************** toString()******************************/
183
	
184
	@Override
185
	public String toString(){
186
		String result = "[";
187
		for (UnmatchedLeadsKey key : map.keySet()){
188
			result += (result.equals("[")? "":"; ") + key.toString();
189
		}
190
		return result + "]";
191
	}
192
	
193
	
194
}
(10-10/11)