// Copyright (c) 2001 Oracle Corporation
package oracle.portal.sample.v2.devguide.validatecache;

import java.util.Date;
import java.text.SimpleDateFormat;

import oracle.portal.provider.v2.*;
import oracle.portal.provider.v2.render.*;
import oracle.portal.provider.v2.render.http.ResourceValidateRenderer;
import oracle.portal.provider.v2.http.*;

/**
 * SampleValidateRenderer is a sample implementation of a {@link
 * ManagedRenderer} which renders web container resources using validation
 * based caching. It extends the {@link ResourceValidateRenderer} base
 * implementation for validation based caching.
*/
public class SampleValidateRenderer extends ResourceValidateRenderer
{
    protected SimpleDateFormat mFormatter =
        new SimpleDateFormat ("mm");

    /**
     * Null Constructor
     */
    public SampleValidateRenderer()
    {
        super();
    }


    /*
     * Decide the current version of the page to be generated. In this sample
     * implementation, the version is based on the minute of the current time.
     * 
     * @param pr the request object containing the current request details.
     * @return the current version of the page to be generated
     */
    public String getNewVersion(PortletRenderRequest pr)
    {
        Date t = new Date(System.currentTimeMillis());
        return mFormatter.format(t);
    }

    /**
     * Determine whether the given version of the page to be generated is still
     * valid. In this sample implementation, we simply return <code>true</code>
     * if the version matches the one returned by {@link
     * #getNewVersion(PortletRenderRequest)}.
     * @param pr the request object containing the current request details.
     * @param oldVersion String identifying the cached version of the page to
     * be generated.
     * @return <code>true</code> if the given version is still valid.
     */
    public boolean isValid(PortletRenderRequest pr, String oldVersion)
    {
        return getNewVersion(pr).equals(oldVersion);
    }

}
