1 package net.sourceforge.finmodel.plan;
2
3 import java.math.BigDecimal;
4 import java.util.ArrayList;
5 import java.util.Calendar;
6 import java.util.Collections;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Set;
10
11 import net.sourceforge.finmodel.account.Account;
12 import net.sourceforge.finmodel.account.AccountImpl;
13 import net.sourceforge.finmodel.account.CashAccount;
14 import net.sourceforge.finmodel.rules.OneTimeRule;
15 import net.sourceforge.finmodel.rules.Rule;
16 import net.sourceforge.finmodel.rules.RuleImpl;
17
18 import org.hibernate.Session;
19 import org.hibernate.SessionFactory;
20 import org.hibernate.Transaction;
21 import org.hibernate.cfg.Configuration;
22 import org.jfin.date.ScheduleException;
23
24 public class FinancialPlan {
25 private long id;
26 private String name;
27 private Set<RuleImpl> rules = new HashSet<RuleImpl>();
28 private AccountImpl openingBalance;
29 private Calendar initiationDate;
30
31
32
33 public FinancialPlan() {
34 this.name = "New Plan";
35 this.openingBalance = new CashAccount("Opening Balance");
36 }
37
38 private static Session getSession() {
39 Session session = null;
40
41 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
42 session = sessionFactory.openSession();
43
44 return session;
45 }
46
47 public void save() {
48 Session session = getSession();
49 Transaction tx = session.beginTransaction();
50 session.save(this);
51 session.flush();
52 tx.commit();
53 }
54
55 public static FinancialPlan load( long id )
56 {
57 Session session = getSession();
58 return (FinancialPlan) session.load(FinancialPlan.class, id);
59 }
60
61 public static List<FinancialPlan> allPlans()
62 {
63 Session session = getSession();
64 List<FinancialPlan> rc = session.createQuery("from net.sourceforge.finmodel.plan.FinancialPlan").list();
65
66 return rc;
67 }
68
69 public void addAccount( Account acct, BigDecimal openingBal) {
70 OneTimeRule openRule = new OneTimeRule();
71 openRule.setSource( openingBalance );
72 openRule.setDestination(acct);
73 openRule.setAmount(openingBal);
74 openRule.setDate(initiationDate);
75
76 addRule(openRule);
77 }
78
79 public Set<Account> getAccounts() {
80 Set<Account> rc = new HashSet<Account>();
81
82 for (RuleImpl rule: rules) {
83 rc.add(rule.getSource());
84 rc.add(rule.getDestination());
85 }
86
87 return rc;
88 }
89
90 public void setRules( Set<RuleImpl> rules ) {
91 this.rules = rules;
92 }
93
94 public Set<RuleImpl> getRules() {
95 return this.rules;
96 }
97
98 public void addRule( RuleImpl rule ) {
99 rules.add( rule );
100 rule.setPlan(this);
101 }
102
103 public void removeRule( RuleImpl rule ) {
104 rules.remove( rule );
105 }
106
107 public List<net.sourceforge.finmodel.plan.Transaction> getScheduledTransactions()
108 throws ScheduleException
109 {
110 List<net.sourceforge.finmodel.plan.Transaction> rc =
111 new ArrayList<net.sourceforge.finmodel.plan.Transaction>();
112 for (Rule rule: rules) {
113 for (Calendar time: rule.getSchedule()) {
114 rc.add( rule.createTransaction( time ));
115 }
116 }
117 Collections.sort(rc, new TransactionComparator());
118 return rc;
119 }
120
121
122
123
124 public void setName(String name) {
125 this.name = name;
126 }
127
128
129
130
131 public String getName() {
132 return name;
133 }
134
135
136
137
138 public void setId(long id) {
139 this.id = id;
140 }
141
142
143
144
145 public long getId() {
146 return id;
147 }
148 }