1 Reply Latest reply on Oct 31, 2007 1:22 AM by thejavafreak

    @Factory on initial page load

    kragoth

      Hi,

      I've got a Search screen where users will enter their criteria.

      Below this I have an area for the results to be displayed.
      My problem is that the @Factory for the results section runs on the first load of the page and as no criteria have been entered it will return all records in the database.

      Obviously this is not what I want to achieve and the only solution I have at the moment is putting a whole bunch of if else statements in my bean to determine if the factory should run or not.


      Eg look at my @Factory code and then my doSearch() method which is really where I want to do my validation!

      package gekko.web.actionbean.enquiryservices;
      
      import static org.jboss.seam.ScopeType.PAGE;
      import gekko.domain.calculator.ltlr.UvFactor;
      import gekko.domain.reference.ltl.CategoryCode;
      import gekko.domain.reference.ltl.SubcategoryCode;
      import gekko.services.query.Query;
      import gekko.services.query.QueryService;
      
      import java.util.ArrayList;
      import java.util.Date;
      import java.util.List;
      
      import javax.faces.application.FacesMessage;
      import javax.faces.context.FacesContext;
      
      import org.jboss.seam.annotations.Factory;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Observer;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.datamodel.DataModel;
      
      @Name("factorEnquiry")
      @Scope(PAGE)
      public class FactorEnquiry {
      
       @In("#{queryServiceImpl}")
       private QueryService querySvc; // NOPMD
      
       @In(value = "#{catSubcatDateSearch.categoryCode}", required = false)
       private CategoryCode selectedCategoryCode;
      
       @In(value = "#{catSubcatDateSearch.subcategoryCode}", required = false)
       private SubcategoryCode selectedSubcategoryCode;
      
       @In(value = "#{catSubcatDateSearch.activeDate}", required = false)
       private Date activeDate;
      
       @DataModel("uvFactors")
       private List<UvFactor> uvFactorList; // NOPMD
      
       @Factory(value = "uvFactors")
       public void getUvFactors() {
       Query<UvFactor> qFact = querySvc.createQuery(UvFactor.class);
       if (selectedCategoryCode != null) {
       qFact.equalsConstraint("subcategory.id.category", selectedCategoryCode);
       } else if (selectedSubcategoryCode != null) {
       qFact.equalsConstraint("subcategory", selectedSubcategoryCode);
       } else if (activeDate != null) {
       qFact.range("effective", "expiry", activeDate);
       } else {
       uvFactorList = new ArrayList<UvFactor>();
       return;
       }
      
       uvFactorList = querySvc.list(qFact);
       }
      
       public List<UvFactor> getUvFactorList() {
       return uvFactorList;
       }
      
       public void setUvFactorList(List<UvFactor> uvFactorList) {
       this.uvFactorList = uvFactorList;
       }
      
       public CategoryCode getSelectedCategoryCode() {
       return selectedCategoryCode;
       }
      
       public void setSelectedCategoryCode(CategoryCode selectedCategoryCode) {
       this.selectedCategoryCode = selectedCategoryCode;
       }
      
       public SubcategoryCode getSelectedSubcategoryCode() {
       return selectedSubcategoryCode;
       }
      
       public void setSelectedSubcategoryCode(SubcategoryCode selectedSubcategoryCode) {
       this.selectedSubcategoryCode = selectedSubcategoryCode;
       }
      
       public Date getActiveDate() {
       return activeDate;
       }
      
       public void setActiveDate(Date activeDate) {
       this.activeDate = activeDate;
       }
      
       public String doSearch() {
      
       if ((selectedCategoryCode == null) && (activeDate == null)) {
      
       FacesMessage message = new FacesMessage();
       message
       .setDetail("At least one of Category or Date must be filled in to performa a search.");
       message.setSummary("No search criteria given");
       message.setSeverity(FacesMessage.SEVERITY_ERROR);
      
       FacesContext.getCurrentInstance().addMessage(null, message);
      
       }
      
       getUvFactors();
       return "";
       }
      
      }
      




      Can someone tell me how I can either
      1. Not run the Factory on the first load of the page
      2. Not display the error message that no search criteria has been entered on the first load of the page


      Thank you