`ApptransCalculator.java`
`01 package trail.apptrans; 02 03 import trail.entity.beans.*; 04 import javax.ejb.*; 05 import javax.persistence.*; 06 import java.util.*; 07 import java.sql.Timestamp; 08 import java.io.Serializable; 09 10 @Stateful 11 public class ApptransCalculator implements Calculator, Serializable { 12 13 @PersistenceContext( 14 // unitName="cal", 15 type=PersistenceContextType.EXTENDED 16 ) 17 protected EntityManager em; 18 19 Collection <TimedRecord> rc; 20 21 @TransactionAttribute(TransactionAttributeType.REQUIRED) 22 // @PostConstruct 23 protected void retrieveRecords () { 24 rc = em.createQuery("from TimedRecord r order by r.id desc") 25 .getResultList(); 26 } 27 28 public Collection <TimedRecord> getRecords () { 29 retrieveRecords (); 30 return rc; 31 } 32 33 @TransactionAttribute(TransactionAttributeType.REQUIRED) 34 // @FlushMode(FlushModeType.NEVER) 35 public void updateExchangeRate (double newrate) { 36 if (rc == null) { 37 System.out.println ("This might indicate a problem"); 38 retrieveRecords (); 39 } 40 41 for (Iterator iter = rc.iterator(); iter.hasNext();) { 42 TimedRecord r = (TimedRecord) iter.next(); 43 r.setSaving(r.getSaving() * newrate); 44 r.setResult(r.getResult() * newrate); 45 } 46 } 47 48 @TransactionAttribute(TransactionAttributeType.REQUIRED) 49 // @FlushMode(FlushModeType.NEVER) 50 public void updateTimestamp () { 51 if (rc == null) { 52 System.out.println ("This might indicate a problem"); 53 retrieveRecords (); 54 } 55 56 for (Iterator iter = rc.iterator(); iter.hasNext();) { 57 TimedRecord r = (TimedRecord) iter.next(); 58 r.setTs(new Timestamp (System.currentTimeMillis())); 59 } 60 } 61 62 @Remove 63 public void checkout() { 64 // flushes all changes 65 em.flush(); 66 } 67 }`