KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > perf > ejb > CheckBookMgrBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.perf.ejb;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.Calendar JavaDoc;
26 import java.util.Collection JavaDoc;
27 import javax.ejb.CreateException JavaDoc;
28 import javax.ejb.EJBException JavaDoc;
29 import javax.ejb.FinderException JavaDoc;
30 import javax.ejb.SessionBean JavaDoc;
31 import javax.ejb.SessionContext JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.Context JavaDoc;
34
35 import org.jboss.test.cmp2.perf.interfaces.LocalCheckBook;
36 import org.jboss.test.cmp2.perf.interfaces.LocalCheckBookEntry;
37 import org.jboss.test.cmp2.perf.interfaces.LocalCheckBookHome;
38 import org.jboss.test.cmp2.perf.interfaces.LocalCheckBookEntryHome;
39 import org.jboss.logging.Logger;
40
41 /**
42  * @author Scott.Stark@jboss.org
43  * @version $Revision: 58115 $
44  */

45 public class CheckBookMgrBean implements SessionBean JavaDoc
46 {
47    private static Logger log = Logger.getLogger(CheckBookMgrBean.class);
48    private LocalCheckBook checkBook;
49
50    public CheckBookMgrBean()
51    {
52    }
53
54    public void ejbCreate(String JavaDoc account, double balance) throws CreateException JavaDoc
55    {
56       try
57       {
58          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
59          Context JavaDoc enc = (Context JavaDoc) ctx.lookup("java:comp/env");
60          LocalCheckBookHome home = (LocalCheckBookHome) enc.lookup("ejb/LocalCheckBookHome");
61          try
62          {
63             checkBook = home.findByPrimaryKey(account);
64          }
65          catch(FinderException JavaDoc e)
66          {
67             log.info("Failed to find CheckBook for: "+account);
68             checkBook = home.create(account, balance);
69             // Populate the check book
70
LocalCheckBookEntryHome home2 = (LocalCheckBookEntryHome) enc.lookup("ejb/LocalCheckBookEntryHome");
71             populateCheckBook(home2);
72          }
73       }
74       catch(Exception JavaDoc e)
75       {
76          log.error("Failed to setup CheckBookMgrBean", e);
77          throw new CreateException JavaDoc("Failed to setup CheckBookMgrBean: "+e.getMessage());
78       }
79    }
80
81    public void ejbActivate() throws EJBException JavaDoc
82    {
83    }
84
85    public void ejbPassivate() throws EJBException JavaDoc
86    {
87    }
88
89    public void ejbRemove() throws EJBException JavaDoc
90    {
91    }
92
93    public void setSessionContext(SessionContext JavaDoc ctx) throws EJBException JavaDoc
94    {
95    }
96
97    public int getEntryCount()
98    {
99       log.info("Begin getEntryCount");
100       Collection JavaDoc entries = checkBook.getCheckBookEntries();
101       int size = entries.size();
102       log.info("End getEntryCount");
103       return size;
104    }
105    public double getBalance()
106    {
107       log.info("Begin getBalance");
108       double total = checkBook.getBalance();
109
110       Iterator JavaDoc entries = checkBook.getCheckBookEntries().iterator();
111       while (entries.hasNext())
112       {
113           LocalCheckBookEntry entry = (LocalCheckBookEntry) entries.next();
114           total -= entry.getAmount();
115       }
116       log.info("End getBalance");
117
118       return total;
119    }
120    public double entryTotalByLogger(String JavaDoc category)
121    {
122       double total = 0;
123       return total;
124    }
125    public double[] entryTotalByMonth(int year)
126    {
127       double[] months = new double[12];
128       return months;
129    }
130
131    public StringBuffer JavaDoc createAnnualReport(int year)
132    {
133       StringBuffer JavaDoc report = new StringBuffer JavaDoc();
134       return report;
135    }
136
137    private void populateCheckBook(LocalCheckBookEntryHome home)
138       throws CreateException JavaDoc
139    {
140       Calendar JavaDoc cal = Calendar.getInstance();
141       Collection JavaDoc entries = checkBook.getCheckBookEntries();
142       String JavaDoc[] categories = {"Business", "Personal", "Travel", "Expenses", "Misc"};
143       int entryNo = 0;
144       for(int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month ++)
145       {
146          cal.set(2003, month, 1);
147          int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
148          for(int day = 2; day < lastDay; day ++)
149          {
150             long timestamp = cal.getTime().getTime();
151             for(int n = 0; n < categories.length; n ++)
152             {
153                LocalCheckBookEntry entry = home.create(new Integer JavaDoc(entryNo));
154                entryNo ++;
155                entry.setAmount(1);
156                entry.setTimestamp(timestamp);
157                entry.setLogger(categories[n]);
158                entries.add(entry);
159                timestamp += 3600 * 1000;
160             }
161             cal.set(2003, month, day);
162          }
163       }
164    }
165 }
166
Popular Tags