1 package net.sourceforge.finmodel.rules;
2
3 import java.io.Serializable;
4 import java.math.BigDecimal;
5 import java.util.ArrayList;
6 import java.util.Calendar;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Set;
10
11 import org.jfin.date.ScheduleException;
12
13 import net.sourceforge.finmodel.account.Account;
14 import net.sourceforge.finmodel.plan.FinancialPlan;
15 import net.sourceforge.finmodel.plan.Transaction;
16 import net.sourceforge.finmodel.plan.TransactionImpl;
17
18 public class RuleImpl implements Rule, Serializable {
19 public static final long serialVersionUID = 1L;
20
21 private long id;
22 private Account source;
23 private Account dest;
24 private FinancialPlan plan;
25 private Set<RuleListener> listeners = new HashSet<RuleListener>();
26
27 protected BigDecimal amount = BigDecimal.ZERO;
28
29 public RuleImpl() {
30 super();
31 }
32
33 public void setSource(Account source) {
34 Account oldSource = this.source;
35 this.source = source;
36 source.addSourceRule( this );
37 if (oldSource != null) {
38 oldSource.removeSourceRule( this );
39 }
40
41 if (oldSource == null || !oldSource.equals(source)) {
42 notifySourceChange(oldSource);
43 }
44 }
45
46 public Account getSource() {
47 return source;
48 }
49
50 public void setDestination(Account dest) {
51 Account oldDest = this.dest;
52 this.dest = dest;
53 dest.addDestinationRule( this );
54 if (oldDest != null) {
55 oldDest.removeDestinationRule( this );
56 }
57
58 if (oldDest == null || !oldDest.equals(dest)) {
59 notifyDestinationChange(oldDest);
60 }
61 }
62
63 public Account getDestination() {
64 return dest;
65 }
66
67 protected void notifyScheduleChange() {
68 for (RuleListener listener: listeners) {
69 listener.eventScheduleChange(this);
70 }
71 }
72
73 protected void notifySourceChange(Account oldSource) {
74 for (RuleListener listener: listeners) {
75 listener.eventSourceChange(this, oldSource);
76 }
77 }
78
79 protected void notifyDestinationChange(Account oldDest) {
80 for (RuleListener listener: listeners) {
81 listener.eventDestinationChange(this, oldDest);
82 }
83 }
84
85 public void addRuleListener(RuleListener listener) {
86 listeners.add(listener);
87 }
88
89 public void removeRuleListener(RuleListener listener) {
90 listeners.remove(listener);
91 }
92
93
94
95
96 protected void setDest(Account dest) {
97 this.dest = dest;
98 }
99
100
101
102
103 protected Account getDest() {
104 return dest;
105 }
106
107
108
109
110 public void setId(long id) {
111 this.id = id;
112 }
113
114
115
116
117 public long getId() {
118 return id;
119 }
120
121 public List<Calendar> getSchedule() throws ScheduleException {
122 return new ArrayList<Calendar>();
123 }
124
125
126
127
128 public void setPlan(FinancialPlan plan) {
129 this.plan = plan;
130 }
131
132
133
134
135 public FinancialPlan getPlan() {
136 return plan;
137 }
138
139 protected Transaction makeTransaction(Calendar date, BigDecimal value) {
140 return new TransactionImpl(getSource(), getDestination(), date, value );
141 }
142
143
144
145
146
147
148
149
150
151
152
153 protected BigDecimal getAmount(Calendar day) {
154 return amount;
155 }
156
157
158
159
160
161
162
163
164
165 public Transaction createTransaction(Calendar day)
166 throws ScheduleException
167 {
168 List<Calendar> sched = getSchedule();
169 if (sched.contains(day)) {
170 return makeTransaction(day, amount);
171 } else {
172 return makeTransaction(day, BigDecimal.ZERO);
173 }
174 }
175 }