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