4740 |
05 Feb 09 |
nicklas |
1 |
/** |
4740 |
05 Feb 09 |
nicklas |
$Id$ |
4740 |
05 Feb 09 |
nicklas |
3 |
|
4740 |
05 Feb 09 |
nicklas |
Copyright (C) 2008 Nicklas Nordborg |
4740 |
05 Feb 09 |
nicklas |
5 |
|
4740 |
05 Feb 09 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4740 |
05 Feb 09 |
nicklas |
Available at http://base.thep.lu.se/ |
4740 |
05 Feb 09 |
nicklas |
8 |
|
4740 |
05 Feb 09 |
nicklas |
BASE is free software; you can redistribute it and/or |
4740 |
05 Feb 09 |
nicklas |
modify it under the terms of the GNU General Public License |
4740 |
05 Feb 09 |
nicklas |
as published by the Free Software Foundation; either version 3 |
4740 |
05 Feb 09 |
nicklas |
of the License, or (at your option) any later version. |
4740 |
05 Feb 09 |
nicklas |
13 |
|
4740 |
05 Feb 09 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4740 |
05 Feb 09 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4740 |
05 Feb 09 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4740 |
05 Feb 09 |
nicklas |
GNU General Public License for more details. |
4740 |
05 Feb 09 |
nicklas |
18 |
|
4740 |
05 Feb 09 |
nicklas |
You should have received a copy of the GNU General Public License |
4740 |
05 Feb 09 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
4740 |
05 Feb 09 |
nicklas |
21 |
*/ |
4740 |
05 Feb 09 |
nicklas |
22 |
package net.sf.basedb.util.overview.validator; |
4740 |
05 Feb 09 |
nicklas |
23 |
|
4740 |
05 Feb 09 |
nicklas |
24 |
import net.sf.basedb.core.BasicItem; |
4740 |
05 Feb 09 |
nicklas |
25 |
import net.sf.basedb.core.DbControl; |
4740 |
05 Feb 09 |
nicklas |
26 |
import net.sf.basedb.core.Item; |
5630 |
13 May 11 |
nicklas |
27 |
import net.sf.basedb.core.ItemSubtype; |
4740 |
05 Feb 09 |
nicklas |
28 |
import net.sf.basedb.core.PermissionDeniedException; |
4740 |
05 Feb 09 |
nicklas |
29 |
import net.sf.basedb.core.Project; |
4740 |
05 Feb 09 |
nicklas |
30 |
import net.sf.basedb.core.Software; |
5651 |
08 Jun 11 |
nicklas |
31 |
import net.sf.basedb.core.Subtypable; |
5651 |
08 Jun 11 |
nicklas |
32 |
import net.sf.basedb.core.SystemItems; |
4740 |
05 Feb 09 |
nicklas |
33 |
import net.sf.basedb.util.overview.Fix; |
4740 |
05 Feb 09 |
nicklas |
34 |
import net.sf.basedb.util.overview.OverviewContext; |
4740 |
05 Feb 09 |
nicklas |
35 |
import net.sf.basedb.util.overview.Validator; |
4740 |
05 Feb 09 |
nicklas |
36 |
import net.sf.basedb.util.overview.Node; |
4740 |
05 Feb 09 |
nicklas |
37 |
|
4740 |
05 Feb 09 |
nicklas |
38 |
/** |
4740 |
05 Feb 09 |
nicklas |
Validator implementation for software. Validation rules: |
4740 |
05 Feb 09 |
nicklas |
<ul> |
4740 |
05 Feb 09 |
nicklas |
<li>Missing item: {@link Validator#MISSING_SOFTWARE} |
4740 |
05 Feb 09 |
nicklas |
<li>Access denied: {@link Validator#DENIED_SOFTWARE} |
4740 |
05 Feb 09 |
nicklas |
<li>Incorrect software type: {@link Validator#INCORRECT_SOFTWARETYPE} |
4740 |
05 Feb 09 |
nicklas |
<li>Non-default software: {@link Validator#NONDEFAULT_SOFTWARE} |
4740 |
05 Feb 09 |
nicklas |
</ul> |
4740 |
05 Feb 09 |
nicklas |
46 |
|
4740 |
05 Feb 09 |
nicklas |
@author Nicklas |
4740 |
05 Feb 09 |
nicklas |
@version 2.10 |
4740 |
05 Feb 09 |
nicklas |
@base.modified $Date$ |
4740 |
05 Feb 09 |
nicklas |
50 |
*/ |
4740 |
05 Feb 09 |
nicklas |
51 |
public class SoftwareValidator |
4764 |
16 Feb 09 |
nicklas |
52 |
extends NameableNodeValidator<Software> |
4740 |
05 Feb 09 |
nicklas |
53 |
{ |
4740 |
05 Feb 09 |
nicklas |
54 |
|
4740 |
05 Feb 09 |
nicklas |
55 |
public SoftwareValidator() |
4740 |
05 Feb 09 |
nicklas |
56 |
{ |
4740 |
05 Feb 09 |
nicklas |
57 |
super(Validator.MISSING_SOFTWARE, Validator.DENIED_SOFTWARE); |
4740 |
05 Feb 09 |
nicklas |
58 |
} |
4740 |
05 Feb 09 |
nicklas |
59 |
|
4740 |
05 Feb 09 |
nicklas |
60 |
|
4740 |
05 Feb 09 |
nicklas |
61 |
/* |
4740 |
05 Feb 09 |
nicklas |
From BasicValidator class |
4740 |
05 Feb 09 |
nicklas |
63 |
------------------------- |
4740 |
05 Feb 09 |
nicklas |
64 |
*/ |
4740 |
05 Feb 09 |
nicklas |
65 |
/** |
5807 |
14 Oct 11 |
nicklas |
If the parent item has a subtype that is related to a SOFTWARE subtype, report |
5807 |
14 Oct 11 |
nicklas |
the missing item as a {@link Validator#MISSING_SOFTWARE} failure. Otherwise, |
5807 |
14 Oct 11 |
nicklas |
ignore the missing software. |
5807 |
14 Oct 11 |
nicklas |
@return Always false |
5807 |
14 Oct 11 |
nicklas |
@since 3.0 |
5807 |
14 Oct 11 |
nicklas |
71 |
*/ |
5807 |
14 Oct 11 |
nicklas |
72 |
@Override |
5807 |
14 Oct 11 |
nicklas |
73 |
public boolean preMissingItem(DbControl dc, OverviewContext context, Node parentNode) |
5807 |
14 Oct 11 |
nicklas |
74 |
{ |
5807 |
14 Oct 11 |
nicklas |
75 |
BasicItem parentItem = parentNode.getItem(dc); |
5807 |
14 Oct 11 |
nicklas |
76 |
ItemSubtype expectedSubtype = getExpectedSoftwareSubtype(dc, parentItem); |
5807 |
14 Oct 11 |
nicklas |
77 |
if (expectedSubtype != null) |
5807 |
14 Oct 11 |
nicklas |
78 |
{ |
5807 |
14 Oct 11 |
nicklas |
79 |
context.createFailure(Validator.MISSING_SOFTWARE, parentNode, |
5807 |
14 Oct 11 |
nicklas |
80 |
"Missing software: " + expectedSubtype.getName(), getMissingItemFix(dc, parentNode)); |
5807 |
14 Oct 11 |
nicklas |
81 |
} |
5807 |
14 Oct 11 |
nicklas |
82 |
return false; |
5807 |
14 Oct 11 |
nicklas |
83 |
} |
5807 |
14 Oct 11 |
nicklas |
84 |
|
5807 |
14 Oct 11 |
nicklas |
85 |
|
5807 |
14 Oct 11 |
nicklas |
86 |
/** |
4740 |
05 Feb 09 |
nicklas |
Checks if the software is of the correct software type |
4740 |
05 Feb 09 |
nicklas |
and, if a project is active, the software is the same as |
4740 |
05 Feb 09 |
nicklas |
the default for the project. |
4740 |
05 Feb 09 |
nicklas |
90 |
*/ |
4740 |
05 Feb 09 |
nicklas |
91 |
@Override |
4740 |
05 Feb 09 |
nicklas |
92 |
public void postValidate(DbControl dc, OverviewContext context, Node node, Node parentNode) |
4740 |
05 Feb 09 |
nicklas |
93 |
{ |
4740 |
05 Feb 09 |
nicklas |
94 |
super.postValidate(dc, context, node, parentNode); |
5653 |
13 Jun 11 |
nicklas |
95 |
Software software = (Software)node.getItem(dc); |
5651 |
08 Jun 11 |
nicklas |
96 |
|
5651 |
08 Jun 11 |
nicklas |
// Validate the subtype of the software |
5653 |
13 Jun 11 |
nicklas |
98 |
BasicItem parentItem = parentNode.getItem(dc); |
5651 |
08 Jun 11 |
nicklas |
99 |
ItemSubtype expectedSubtype = getExpectedSoftwareSubtype(dc, parentItem); |
5651 |
08 Jun 11 |
nicklas |
100 |
try |
4740 |
05 Feb 09 |
nicklas |
101 |
{ |
5651 |
08 Jun 11 |
nicklas |
102 |
ItemSubtype subtype = software.getItemSubtype(); |
5651 |
08 Jun 11 |
nicklas |
103 |
if (expectedSubtype != null && !expectedSubtype.equals(subtype)) |
5651 |
08 Jun 11 |
nicklas |
104 |
{ |
7848 |
18 Sep 20 |
nicklas |
105 |
Fix subtypeFix = null; |
7848 |
18 Sep 20 |
nicklas |
106 |
if (subtype != null && parentItem instanceof Subtypable) |
7848 |
18 Sep 20 |
nicklas |
107 |
{ |
7848 |
18 Sep 20 |
nicklas |
108 |
ItemSubtype mySubtype = ((Subtypable)parentItem).getItemSubtype(); |
7848 |
18 Sep 20 |
nicklas |
109 |
if (mySubtype != null) |
7848 |
18 Sep 20 |
nicklas |
110 |
{ |
7848 |
18 Sep 20 |
nicklas |
111 |
subtypeFix = new Fix("Change related software subtype of '" + mySubtype.getName() + |
7848 |
18 Sep 20 |
nicklas |
112 |
"' to '" + subtype.getName() + "'", mySubtype); |
7848 |
18 Sep 20 |
nicklas |
113 |
} |
7848 |
18 Sep 20 |
nicklas |
114 |
} |
5807 |
14 Oct 11 |
nicklas |
115 |
|
5651 |
08 Jun 11 |
nicklas |
116 |
context.createFailure(Validator.INCORRECT_SOFTWARETYPE, parentNode, |
5651 |
08 Jun 11 |
nicklas |
117 |
"Expected a software with subtype: " + expectedSubtype.getName(), |
5651 |
08 Jun 11 |
nicklas |
118 |
new Fix("Change software of '" + parentNode.getTitle() + "'", parentItem), |
5807 |
14 Oct 11 |
nicklas |
119 |
new Fix("Change software type of '" + software.getName() + "' to '" + expectedSubtype.getName() + "'", software), |
5807 |
14 Oct 11 |
nicklas |
120 |
subtypeFix |
5651 |
08 Jun 11 |
nicklas |
121 |
); |
5651 |
08 Jun 11 |
nicklas |
122 |
} |
4740 |
05 Feb 09 |
nicklas |
123 |
} |
5651 |
08 Jun 11 |
nicklas |
124 |
catch (PermissionDeniedException ex) |
5651 |
08 Jun 11 |
nicklas |
125 |
{} |
4740 |
05 Feb 09 |
nicklas |
126 |
|
4740 |
05 Feb 09 |
nicklas |
// Check if using project default software |
4740 |
05 Feb 09 |
nicklas |
128 |
Project project = context.getProject(); |
5652 |
10 Jun 11 |
nicklas |
129 |
if (project != null) |
4740 |
05 Feb 09 |
nicklas |
130 |
{ |
5814 |
18 Oct 11 |
nicklas |
131 |
if (!project.isDefaultItem(software) && project.hasDefaultForItem(software)) |
5652 |
10 Jun 11 |
nicklas |
132 |
{ |
5652 |
10 Jun 11 |
nicklas |
133 |
context.createFailure(Validator.NONDEFAULT_SOFTWARE, parentNode, null, |
5652 |
10 Jun 11 |
nicklas |
134 |
new Fix("Change software of '" + parentNode.getTitle() + "'", parentItem), |
5652 |
10 Jun 11 |
nicklas |
135 |
new Fix("Add '" + software.getName() + "' as default software to project", project) |
5652 |
10 Jun 11 |
nicklas |
136 |
); |
5652 |
10 Jun 11 |
nicklas |
137 |
} |
4740 |
05 Feb 09 |
nicklas |
138 |
} |
4740 |
05 Feb 09 |
nicklas |
139 |
} |
4740 |
05 Feb 09 |
nicklas |
140 |
|
4740 |
05 Feb 09 |
nicklas |
141 |
/** |
4740 |
05 Feb 09 |
nicklas |
@return Suggested fix is to add a software to the parent item |
4740 |
05 Feb 09 |
nicklas |
143 |
*/ |
4740 |
05 Feb 09 |
nicklas |
144 |
@Override |
5651 |
08 Jun 11 |
nicklas |
145 |
protected Fix getMissingItemFix(DbControl dc, Node parentNode) |
4740 |
05 Feb 09 |
nicklas |
146 |
{ |
5651 |
08 Jun 11 |
nicklas |
147 |
BasicItem parentItem = parentNode.getItem(); |
5651 |
08 Jun 11 |
nicklas |
148 |
return new Fix("Add software to '" + parentNode.getTitle() + "'", parentItem); |
5651 |
08 Jun 11 |
nicklas |
149 |
} |
5651 |
08 Jun 11 |
nicklas |
150 |
// -------------------------------- |
5651 |
08 Jun 11 |
nicklas |
151 |
|
5651 |
08 Jun 11 |
nicklas |
152 |
/** |
5651 |
08 Jun 11 |
nicklas |
Get the subtype of the associated software that we expect for the given |
5651 |
08 Jun 11 |
nicklas |
parent item. |
5651 |
08 Jun 11 |
nicklas |
155 |
*/ |
5651 |
08 Jun 11 |
nicklas |
156 |
protected ItemSubtype getExpectedSoftwareSubtype(DbControl dc, BasicItem parentItem) |
5651 |
08 Jun 11 |
nicklas |
157 |
{ |
5651 |
08 Jun 11 |
nicklas |
158 |
ItemSubtype expectedSubtype = null; |
5651 |
08 Jun 11 |
nicklas |
159 |
int defaultSubtypeForItemType = SystemItems.getId(Software.getDefaultSystemId(parentItem.getType())); |
5651 |
08 Jun 11 |
nicklas |
160 |
try |
4740 |
05 Feb 09 |
nicklas |
161 |
{ |
5651 |
08 Jun 11 |
nicklas |
162 |
if (parentItem instanceof Subtypable) |
5651 |
08 Jun 11 |
nicklas |
163 |
{ |
5651 |
08 Jun 11 |
nicklas |
164 |
expectedSubtype = ItemSubtype.getRelatedSubtype(dc, |
5651 |
08 Jun 11 |
nicklas |
165 |
(Subtypable)parentItem, Item.SOFTWARE, defaultSubtypeForItemType); |
5651 |
08 Jun 11 |
nicklas |
166 |
} |
5651 |
08 Jun 11 |
nicklas |
167 |
else if (defaultSubtypeForItemType != 0) |
5651 |
08 Jun 11 |
nicklas |
168 |
{ |
5651 |
08 Jun 11 |
nicklas |
169 |
expectedSubtype = ItemSubtype.getById(dc, defaultSubtypeForItemType); |
5651 |
08 Jun 11 |
nicklas |
170 |
} |
4740 |
05 Feb 09 |
nicklas |
171 |
} |
5651 |
08 Jun 11 |
nicklas |
172 |
catch (PermissionDeniedException ex) |
5651 |
08 Jun 11 |
nicklas |
173 |
{} |
5651 |
08 Jun 11 |
nicklas |
174 |
return expectedSubtype; |
4740 |
05 Feb 09 |
nicklas |
175 |
} |
5651 |
08 Jun 11 |
nicklas |
176 |
|
4740 |
05 Feb 09 |
nicklas |
177 |
} |