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