SecureCalculator.java
01 package trail.security;
02 
03 import trail.entity.beans.*;
04 import javax.ejb.*;
05 import javax.persistence.*;
06 import javax.annotation.security.*;
07 import java.util.*;
08 import java.sql.Timestamp;
09 
10 import org.jboss.annotation.security.SecurityDomain;
11 
12 @Stateless
13 @SecurityDomain("other")
14 public class SecureCalculator implements Calculator {
15 
16   @PersistenceContext
17   protected EntityManager em;
18 
19   @RolesAllowed({"AdminUser"})
20   public void addFund (String name, double growthrate) {
21     Fund fund = new Fund (name, growthrate);
22     em.persist (fund);
23   }
24 
25   @RolesAllowed({"AdminUser"})
26   public void addInvestor (String name, int start, int end) {
27     Investor investor = new Investor (name, start, end);
28     em.persist (investor);
29   }
30 
31   @PermitAll
32   public Collection<Fund> getFunds () {
33     return em.createQuery("from Fund f").getResultList();
34   }
35   @PermitAll
36   public Collection <Investor> getInvestors () {
37     return em.createQuery("from Investor p").getResultList();
38   }
39   @PermitAll
40   public Collection <TimedRecord> getRecords () {
41     return em.createQuery("from TimedRecord r order by r.id desc").getResultList();
42   }
43 
44   @RolesAllowed({"RegularUser"})
45   public double calculate (int fundId, int investorId, double saving) {
46 
47     Investor investor =
48         em.find(Investor.class,
49                 Integer.valueOf(investorId));
50     Fund fund =
51         em.find(Fund.class,
52                 Integer.valueOf(fundId));
53 
54     int start = investor.getStartAge();
55     int end = investor.getEndAge();
56     double growthrate = fund.getGrowthrate();
57 
58     double tmp = Math.pow(1. + growthrate / 12.12. (end - start1);
59     double result = saving * 12. (tmp - 1/ growthrate;
60     Timestamp ts = new Timestamp (System.currentTimeMillis());
61 
62     TimedRecord rec =
63         new TimedRecord (fund, investor, saving, result, ts);
64     em.persist (rec);
65 
66     return result;
67   }
68 }