View Javadoc

1   /*
2    * $Id$
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.example.bookstore;
12  
13  import org.mule.api.lifecycle.Initialisable;
14  import org.mule.api.lifecycle.InitialisationException;
15  
16  import java.util.ArrayList;
17  import java.util.Collection;
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import javax.jws.WebService;
23  
24  /**
25   * Bookstore catalog service which implements both the public interface for 
26   * browsing the catalog and the admin interface for adding books to the catalog.
27   * 
28   * @see CatalogService
29   * @see CatalogAdminService
30   */
31  @WebService(serviceName="CatalogService", endpointInterface="org.mule.example.bookstore.CatalogService")
32  public class CatalogServiceImpl implements CatalogService, CatalogAdminService, Initialisable
33  {
34      /** Simple hashmap used to store the catalog, in real life this would be a database */
35      private Map <Long, Book> books = new HashMap <Long, Book> ();
36      
37      public void initialise() throws InitialisationException
38      {
39          books = new HashMap <Long, Book> ();
40  
41          // Add some initial test data
42          addBook(new Book("J.R.R. Tolkien", "The Fellowship of the Ring", 8));
43          addBook(new Book("J.R.R. Tolkien", "The Two Towers", 10));
44          addBook(new Book("J.R.R. Tolkien", "The Return of the King", 10));
45          addBook(new Book("C.S. Lewis", "The Lion, the Witch and the Wardrobe", 6));
46          addBook(new Book("C.S. Lewis", "Prince Caspian", 8));
47          addBook(new Book("C.S. Lewis", "The Voyage of the Dawn Treader", 6));
48          addBook(new Book("Leo Tolstoy", "War and Peace", 8));
49          addBook(new Book("Leo Tolstoy", "Anna Karenina", 6));
50          addBook(new Book("Henry David Thoreau", "Walden", 8));
51          addBook(new Book("Harriet Beecher Stowe", "Uncle Tom's Cabin", 6));
52          addBook(new Book("George Orwell", "1984", 8));
53          addBook(new Book("George Orwell", "Animal Farm", 8));
54          addBook(new Book("Aldous Huxley", "Brave New World", 8));
55  
56      }
57  
58      public long addBook(Book book)
59      {
60          System.out.println("Adding book " + book.getTitle());
61          long id = books.size() + 1;
62          book.setId(id);
63          books.put(id, book);
64          return id;
65      }
66  
67      public Collection <Long> addBooks(Collection<Book> booksToAdd)
68      {
69          List <Long> ids = new ArrayList <Long> ();
70          if (booksToAdd != null)
71          {
72              for (Book book : booksToAdd)
73              {
74                  ids.add(addBook(book));
75              }
76          }
77          return ids;
78      }
79  
80      public Collection <Book> getBooks()
81      {
82          return books.values();
83      }
84  
85      public Book getBook(long bookId)
86      {
87          return books.get(bookId);
88      }
89  }