0 Replies Latest reply on Oct 21, 2009 5:31 PM by Val Sw

    Customizing Search - default generated by Seam - pls suggest

    Val Sw Expert
      Hi,

      I am trying to customize (non-alphanumeric) search functionality for entity Item, list object as itemlist (default generated by seam).
      Though I have posted the issue on other thread but thought of creating fresh so that it does not mess and it might help (http://www.seamframework.org/Community/IssueUsingOrderByInGetEjbqlxxxxlistjava)

      Requirement :
      Say there are few items (Item-A, Item A, Item-&^&*A), if user enters Item*****A and clicks search button, item resultList should return all 3 items i.e. Item-A, Item A, Item-&^&*A

      I am calling "itemList.getItemResult" from ui. getItemResult() checks the corresponding values from entity, adds it to List and returns the list.

      Looks like I am close and missing something while setting the returned values, but having issue displaying to ui, value is printing correctly in log, but onclicking search it shows "The item search returned no results."

      Log :
      ------------------------------------------------------
      Item name passed from UI : Item**********A
      Hibernate:
          select
              item0_.HJID as HJID1_,
              item0_.DATE_CREATED as DATE2_1_,
              item0_.ITEM_NAME as ITEM3_1_
          from
              MET_TEST_ITEM item0_
      Corresponding items are > UI value is : ItemA, DB value is : ItemA
      Corresponding items are > UI value is : ItemA, DB value is : ItemA
      Corresponding items are > UI value is : ItemA, DB value is : ItemA
      Items are : com.entity.Item@1be3ce6[  itemName=Item-A  dateCreated=<null>]
      Items are : com.entity.Item@9042c8[  itemName=Item A  dateCreated=<null>]
      Items are : com.entity.Item@b30073[  itemName=Item-&^&*A  dateCreated=<null>]
      Final resultlist size is : 3
      ------------------------------------------------------

      ItemList.java
      ------------------------------------------------------
      @Name("itemList")
      public class ItemList extends EntityQuery {
           
           private static final String[] RESTRICTIONS = {"lower(item.itemName) like concat(lower(#{itemList.item.itemName}),'%')",};

           private Item item = new Item();
           
           public ItemList() {
              setEjbql("select item from Item item");
              setOrder("item.itemName");       
          }
           
           public List<Item> getItemResult(){
                System.out.println("Item name passed from UI : "+item.getItemName());          
                String itemNameUI = item.getItemName();               
                List<Item> resultList = new ArrayList<Item>();
                
                try {
                     Iterator<Item> iterator = getItemsList().iterator();
                     while (iterator.hasNext()) {
                        Item elem = ((Item) iterator.next());
                        if(removeAlphaNumericValue(itemNameUI.trim()).equalsIgnoreCase(removeAlphaNumericValue(elem.getItemName()))) {
                             System.out.println("Corresponding items are > UI value is : "+removeAlphaNumericValue(itemNameUI.trim())+", DB value is : "+removeAlphaNumericValue(elem.getItemName()));
                             resultList.add(elem);
                        }else{
                            //do nothing
                       }
                   }
                     
                } catch (NoResultException nre) {
                     //do nothing
                }
                
                //JUST for test
                for(int i=0;i<resultList.size();i++){
                     System.out.println("Items are : "+resultList.get(i));                    
                }
                
                System.out.println("Final resultlist size is : "+resultList.size());
                return resultList;
           }
           public Item getItem() {
                return item;
           }
           @Override
           public List<String> getRestrictions() {
                return Arrays.asList(RESTRICTIONS);
           }
      }
      ------------------------------------------------------

      ItemList.xhtml
      ------------------------------------------------------
      <h:form id="itemSearch" styleClass="edit">
           <h:inputText id="itemName" value="#{itemList.item.itemName}"/>
           <div class="actionButtons">
                <!--<h:commandButton id="search" value="Search" action="/ItemList.xhtml"/>-->
                <h:commandButton id="search" value="Search" action="#{itemList.getItemResult}" />
           </div>     
      </h:form>

      <rich:panel>
           <f:facet name="header">Item search results</f:facet>
      <div class="results" id="itemList">
      <h:outputText value="The item search returned no results." rendered="#{empty itemList.resultList}"/>
                  
      <rich:dataTable id="itemList" var="item" value="#{itemList.resultList}" rendered="#{not empty itemList.resultList}">
           <h:column>
                <f:facet name="header">
                     <s:link styleClass="columnHeader"
                                     value="itemName #{itemList.order=='itemName asc' ? messages.down : ( itemList.order=='itemName desc' ? messages.up : '' )}">
                          <f:param name="order" value="#{itemList.order=='itemName asc' ? 'itemName desc' : 'itemName asc'}"/>
                     </s:link>
                </f:facet>
                #{item.itemName}
           </h:column>
      ....
      </rich:dataTable>
      </div>
      </rich:panel>
      ....