[Previous slide] [Next slide] Moving to XML

Let's see that again [i] the source

public class DayView extends DocumentGeneratorImpl
{
    //~ Static fields/initializers --------------------------------------------

    /**
     * the name of the convenience view in the database from which I will
     * collect all the information I need
     */
    protected static final String VIEW = "events_view";

    /** the field in that view which represents the date of the event */
    protected static final String EVENTDATEFIELD = "when";

    //~ Instance fields -------------------------------------------------------

    /** a generate to generate the event elements which will be my children */
    protected EventElementGenerator eventEltGenerator =
        new EventElementGenerator(  );

    //~ Methods ---------------------------------------------------------------

    /**
     * generate a document containing all the events on the day implied by
     * this context
     */
    public Document generate( Context context ) throws GenerationException
    {
        DOMImplementation di = DOMStub.getDOMImplementation( context );
        Document doc = di.createDocument( "", "eventsdiary", null );

        String day = context.getValueAsString( "day" );
        uk.co.weft.dbutil.Calendar when = new uk.co.weft.dbutil.Calendar(  );

        if ( day != null )
        {
            // if we've got a date, set my calendar to that day
            // (by default it sets itself to today)
            when.setTime( java.sql.Date.valueOf( day ) );
        }

        Element content = doc.getDocumentElement(  );

        content.setAttribute( "date", when.toString(  ) );

        try
        {
            // create a new, blank, context as a pattern to match
            Context pattern = new Context(  );

            // give it the database username, password and url from the current context
            pattern.copyDBTokens( context );

            // put the date we're interested in into the pattern
            pattern.put( EVENTDATEFIELD, when );

            // match the pattern against the cnvenience view and pull
            // back the rows that match as namespaces
            Contexts events =
                TableDescriptor.getDescriptor( VIEW, null, context ).match( pattern );

            Enumeration e = events.elements(  );

            // and pass each of those namespaces in turn to my event element 
            // generator to generate children for my element
            while ( e.hasMoreElements(  ) )
                content.appendChild( eventEltGenerator.generate( doc,
                        (Context) e.nextElement(  ) ) );
        }
        catch ( DataStoreException dex )
        {
            throw new GenerationException( "Failed to read from data store: " +
                dex.getMessage(  ) );
        }

        return doc;
    }