4764 |
16 Feb 09 |
nicklas |
1 |
/** |
4764 |
16 Feb 09 |
nicklas |
$Id: $ |
4764 |
16 Feb 09 |
nicklas |
3 |
|
4764 |
16 Feb 09 |
nicklas |
Copyright (C) 2009 Nicklas Nordborg |
4764 |
16 Feb 09 |
nicklas |
5 |
|
4764 |
16 Feb 09 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4764 |
16 Feb 09 |
nicklas |
Available at http://base.thep.lu.se/ |
4764 |
16 Feb 09 |
nicklas |
8 |
|
4764 |
16 Feb 09 |
nicklas |
BASE is free software; you can redistribute it and/or |
4764 |
16 Feb 09 |
nicklas |
modify it under the terms of the GNU General Public License |
4764 |
16 Feb 09 |
nicklas |
as published by the Free Software Foundation; either version 3 |
4764 |
16 Feb 09 |
nicklas |
of the License, or (at your option) any later version. |
4764 |
16 Feb 09 |
nicklas |
13 |
|
4764 |
16 Feb 09 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4764 |
16 Feb 09 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4764 |
16 Feb 09 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4764 |
16 Feb 09 |
nicklas |
GNU General Public License for more details. |
4764 |
16 Feb 09 |
nicklas |
18 |
|
4764 |
16 Feb 09 |
nicklas |
You should have received a copy of the GNU General Public License |
4764 |
16 Feb 09 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
4764 |
16 Feb 09 |
nicklas |
21 |
*/ |
4764 |
16 Feb 09 |
nicklas |
22 |
package net.sf.basedb.util.overview.validator; |
4764 |
16 Feb 09 |
nicklas |
23 |
|
4764 |
16 Feb 09 |
nicklas |
24 |
import java.util.HashMap; |
4764 |
16 Feb 09 |
nicklas |
25 |
import java.util.HashSet; |
4764 |
16 Feb 09 |
nicklas |
26 |
import java.util.LinkedList; |
4764 |
16 Feb 09 |
nicklas |
27 |
import java.util.List; |
4764 |
16 Feb 09 |
nicklas |
28 |
import java.util.Map; |
4764 |
16 Feb 09 |
nicklas |
29 |
import java.util.Set; |
4764 |
16 Feb 09 |
nicklas |
30 |
|
4764 |
16 Feb 09 |
nicklas |
31 |
import net.sf.basedb.core.BasicItem; |
4764 |
16 Feb 09 |
nicklas |
32 |
import net.sf.basedb.core.DbControl; |
4764 |
16 Feb 09 |
nicklas |
33 |
import net.sf.basedb.core.Item; |
4764 |
16 Feb 09 |
nicklas |
34 |
import net.sf.basedb.core.Nameable; |
4764 |
16 Feb 09 |
nicklas |
35 |
import net.sf.basedb.util.overview.Fix; |
4764 |
16 Feb 09 |
nicklas |
36 |
import net.sf.basedb.util.overview.Node; |
4764 |
16 Feb 09 |
nicklas |
37 |
import net.sf.basedb.util.overview.OverviewContext; |
4764 |
16 Feb 09 |
nicklas |
38 |
import net.sf.basedb.util.overview.Validator; |
4764 |
16 Feb 09 |
nicklas |
39 |
|
4764 |
16 Feb 09 |
nicklas |
40 |
public abstract class NameableNodeValidator<I extends Nameable> |
4764 |
16 Feb 09 |
nicklas |
41 |
extends BasicNodeValidator<I> |
4764 |
16 Feb 09 |
nicklas |
42 |
{ |
4764 |
16 Feb 09 |
nicklas |
43 |
/** |
4764 |
16 Feb 09 |
nicklas |
Creates a new validator. |
4764 |
16 Feb 09 |
nicklas |
@param ruleMissing The validation rule that is broken if an item is missing, |
4764 |
16 Feb 09 |
nicklas |
or null to not report missing items as a failure |
4764 |
16 Feb 09 |
nicklas |
@param ruleDenied The validation rule that is broken if we are denied access |
4764 |
16 Feb 09 |
nicklas |
to an item, or null to not report this as a failure |
4764 |
16 Feb 09 |
nicklas |
49 |
*/ |
4764 |
16 Feb 09 |
nicklas |
50 |
public NameableNodeValidator(Validator ruleMissing, Validator ruleDenied) |
4764 |
16 Feb 09 |
nicklas |
51 |
{ |
4764 |
16 Feb 09 |
nicklas |
52 |
super(ruleMissing, ruleDenied); |
4764 |
16 Feb 09 |
nicklas |
53 |
} |
4764 |
16 Feb 09 |
nicklas |
54 |
|
4764 |
16 Feb 09 |
nicklas |
55 |
@SuppressWarnings("unchecked") |
4764 |
16 Feb 09 |
nicklas |
56 |
@Override |
4764 |
16 Feb 09 |
nicklas |
57 |
public void postValidate(DbControl dc, OverviewContext context, Node node, Node parentNode) |
4764 |
16 Feb 09 |
nicklas |
58 |
{ |
4764 |
16 Feb 09 |
nicklas |
59 |
super.postValidate(dc, context, node, parentNode); |
4764 |
16 Feb 09 |
nicklas |
60 |
|
4764 |
16 Feb 09 |
nicklas |
61 |
BasicItem item = node.getItem(); |
4764 |
16 Feb 09 |
nicklas |
62 |
String name = ((Nameable)item).getName(); |
4764 |
16 Feb 09 |
nicklas |
63 |
Item itemType = node.getItemType(); |
4764 |
16 Feb 09 |
nicklas |
64 |
Map<String, List<Node>> namedNodes = (Map<String, List<Node>>)context.getCachedObject("names.used"); |
4764 |
16 Feb 09 |
nicklas |
65 |
Set<String> knownDuplicates = (Set<String>)context.getCachedObject("names.duplicate"); |
4764 |
16 Feb 09 |
nicklas |
66 |
if (namedNodes == null) |
4764 |
16 Feb 09 |
nicklas |
67 |
{ |
4764 |
16 Feb 09 |
nicklas |
68 |
namedNodes = new HashMap<String, List<Node>>(); |
4764 |
16 Feb 09 |
nicklas |
69 |
context.setCachedObject("names.used", namedNodes); |
4764 |
16 Feb 09 |
nicklas |
70 |
} |
4764 |
16 Feb 09 |
nicklas |
71 |
if (knownDuplicates == null) |
4764 |
16 Feb 09 |
nicklas |
72 |
{ |
4764 |
16 Feb 09 |
nicklas |
73 |
knownDuplicates = new HashSet<String>(); |
4764 |
16 Feb 09 |
nicklas |
74 |
context.setCachedObject("names.duplicate", knownDuplicates); |
4764 |
16 Feb 09 |
nicklas |
75 |
} |
4764 |
16 Feb 09 |
nicklas |
76 |
|
4764 |
16 Feb 09 |
nicklas |
// If we have seen this name before, there is an entry in the cache |
4764 |
16 Feb 09 |
nicklas |
// with a list of all nodes that contains an item with the same name |
4764 |
16 Feb 09 |
nicklas |
// NOTE! Multiple nodes may reference the SAME item. This should |
4764 |
16 Feb 09 |
nicklas |
// of course not be reported as duplicates. |
4764 |
16 Feb 09 |
nicklas |
81 |
List<Node> list = namedNodes.get(name); |
4764 |
16 Feb 09 |
nicklas |
82 |
if (list != null) |
4764 |
16 Feb 09 |
nicklas |
83 |
{ |
4764 |
16 Feb 09 |
nicklas |
84 |
for (Node other : list) |
4764 |
16 Feb 09 |
nicklas |
85 |
{ |
4764 |
16 Feb 09 |
nicklas |
86 |
if (itemType != other.getItemType()) |
4764 |
16 Feb 09 |
nicklas |
87 |
{ |
4764 |
16 Feb 09 |
nicklas |
// An item of a different type has the same name |
4764 |
16 Feb 09 |
nicklas |
89 |
createFailure(context, knownDuplicates, Validator.NONUNIQUE_NAME_GLOBAL, node, name); |
4764 |
16 Feb 09 |
nicklas |
90 |
createFailure(context, knownDuplicates, Validator.NONUNIQUE_NAME_GLOBAL, other, name); |
4764 |
16 Feb 09 |
nicklas |
91 |
} |
4764 |
16 Feb 09 |
nicklas |
92 |
else if (!item.equals(other.getItem())) |
4764 |
16 Feb 09 |
nicklas |
93 |
{ |
4764 |
16 Feb 09 |
nicklas |
// Another item of the same type has the same name |
4764 |
16 Feb 09 |
nicklas |
95 |
createFailure(context, knownDuplicates, Validator.NONUNIQUE_NAME_FOR_TYPE, node, name); |
4764 |
16 Feb 09 |
nicklas |
96 |
createFailure(context, knownDuplicates, Validator.NONUNIQUE_NAME_FOR_TYPE, other, name); |
4764 |
16 Feb 09 |
nicklas |
97 |
} |
4764 |
16 Feb 09 |
nicklas |
98 |
} |
4764 |
16 Feb 09 |
nicklas |
99 |
list.add(node); |
4764 |
16 Feb 09 |
nicklas |
100 |
} |
4764 |
16 Feb 09 |
nicklas |
101 |
else |
4764 |
16 Feb 09 |
nicklas |
102 |
{ |
4764 |
16 Feb 09 |
nicklas |
103 |
list = new LinkedList<Node>(); |
4764 |
16 Feb 09 |
nicklas |
104 |
namedNodes.put(name, list); |
4764 |
16 Feb 09 |
nicklas |
105 |
} |
4764 |
16 Feb 09 |
nicklas |
106 |
list.add(node); |
4764 |
16 Feb 09 |
nicklas |
107 |
} |
4764 |
16 Feb 09 |
nicklas |
108 |
|
4764 |
16 Feb 09 |
nicklas |
109 |
/** |
4764 |
16 Feb 09 |
nicklas |
Create a failure on this node unless we have created a failure |
4764 |
16 Feb 09 |
nicklas |
for the same validator already. |
4764 |
16 Feb 09 |
nicklas |
112 |
*/ |
4764 |
16 Feb 09 |
nicklas |
113 |
private void createFailure(OverviewContext context, Set<String> knownDuplicates, |
4764 |
16 Feb 09 |
nicklas |
114 |
Validator validator, Node node, String name) |
4764 |
16 Feb 09 |
nicklas |
115 |
{ |
4764 |
16 Feb 09 |
nicklas |
116 |
if (knownDuplicates.add(node.getId() + ":" + validator.getId())) |
4764 |
16 Feb 09 |
nicklas |
117 |
{ |
4764 |
16 Feb 09 |
nicklas |
118 |
String itemName = node.getItemType().toString().toLowerCase(); |
4764 |
16 Feb 09 |
nicklas |
// This node was previously an unknown duplicate |
4764 |
16 Feb 09 |
nicklas |
120 |
context.createFailure(validator, node, |
4764 |
16 Feb 09 |
nicklas |
121 |
validator.getFailureSummary() + " for " + itemName + ": '" + name + "'", |
4764 |
16 Feb 09 |
nicklas |
122 |
new Fix("Change name of " + itemName, node.getItem())); |
4764 |
16 Feb 09 |
nicklas |
123 |
} |
4764 |
16 Feb 09 |
nicklas |
124 |
} |
4764 |
16 Feb 09 |
nicklas |
125 |
|
4764 |
16 Feb 09 |
nicklas |
126 |
} |