FileDocCategorySizeDatePackage
DocBookServiceServant.javaAPI DocExample3407Tue Dec 17 19:57:50 GMT 2002ora.jwsnut.chapter6.docbookservice

DocBookServiceServant.java

package ora.jwsnut.chapter6.docbookservice;

import java.util.HashMap;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;

/**
 * Implementation class for the books web service.
 */
public class DocBookServiceServant implements DocBookQuery {
    
    /**
     * SOAPFactory used to create SOAPElements
     */
    private SOAPFactory factory;
    
    /**
     * Gets the number of books known to the service
     * @return the number of books known to the service.
     */
    public int getBookCount() {
        return DocBookServiceServantData.getBookInfo().length;
    }
    
    /**
     * Gets the title for a book with a given index.
     * @param index the index of the book for which the
     * title is required.
     * @return the book title, or <code>null</code> if the 
     * index is invalid
     */
    public String getBookTitle(int index) {
        int count = getBookCount();
        String title = null;
        if (index >= 0 || index < count) {
            title = DocBookServiceServantData.getBookInfo()[index].getTitle();
        }
        return title;
    }
    
    /**
     * Get the author for a book with a given name.
     * @param name the name of the book.
     * @return the author of the book, or <code>null</code>
     * if no matching book can be found.
     */
    public String getBookAuthor(String name) {
        BookInfo book = findBook(name);
        return book == null ? null : book.getAuthor();
    }

        
    /**
     * Gets all details for a book with a given title.
     * @param title the title of the book.
     * @return the BookInfo for the book, or <code>null</code>
     * if no matching book can be found.
     */
    public BookInfo getBookInfo(String title) {
        return findBook(title);
    }    
    
    /**
     * Gets the stock level for a book with a given title.
     * @param title the title of the book.
     * @return the current stock level of the book.
     */
    public SOAPElement getStockInfo(String title) {
        BookInfo book = findBook(title);
        SOAPElement element = null;
        if (book != null) {
            try {
                if (factory == null) {
                    factory = SOAPFactory.newInstance();
                }
                element = factory.createElement("stock");
                element.addTextNode(String.valueOf(book.getStock())); 
            } catch (SOAPException ex) {
                // Just return null in this case
                element = null;
            }
        }
        return element;
    }
    
    /* -- Implementation details -- */
    /**
     * Looks for a book whose title matches a given string.
     * The comparison is case-insensitive.
     * @param name the string to match against the book name
     * @return the <code>BookInfo</code> object for the first
     * matching book, or <code>null</code> if no match exists.
     */
    private BookInfo findBook(String name) {
        BookInfo[] books = DocBookServiceServantData.getBookInfo();
        for (int i = 0; i < books.length; i++) {
            if (books[i].getTitle().equalsIgnoreCase(name)) {
                // Found a match
                return books[i];
            }
        }
        return null;        // No match
    }
}