KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > session > helper > ItemSpecialHelperBean


1 /*
2  * Created on Dec 10, 2004
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package olstore.session.helper;
8
9 import java.util.Collection;
10 import java.util.Iterator;
11
12 import olstore.entity.ItemLocal;
13 import olstore.entity.ItemLocalHome;
14 import olstore.entity.TypeLocalHome;
15 import olstore.entity.TypeLocal;
16 import olstore.framework.EJBHomeFactory;
17 import olstore.entity.PictureLocal;
18
19 import javax.ejb.SessionBean;
20 import javax.ejb.SessionContext;
21 import javax.ejb.CreateException;
22 import javax.activation.DataHandler;
23
24 import javax.xml.soap.AttachmentPart;
25
26 import org.apache.axis.MessageContext;
27
28 import org.apache.axis.attachments.AttachmentsImpl;
29 import org.apache.axis.Message;
30
31 /**
32  * @author mwringe
33  *
34  * @ejb.bean name="ItemSpecialHelper" type="Stateless"
35  * view-type="remote-service-endpoint" jndi-name="ItemSpecialRemote"
36  *
37  * @ejb.transaction type="Required"
38  *
39  * -- This is need for JOnAS. If you are not using JOnAS you can safetly remove
40  * the tags below.
41  *
42  * @jonas.bean ejb-name="ItemSpecialHelper" jndi-name="ItemSpecialRemote"
43  * --
44  *
45  */

46 public class ItemSpecialHelperBean implements SessionBean {
47
48     private SessionContext ejbContext;
49
50     public void setSessionContext(SessionContext ctx) {
51         ejbContext = ctx;
52     }
53
54     public void ejbRemove() {
55     }
56
57     public void ejbCreate() throws CreateException {
58     }
59
60     public void ejbPassivate() {
61     }
62
63     public void ejbActivate() {
64     }
65
66     /**
67      *
68      * @return All items on special (name, description and price)
69      * @throws Exception
70      *
71      * @ejb.interface-method view-type="remote-service-endpoint"
72      * generate="service-endpoint"
73      * service-endpoint-extends="java.rmi.Remote"
74      *
75      */

76     public String[][] allSpecials() throws Exception {
77         EJBHomeFactory factory = EJBHomeFactory.getInstance();
78         ItemLocalHome home = (ItemLocalHome) factory
79                 .getLocalHome(EJBHomeFactory.ITEM);
80         Collection specials = home.findBySpecialOffer();
81         Iterator specialIt = specials.iterator();
82
83         String sArray[][] = new String[specials.size()][5];
84
85         // get the response message for this request
86
MessageContext msgContext = MessageContext.getCurrentContext();
87         Message resMsg = msgContext.getResponseMessage();
88
89         int counter = 0;
90         while (specialIt.hasNext()) {
91             ItemLocal item = (ItemLocal) specialIt.next();
92
93             // Create the attachment as a MIME attachment
94
AttachmentPart attachment = resMsg.createAttachmentPart();
95             resMsg.getAttachmentsImpl().setSendType(
96                     AttachmentsImpl.SEND_TYPE_MIME);
97             attachment.setDataHandler(getPictureForItem(item));
98             attachment.setContentType("images/jpeg");
99             resMsg.addAttachmentPart(attachment);
100
101             sArray[counter][0] = item.getTypeId().getName();
102             sArray[counter][1] = item.getName();
103             sArray[counter][2] = item.getLongDesc();
104             sArray[counter][3] = (item.getPrice()).toString();
105             sArray[counter][4] = item.getItemId().toString();
106             counter++;
107         }
108
109         return sArray;
110
111     }
112
113     private DataHandler getPictureForItem(ItemLocal item) throws Exception {
114         Collection pics = item.getPictures();
115         Iterator it = pics.iterator();
116         PictureLocal pic = null;
117
118         while (it.hasNext()) {
119             pic = (PictureLocal) it.next();
120             if (pic.getPriority() == 0) {
121                 break;
122             }
123         }
124
125         
126         DataHandler dh = new DataHandler(this.getClass().getResource(pic.getLocation()));
127
128         return dh;
129     }
130
131     public String[][] getSpecials(String type) throws Exception {
132         EJBHomeFactory factory = EJBHomeFactory.getInstance();
133         ItemLocalHome home = (ItemLocalHome) factory
134                 .getLocalHome(EJBHomeFactory.ITEM);
135         Collection specials = home.findByTypeAndSpecialOffer(type);
136         Iterator specialIt = specials.iterator();
137
138         String sArray[][] = new String[specials.size()][5];
139
140         // get the response message for this request
141
MessageContext msgContext = MessageContext.getCurrentContext();
142         Message resMsg = msgContext.getResponseMessage();
143
144         int counter = 0;
145
146         while (specialIt.hasNext()) {
147             ItemLocal item = (ItemLocal) specialIt.next();
148
149             // Create the attachment as a MIME attachment
150
AttachmentPart attachment = resMsg.createAttachmentPart();
151             resMsg.getAttachmentsImpl().setSendType(
152                     AttachmentsImpl.SEND_TYPE_MIME);
153             attachment.setDataHandler(getPictureForItem(item));
154             attachment.setContentType("images/jpeg");
155             resMsg.addAttachmentPart(attachment);
156
157             sArray[counter][0] = item.getTypeId().getName();
158             sArray[counter][1] = item.getName();
159             sArray[counter][2] = item.getLongDesc();
160             sArray[counter][3] = (item.getPrice()).toString();
161             sArray[counter][4] = item.getItemId().toString();
162             counter++;
163         }
164
165         return sArray;
166     }
167
168     public String[] getTypes() throws Exception {
169         EJBHomeFactory factory = EJBHomeFactory.getInstance();
170         TypeLocalHome home = (TypeLocalHome) factory
171                 .getLocalHome(EJBHomeFactory.TYPE);
172         Collection types = home.findAll();
173         Iterator typesIt = types.iterator();
174
175         String tArray[] = new String[types.size()];
176         int counter = 0;
177         while (typesIt.hasNext()) {
178             TypeLocal type = (TypeLocal) typesIt.next();
179             tArray[counter] = type.getName();
180             counter++;
181         }
182
183         return tArray;
184     }
185
186 }
187
Popular Tags