1    package com.instantbank.collections.collectionsActivities.ejb;
2    
3    import java.sql.Connection;
4    import java.sql.PreparedStatement;
5    import java.sql.ResultSet;
6    import java.sql.SQLException;
7    import javax.ejb.CreateException;
8    import javax.ejb.EJBContext;
9    import javax.ejb.SessionBean;
10   import javax.ejb.SessionContext;
11   import oracle.xml.parser.v2.XMLDocument;
12   import com.instantbank.collections.ach.AchDAO;
13   import com.instantbank.collections.ach.DataObject;
14   import com.instantbank.collections.util.InstantbankException;
15   import com.instantbank.collections.util.ServiceLocator;
16   import com.instantbank.collections.util.XMLDataAccess;
17   
18   
19   public class CollectionsActivitiesServicesBean
20       implements SessionBean {
21     private EJBContext context;
22   
23   
24     private void setRollbackOnly() {
25       try {
26         this.context.setRollbackOnly();
27       }
28       catch(Exception e) {
29       }
30     }
31   
32   
33     public void createDataObject(DataObject dataObject)
34        throws InstantbankException {
35       try {
36         AchDAO.create(dataObject);
37       }
38       catch(Exception e) {
39         e.printStackTrace();
40         setRollbackOnly();
41         throw new InstantbankException(e, "221001", "Failed to add DataObject: "
42           + dataObject.toString());
43       }
44     }
45   
46   
47     public void updateDataObject(DataObject dataObject)
48        throws InstantbankException {
49       try {
50         AchDAO.update(dataObject);
51       }
52       catch(Exception e) {
53         e.printStackTrace();
54         setRollbackOnly();
55         throw new InstantbankException(e, "221001", "Failed to update DataObject: "
56           + dataObject.toString());
57       }
58     }
59   
60   
61     public void removeDataObject(DataObject dataObject)
62        throws InstantbankException {
63       try {
64         AchDAO.remove(dataObject);
65       }
66       catch(Exception e) {
67         e.printStackTrace();
68         setRollbackOnly();
69         throw new InstantbankException(e, "221001", "Failed to remove DataObject: "
70           + dataObject.toString());
71       }
72     }
73   
74   
75     public CollectionsActivitiesServicesBean() { }
76   
77   
78     public void ejbCreate() throws CreateException {
79       // TODO:  Add custom implementation.
80     }
81   
82   
83     public void ejbActivate() { }
84   
85   
86     public void ejbPassivate() { }
87   
88   
89     public void ejbRemove() { }
90   
91   
92     public String getPromiseHistory(Long companyId, Long agrmId, Long rowNum) throws InstantbankException {
93       long AgrmId;
94       String sql;
95       XMLDocument TransactionHistory;
96       XMLDataAccess xda = null;
97       Long maxRows;
98   
99       try {
100        xda = new XMLDataAccess("");
101        xda.connect();
102        AgrmId = agrmId.longValue();
103        maxRows = new Long("19");
104        sql = "select 	PostingDate,	Operator,	Action,	Result,	Amount,	DueDate,";
105        sql += "NextReviewDate,	Status,	Comments from (SELECT Id,	PostingDate,	Operator,";
106        sql += "Action,	Result,	Amount,	DueDate,	NextReviewDate,	Status,	Comments,	ROWNUM theRow";
107        sql += " FROM(SELECT  PRM_ID Id, to_char(PRM_DATE,'mm-dd-yyyy') PostingDate, USER_USERID Operator, ";
108        sql += " ACT_DESCRIPTION Action, RES_DESCRIPTION Result, PRM_AMOUNT Amount, to_char(PRM_DUE_DATE,'mm-dd-yyyy') DueDate, ";
109        sql += " to_char(ACRH_NEXT_REVIEW_DATE,'mm-dd-yyyy') NextReviewDate, PRM_STATUS Status, ACRH_COMMENTS Comments ";
110        sql += " FROM  ACTION_RESULTS_HISTORY, PROMISES, ACTIONS,RESULTS, USERS WHERE (ACRH_AGRM_ID = " + AgrmId;
111        sql += " ) AND (PRM_ACRH_ID=ACRH_ID) AND (ACT_ID=ACRH_ACT_ID) AND (RES_ID=ACRH_RES_ID) AND (USER_ID(+)=ACRH_USER_ID)";
112        sql += " ORDER BY PRM_DATE DESC, Operator))";
113        sql += " Where theRow between " + rowNum.toString() + "  AND " + new Long(rowNum.longValue() + maxRows.longValue()).toString();
114        String xml = xda.getXml(sql, "PromiseHistory", "Promise");
115        return xml;
116      }
117      catch(Exception e) {
118        this.context.setRollbackOnly();
119        throw new InstantbankException(e, "511012", "Failed to get Promises ");
120      }
121      finally {
122        try {
123          if(xda != null) {
124            xda.disconnect();
125          }
126        }
127        catch(Exception e) {}
128      }
129  
130    }
131  
132  
133    public Long getRowsPromiseHistory(Long companyId, Long agrmId) throws InstantbankException {
134      Connection con = null;
135      PreparedStatement ps = null;
136      ResultSet result = null;
137      String xml = "";
138      Long maxRows = null;
139      String sql;
140  
141      try {
142        sql = "SELECT  count(PRM_ID) FROM  ACTION_RESULTS_HISTORY, ";
143        sql += "PROMISES, ACTIONS,RESULTS, USERS WHERE (ACRH_AGRM_ID = ? ) AND (PRM_ACRH_ID=ACRH_ID) ";
144        sql += " AND (ACT_ID=ACRH_ACT_ID) AND (RES_ID=ACRH_RES_ID) AND (USER_ID(+)=ACRH_USER_ID)";
145        con = ServiceLocator.instance().getConnection();
146        ps = con.prepareStatement(sql);
147        ps.setLong(1, agrmId.longValue());
148        result = ps.executeQuery();
149  
150        if(result.next()) {
151          maxRows = new Long(result.getLong(1));
152        }
153        else {
154          maxRows = new Long("0");
155        }
156  
157        return maxRows;
158      }
159      catch(Exception e) {
160        throw new InstantbankException(e, "131009", "Failed to get the the number of promises ");
161      }
162      finally {
163        try {
164          if(result != null) {
165            result.close();
166          }
167          if(ps != null) {
168            ps.close();
169          }
170          if(con != null) {
171            con.close();
172          }
173        }
174        catch(SQLException se) {
175          se.printStackTrace();
176        }
177      }
178  
179    }
180  
181  
182    public void setSessionContext(SessionContext ctx) {
183      this.context = ctx;
184    }
185  }
186