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)
027// import org.opengion.hayabusa.common.HybsSystem;                                                      // 6.9.3.0 (2018/03/26)6.9.3.0 (2018/03/26)
028import static org.opengion.fukurou.system.HybsConst.DB_FETCH_SIZE;              // 6.9.4.1 (2018/04/09)
029
030import org.jfree.data.gantt.TaskSeriesCollection;
031import org.jfree.data.gantt.TaskSeries;
032import org.jfree.data.gantt.Task;
033
034/**
035 * HybsTaskSeriesCollection は、org.jfree.data.gantt.TaskSeriesCollection を継承したサブクラスで、
036 * オブジェクト作成とともに JDBC接続して、TaskSeries データを作成し、セットします。
037 * TaskSeriesCollection は、IntervalCategoryDataset, GanttCategoryDataset インターフェースを継承しています。
038 *
039 * データ形式は、シリーズ名、タスク名、開始日時、終了日時 の順で、シリーズ名でソートしておく必要があります。
040 * シリーズ名 は、キーブレイクで、設定する為です。
041 *
042 *  select series,task,st(時間),ed(時間) from XX order by series,task,st(時間)
043 *
044 * @og.rev 5.6.1.0 (2013/02/01) 新規作成
045 *
046 * @version  0.9.0  2001/05/05
047 * @author   Kazuhiko Hasegawa
048 * @since    JDK1.1,
049 */
050public class HybsTaskSeriesCollection extends TaskSeriesCollection {
051        private static final long serialVersionUID = 561020130201L ;
052
053//      /** 6.9.3.0 (2018/03/26) データ検索時のフェッチサイズ  {@value} */
054//      private static final int DB_FETCH_SIZE = HybsSystem.sysInt( "DB_FETCH_SIZE" ) ;
055
056        /**
057         * デフォルトコンストラクター
058         *
059         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
060         */
061        public HybsTaskSeriesCollection() { super(); }          // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
062
063        /**
064         * HybsTaskSeriesCollection オブジェクトの内部に、DB検索結果のデータを設定します(縦持)。
065         *
066         * select series,task,st(時間),ed(時間) from XX order by series,task,st(時間)
067         * シリーズ名 は、キーブレイクで、設定します。
068         * (独自メソッド)
069         *
070         * @og.rev 6.0.4.0 (2014/11/28) ResultSetValue を使用するように変更。
071         * @og.rev 6.4.2.1 (2016/02/05) try-with-resources 文で記述。
072         * @og.rev 6.9.3.0 (2018/03/26) データ検索時のフェッチサイズを設定。
073         *
074         * @param conn  the connection.
075         * @param query  the query.
076         * @throws SQLException データベース実行エラーが発生した場合
077         *
078         */
079        public void executeQuery( final Connection conn, final String query ) throws SQLException {
080                // 6.4.2.1 (2016/02/05) try-with-resources 文
081                try( Statement statement = conn.createStatement();
082                         ResultSet resultSet = statement.executeQuery(query) ) {
083
084                        statement.setFetchSize( DB_FETCH_SIZE );                                // 6.9.3.0 (2018/03/26) データ検索時のフェッチサイズ
085
086                        // 6.0.4.0 (2014/11/28) ResultSetValue を使用するように変更。
087                        final ResultSetValue rsv = new ResultSetValue( resultSet );
088
089                        final int columnCount = rsv.getColumnCount();                                           // 6.0.4.0 (2014/11/28)
090
091                        if( columnCount < 4 ) {
092                                final String errMsg = "HybsTaskSeriesCollection.executeQuery() : 実行できません。\n"
093                                                        + "select series,task,st(時間),ed(時間) は、最低必要です。それ以降は無視します。"
094                                                        + " SQL=" + query ;
095                                throw new SQLException( errMsg );
096                        }
097
098                        String bkSeries = null;         // キーブレイクのための過去のSeries
099
100                        TaskSeries taskseries = null;
101                        while( rsv.next() ) {                                                                                   // 6.0.4.0 (2014/11/28)
102                                // first column contains the row key...
103                                final String seriVal    = rsv.getValue(0);                                                      // シリーズ名
104                                if( seriVal != null && !seriVal.equals( bkSeries ) ) {
105                                        if( taskseries != null ) { add( taskseries ); }                 // キーブレイクでTaskSeriesCollectionにセット
106                                        taskseries = new TaskSeries( seriVal );
107                                        bkSeries = seriVal ;
108                                }
109
110                                // 6.0.4.0 (2014/11/28) ResultSetValue を使用するように変更。
111                                final String taskVal            = rsv.getValue(1);                                      // タスク名
112                                final String stDataVal  = rsv.getValue(2);                                      // st(時間)
113                                final String edDateVal  = rsv.getValue(3);                                      // ed(時間)
114
115                                final Date stDate = HybsDateUtil.getCalendar( stDataVal ).getTime() ;
116                                final Date edDate = HybsDateUtil.getCalendar( edDateVal ).getTime() ;
117
118                                final Task task = new Task( taskVal, stDate, edDate );
119
120                                taskseries.add( task );
121                        }
122                        if( taskseries != null ) { add( taskseries ); }         // キーブレイクでTaskSeriesCollectionにセット
123                }
124        }
125}