001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.perf.ejb;
023:
024: import java.util.Iterator;
025: import java.util.Calendar;
026: import java.util.Collection;
027: import javax.ejb.CreateException;
028: import javax.ejb.EJBException;
029: import javax.ejb.FinderException;
030: import javax.ejb.SessionBean;
031: import javax.ejb.SessionContext;
032: import javax.naming.InitialContext;
033: import javax.naming.Context;
034:
035: import org.jboss.test.cmp2.perf.interfaces.LocalCheckBook;
036: import org.jboss.test.cmp2.perf.interfaces.LocalCheckBookEntry;
037: import org.jboss.test.cmp2.perf.interfaces.LocalCheckBookHome;
038: import org.jboss.test.cmp2.perf.interfaces.LocalCheckBookEntryHome;
039: import org.jboss.logging.Logger;
040:
041: /**
042: * @author Scott.Stark@jboss.org
043: * @version $Revision: 57211 $
044: */
045: public class CheckBookMgrBean implements SessionBean {
046: private static Logger log = Logger
047: .getLogger(CheckBookMgrBean.class);
048: private LocalCheckBook checkBook;
049:
050: public CheckBookMgrBean() {
051: }
052:
053: public void ejbCreate(String account, double balance)
054: throws CreateException {
055: try {
056: InitialContext ctx = new InitialContext();
057: Context enc = (Context) ctx.lookup("java:comp/env");
058: LocalCheckBookHome home = (LocalCheckBookHome) enc
059: .lookup("ejb/LocalCheckBookHome");
060: try {
061: checkBook = home.findByPrimaryKey(account);
062: } catch (FinderException e) {
063: log.info("Failed to find CheckBook for: " + account);
064: checkBook = home.create(account, balance);
065: // Populate the check book
066: LocalCheckBookEntryHome home2 = (LocalCheckBookEntryHome) enc
067: .lookup("ejb/LocalCheckBookEntryHome");
068: populateCheckBook(home2);
069: }
070: } catch (Exception e) {
071: log.error("Failed to setup CheckBookMgrBean", e);
072: throw new CreateException(
073: "Failed to setup CheckBookMgrBean: "
074: + e.getMessage());
075: }
076: }
077:
078: public void ejbActivate() throws EJBException {
079: }
080:
081: public void ejbPassivate() throws EJBException {
082: }
083:
084: public void ejbRemove() throws EJBException {
085: }
086:
087: public void setSessionContext(SessionContext ctx)
088: throws EJBException {
089: }
090:
091: public int getEntryCount() {
092: log.info("Begin getEntryCount");
093: Collection entries = checkBook.getCheckBookEntries();
094: int size = entries.size();
095: log.info("End getEntryCount");
096: return size;
097: }
098:
099: public double getBalance() {
100: log.info("Begin getBalance");
101: double total = checkBook.getBalance();
102:
103: Iterator entries = checkBook.getCheckBookEntries().iterator();
104: while (entries.hasNext()) {
105: LocalCheckBookEntry entry = (LocalCheckBookEntry) entries
106: .next();
107: total -= entry.getAmount();
108: }
109: log.info("End getBalance");
110:
111: return total;
112: }
113:
114: public double entryTotalByCategory(String category) {
115: double total = 0;
116: return total;
117: }
118:
119: public double[] entryTotalByMonth(int year) {
120: double[] months = new double[12];
121: return months;
122: }
123:
124: public StringBuffer createAnnualReport(int year) {
125: StringBuffer report = new StringBuffer();
126: return report;
127: }
128:
129: private void populateCheckBook(LocalCheckBookEntryHome home)
130: throws CreateException {
131: Calendar cal = Calendar.getInstance();
132: Collection entries = checkBook.getCheckBookEntries();
133: String[] categories = { "Business", "Personal", "Travel",
134: "Expenses", "Misc" };
135: int entryNo = 0;
136: for (int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) {
137: cal.set(2003, month, 1);
138: int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
139: for (int day = 2; day < lastDay; day++) {
140: long timestamp = cal.getTime().getTime();
141: for (int n = 0; n < categories.length; n++) {
142: LocalCheckBookEntry entry = home
143: .create(new Integer(entryNo));
144: entryNo++;
145: entry.setAmount(1);
146: entry.setTimestamp(timestamp);
147: entry.setCategory(categories[n]);
148: entries.add(entry);
149: timestamp += 3600 * 1000;
150: }
151: cal.set(2003, month, day);
152: }
153: }
154: }
155: }
|