`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.NOT_SUPPORTED) 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.NOT_SUPPORTED) 34 public void updateExchangeRate (double newrate) { 35 if (rc == null) { 36 System.out.println ("This might indicate a problem"); 37 retrieveRecords (); 38 } 39 40 for (Iterator iter = rc.iterator(); iter.hasNext();) { 41 TimedRecord r = (TimedRecord) iter.next(); 42 r.setSaving(r.getSaving() * newrate); 43 r.setResult(r.getResult() * newrate); 44 } 45 } 46 47 @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) 48 // @FlushMode(FlushModeType.NEVER) 49 public void updateTimestamp () { 50 if (rc == null) { 51 System.out.println ("This might indicate a problem"); 52 retrieveRecords (); 53 } 54 55 for (Iterator iter = rc.iterator(); iter.hasNext();) { 56 TimedRecord r = (TimedRecord) iter.next(); 57 r.setTs(new Timestamp (System.currentTimeMillis())); 58 } 59 } 60 61 @Remove 62 public void checkout() { 63 // flushes all changes 64 em.flush(); 65 } 66 }`