Skip to content

Commit dba992a

Browse files
authored
Avoid NPE when bad queryKey is provided (#252)
1 parent c40bdb7 commit dba992a

File tree

2 files changed

+81
-99
lines changed

2 files changed

+81
-99
lines changed

pepdb/src/org/scharp/atlas/pepdb/PepDBBaseController.java

Lines changed: 60 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -157,87 +157,85 @@ public void setLabId(String labId)
157157
this.labId = labId;
158158
}
159159

160-
public boolean validate(Errors errors) throws SQLException
160+
public boolean validate(Errors errors)
161161
{
162162
if(getQueryKey() == null || StringUtils.trimToNull(getQueryKey()) == null)
163163
errors.reject(null, "The Search Criteria must be entered.");
164164
String qValue = getQueryValue();
165-
if (getQueryKey() != null && getQueryKey().equals(PepDBSchema.COLUMN_PARENT_SEQUENCE))
165+
if (getQueryKey() != null)
166166
{
167-
if (StringUtils.trimToNull(qValue) == null)
168-
errors.reject(null, "The Parent Sequence must be entered.");
169-
}
170-
if (getQueryKey() != null && getQueryKey().equals(PepDBSchema.COLUMN_CHILD_SEQUENCE))
171-
{
172-
if (StringUtils.trimToNull(qValue) == null)
173-
errors.reject(null, "The Child Sequence must be entered.");
174-
}
175-
if (getQueryKey() != null && getQueryKey().equals(PepDBSchema.COLUMN_PEPTIDE_GROUP_ID))
176-
{
177-
if (StringUtils.trimToNull(qValue) == null)
178-
errors.reject(null, "Peptide Group must be selected to get peptides in a group.");
179-
/*
180-
if(StringUtils.trimToNull(getLabId()) == null)
181-
errors.reject(null, "Peptide Number must be entered.");
182-
*/
183-
}
184-
if (getQueryKey() != null && getQueryKey().equals(PepDBSchema.COLUMN_PEPTIDE_POOL_ID))
185-
{
186-
if (StringUtils.trimToNull(qValue) == null)
187-
errors.reject(null, "Peptide Pool Name must be selected to get peptides in a pool.");
188-
}
189-
if (getQueryKey() != null && getQueryKey().equals(PepDBSchema.COLUMN_PROTEIN_CAT_ID))
190-
{
191-
if (StringUtils.trimToNull(qValue) == null)
192-
errors.reject(null, "Protein Category must be selected to get peptides in a protein category.");
193-
else
167+
switch (getQueryKey())
194168
{
195-
ProteinCategory pc = PepDBManager.getProCatByID(Integer.parseInt(getQueryValue()));
196-
if(pc.getProtein_cat_desc().trim().contains("-"))
169+
case PepDBSchema.COLUMN_PARENT_SEQUENCE ->
197170
{
198-
if(StringUtils.trimToNull(getAAStart()) != null || StringUtils.trimToNull(getAAEnd()) != null)
199-
errors.reject(null,"When you select a hyphanated Protein Category : "+pc.getProtein_cat_desc()+" AAStart & AAEnd values are not allowed.");
171+
if (StringUtils.trimToNull(qValue) == null)
172+
errors.reject(null, "The Parent Sequence must be entered.");
200173
}
201-
else
174+
case PepDBSchema.COLUMN_CHILD_SEQUENCE ->
202175
{
203-
if(StringUtils.trimToNull(getAAStart()) != null && validateInteger(getAAStart().trim()) == null)
204-
errors.reject(null, "AAStart must be an Integer.");
205-
if(StringUtils.trimToNull(getAAEnd()) != null && validateInteger(getAAEnd().trim()) == null)
206-
errors.reject(null, "AAEnd must be an Integer.");
207-
if(StringUtils.trimToNull(getAAStart()) != null && validateInteger(getAAStart().trim()) != null
208-
&& StringUtils.trimToNull(getAAEnd()) != null && validateInteger(getAAEnd().trim()) != null
209-
&& validateInteger(getAAStart().trim()) > validateInteger(getAAEnd().trim()))
210-
errors.reject(null, "AAStart must be less than or equal to AAEnd.");
176+
if (StringUtils.trimToNull(qValue) == null)
177+
errors.reject(null, "The Child Sequence must be entered.");
211178
}
212-
}
213-
}
214-
if (getQueryKey() != null && getQueryKey().equals(PepDBSchema.COLUMN_PEPTIDE_ID))
215-
{
216-
if (StringUtils.trimToNull(qValue) == null)
217-
errors.reject(null, "The Peptide Id range must be entered.");
218-
if (qValue != null && !qValue.isEmpty())
219-
{
220-
if (!(qValue.matches("\\d+-\\d+")))
179+
case PepDBSchema.COLUMN_PEPTIDE_GROUP_ID ->
221180
{
222-
errors.reject(null, "To get the peptides in the range you should specify the Range of numbers.\n" +
223-
"The format for specify the range of peptide is <number>-<number> Example would be 30-100");
181+
if (StringUtils.trimToNull(qValue) == null)
182+
errors.reject(null, "Peptide Group must be selected to get peptides in a group.");
224183
}
225-
else
184+
case PepDBSchema.COLUMN_PEPTIDE_POOL_ID ->
185+
{
186+
if (StringUtils.trimToNull(qValue) == null)
187+
errors.reject(null, "Peptide Pool Name must be selected to get peptides in a pool.");
188+
}
189+
case PepDBSchema.COLUMN_PROTEIN_CAT_ID ->
226190
{
227-
String[] range = qValue.split("-");
228-
if (Integer.parseInt(range[0]) > Integer.parseInt(range[1]))
191+
if (StringUtils.trimToNull(qValue) == null)
192+
errors.reject(null, "Protein Category must be selected to get peptides in a protein category.");
193+
else
229194
{
230-
errors.reject(null, "The minimum value which is before '-' should be less than the max value which is after '-'.\n");
231-
195+
ProteinCategory pc = PepDBManager.getProCatByID(Integer.parseInt(getQueryValue()));
196+
if (pc.getProtein_cat_desc().trim().contains("-"))
197+
{
198+
if (StringUtils.trimToNull(getAAStart()) != null || StringUtils.trimToNull(getAAEnd()) != null)
199+
errors.reject(null, "When you select a hyphanated Protein Category : " + pc.getProtein_cat_desc() + " AAStart & AAEnd values are not allowed.");
200+
}
201+
else
202+
{
203+
if (StringUtils.trimToNull(getAAStart()) != null && validateInteger(getAAStart().trim()) == null)
204+
errors.reject(null, "AAStart must be an Integer.");
205+
if (StringUtils.trimToNull(getAAEnd()) != null && validateInteger(getAAEnd().trim()) == null)
206+
errors.reject(null, "AAEnd must be an Integer.");
207+
if (StringUtils.trimToNull(getAAStart()) != null && validateInteger(getAAStart().trim()) != null
208+
&& StringUtils.trimToNull(getAAEnd()) != null && validateInteger(getAAEnd().trim()) != null
209+
&& validateInteger(getAAStart().trim()) > validateInteger(getAAEnd().trim()))
210+
errors.reject(null, "AAStart must be less than or equal to AAEnd.");
211+
}
212+
}
213+
}
214+
case PepDBSchema.COLUMN_PEPTIDE_ID ->
215+
{
216+
if (StringUtils.trimToNull(qValue) == null)
217+
errors.reject(null, "The Peptide Id range must be entered.");
218+
if (qValue != null && !qValue.isEmpty())
219+
{
220+
if (!(qValue.matches("\\d+-\\d+")))
221+
{
222+
errors.reject(null, "To get the peptides in the range you should specify the Range of numbers.\n" +
223+
"The format for specify the range of peptide is <number>-<number> Example would be 30-100");
224+
}
225+
else
226+
{
227+
String[] range = qValue.split("-");
228+
if (Integer.parseInt(range[0]) > Integer.parseInt(range[1]))
229+
{
230+
errors.reject(null, "The minimum value which is before '-' should be less than the max value which is after '-'.\n");
231+
}
232+
}
232233
}
233234
}
234235
}
235236
}
236-
if(errors != null && errors.getErrorCount() >0)
237-
return false;
238-
return true;
237+
return errors == null || !errors.hasErrors();
239238
}
240-
241239
}
242240

243241
public static class DisplayPeptideForm

pepdb/src/org/scharp/atlas/pepdb/PepDBController.java

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.labkey.api.view.ActionURL;
3333
import org.labkey.api.view.DetailsView;
3434
import org.labkey.api.view.GridView;
35-
import org.labkey.api.view.HttpView;
3635
import org.labkey.api.view.InsertView;
3736
import org.labkey.api.view.JspView;
3837
import org.labkey.api.view.NavTree;
@@ -170,44 +169,29 @@ public class GetPeptidesAction extends SimpleViewAction<PeptideQueryForm>
170169
@Override
171170
public ModelAndView getView(PeptideQueryForm form, BindException errors) throws Exception
172171
{
173-
if (!form.validate(errors))
172+
if (form.validate(errors))
174173
{
175-
return new JspView<>(PAGE_PEPTIDE_GROUP_SELECT, form, errors);
176-
}
177-
PropertyValues pv = this.getPropertyValues();
178-
ViewContext ctx = getViewContext();
179-
HttpSession session = ctx.getRequest().getSession(true);
180-
session.setAttribute("QUERY_FORM", form);
181-
GridView gridView = new GridView(new DataRegion(), (BindException) null);
182-
if (form.getQueryKey().equals(PepDBSchema.COLUMN_PEPTIDE_GROUP_ID))
183-
{
184-
gridView = getGridViewByGroup(form, pv);
185-
}
186-
if (form.getQueryKey().equals(PepDBSchema.COLUMN_PEPTIDE_POOL_ID))
187-
{
188-
gridView = getGridViewByPool(form, pv);
189-
}
190-
if (form.getQueryKey().equals(PepDBSchema.COLUMN_PROTEIN_CAT_ID))
191-
{
192-
gridView = getGridViewByProtein(form, pv);
193-
}
194-
if (form.getQueryKey().equals(PepDBSchema.COLUMN_PEPTIDE_SEQUENCE))
195-
{
196-
gridView = getGridViewBySequence(form, pv);
197-
}
198-
if (form.getQueryKey().equals(PepDBSchema.COLUMN_PARENT_SEQUENCE))
199-
{
200-
gridView = getGridViewByParent(form, pv);
201-
}
202-
if (form.getQueryKey().equals(PepDBSchema.COLUMN_CHILD_SEQUENCE))
203-
{
204-
gridView = getGridViewByChild(form, pv);
205-
}
206-
if (gridView == null)
207-
{
208-
HttpView.redirect(new ActionURL(SearchForPeptidesAction.class, getContainer()));
174+
PropertyValues pv = this.getPropertyValues();
175+
GridView gridView = switch (form.getQueryKey())
176+
{
177+
case PepDBSchema.COLUMN_PEPTIDE_GROUP_ID -> getGridViewByGroup(form, pv);
178+
case PepDBSchema.COLUMN_PEPTIDE_POOL_ID -> getGridViewByPool(form, pv);
179+
case PepDBSchema.COLUMN_PROTEIN_CAT_ID -> getGridViewByProtein(form, pv);
180+
case PepDBSchema.COLUMN_PEPTIDE_SEQUENCE -> getGridViewBySequence(form, pv);
181+
case PepDBSchema.COLUMN_PARENT_SEQUENCE -> getGridViewByParent(form, pv);
182+
case PepDBSchema.COLUMN_CHILD_SEQUENCE -> getGridViewByChild(form, pv);
183+
default -> null;
184+
};
185+
if (gridView != null)
186+
{
187+
ViewContext ctx = getViewContext();
188+
HttpSession session = ctx.getRequest().getSession(true);
189+
session.setAttribute("QUERY_FORM", form);
190+
return gridView;
191+
}
192+
errors.reject(null, "Unrecognized queryKey: " + form.getQueryKey());
209193
}
210-
return gridView;
194+
return new JspView<>(PAGE_PEPTIDE_GROUP_SELECT, form, errors);
211195
}
212196

213197
@Override

0 commit comments

Comments
 (0)