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.OTDividend;
023    import org.otfeed.event.DividendPropertyEnum;
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    import java.util.Date;
030    import java.util.Set;
031    
032    
033    /**
034     * Request to receive dividend info.
035     * 
036     * @see OTDividend
037     */
038    public final class DividendRequest extends AbstractSessionRequest {
039    
040            private final String exchangeCode;
041            private final String symbolCode;
042            private final Date   startDate;
043            private final Date   endDate;
044    
045            private final IDataDelegate<OTDividend> dataDelegate;
046    
047            public String getExchangeCode()  { return exchangeCode; }
048            public String getSymbolCode()    { return symbolCode; }
049    
050            public DividendRequest(int requestId, 
051                            String exchangeCode,
052                            String symbolCode,
053                            Date   startDate,
054                            Date   endDate,
055                            IDataDelegate<OTDividend> dataDelegate,
056                            ICompletionDelegate completionDelegate) {
057    
058                    super(CommandEnum.REQUEST_DIVIDENDS, 
059                                    requestId,
060                                    completionDelegate);
061    
062                    Check.notNull(exchangeCode,  "exchangeCode");
063                    Check.notNull(symbolCode,  "symbolCode");
064                    Check.notNull(startDate, "startDate");
065                    Check.notNull(endDate, "endDate");
066                    Check.notNull(dataDelegate,  "dataDelegate");
067                    
068                    this.exchangeCode  = exchangeCode;
069                    this.symbolCode    = symbolCode;
070                    this.startDate     = startDate;
071                    this.endDate       = endDate;
072    
073                    this.dataDelegate  = dataDelegate;
074            }
075    
076            @Override
077            public void writeRequest(ByteBuffer out) {
078                    super.writeRequest(out);
079    
080                    Util.writeString(out, exchangeCode, 15);
081                    Util.writeString(out, symbolCode, 15);
082                    Util.writeDate(out, startDate);
083                    Util.writeDate(out, endDate);
084            }
085            
086            @Override
087            public JobStatus handleMessage(Header header, ByteBuffer in) {
088                    super.handleMessage(header, in);
089    
090                    int typeCode = in.get();
091                    if(typeCode == DataEnum.EOD.code) {
092                            return JobStatus.FINISHED;
093                    }
094    
095                    if(typeCode != DataEnum.DIVIDEND.code) {
096                            throw new ProtocolException("unrecognized type: " + typeCode, in);
097                    }
098    
099                    double price         = in.getDouble();
100                    Date declarationDate = Util.readDate(in);
101                    Date executionDate   = Util.readDate(in);
102                    Date recordDate      = Util.readDate(in);
103                    Date paymentDate     = Util.readDate(in);
104                    Set<DividendPropertyEnum> props = Util.readDividendProperrtyEnumSet(in);
105    
106                    OTDividend divident = new OTDividend(
107                            price,
108                            declarationDate,
109                            executionDate,
110                            recordDate,
111                            paymentDate,
112                            props);
113    
114                    dataDelegate.onData(divident);
115    
116                    return JobStatus.FINISHED;
117            }
118    }