0

I have the following code that defines a getParts method to find a given Part Name and Part Number in the system. Note that this code comes from our system's API, so if no one can help I'll just delete this question. I figured someone could potentially see a solution or help me along the way.

<%! private QueryResult getParts( String name, String number )
        throws WTException, WTPropertyVetoException {

    Class cname = wt.part.WTPart.class;
    QuerySpec qs = new QuerySpec(cname);
    QueryResult qr = null;

    qs.appendWhere
        (new SearchCondition(cname,
                                 "master>name",
                                 SearchCondition.EQUAL,
                                 name, 
                                 false));
    qs.appendAnd();
    qs.appendWhere
        (new SearchCondition(cname,
                                 "master>number",
                                 SearchCondition.EQUAL,
                                 number, 
                                 false));


    qr = PersistenceHelper.manager.find(qs);

    System.out.println("...found: " + qr.size());

    return qr;
}
%>

But I would like to allow the user more flexibility in finding these parts. So I set up conditional statements to check for a radio button. This allows them to search by part name and part number, find all, or search using a wildcard. However, I'm having trouble implementing the two latter options.

To attempt to accomplish the above, I have written the below code:

<% 
String partName = request.getParameter("nameInput");
String partNumber = request.getParameter("numberInput");
String searchMethod = request.getParameter("selection");

//out.print(searchMethod);

QueryResult myResult = new QueryResult();

if(searchMethod.equals("search"))
    myResult = getParts(partName, partNumber);

else if(searchMethod.equals("all"))
{
    //Should I write a new function and do this?
    //myResult = getAllParts();
    //or is there a way I could use a for each loop to accomplish this?

}

//else if(searchMethod.equals("wildcard"))
    //get parts matching %wildcard%


while(myResult.hasMoreElements())
{
        out.print(myResult.nextElement().toString());
}

%>

Basically, it accepts user input and checks what type of search they would like to perform. Is there an easy way to pass all the values into the myResult object? And likewise for the wildcard search? Like I said before, it may be futile trying to help without access to the API, but hopefully it isn't.

Thanks!

  • 2
    Having business logic in JSP is bad design. Sun suggests, whenever possible avoid scriptlets. http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – kosa Jul 10 '12 at 19:49

1 Answers1

1

You can (and should) reuse the function, but in order to do so, you will need a part name and number (as those are its input parameters). So for the multi-result options you will need to get a list/collection of part names+numbers and feed them individually to the function, then collect the result in the format that is most appropriate for your needs

Attila
  • 28,265
  • 3
  • 46
  • 55