Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/obs/client/accounting/delegates/GeneralJournalDelegate.java


1   package com.obs.client.accounting.delegates;
2   
3   import java.rmi.RemoteException;
4   
5   import java.util.Collection;
6   import java.util.Iterator;
7   import java.util.ArrayList;
8   
9   import javax.ejb.CreateException;
10  import javax.ejb.RemoveException;
11  import javax.ejb.Handle;
12  import javax.naming.InitialContext;
13  import javax.naming.NamingException;
14  import javax.rmi.PortableRemoteObject;
15  
16  import com.obs.client.templates.EJBDelegate;
17  import com.obs.common.accounting.objects.JournalLineType;
18  import com.obs.common.accounting.views.JournalLineView;
19  import com.obs.common.exceptions.*;
20  import com.obs.ejb.accounting.interfaces.GeneralJournal;
21  import com.obs.ejb.accounting.interfaces.GeneralJournalHome;
22  
23  /**
24   * @author David Durst
25   */
26  
27  public class GeneralJournalDelegate implements EJBDelegate {
28    
29    private InitialContext ictx = null;
30    private GeneralJournal generalJournal = null;
31    private Handle handle = null;
32    
33    
34    private String localJournaEntryDescription = "";
35    private Collection localJournalEntryLines = new ArrayList();
36    
37    
38    public GeneralJournalDelegate() throws ServerContactException {
39      activate();
40    }
41    
42    public void setJournalEntryDescription(String journalEntryDescription) throws ServerContactException {
43      try {
44        generalJournal.setJournalEntryDescription(journalEntryDescription);
45        localJournaEntryDescription = journalEntryDescription;
46      }
47      catch(RemoteException remote_ex) {
48        throw new ServerContactException();
49      }
50    }
51    
52    public String getJournalEntryDescription() throws ServerContactException {
53      return localJournaEntryDescription;
54    }
55    
56    public void addJournalLine(JournalLineView jLineView) throws ServerContactException {
57      try {
58        localJournalEntryLines = generalJournal.addJournalLine(jLineView);
59      }
60      catch(RemoteException remote_ex) {
61        throw new ServerContactException();
62      }
63    }
64  
65    public void removeJournalLine(String journalLineID) throws ServerContactException {
66      try {
67        localJournalEntryLines = generalJournal.removeJournalLine(journalLineID);
68      }
69      catch(RemoteException remote_ex) {
70        throw new ServerContactException();
71      }
72    }
73  
74    public Collection getJournalEntryLines() throws ServerContactException {
75      return localJournalEntryLines;
76    }
77  
78    public boolean doesJournalEntryBalance() throws ServerContactException {
79  
80      double  creditSum  = 0.00, 
81              debitSum   = 0.00,
82              lineAmount = 0.00;
83  
84      Iterator i;
85      JournalLineView line;
86  
87      if(localJournalEntryLines == null || localJournalEntryLines.isEmpty()) { 
88        return false; 
89      }
90  
91      i = localJournalEntryLines.iterator();
92      
93      while(i.hasNext()) {
94        if( ((JournalLineView)i.next()).isMalformed() )
95        return false;
96      }
97  
98      i = localJournalEntryLines.iterator();
99      while(i.hasNext()) {
100 
101       line       = (JournalLineView)i.next();
102       lineAmount = line.getAmount().doubleValue();
103 
104       if(line.getType().equals(JournalLineType.DEBIT)) { 
105         debitSum = debitSum + lineAmount; 
106       }
107       else { 
108         creditSum = creditSum + lineAmount; 
109       }
110     }
111     
112     return (creditSum != 0.00 && creditSum == debitSum);
113   }
114   
115   public void commitJournalEntry() throws ServerContactException, 
116                                                                         EntryBalanceException,
117                                                                         EntryMalformedException {
118     try{
119       generalJournal.commitJournalEntry();
120     }
121     catch(RemoteException remote_ex){
122       throw new ServerContactException();
123     }
124   }
125   
126   
127   public void destroy() throws ServerContactException {
128     try{
129             generalJournal.remove();
130       generalJournal = null;
131       handle = null;
132       localJournaEntryDescription = "";
133       localJournalEntryLines = null;
134     }
135     catch(RemoteException remote_ex){
136       throw new ServerContactException();
137     }
138         catch(RemoveException remove_ex){
139             throw new ServerContactException();
140         }
141     
142   }
143   
144   public void passivate() throws ServerContactException {
145     try {
146       if(generalJournal != null) {
147         handle = generalJournal.getHandle();
148         generalJournal = null;
149       }
150     }
151     catch(RemoteException remote_ex){
152       throw new ServerContactException();
153     }
154   }
155   
156   public void activate() throws ServerContactException {
157     if(handle != null) {
158       try {
159         generalJournal = (GeneralJournal)handle.getEJBObject();
160       }
161       catch(RemoteException remote_ex) {
162         throw new ServerContactException();
163       }
164     /*        catch(ClassCastException cast_ex){
165                 cast_ex.printStackTrace();
166             }*/
167     }
168     else {
169       try {
170         ictx = new InitialContext();
171         GeneralJournalHome glHome = (GeneralJournalHome)
172                  PortableRemoteObject.narrow(
173                    ictx.lookup("com/obs/ejb/accounting/GeneralJournalEJB"),
174                    GeneralJournalHome.class);
175         generalJournal = glHome.create();
176       }
177       catch(NamingException naming_ex) {
178         throw new ServerContactException();
179       }
180       catch(RemoteException remote_ex) {
181         throw new ServerContactException();
182       }
183       catch(CreateException create_ex) {
184       }      
185     }
186   }
187   
188 }