001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.hayabusa.io;
017
018import java.sql.Connection;
019import java.sql.ResultSet;
020import java.sql.ResultSetMetaData;
021import java.sql.SQLException;
022import java.sql.Statement;
023
024import java.util.Date;
025
026import org.opengion.fukurou.util.Closer;
027import org.opengion.fukurou.util.HybsDateUtil;
028
029import org.jfree.data.gantt.TaskSeriesCollection;
030import org.jfree.data.gantt.TaskSeries;
031import org.jfree.data.gantt.Task;
032
033/**
034 * HybsTaskSeriesCollection は、org.jfree.data.gantt.TaskSeriesCollection を継承したサブクラスで、
035 * オブジェクト作成とともに JDBC接続して、TaskSeries データを作成し、セットします。
036 * TaskSeriesCollection は、IntervalCategoryDataset, GanttCategoryDataset インターフェースを継承しています。
037 *
038 * データ形式は、シリーズ名、タスク名、開始日時、終了日時 の順で、シリーズ名でソートしておく必要があります。
039 * シリーズ名 は、キーブレイクで、設定する為です。
040 * 
041 *  select series,task,st(時間),ed(時間) from XX order by series,task,st(時間)
042 *
043 * @og.rev 5.6.1.0 (2013/02/01) 新規作成
044 *
045 * @version  0.9.0  2001/05/05
046 * @author   Kazuhiko Hasegawa
047 * @since    JDK1.1,
048 */
049public class HybsTaskSeriesCollection extends TaskSeriesCollection {
050        private static final long serialVersionUID = 561020130201L ;
051
052        /**
053         * HybsTaskSeriesCollection オブジェクトの内部に、DB検索結果のデータを設定します(縦持)。
054         *
055         *  select series,task,st(時間),ed(時間) from XX order by series,task,st(時間)
056         * シリーズ名 は、キーブレイクで、設定します。
057         *
058         * @param con  the connection.
059         * @param query  the query.
060         * @throws SQLException データベース実行エラーが発生した場合
061         *
062         */
063        public void executeQuery( final Connection con, final String query ) throws SQLException {
064
065                Statement statement = null;
066                ResultSet resultSet = null;
067                try {
068                        statement = con.createStatement();
069                        resultSet = statement.executeQuery(query);
070                        ResultSetMetaData metaData = resultSet.getMetaData();
071
072                        int columnCount = metaData.getColumnCount();
073
074                        if(columnCount < 4) {
075                                String errMsg = "HybsTaskSeriesCollection.executeQuery() : 実行できません。\n"
076                                                        + "select series,task,st(時間),ed(時間) は、最低必要です。それ以降は無視します。"
077                                                        + " SQL=" + query ;
078                                throw new SQLException( errMsg );
079                        }
080
081                        String bkSeries = null;         // キーブレイクのための過去のSeries
082
083                        TaskSeries taskseries = null;
084                        while (resultSet.next()) {
085                                // first column contains the row key...
086                                String seriVal  = resultSet.getString(1);                                       // シリーズ名
087                                if( seriVal != null && !seriVal.equals( bkSeries ) ) {
088                                        if( taskseries != null ) { add( taskseries ); }                 // キーブレイクでTaskSeriesCollectionにセット
089                                        taskseries = new TaskSeries( seriVal );
090                                        bkSeries = seriVal ;
091                                }
092
093                                String taskVal          = resultSet.getString(2);                       // タスク名
094                                String stDataVal        = resultSet.getString(3);                       // st(時間)
095                                String edDateVal        = resultSet.getString(4);                       // ed(時間)
096
097                                Date stDate = HybsDateUtil.getCalendar( stDataVal ).getTime() ;
098                                Date edDate = HybsDateUtil.getCalendar( edDateVal ).getTime() ;
099
100                                Task task = new Task( taskVal, stDate, edDate );
101
102                                taskseries.add( task );
103                        }
104                        if( taskseries != null ) { add( taskseries ); }         // キーブレイクでTaskSeriesCollectionにセット
105                }
106                finally {
107                        Closer.resultClose( resultSet ) ;
108                        Closer.stmtClose( statement ) ;
109                }
110        }
111}