refactoring for SDDImport
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / eflora / UnmatchedLeads.java
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.common.CdmUtils;
21 import eu.etaxonomy.cdm.model.description.FeatureNode;
22 import eu.etaxonomy.cdm.model.description.PolytomousKey;
23
24 public class UnmatchedLeads {
25 private static final Logger logger = Logger.getLogger(UnmatchedLeads.class);
26
27
28 private Map<UnmatchedLeadsKey, Set<FeatureNode>> map = new HashMap<UnmatchedLeadsKey, Set<FeatureNode>>();
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 public boolean isInnerLead(){
58 return (key != null);
59 }
60
61 public boolean isTaxonLead(){
62 return (numAndTaxon != null);
63 }
64
65 @Override
66 public boolean equals(Object object){
67 if (object == null || ! (object instanceof UnmatchedLeadsKey)){
68 return false;
69 }
70 UnmatchedLeadsKey unmatchedLeadsKey = (UnmatchedLeadsKey)object;
71 if (! CdmUtils.nullSafeEqual(this.num, unmatchedLeadsKey.num)){
72 return false;
73 }
74 if (! CdmUtils.nullSafeEqual(this.numAndTaxon, unmatchedLeadsKey.numAndTaxon)){
75 return false;
76 }
77 if (! CdmUtils.nullSafeEqual(this.key, unmatchedLeadsKey.key)){
78 return false;
79 }
80 return true;
81 }
82
83 @Override
84 public int hashCode() {
85 int hashCode = 29 * 7;
86 if(this.key != null && this.key.getUuid() != null) {
87 hashCode = hashCode + this.key.getUuid().hashCode();
88 }
89 if(this.numAndTaxon != null ) {
90 hashCode = hashCode + this.numAndTaxon.hashCode();
91 }
92 if(this.num != null) {
93 hashCode = hashCode + this.num.hashCode();
94 }
95 return hashCode;
96 }
97
98 @Override
99 public String toString(){
100 String result = "";
101 if (this.num != null){
102 result += num;
103 }
104 if (this.key != null){
105 result += ":" + this.key.getUuid();
106 }
107 if (this.numAndTaxon != null){
108 result += this.numAndTaxon;
109 }
110 return result;
111 }
112 }
113
114
115 //************************* FACTORY METHODS ********************************/
116
117 public static UnmatchedLeads NewInstance(){
118 return new UnmatchedLeads();
119 }
120
121 //************************* METHODS ********************************/
122
123
124 public void addKey(UnmatchedLeadsKey key, FeatureNode node){
125 Set<FeatureNode> nodes = map.get(key);
126 if (nodes == null){
127 nodes = new HashSet<FeatureNode>();
128 map.put(key, nodes);
129 }else{
130 logger.info("A Feature node for this key does already exist: " + key.toString());
131 }
132 nodes.add(node);
133 }
134
135 public Set<FeatureNode> getNodes(UnmatchedLeadsKey key){
136 Set<FeatureNode> result = new HashSet<FeatureNode>();
137 Set<FeatureNode> nodes = map.get(key);
138 if (nodes != null){
139 result.addAll(nodes);
140 }
141 return result;
142 }
143
144 public boolean removeNode(UnmatchedLeadsKey key, FeatureNode matchingNode) {
145 Set<FeatureNode> nodes = map.get(key);
146 if (nodes != null){
147 boolean result = nodes.remove(matchingNode);
148 if (nodes.isEmpty()){
149 map.remove(key);
150 }
151 return result;
152 }
153 return false;
154 }
155
156 public void saveToSession(IFeatureTreeService service){
157 Set<FeatureNode> allNodes = new HashSet<FeatureNode>();
158 for (Set<FeatureNode> set :map.values()){
159 allNodes.addAll(set);
160 }
161 service.saveOrUpdateFeatureNodesAll(allNodes);
162 }
163
164 //********************** toString()******************************/
165
166 @Override
167 public String toString(){
168 String result = "[";
169 for (UnmatchedLeadsKey key : map.keySet()){
170 result += (result.equals("[")? "":"; ") + key.toString();
171 }
172 return result + "]";
173 }
174
175
176 }