Sorry if this is considered spamming the list ....

Speaking of storing stuff in session scope. I've developed what I think is a handy class which I think it useful for
when I want to store something like a search in a session variable.


For instance if I want to page through it ( using displaytags.org (10 out of 10)) or simply if I don't want to fetch
from the database time and time again.
By default the data expires on an hourly basis. And if a different combination of parameters is sent it expires
the cached stuff.


Here is the class

snip=
package ie.jestate.struts.client;

import java.util.Date;

/**
* @author Bryan Hunt
*/
//TODO:Perhaps I should be generating a MD5 or something but I can't see a good reason to use any more CPU


public class HashGenerator {
private StringBuffer buffer;
/**
*
*/
public HashGenerator() {
super();
buffer = new StringBuffer(50);
// TODO Auto-generated constructor stub
}


   /**
    * @param urbanAreas
    */
   public void add(Integer[] array,String fieldname) {
       for (int i = 0; i < array.length; i++) {
           Integer integer = array[i];
           buffer.append(integer);
       }
       buffer.append(fieldname);
       buffer.append("-");
   }

   /**
    * @param priceRangeStart
    */
   public void add(Integer integer,String fieldname) {
       // TODO Auto-generated method stub
       buffer.append(integer);
       buffer.append(fieldname);
       buffer.append("-");
   }

/**
* @return
*/
public String getHash() {
//I append the hour onto the end of the string so that a hash will
//only be good for a max of one hour.
Date date = new Date();
return buffer.toString()+ "-" + new Integer(date.getHours());
}


}

=snip

And here is example usage.
snip=
/*
* Created on 24-Jun-2004
*/
package ie.jestate.struts.client;

import ie.jestate.struts.BaseAction;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

/**
* @author Bryan Hunt
 *
*/
public class ForSaleSearchAction extends BaseAction {

/* (non-Javadoc)
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public ActionForward execute(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {


//TODO: Database based initialization of the countries collection
DynaActionForm dynaform = (DynaActionForm) actionForm;
HttpSession session = request.getSession();
String forSalePropertiesSearchCheckSum = (String) session.getAttribute(Constants.FOR_SALE_CHECK_SUM);
String newForSalePropertiesSearchCheckSum = null;
Integer[] urbanAreas = (Integer[]) dynaform.get("urbanAreas");
Integer priceRangeStart = (Integer) dynaform.get("priceRangeStart");
Integer priceRangeEnd = (Integer) dynaform.get("priceRangeEnd");
Integer[] propertyTypes = (Integer[]) dynaform.get("propertyTypes");
HashGenerator hashGenerator = new HashGenerator();
hashGenerator.add(urbanAreas,"urbanAreas");
hashGenerator.add(priceRangeStart,"priceRangeStart");
hashGenerator.add(priceRangeEnd,"priceRangeEnd");
hashGenerator.add(propertyTypes,"propertyTypes");
newForSalePropertiesSearchCheckSum = hashGenerator.getHash();
if(forSalePropertiesSearchCheckSum != null && forSalePropertiesSearchCheckSum.equals(newForSalePropertiesSearchCheckSum)) {
System.out.println("Checksums match");
}
if (forSalePropertiesSearchCheckSum == null
|| !forSalePropertiesSearchCheckSum.equals(newForSalePropertiesSearchCheckSum)){


Set forSaleProperties =
findForSaleProperties(
urbanAreas,
priceRangeStart,
priceRangeEnd,
propertyTypes);
session.setAttribute(Constants.FOR_SALE_PROPERTIES_SEARCH_RESULT, forSaleProperties);
session.setAttribute(
Constants.FOR_SALE_CHECK_SUM,
newForSalePropertiesSearchCheckSum);
// TODO:Add logging functionality
System.out.println("Returning fresh search results");
} else
/*
if (
session.getAttribute("forSalePropertiesSearchCheckSum").equals(
forSalePropertiesSearchCheckSum))
*/
{
//This means that it is the same search, just called again.
//TODO:Add logging functionality
System.out.println("Returning cached search results");


}
//TODO: Decent logging is needed here
System.out.println(forSalePropertiesSearchCheckSum);
System.out.println(newForSalePropertiesSearchCheckSum);
System.out.println(" ---------------------------------------" );
return actionMapping.findForward("success");
}
}


=snip




Christina Siena wrote:

I recently developed an application with a complex UI. One of the pages required querying the database based on user selection and re-displaying the page with the retrieved data and any previous existing user selections. Four different fields can trigger a db query resulting in page re-display and validations can also result in page re-display. Each time the page is re-displayed, the "state" of the page must be "remembered" from the last time it was displayed. (still with me so far?) Most of the data re



--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to