Project

General

Profile

Download (4.86 KB) Statistics
| Branch: | 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.eflora;
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.IFeatureTreeService;
20
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
21
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
22
import eu.etaxonomy.cdm.common.CdmUtils;
23
import eu.etaxonomy.cdm.model.description.FeatureNode;
24
import eu.etaxonomy.cdm.model.description.PolytomousKey;
25
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
26

    
27
public class UnmatchedLeads {
28
	private static final Logger logger = Logger.getLogger(UnmatchedLeads.class);
29

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

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

    
147
	public boolean removeNode(UnmatchedLeadsKey key, PolytomousKeyNode matchingNode) {
148
		Set<PolytomousKeyNode> nodes = map.get(key);
149
		if (nodes != null){
150
			boolean result = nodes.remove(matchingNode);
151
			if (nodes.isEmpty()){
152
				map.remove(key);
153
			}
154
			return result;
155
		}
156
		return false;
157
	}
158
	
159
	public void saveToSession(IPolytomousKeyNodeService service){
160
		Set<PolytomousKeyNode> allNodes = new HashSet<PolytomousKeyNode>();
161
		for (Set<PolytomousKeyNode> set :map.values()){
162
			allNodes.addAll(set);
163
		}
164
		service.saveOrUpdate(allNodes);
165
	}
166
	
167
//********************** toString()******************************/
168
	
169
	@Override
170
	public String toString(){
171
		String result = "[";
172
		for (UnmatchedLeadsKey key : map.keySet()){
173
			result += (result.equals("[")? "":"; ") + key.toString();
174
		}
175
		return result + "]";
176
	}
177
	
178
	
179
}
(6-6/6)