001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.geronimo.samples.daytrader; 018 019 import java.io.Serializable; 020 import java.math.BigDecimal; 021 import java.sql.Timestamp; 022 import java.util.Collection; 023 import java.util.Date; 024 025 import javax.ejb.EJBException; 026 import javax.persistence.CascadeType; 027 import javax.persistence.Entity; 028 import javax.persistence.GeneratedValue; 029 import javax.persistence.Id; 030 import javax.persistence.OneToMany; 031 import javax.persistence.OneToOne; 032 import javax.persistence.Version; 033 import javax.persistence.Table; 034 import javax.persistence.JoinColumn; 035 import javax.persistence.Column; 036 import javax.persistence.Transient; 037 038 import org.apache.geronimo.samples.daytrader.util.Log; 039 040 @Entity(name = "accountejb") 041 @Table(name = "accountejb") 042 public class AccountDataBean implements Serializable { 043 044 /* Accessor methods for persistent fields */ 045 @Id 046 @GeneratedValue 047 private Integer accountID; /* accountID */ 048 private int loginCount; /* loginCount */ 049 private int logoutCount; /* logoutCount */ 050 private Date lastLogin; /* lastLogin Date */ 051 private Date creationDate; /* creationDate */ 052 private BigDecimal balance; /* balance */ 053 private BigDecimal openBalance; /* open balance */ 054 @OneToMany(mappedBy = "account", cascade = CascadeType.ALL) 055 private Collection<OrderDataBean> orders; 056 @OneToOne(cascade = CascadeType.ALL) 057 @JoinColumn(name = "PROFILE_USERID", referencedColumnName = "USERID") 058 @Column(length = 250) 059 private AccountProfileDataBean profile; 060 // @Version 061 // private Integer optLock; 062 063 /* Accessor methods for relationship fields are only included for the AccountProfile profileID */ 064 @Transient 065 private String profileID; 066 067 public AccountDataBean() { 068 } 069 070 public AccountDataBean(Integer accountID, 071 int loginCount, 072 int logoutCount, 073 Date lastLogin, 074 Date creationDate, 075 BigDecimal balance, 076 BigDecimal openBalance, 077 String profileID) { 078 setAccountID(accountID); 079 setLoginCount(loginCount); 080 setLogoutCount(logoutCount); 081 setLastLogin(lastLogin); 082 setCreationDate(creationDate); 083 setBalance(balance); 084 setOpenBalance(openBalance); 085 setProfileID(profileID); 086 } 087 088 public AccountDataBean(int loginCount, 089 int logoutCount, 090 Date lastLogin, 091 Date creationDate, 092 BigDecimal balance, 093 BigDecimal openBalance, 094 String profileID) { 095 setLoginCount(loginCount); 096 setLogoutCount(logoutCount); 097 setLastLogin(lastLogin); 098 setCreationDate(creationDate); 099 setBalance(balance); 100 setOpenBalance(openBalance); 101 setProfileID(profileID); 102 } 103 104 public static AccountDataBean getRandomInstance() { 105 return new AccountDataBean(new Integer(TradeConfig.rndInt(100000)), //accountID 106 TradeConfig.rndInt(10000), //loginCount 107 TradeConfig.rndInt(10000), //logoutCount 108 new java.util.Date(), //lastLogin 109 new java.util.Date(TradeConfig.rndInt(Integer.MAX_VALUE)), //creationDate 110 TradeConfig.rndBigDecimal(1000000.0f), //balance 111 TradeConfig.rndBigDecimal(1000000.0f), //openBalance 112 TradeConfig.rndUserID() //profileID 113 ); 114 } 115 116 public String toString() { 117 return "\n\tAccount Data for account: " + getAccountID() 118 + "\n\t\t loginCount:" + getLoginCount() 119 + "\n\t\t logoutCount:" + getLogoutCount() 120 + "\n\t\t lastLogin:" + getLastLogin() 121 + "\n\t\t creationDate:" + getCreationDate() 122 + "\n\t\t balance:" + getBalance() 123 + "\n\t\t openBalance:" + getOpenBalance() 124 + "\n\t\t profileID:" + getProfileID() 125 ; 126 } 127 128 public String toHTML() { 129 return "<BR>Account Data for account: <B>" + getAccountID() + "</B>" 130 + "<LI> loginCount:" + getLoginCount() + "</LI>" 131 + "<LI> logoutCount:" + getLogoutCount() + "</LI>" 132 + "<LI> lastLogin:" + getLastLogin() + "</LI>" 133 + "<LI> creationDate:" + getCreationDate() + "</LI>" 134 + "<LI> balance:" + getBalance() + "</LI>" 135 + "<LI> openBalance:" + getOpenBalance() + "</LI>" 136 + "<LI> profileID:" + getProfileID() + "</LI>" 137 ; 138 } 139 140 public void print() { 141 Log.log(this.toString()); 142 } 143 144 /** 145 * Gets the accountID 146 * 147 * @return Returns a Integer 148 */ 149 public Integer getAccountID() { 150 return accountID; 151 } 152 153 /** 154 * Sets the accountID 155 * 156 * @param accountID The accountID to set 157 */ 158 public void setAccountID(Integer accountID) { 159 this.accountID = accountID; 160 } 161 162 /** 163 * Gets the loginCount 164 * 165 * @return Returns a int 166 */ 167 public int getLoginCount() { 168 return loginCount; 169 } 170 171 /** 172 * Sets the loginCount 173 * 174 * @param loginCount The loginCount to set 175 */ 176 public void setLoginCount(int loginCount) { 177 this.loginCount = loginCount; 178 } 179 180 /** 181 * Gets the logoutCount 182 * 183 * @return Returns a int 184 */ 185 public int getLogoutCount() { 186 return logoutCount; 187 } 188 189 /** 190 * Sets the logoutCount 191 * 192 * @param logoutCount The logoutCount to set 193 */ 194 public void setLogoutCount(int logoutCount) { 195 this.logoutCount = logoutCount; 196 } 197 198 /** 199 * Gets the lastLogin 200 * 201 * @return Returns a Date 202 */ 203 public Date getLastLogin() { 204 return lastLogin; 205 } 206 207 /** 208 * Sets the lastLogin 209 * 210 * @param lastLogin The lastLogin to set 211 */ 212 public void setLastLogin(Date lastLogin) { 213 this.lastLogin = lastLogin; 214 } 215 216 /** 217 * Gets the creationDate 218 * 219 * @return Returns a Date 220 */ 221 public Date getCreationDate() { 222 return creationDate; 223 } 224 225 /** 226 * Sets the creationDate 227 * 228 * @param creationDate The creationDate to set 229 */ 230 public void setCreationDate(Date creationDate) { 231 this.creationDate = creationDate; 232 } 233 234 /** 235 * Gets the balance 236 * 237 * @return Returns a BigDecimal 238 */ 239 public BigDecimal getBalance() { 240 return balance; 241 } 242 243 /** 244 * Sets the balance 245 * 246 * @param balance The balance to set 247 */ 248 public void setBalance(BigDecimal balance) { 249 this.balance = balance; 250 } 251 252 /** 253 * Gets the openBalance 254 * 255 * @return Returns a BigDecimal 256 */ 257 public BigDecimal getOpenBalance() { 258 return openBalance; 259 } 260 261 /** 262 * Sets the openBalance 263 * 264 * @param openBalance The openBalance to set 265 */ 266 public void setOpenBalance(BigDecimal openBalance) { 267 this.openBalance = openBalance; 268 } 269 270 /** 271 * Gets the profileID 272 * 273 * @return Returns a String 274 */ 275 public String getProfileID() { 276 return profileID; 277 } 278 279 /** 280 * Sets the profileID 281 * 282 * @param profileID The profileID to set 283 */ 284 public void setProfileID(String profileID) { 285 this.profileID = profileID; 286 } 287 288 /** 289 * Gets the profileID 290 * 291 * @return Returns a String 292 */ 293 /* Disabled for D185273 294 public String getUserID() { 295 return getProfileID(); 296 } 297 */ 298 public Collection<OrderDataBean> getOrders() { 299 return orders; 300 } 301 302 public void setOrders(Collection<OrderDataBean> orders) { 303 this.orders = orders; 304 } 305 306 public AccountProfileDataBean getProfile() { 307 return profile; 308 } 309 310 public void setProfile(AccountProfileDataBean profile) { 311 this.profile = profile; 312 } 313 314 public void login(String password) { 315 AccountProfileDataBean profile = getProfile(); 316 if ((profile == null) || (profile.getPassword().equals(password) == false)) { 317 String error = "AccountBean:Login failure for account: " + getAccountID() + 318 ((profile == null) ? "null AccountProfile" : 319 "\n\tIncorrect password-->" + profile.getUserID() + ":" + profile.getPassword()); 320 throw new EJBException(error); 321 } 322 323 setLastLogin(new Timestamp(System.currentTimeMillis())); 324 setLoginCount(getLoginCount() + 1); 325 } 326 327 public void logout() { 328 setLogoutCount(getLogoutCount() + 1); 329 } 330 331 }