001: /*
002: * Created on Dec 10, 2004
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package olstore.session.helper;
008:
009: import java.util.Collection;
010: import java.util.Iterator;
011:
012: import olstore.entity.ItemLocal;
013: import olstore.entity.ItemLocalHome;
014: import olstore.entity.TypeLocalHome;
015: import olstore.entity.TypeLocal;
016: import olstore.framework.EJBHomeFactory;
017: import olstore.entity.PictureLocal;
018:
019: import javax.ejb.SessionBean;
020: import javax.ejb.SessionContext;
021: import javax.ejb.CreateException;
022: import javax.activation.DataHandler;
023:
024: import javax.xml.soap.AttachmentPart;
025:
026: import org.apache.axis.MessageContext;
027:
028: import org.apache.axis.attachments.AttachmentsImpl;
029: import org.apache.axis.Message;
030:
031: /**
032: * @author mwringe
033: *
034: * @ejb.bean name="ItemSpecialHelper" type="Stateless"
035: * view-type="remote-service-endpoint" jndi-name="ItemSpecialRemote"
036: *
037: * @ejb.transaction type="Required"
038: *
039: * -- This is need for JOnAS. If you are not using JOnAS you can safetly remove
040: * the tags below.
041: *
042: * @jonas.bean ejb-name="ItemSpecialHelper" jndi-name="ItemSpecialRemote"
043: * --
044: *
045: */
046: public class ItemSpecialHelperBean implements SessionBean {
047:
048: private SessionContext ejbContext;
049:
050: public void setSessionContext(SessionContext ctx) {
051: ejbContext = ctx;
052: }
053:
054: public void ejbRemove() {
055: }
056:
057: public void ejbCreate() throws CreateException {
058: }
059:
060: public void ejbPassivate() {
061: }
062:
063: public void ejbActivate() {
064: }
065:
066: /**
067: *
068: * @return All items on special (name, description and price)
069: * @throws Exception
070: *
071: * @ejb.interface-method view-type="remote-service-endpoint"
072: * generate="service-endpoint"
073: * service-endpoint-extends="java.rmi.Remote"
074: *
075: */
076: public String[][] allSpecials() throws Exception {
077: EJBHomeFactory factory = EJBHomeFactory.getInstance();
078: ItemLocalHome home = (ItemLocalHome) factory
079: .getLocalHome(EJBHomeFactory.ITEM);
080: Collection specials = home.findBySpecialOffer();
081: Iterator specialIt = specials.iterator();
082:
083: String sArray[][] = new String[specials.size()][5];
084:
085: // get the response message for this request
086: MessageContext msgContext = MessageContext.getCurrentContext();
087: Message resMsg = msgContext.getResponseMessage();
088:
089: int counter = 0;
090: while (specialIt.hasNext()) {
091: ItemLocal item = (ItemLocal) specialIt.next();
092:
093: // Create the attachment as a MIME attachment
094: AttachmentPart attachment = resMsg.createAttachmentPart();
095: resMsg.getAttachmentsImpl().setSendType(
096: AttachmentsImpl.SEND_TYPE_MIME);
097: attachment.setDataHandler(getPictureForItem(item));
098: attachment.setContentType("images/jpeg");
099: 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)
114: throws Exception {
115: Collection pics = item.getPictures();
116: Iterator it = pics.iterator();
117: PictureLocal pic = null;
118:
119: while (it.hasNext()) {
120: pic = (PictureLocal) it.next();
121: if (pic.getPriority() == 0) {
122: break;
123: }
124: }
125:
126: DataHandler dh = new DataHandler(this .getClass().getResource(
127: pic.getLocation()));
128:
129: return dh;
130: }
131:
132: public String[][] getSpecials(String type) throws Exception {
133: EJBHomeFactory factory = EJBHomeFactory.getInstance();
134: ItemLocalHome home = (ItemLocalHome) factory
135: .getLocalHome(EJBHomeFactory.ITEM);
136: Collection specials = home.findByTypeAndSpecialOffer(type);
137: Iterator specialIt = specials.iterator();
138:
139: String sArray[][] = new String[specials.size()][5];
140:
141: // get the response message for this request
142: MessageContext msgContext = MessageContext.getCurrentContext();
143: Message resMsg = msgContext.getResponseMessage();
144:
145: int counter = 0;
146:
147: while (specialIt.hasNext()) {
148: ItemLocal item = (ItemLocal) specialIt.next();
149:
150: // Create the attachment as a MIME attachment
151: AttachmentPart attachment = resMsg.createAttachmentPart();
152: resMsg.getAttachmentsImpl().setSendType(
153: AttachmentsImpl.SEND_TYPE_MIME);
154: attachment.setDataHandler(getPictureForItem(item));
155: attachment.setContentType("images/jpeg");
156: resMsg.addAttachmentPart(attachment);
157:
158: sArray[counter][0] = item.getTypeId().getName();
159: sArray[counter][1] = item.getName();
160: sArray[counter][2] = item.getLongDesc();
161: sArray[counter][3] = (item.getPrice()).toString();
162: sArray[counter][4] = item.getItemId().toString();
163: counter++;
164: }
165:
166: return sArray;
167: }
168:
169: public String[] getTypes() throws Exception {
170: EJBHomeFactory factory = EJBHomeFactory.getInstance();
171: TypeLocalHome home = (TypeLocalHome) factory
172: .getLocalHome(EJBHomeFactory.TYPE);
173: Collection types = home.findAll();
174: Iterator typesIt = types.iterator();
175:
176: String tArray[] = new String[types.size()];
177: int counter = 0;
178: while (typesIt.hasNext()) {
179: TypeLocal type = (TypeLocal) typesIt.next();
180: tArray[counter] = type.getName();
181: counter++;
182: }
183:
184: return tArray;
185: }
186:
187: }
|