I thought I'd quickly disect EditBook.java from the Vlib.

EditBook is invoked from the MyLibrary page to edit a 
book.  It uses a Form.  The Form contains a Hidden to 
syncrhronize the page to the right book when the form is 
submitted.

//
// Tapestry Web Application Framework
// Copyright (c) 2000-2002 by Howard Lewis Ship
//
// Howard Lewis Ship
// http://sf.net/projects/tapestry
// mailto:[EMAIL PROTECTED]
//
// This library is free software.
//
// You may redistribute it and/or modify it under the 
terms of the GNU
// Lesser General Public License as published by the 
Free Software Foundation.
//
// Version 2.1 of the license should be included with 
this distribution in
// the file LICENSE, as well as License.html. If the 
license is not
// included with this distribution, you may find a copy 
at the FSF web
// site at 'www.gnu.org' or 'www.fsf.org', or you may 
write to the
// Free Software Foundation, 675 Mass Ave, Cambridge, MA 
02139 USA.
//
// This library is distributed in the hope that it will 
be useful,
// but WITHOUT ANY WARRANTY; without even the implied 
waranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
See the GNU
// Lesser General Public License for more details.
//

package net.sf.tapestry.vlib.pages;

import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;

import javax.ejb.CreateException;
import javax.ejb.FinderException;

import net.sf.tapestry.ApplicationRuntimeException;
import net.sf.tapestry.IRequestCycle;
import net.sf.tapestry.Tapestry;
import net.sf.tapestry.vlib.Protected;
import net.sf.tapestry.vlib.VirtualLibraryEngine;
import net.sf.tapestry.vlib.Visit;
import net.sf.tapestry.vlib.ejb.IOperations;

/**
 *  Edits the properties of at book.
 *
 *  @author Howard Lewis Ship
 *  @version $Id: EditBook.java,v 1.3 2002/05/05 
17:00:36 hship Exp $
 **/

public class EditBook extends Protected
{
    private Integer bookPK;
    private Map attributes;
    private String publisherName;

    private static final int MAP_SIZE = 11;

    public void detach()
    {
        attributes = null;
        bookPK = null;
        publisherName = null;

        super.detach();
    }

    public Map getAttributes()
    {
        if (attributes == null)
            attributes = new HashMap(MAP_SIZE);

        return attributes;
    }

    public String getPublisherName()
    {
        return publisherName;
    }

    public void setPublisherName(String value)
    {
        publisherName = value;
    }

    /**
     *  Gets the book's primary key as a String.
     *
     **/

    public String getBookPrimaryKey()
    {
        return bookPK.toString();
    }

**** This is read by the Hidden during render


    /**
     *  Updates the book's primary key value (converting 
from String to Integer).
     *  This allows a Hidden component in the form to 
synchronize the book being
     *  editted ... which fixes the Browser Back Button 
problem.
     *
     **/

    public void setBookPrimaryKey(String value)
    {
        bookPK = new Integer(value);
    }

*** This is updated by Hidden during rewind

    /**
     *  Invoked (from {@link MyLibrary}) to begin 
editting a book.
     *  Gets the attributes from the {@link IBook} and 
updates
     *  the request cycle to render this page,
     *
     **/

    public void beginEdit(Integer bookPK, IRequestCycle 
cycle)
    {
        this.bookPK = bookPK;

**** we store the primary key temporarily, until
**** we render, at which point it goes
**** into a Hidden
**** The rest of this code reads the Book and stores
**** the attribute values where the form components
**** can edit them.

        VirtualLibraryEngine vengine = 
(VirtualLibraryEngine) engine;

        for (int i = 0; i < 2; i++)
        {
            try
            {
                // Get the attributes as a source for 
our input fields.

                IOperations operations = 
vengine.getOperations();

                attributes = operations.getBookAttributes
(bookPK);

                break;
            }
            catch (FinderException ex)
            {
                // TBD:  Dress this up and send them 
back to the Search or
                // MyLibrary page.

                throw new ApplicationRuntimeException
(ex);
            }
            catch (RemoteException ex)
            {
                vengine.rmiFailure("Remote exception 
setting up page for book #" + bookPK + ".", ex, i > 0);
            }
        }

        cycle.setPage(this);
    }

    /**
     *  Used to update the book when the form is 
submitted.
     *
     **/

    public void formSubmit(IRequestCycle cycle)
    {
        Integer publisherPK = (Integer) attributes.get
("publisherPK");

        if (publisherPK == null && Tapestry.isNull
(publisherName))
        {
            setErrorField(
                "inputPublisherName",
                "Must provide a publisher name if the 
publisher option is empty.",
                null);
            return;
        }

        if (publisherPK != null && !Tapestry.isNull
(publisherName))
        {
            setErrorField(
                "inputPublisherName",
                "Must leave the publisher name blank if 
selecting a publisher from the list.",
                publisherName);
            return;
        }

        // Check for an error from a validation field

        if (isInError())
            return;

        // OK, do the update.

**** So, the Hidden has restore the bookPK via
**** setBookPrimaryKey(String), and the
**** other form components have stuffed updated
**** values into the attributes Map ... now
**** we just have to propogate those changes
**** to the backend.

        Visit visit = (Visit) getVisit();
        VirtualLibraryEngine vengine = visit.getEngine();

        for (int i = 0; i < 2; i++)
        {

            IOperations bean = vengine.getOperations();

            try
            {
                if (publisherPK != null)
                    bean.updateBook(bookPK, attributes);
                else
                {
                    bean.updateBook(bookPK, attributes, 
publisherName);
                    visit.clearCache();
                }

                break;
            }
            catch (FinderException ex)
            {
                throw new ApplicationRuntimeException
(ex);
            }
            catch (CreateException ex)
            {
                throw new ApplicationRuntimeException
(ex);
            }
            catch (RemoteException ex)
            {
                vengine.rmiFailure("Remote exception 
updating book #" + bookPK + ".", ex, i > 0);

                continue;
            }
        }

        MyLibrary page = (MyLibrary) cycle.getPage
("MyLibrary");
        page.setMessage("Updated book.");

        cycle.setPage(page);

**** If we didn't change pages, we would redisplay
**** this page (EditBook, the page with the form)
**** again.

    }

}

--
[EMAIL PROTECTED]

http://tapestry.sf.net

_______________________________________________________________

Have big pipes? SourceForge.net is looking for download mirrors. We supply
the hardware. You get the recognition. Email Us: [EMAIL PROTECTED]
_______________________________________________
Tapestry-developer mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/tapestry-developer

Reply via email to