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
.markup
;
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
.IPolytomousKeyNodeService
;
20 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
21 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
22 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
24 public class UnmatchedLeads
{
25 private static final Logger logger
= Logger
.getLogger(UnmatchedLeads
.class);
28 private final Map
<UnmatchedLeadsKey
, Set
<PolytomousKeyNode
>> map
= new HashMap
<UnmatchedLeadsKey
, Set
<PolytomousKeyNode
>>();
30 public static class UnmatchedLeadsKey
{
31 public static UnmatchedLeadsKey
NewInstance(PolytomousKey key
, String num
){
32 UnmatchedLeadsKey result
= new UnmatchedLeadsKey();
34 result
.num
= num
== null?
null : num
.toLowerCase();
38 public static UnmatchedLeadsKey
NewInstance(String num
, String taxon
){
39 num
= (StringUtils
.isBlank(num
) ?
"" : num
+ " " );
40 return NewInstance(num
+ taxon
);
43 public static UnmatchedLeadsKey
NewInstance(String numAndTaxon
){
44 UnmatchedLeadsKey result
= new UnmatchedLeadsKey();
45 result
.numAndTaxon
= numAndTaxon
.toLowerCase();
58 public boolean isInnerLead(){
62 public boolean isTaxonLead(){
63 return (numAndTaxon
!= null);
67 public boolean equals(Object object
){
68 if (object
== null || ! (object
instanceof UnmatchedLeadsKey
)){
71 UnmatchedLeadsKey unmatchedLeadsKey
= (UnmatchedLeadsKey
)object
;
72 if (! CdmUtils
.nullSafeEqual(this.num
, unmatchedLeadsKey
.num
)){
75 if (! CdmUtils
.nullSafeEqual(this.numAndTaxon
, unmatchedLeadsKey
.numAndTaxon
)){
78 if (! CdmUtils
.nullSafeEqual(this.key
, unmatchedLeadsKey
.key
)){
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();
90 if(this.numAndTaxon
!= null ) {
91 hashCode
= hashCode
+ this.numAndTaxon
.hashCode();
93 if(this.num
!= null) {
94 hashCode
= hashCode
+ this.num
.hashCode();
100 public String
toString(){
102 if (this.num
!= null){
105 if (this.key
!= null){
106 result
+= ":" + this.key
.getUuid();
108 if (this.numAndTaxon
!= null){
109 result
+= this.numAndTaxon
;
116 //************************* FACTORY METHODS ********************************/
118 public static UnmatchedLeads
NewInstance(){
119 return new UnmatchedLeads();
122 //************************* METHODS ********************************/
126 * Adds a polytomous key node to the
130 public void addKey(UnmatchedLeadsKey key
, PolytomousKeyNode node
){
131 Set
<PolytomousKeyNode
> nodes
= map
.get(key
);
133 nodes
= new HashSet
<>();
136 String message
= "A key node for this key does already exist: %s";
137 message
= String
.format(message
, key
.toString());
138 logger
.info(message
);
143 public Set
<PolytomousKeyNode
> getNodes(UnmatchedLeadsKey key
){
144 Set
<PolytomousKeyNode
> result
= new HashSet
<>();
145 Set
<PolytomousKeyNode
> nodes
= map
.get(key
);
147 result
.addAll(nodes
);
152 public boolean removeNode(UnmatchedLeadsKey key
, PolytomousKeyNode matchingNode
) {
153 Set
<PolytomousKeyNode
> nodes
= map
.get(key
);
155 boolean result
= nodes
.remove(matchingNode
);
156 if (nodes
.isEmpty()){
170 * SaveOrUpdates all polytomousKeyNodes in the unmatchedLeadsKey map.
171 * Used to move nodes from one transaction to another.
174 public void saveToSession(IPolytomousKeyNodeService service
){
175 Set
<PolytomousKeyNode
> allNodes
= new HashSet
<PolytomousKeyNode
>();
176 for (Set
<PolytomousKeyNode
> set
:map
.values()){
177 allNodes
.addAll(set
);
179 service
.saveOrUpdate(allNodes
);
182 //********************** toString()******************************/
185 public String
toString(){
187 for (UnmatchedLeadsKey key
: map
.keySet()){
188 result
+= (result
.equals("[")?
"":"; ") + key
.toString();