001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.cmr.ejb;
023:
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.SortedMap;
027:
028: import javax.ejb.CreateException;
029: import javax.ejb.EJBException;
030: import javax.ejb.SessionBean;
031: import javax.ejb.SessionContext;
032:
033: import javax.naming.InitialContext;
034: import javax.naming.NamingException;
035:
036: import org.apache.log4j.Category;
037:
038: import org.jboss.test.cmp2.cmr.interfaces.CMRBugEJBLocalHome;
039: import org.jboss.test.cmp2.cmr.interfaces.CMRBugEJBLocal;
040:
041: /**
042: * Describe class <code>CMRBugManagerBean</code> here.
043: *
044: * @author <a href="mailto:MNewcomb@tacintel.com">Michael Newcomb</a>
045: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
046: * @version 1.0
047: * @ejb:bean type="Stateless" name="CMRBugManagerEJB" jndi-name="CMRBugManager"
048: * @ejb:ejb-ref ejb-name="CMRBugEJB"
049: * view-type="local"
050: * ref-name="ejb/CMRBug"
051: * @ejb:transaction type="Required"
052: * @ejb:transaction-type type="Container"
053: */
054: public class CMRBugManagerBean implements SessionBean {
055: private CMRBugEJBLocalHome cmrBugHome;
056:
057: private Category log = Category.getInstance(getClass());
058:
059: public CMRBugManagerBean() {
060: }
061:
062: /**
063: * Describe <code>createCMRBugs</code> method here.
064: *
065: * @param cmrBugs a <code>SortedMap</code> value
066: * @ejb:interface-method view-type="remote"
067: */
068: public void createCMRBugs(SortedMap cmrBugs) {
069: try {
070: if (!cmrBugs.isEmpty()) {
071: Iterator i = cmrBugs.entrySet().iterator();
072: Map.Entry entry = (Map.Entry) i.next();
073:
074: // the root id (of which all others are based) is the first key in
075: // the SortedMap
076: //
077: String root = (String) entry.getKey();
078:
079: String id = root;
080: String description = (String) entry.getValue();
081:
082: CMRBugEJBLocal parent = cmrBugHome.create(id,
083: description, null);
084: entry.setValue(parent);
085:
086: while (i.hasNext()) {
087: entry = (Map.Entry) i.next();
088:
089: id = (String) entry.getKey();
090: description = (String) entry.getValue();
091:
092: int index = id.lastIndexOf(".");
093: if (index != -1) {
094: // determine the parent id and then try to find the parent's
095: // CMRBugEJBLocal in the map
096: //
097: String parentId = id.substring(0, index);
098: parent = (CMRBugEJBLocal) cmrBugs.get(parentId);
099: }
100: entry.setValue(cmrBugHome.create(id, description,
101: parent));
102: }
103: }
104: } catch (Exception e) {
105: throw new EJBException("createCMRBugs", e);
106: }
107: }
108:
109: /**
110: * Describe <code>getParentFor</code> method here.
111: *
112: * @param id a <code>String</code> value
113: * @return a <code>String[]</code> value
114: * @ejb:interface-method view-type="remote"
115: */
116: public String[] getParentFor(String id) {
117: try {
118: CMRBugEJBLocal cmrBug = cmrBugHome.findByPrimaryKey(id);
119: CMRBugEJBLocal parent = cmrBug.getParent();
120:
121: String[] parentIdAndDescription = null;
122: if (parent != null) {
123: parentIdAndDescription = new String[2];
124: parentIdAndDescription[0] = parent.getId();
125: parentIdAndDescription[1] = parent.getDescription();
126: }
127:
128: return parentIdAndDescription;
129: } catch (Exception e) {
130: throw new EJBException("getParentFor", e);
131: }
132: }
133:
134: /**
135: * @ejb.interface-method
136: * @ejb.transaction type="RequiresNew"
137: */
138: public void setupLoadFKState() throws Exception {
139: CMRBugEJBLocal bug1 = cmrBugHome.create("first", null, null);
140: CMRBugEJBLocal bug2 = cmrBugHome.create("second", null, null);
141: CMRBugEJBLocal bug3 = cmrBugHome.create("third", null, null);
142: CMRBugEJBLocal bug4 = cmrBugHome.create("forth", null, null);
143:
144: bug1.setNextNode(bug2);
145: bug2.setNextNode(bug3);
146: bug3.setNextNode(bug4);
147:
148: bug4.setPrevNode(bug3);
149: bug3.setPrevNode(bug2);
150: bug2.setPrevNode(bug1);
151: }
152:
153: /**
154: * @ejb.interface-method
155: * @ejb.transaction type="RequiresNew"
156: */
157: public void moveLastNodeBack() throws Exception {
158: CMRBugEJBLocal bug = cmrBugHome.findByPrimaryKey("forth");
159:
160: CMRBugEJBLocal prev = bug.getPrevNode();
161: CMRBugEJBLocal next = bug.getNextNode();
162: CMRBugEJBLocal prevPrev = prev.getPrevNode();
163:
164: prevPrev.setNextNode(bug);
165: bug.setPrevNode(prevPrev);
166: bug.setNextNode(prev);
167: prev.setPrevNode(bug);
168: prev.setNextNode(next);
169: }
170:
171: /**
172: * @ejb.interface-method
173: * @ejb.transaction type="RequiresNew"
174: */
175: public boolean lastHasNextNode() throws Exception {
176: CMRBugEJBLocal bug = cmrBugHome.findByPrimaryKey("third");
177: return bug.getNextNode() != null;
178: }
179:
180: /**
181: * @ejb.interface-method
182: * @ejb.transaction type="RequiresNew"
183: */
184: public void tearDownLoadFKState() throws Exception {
185: cmrBugHome.remove("first");
186: cmrBugHome.remove("second");
187: cmrBugHome.remove("third");
188: cmrBugHome.remove("forth");
189: }
190:
191: // --------------------------------------------------------------------------
192: // SessionBean methods
193: //
194:
195: /**
196: * Describe <code>ejbCreate</code> method here.
197: *
198: * @exception CreateException if an error occurs
199: */
200: public void ejbCreate() throws CreateException {
201: try {
202: cmrBugHome = lookupCMRBugHome();
203: } catch (Exception e) {
204: throw new CreateException(e.getMessage());
205: }
206: }
207:
208: public void ejbActivate() {
209: try {
210: cmrBugHome = lookupCMRBugHome();
211: } catch (Exception e) {
212: throw new EJBException("ejbActivate", e);
213: }
214: }
215:
216: public void ejbPassivate() {
217: cmrBugHome = null;
218: }
219:
220: public void ejbRemove() {
221: }
222:
223: public void setSessionContext(SessionContext sessionContext) {
224: }
225:
226: private CMRBugEJBLocalHome lookupCMRBugHome()
227: throws NamingException {
228: InitialContext initialContext = new InitialContext();
229: return (CMRBugEJBLocalHome) initialContext
230: .lookup("java:comp/env/ejb/CMRBug");
231: }
232: }
|