2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.eflora
;
11 import java
.util
.HashMap
;
12 import java
.util
.HashSet
;
16 import org
.apache
.commons
.lang
.StringUtils
;
17 import org
.apache
.log4j
.Logger
;
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
;
27 public class UnmatchedLeads
{
28 private static final Logger logger
= Logger
.getLogger(UnmatchedLeads
.class);
31 private Map
<UnmatchedLeadsKey
, Set
<PolytomousKeyNode
>> map
= new HashMap
<UnmatchedLeadsKey
, Set
<PolytomousKeyNode
>>();
33 public static class UnmatchedLeadsKey
{
34 public static UnmatchedLeadsKey
NewInstance(PolytomousKey key
, String num
){
35 UnmatchedLeadsKey result
= new UnmatchedLeadsKey();
41 public static UnmatchedLeadsKey
NewInstance(String num
, String taxon
){
42 num
= (StringUtils
.isBlank(num
) ?
"" : num
+ " " );
43 return NewInstance(num
+ taxon
);
46 public static UnmatchedLeadsKey
NewInstance(String numAndTaxon
){
47 UnmatchedLeadsKey result
= new UnmatchedLeadsKey();
48 result
.numAndTaxon
= numAndTaxon
;
60 public boolean isInnerLead(){
64 public boolean isTaxonLead(){
65 return (numAndTaxon
!= null);
69 public boolean equals(Object object
){
70 if (object
== null || ! (object
instanceof UnmatchedLeadsKey
)){
73 UnmatchedLeadsKey unmatchedLeadsKey
= (UnmatchedLeadsKey
)object
;
74 if (! CdmUtils
.nullSafeEqual(this.num
, unmatchedLeadsKey
.num
)){
77 if (! CdmUtils
.nullSafeEqual(this.numAndTaxon
, unmatchedLeadsKey
.numAndTaxon
)){
80 if (! CdmUtils
.nullSafeEqual(this.key
, unmatchedLeadsKey
.key
)){
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();
92 if(this.numAndTaxon
!= null ) {
93 hashCode
= hashCode
+ this.numAndTaxon
.hashCode();
95 if(this.num
!= null) {
96 hashCode
= hashCode
+ this.num
.hashCode();
102 public String
toString(){
104 if (this.num
!= null){
107 if (this.key
!= null){
108 result
+= ":" + this.key
.getUuid();
110 if (this.numAndTaxon
!= null){
111 result
+= this.numAndTaxon
;
118 //************************* FACTORY METHODS ********************************/
120 public static UnmatchedLeads
NewInstance(){
121 return new UnmatchedLeads();
124 //************************* METHODS ********************************/
127 public void addKey(UnmatchedLeadsKey key
, PolytomousKeyNode node
){
128 Set
<PolytomousKeyNode
> nodes
= map
.get(key
);
130 nodes
= new HashSet
<PolytomousKeyNode
>();
133 logger
.info("A Feature node for this key does already exist: " + key
.toString());
138 public Set
<PolytomousKeyNode
> getNodes(UnmatchedLeadsKey key
){
139 Set
<PolytomousKeyNode
> result
= new HashSet
<PolytomousKeyNode
>();
140 Set
<PolytomousKeyNode
> nodes
= map
.get(key
);
142 result
.addAll(nodes
);
147 public boolean removeNode(UnmatchedLeadsKey key
, PolytomousKeyNode matchingNode
) {
148 Set
<PolytomousKeyNode
> nodes
= map
.get(key
);
150 boolean result
= nodes
.remove(matchingNode
);
151 if (nodes
.isEmpty()){
159 public void saveToSession(IPolytomousKeyNodeService service
){
160 Set
<PolytomousKeyNode
> allNodes
= new HashSet
<PolytomousKeyNode
>();
161 for (Set
<PolytomousKeyNode
> set
:map
.values()){
162 allNodes
.addAll(set
);
164 service
.saveOrUpdate(allNodes
);
167 //********************** toString()******************************/
170 public String
toString(){
172 for (UnmatchedLeadsKey key
: map
.keySet()){
173 result
+= (result
.equals("[")?
"":"; ") + key
.toString();