KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > cmr > ejb > CMRBugManagerBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.cmr.ejb;
23
24
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.SortedMap JavaDoc;
28
29 import javax.ejb.CreateException JavaDoc;
30 import javax.ejb.EJBException JavaDoc;
31 import javax.ejb.SessionBean JavaDoc;
32 import javax.ejb.SessionContext JavaDoc;
33
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36
37 import org.jboss.logging.Logger;
38
39 import org.jboss.test.cmp2.cmr.interfaces.CMRBugEJBLocalHome;
40 import org.jboss.test.cmp2.cmr.interfaces.CMRBugEJBLocal;
41
42 /**
43  * Describe class <code>CMRBugManagerBean</code> here.
44  *
45  * @author <a HREF="mailto:MNewcomb@tacintel.com">Michael Newcomb</a>
46  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
47  * @version 1.0
48  * @ejb:bean type="Stateless" name="CMRBugManagerEJB" jndi-name="CMRBugManager"
49  * @ejb:ejb-ref ejb-name="CMRBugEJB"
50  * view-type="local"
51  * ref-name="ejb/CMRBug"
52  * @ejb:transaction type="Required"
53  * @ejb:transaction-type type="Container"
54  */

55 public class CMRBugManagerBean
56    implements SessionBean JavaDoc
57 {
58    private CMRBugEJBLocalHome cmrBugHome;
59
60    private Logger log = Logger.getLogger(getClass());
61
62    public CMRBugManagerBean()
63    {
64    }
65
66    /**
67     * Describe <code>createCMRBugs</code> method here.
68     *
69     * @param cmrBugs a <code>SortedMap</code> value
70     * @ejb:interface-method view-type="remote"
71     */

72    public void createCMRBugs(SortedMap JavaDoc cmrBugs)
73    {
74       try
75       {
76          if(!cmrBugs.isEmpty())
77          {
78             Iterator JavaDoc i = cmrBugs.entrySet().iterator();
79             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
80
81             // the root id (of which all others are based) is the first key in
82
// the SortedMap
83
//
84
String JavaDoc root = (String JavaDoc)entry.getKey();
85
86             String JavaDoc id = root;
87             String JavaDoc description = (String JavaDoc)entry.getValue();
88
89             CMRBugEJBLocal parent = cmrBugHome.create(id, description, null);
90             entry.setValue(parent);
91
92             while(i.hasNext())
93             {
94                entry = (Map.Entry JavaDoc)i.next();
95
96                id = (String JavaDoc)entry.getKey();
97                description = (String JavaDoc)entry.getValue();
98
99                int index = id.lastIndexOf(".");
100                if(index != -1)
101                {
102                   // determine the parent id and then try to find the parent's
103
// CMRBugEJBLocal in the map
104
//
105
String JavaDoc parentId = id.substring(0, index);
106                   parent = (CMRBugEJBLocal)cmrBugs.get(parentId);
107                }
108                entry.setValue(cmrBugHome.create(id, description, parent));
109             }
110          }
111       }
112       catch(Exception JavaDoc e)
113       {
114          e.printStackTrace();
115          throw new EJBException JavaDoc(e.getMessage());
116       }
117    }
118
119    /**
120     * Describe <code>getParentFor</code> method here.
121     *
122     * @param id a <code>String</code> value
123     * @return a <code>String[]</code> value
124     * @ejb:interface-method view-type="remote"
125     */

126    public String JavaDoc[] getParentFor(String JavaDoc id)
127    {
128       try
129       {
130          CMRBugEJBLocal cmrBug = cmrBugHome.findByPrimaryKey(id);
131          CMRBugEJBLocal parent = cmrBug.getParent();
132
133          String JavaDoc[] parentIdAndDescription = null;
134          if(parent != null)
135          {
136             parentIdAndDescription = new String JavaDoc[2];
137             parentIdAndDescription[0] = parent.getId();
138             parentIdAndDescription[1] = parent.getDescription();
139          }
140
141          return parentIdAndDescription;
142       }
143       catch(Exception JavaDoc e)
144       {
145          e.printStackTrace();
146          throw new EJBException JavaDoc(e.getMessage());
147       }
148    }
149
150    /**
151     * @ejb.interface-method
152     * @ejb.transaction type="RequiresNew"
153     */

154    public void setupLoadFKState()
155       throws Exception JavaDoc
156    {
157       CMRBugEJBLocal bug1 = cmrBugHome.create("first", null, null);
158       CMRBugEJBLocal bug2 = cmrBugHome.create("second", null, null);
159       CMRBugEJBLocal bug3 = cmrBugHome.create("third", null, null);
160       CMRBugEJBLocal bug4 = cmrBugHome.create("forth", null, null);
161
162       bug1.setNextNode(bug2);
163       bug2.setNextNode(bug3);
164       bug3.setNextNode(bug4);
165
166       bug4.setPrevNode(bug3);
167       bug3.setPrevNode(bug2);
168       bug2.setPrevNode(bug1);
169    }
170
171    /**
172     * @ejb.interface-method
173     * @ejb.transaction type="RequiresNew"
174     */

175    public void moveLastNodeBack()
176       throws Exception JavaDoc
177    {
178       CMRBugEJBLocal bug = cmrBugHome.findByPrimaryKey("forth");
179
180       CMRBugEJBLocal prev = bug.getPrevNode();
181       CMRBugEJBLocal next = bug.getNextNode();
182       CMRBugEJBLocal prevPrev = prev.getPrevNode();
183
184       prevPrev.setNextNode(bug);
185       bug.setPrevNode(prevPrev);
186       bug.setNextNode(prev);
187       prev.setPrevNode(bug);
188       prev.setNextNode(next);
189    }
190
191    /**
192     * @ejb.interface-method
193     * @ejb.transaction type="RequiresNew"
194     */

195    public boolean lastHasNextNode()
196       throws Exception JavaDoc
197    {
198       CMRBugEJBLocal bug = cmrBugHome.findByPrimaryKey("third");
199       return bug.getNextNode() != null;
200    }
201
202    /**
203     * @ejb.interface-method
204     * @ejb.transaction type="RequiresNew"
205     */

206    public void tearDownLoadFKState()
207       throws Exception JavaDoc
208    {
209       cmrBugHome.remove("first");
210       cmrBugHome.remove("second");
211       cmrBugHome.remove("third");
212       cmrBugHome.remove("forth");
213    }
214
215    // --------------------------------------------------------------------------
216
// SessionBean methods
217
//
218

219    /**
220     * Describe <code>ejbCreate</code> method here.
221     *
222     * @exception CreateException if an error occurs
223     */

224    public void ejbCreate()
225       throws CreateException JavaDoc
226    {
227       try
228       {
229          cmrBugHome = lookupCMRBugHome();
230       }
231       catch(Exception JavaDoc e)
232       {
233          throw new CreateException JavaDoc(e.getMessage());
234       }
235    }
236
237    public void ejbActivate()
238    {
239       try
240       {
241          cmrBugHome = lookupCMRBugHome();
242       }
243       catch(Exception JavaDoc e)
244       {
245          throw new EJBException JavaDoc(e.getMessage());
246       }
247    }
248
249    public void ejbPassivate()
250    {
251       cmrBugHome = null;
252    }
253
254    public void ejbRemove()
255    {
256    }
257
258    public void setSessionContext(SessionContext JavaDoc sessionContext)
259    {
260    }
261
262    private CMRBugEJBLocalHome lookupCMRBugHome()
263       throws NamingException JavaDoc
264    {
265       InitialContext JavaDoc initialContext = new InitialContext JavaDoc();
266       return (CMRBugEJBLocalHome)initialContext.lookup("java:comp/env/ejb/CMRBug");
267    }
268 }
269
Popular Tags