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.SQLException;
021import java.sql.Statement;
022
023import java.util.Date;
024
025import org.opengion.fukurou.util.HybsDateUtil;
026import org.opengion.fukurou.db.ResultSetValue;          // 6.0.4.0 (2014/11/28)
027import org.opengion.hayabusa.common.HybsSystem;         // 6.9.3.0 (2018/03/26)6.9.3.0 (2018/03/26)
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        /** 6.9.3.0 (2018/03/26) データ検索時のフェッチサイズ  {@value} */
053        private static final int DB_FETCH_SIZE = HybsSystem.sysInt( "DB_FETCH_SIZE" ) ;
054
055        /**
056         * デフォルトコンストラクター
057         *
058         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
059         */
060        public HybsTaskSeriesCollection() { super(); }          // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
061
062        /**
063         * HybsTaskSeriesCollection オブジェクトの内部に、DB検索結果のデータを設定します(縦持)。
064         *
065         * select series,task,st(時間),ed(時間) from XX order by series,task,st(時間)
066         * シリーズ名 は、キーブレイクで、設定します。
067         * (独自メソッド)
068         *
069         * @og.rev 6.0.4.0 (2014/11/28) ResultSetValue を使用するように変更。
070         * @og.rev 6.4.2.1 (2016/02/05) try-with-resources 文で記述。
071         * @og.rev 6.9.3.0 (2018/03/26) データ検索時のフェッチサイズを設定。
072         *
073         * @param conn  the connection.
074         * @param query  the query.
075         * @throws SQLException データベース実行エラーが発生した場合
076         *
077         */
078        public void executeQuery( final Connection conn, final String query ) throws SQLException {
079                // 6.4.2.1 (2016/02/05) try-with-resources 文
080                try( final Statement statement = conn.createStatement();
081                        final ResultSet resultSet = statement.executeQuery(query) ) {
082
083                        statement.setFetchSize( DB_FETCH_SIZE );                                // 6.9.3.0 (2018/03/26) データ検索時のフェッチサイズ
084
085                        // 6.0.4.0 (2014/11/28) ResultSetValue を使用するように変更。
086                        final ResultSetValue rsv = new ResultSetValue( resultSet );
087
088                        final int columnCount = rsv.getColumnCount();                                           // 6.0.4.0 (2014/11/28)
089
090                        if( columnCount < 4 ) {
091                                final String errMsg = "HybsTaskSeriesCollection.executeQuery() : 実行できません。\n"
092                                                        + "select series,task,st(時間),ed(時間) は、最低必要です。それ以降は無視します。"
093                                                        + " SQL=" + query ;
094                                throw new SQLException( errMsg );
095                        }
096
097                        String bkSeries = null;         // キーブレイクのための過去のSeries
098
099                        TaskSeries taskseries = null;
100                        while( rsv.next() ) {                                                                                   // 6.0.4.0 (2014/11/28)
101                                // first column contains the row key...
102                                final String seriVal    = rsv.getValue(0);                                                      // シリーズ名
103                                if( seriVal != null && !seriVal.equals( bkSeries ) ) {
104                                        if( taskseries != null ) { add( taskseries ); }                 // キーブレイクでTaskSeriesCollectionにセット
105                                        taskseries = new TaskSeries( seriVal );
106                                        bkSeries = seriVal ;
107                                }
108
109                                // 6.0.4.0 (2014/11/28) ResultSetValue を使用するように変更。
110                                final String taskVal            = rsv.getValue(1);                                      // タスク名
111                                final String stDataVal  = rsv.getValue(2);                                      // st(時間)
112                                final String edDateVal  = rsv.getValue(3);                                      // ed(時間)
113
114                                final Date stDate = HybsDateUtil.getCalendar( stDataVal ).getTime() ;
115                                final Date edDate = HybsDateUtil.getCalendar( edDateVal ).getTime() ;
116
117                                final Task task = new Task( taskVal, stDate, edDate );
118
119                                taskseries.add( task );
120                        }
121                        if( taskseries != null ) { add( taskseries ); }         // キーブレイクでTaskSeriesCollectionにセット
122                }
123        }
124}