001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     */
017    
018    package org.otfeed.protocol.request;
019    
020    import org.otfeed.event.ICompletionDelegate;
021    import org.otfeed.event.IDataDelegate;
022    import org.otfeed.event.InstrumentEnum;
023    import org.otfeed.event.OTEquityInit;
024    import org.otfeed.protocol.CommandEnum;
025    import org.otfeed.protocol.DataEnum;
026    import org.otfeed.protocol.ProtocolException;
027    
028    import java.nio.ByteBuffer;
029    
030    /**
031     * request to receive equity info.
032     */
033    public final class EquityInitRequest extends AbstractSessionRequest {
034    
035            private final String exchangeCode;
036            private final String symbolCode;
037    
038            private final IDataDelegate<OTEquityInit> dataDelegate;
039    
040            public String getExchangeCode()  { return exchangeCode; }
041            public String getSymbolCode()    { return symbolCode; }
042    
043            public EquityInitRequest(int requestId, 
044                            String exchangeCode,
045                            String symbolCode,
046                            IDataDelegate<OTEquityInit> dataDelegate,
047                            ICompletionDelegate completionDelegate) {
048    
049                    super(CommandEnum.REQUEST_EQUITY_INIT, 
050                                    requestId, completionDelegate);
051    
052                    Check.notNull(exchangeCode,  "exchangeCode");
053                    Check.notNull(symbolCode,  "symbolCode");
054                    Check.notNull(dataDelegate,  "dataDelegate");
055    
056                    this.exchangeCode  = exchangeCode;
057                    this.symbolCode    = symbolCode;
058    
059                    this.dataDelegate  = dataDelegate;
060            }
061    
062            @Override
063            public void writeRequest(ByteBuffer out) {
064                    super.writeRequest(out);
065    
066                    Util.writeString(out, exchangeCode, 15);
067                    Util.writeString(out, symbolCode, 15);
068            }
069    
070            @Override
071            public JobStatus handleMessage(Header header, ByteBuffer in) {
072                    super.handleMessage(header, in);
073    
074                    int typeCode = in.get();
075                    if(typeCode != DataEnum.EQUITY_INIT.code) {
076                            throw new ProtocolException("unrecognized type: " + typeCode, in);
077                    }
078    
079                    String curency         = Util.readString(in, 3);
080                    InstrumentEnum type    = Util.readInstrumentEnum(in);
081                    String company         = Util.readString(in, 80);
082                    double prevClosePrice  = in.getDouble();
083                    String prevCloseDate   = Util.readString(in, 8);
084                    double annualHighPrice = in.getDouble();
085                    String annualHighDate  = Util.readString(in, 8);
086                    double annualLowPrice  = in.getDouble();
087                    String annualLowDate   = Util.readString(in, 8);
088                    double earningsPrice   = in.getDouble();
089                    String earningsDate    = Util.readString(in, 8);
090                    long totalShares       = in.getLong();
091                    long averageVolume     = in.getLong();
092                    String CUSIP           = Util.readString(in, 9);
093                    String ISIN            = Util.readString(in, 12);
094                    boolean isUPC11830     = Util.readBoolean(in);
095                    boolean isSmallCap     = Util.readBoolean(in);
096                    boolean isTestlssue    = Util.readBoolean(in);
097    
098                    OTEquityInit equity = new OTEquityInit(
099                            curency, 
100                            type, 
101                            company,
102                            prevClosePrice, 
103                            prevCloseDate, 
104                            annualHighPrice, 
105                            annualHighDate,
106                            annualLowPrice, 
107                            annualLowDate, 
108                            earningsPrice, 
109                            earningsDate,
110                            totalShares, 
111                            averageVolume, 
112                            CUSIP, 
113                            ISIN, 
114                            isUPC11830,
115                            isSmallCap,
116                            isTestlssue);
117    
118                    dataDelegate.onData(equity);
119    
120                    return JobStatus.FINISHED;
121            }
122    }