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.plugin.view;
017
018import java.util.List;
019
020import org.opengion.fukurou.util.StringUtil;
021import org.opengion.hayabusa.common.HybsSystemException;
022import org.opengion.hayabusa.html.TableFormatter;
023
024/**
025 * JavaScript のツリー階層を持ったテーブル表示を行う、ツリーテーブル表示クラスです。
026 *
027 * AbstractViewForm により、setter/getterメソッドのデフォルト実装を提供しています。
028 * 各HTMLのタグに必要な setter/getterメソッドのみ,追加定義しています。
029 *
030 * AbstractViewForm を継承している為,ロケールに応じたラベルを出力させる事が出来ます。
031 *
032 * @og.group 画面表示
033 *
034 * @version  4.0
035 * @author   Hiroki Nakamura
036 * @since    JDK5.0,
037 */
038public class ViewForm_HTMLCustomTreeBOM extends ViewForm_HTMLTable  {
039        /** このプログラムのVERSION文字列を設定します。   {@value} */
040        private static final String VERSION = "7.3.0.0 (2021/01/06)" ;
041
042        private TableFormatter          headerFormat    ;
043        private TableFormatter[]        bodyFormats             ;
044        private int                                     bodyFormatsCount;
045
046        private static final int BODYFORMAT_MAX_COUNT = 10;
047
048        // 6.4.4.1 (2016/03/18) static final 定数化にします。
049        private static final String FUTTER = "initializeDocument()" + CR + "//-->" + CR + "</script>" + CR + "</table>" + CR ;
050
051        // 6.4.4.1 (2016/03/18) static final 定数化にします。
052        private static final String HEADER = "<table id=\"viewTable\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\"  summary=\"bomTable\">"
053                                                                                + CR + "<script type=\"text/javascript\">" + CR + "<!--" + CR + "aux0 = gFld('" ;
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 ViewForm_HTMLCustomTreeBOM() { super(); }                // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
061
062        /**
063         * DBTableModel から HTML文字列を作成して返します。
064         * startNo(表示開始位置)から、pageSize(表示件数)までのView文字列を作成します。
065         * 表示残りデータが pageSize 以下の場合は,残りのデータをすべて出力します。
066         *
067         * @og.rev 4.3.1.0 (2008/09/08) フォーマットが設定されていない場合のエラー追加
068         * @og.rev 6.2.0.0 (2015/02/27) フォーマット系の noDisplay 対応
069         * @og.rev 6.4.3.4 (2016/03/11) tdに、[カラム]が無いケースで、次の[カラム]のクラス属性が、前方すべてのtdにセットされてしまう対応。
070         * @og.rev 6.4.4.1 (2016/03/18) FUTTER を、static final 定数化にします。
071         * @og.rev 6.4.4.2 (2016/04/01) TableFormatterのタイプ別値取得処理の共通部をまとめる。
072         * @og.rev 6.4.5.0 (2016/04/08) メソッド変更( getColumnDbType(int) → getClassName(int) )
073         *
074         * @param  stNo     表示開始位置
075         * @param  pgSize   表示件数
076         *
077         * @return  DBTableModelから作成された HTML文字列
078         * @og.rtnNotNull
079         */
080        @Override
081        public String create( final int stNo, final int pgSize )  {
082                // このクラスでは、テーブル全データを使用します。
083                if( getRowCount() == 0 ) { return ""; } // 暫定処置
084
085                // 4.3.1.0 (2008/09/08)
086                if( headerFormat == null ) {
087                        final String errMsg = "ViewTagで canUseFormat() = true の場合、Formatter は必須です。";
088                        throw new HybsSystemException( errMsg );
089                }
090
091                final int startNo = 0;
092                final int pageSize = getRowCount();
093
094                final int lastNo = getLastNo( startNo, pageSize );
095
096                headerFormat.makeFormat( getDBTableModel() );
097                // 6.2.0.0 (2015/02/27) フォーマット系の noDisplay 対応
098                setFormatNoDisplay( headerFormat );
099
100                if( bodyFormatsCount == 0 ) {
101                        bodyFormats[0] = headerFormat ;
102                        bodyFormatsCount ++ ;
103                }
104                else {
105                        for( int i=0; i<bodyFormatsCount; i++ ) {
106                                bodyFormats[i].makeFormat( getDBTableModel() );
107                                // 6.2.0.0 (2015/02/27) フォーマット系の noDisplay 対応
108                                setFormatNoDisplay( bodyFormats[i] );
109                        }
110                }
111
112                final StringBuilder out = new StringBuilder( BUFFER_LARGE );
113                out.append( getHeader() );
114
115                int level;
116                // 6.3.9.1 (2015/11/27) Found 'DD'-anomaly for variable(PMD)
117                for( int row=startNo; row<lastNo; row++ ) {
118                        // カラム==0は、レベルを指定する。
119                        level = Integer.parseInt( getValueLabel(row,0) );
120                        final boolean isFld = row+1<lastNo && level < Integer.parseInt( getValueLabel(row+1,0) );
121                        out.append( getLevelScript( level,isFld ) );
122
123                        // 開始
124                        for( int i=0; i<bodyFormatsCount; i++ ) {
125                                final TableFormatter bodyFormat = bodyFormats[i];
126
127                                int cl = 0;
128                                for( ; cl<bodyFormat.getLocationSize(); cl++ ) {
129                                        // 6.3.9.1 (2015/11/27) Found 'DD'-anomaly for variable(PMD)
130                                        String fmt = bodyFormat.getFormat(cl);
131                                        final int loc = bodyFormat.getLocation(cl);
132                                        if( ! bodyFormat.isNoClass() && loc >= 0 ) {
133                                                // 6.4.3.4 (2016/03/11) tdに、[カラム]が無いケースで、次の[カラム]のクラス属性が、前方すべてのtdにセットされてしまう対応。
134                                                final int idx = fmt.lastIndexOf( "<td" );
135                                                if( idx >= 0 ) {        // matchしてるので、あるはず
136                                                        final String tdclass = " class=\"" + getClassName(loc) + "\" ";                 // 6.4.5.0 (2016/04/08)
137                                                        fmt = fmt.substring( 0,idx+3 ) + tdclass + fmt.substring( idx+3 ) ;
138                                                }
139                                        }
140                                        out.append( fmt );
141                                        if( loc >= 0 ) {
142                                                // 6.4.4.2 (2016/04/01) 処理の共通部をまとめる。
143                                                out.append( getTypeCaseValue( bodyFormat.getType(cl),row,loc ) );
144                                        }
145                                }
146                                out.append( StringUtil.replace( bodyFormat.getFormat(cl), "</tr>", "" ) );
147                        }
148                        // 終了
149
150                        out.append( "', '', 'gold')" );
151                        if( level != 0 ) {
152                                out.append( ')' );              // 6.0.2.5 (2014/10/31) char を append する。
153                        }
154                        out.append( CR );
155                }
156                out.append( FUTTER );                   // 6.4.4.1 (2016/03/18)
157
158                return out.toString();
159        }
160
161        /**
162         * DBTableModel から テーブルのヘッダータグ文字列を作成して返します。
163         * JavaScript の TreeBody では、JavaScriptに関連する定義もこのヘッダーに
164         * 含めます。
165         *
166         * @og.rev 6.4.4.1 (2016/03/18) HEADER を、static final 定数化にします。
167         * @og.rev 7.3.0.0 (2021/01/06) SpotBugs:null チェックなしで null 値を利用対策
168         *
169         * @return  テーブルのヘッダータグ文字列
170         * @og.rtnNotNull
171         */
172        @Override
173        protected String getHeader() {
174                // 7.3.0.0 (2021/01/06) SpotBugs:null チェックなしで null 値を利用対策
175                if( headerFormat == null ) {
176                        final String errMsg = "ViewTagで canUseFormat() = true の場合、Formatter は必須です。";
177                        throw new HybsSystemException( errMsg );
178                }
179
180                // 6.4.4.1 (2016/03/18) HEADER を、static final 定数化にします。
181                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE ).append( HEADER );
182
183                int cl = 0;
184                // 6.9.8.0 (2018/05/28) FindBugs:コンストラクタで初期化されていないフィールドを null チェックなしで null 値を利用している
185                // フレームワークとして、create メソッドからしか、呼ばれないため、nullチェック済みです。
186                for( ; cl<headerFormat.getLocationSize(); cl++ ) {
187                        buf.append( StringUtil.replace( headerFormat.getFormat(cl) ,"td","th" ));
188                        final int loc = headerFormat.getLocation(cl);
189                        if( loc >= 0 ) { buf.append( getColumnLabel(loc) ); }
190                        // ヘッダーフォーマット部では、何もしません。
191                }
192                buf.append( StringUtil.replace( StringUtil.replace( headerFormat.getFormat(cl) ,"td","th" ), "</tr>", "" ) )
193                        .append("', '', 'gold')")
194                        .append( CR );
195
196                return buf.toString();
197        }
198
199        /**
200         * 行のレベルに応じた JavaScript関数のヘッダー部分を返します。
201         *
202         * @og.rev 3.5.2.1 (2003/10/27) JavaScript 内のダブルコーテーションをシングルコーテーションに変更する。
203         *
204         * @param       lvl             ツリーのレベル
205         * @param       isFld   フォルダかどうか[true:フォルダ/false:最下層]
206         *
207         * @return  JavaScript関数のヘッダー部分
208         */
209        private String getLevelScript( final int lvl,final boolean isFld ) {
210
211                final String auxX = "\taux" + ( lvl );
212                final String auxY = "aux" + ( lvl-1 );
213
214                final String rtn ;
215                if( isFld ) {
216                        rtn = auxX + " = insFld(" + auxY + ", gFld('";
217                }
218                else {
219                        rtn = "\tinsFld(" + auxY + ", gLnk('CONTENTS','";
220                }
221
222                return rtn;
223        }
224
225        /**
226         * フォーマットを設定します。
227         *
228         * @param       list    TableFormatterのリスト
229         */
230        @Override
231        public void setFormatterList( final List<TableFormatter> list ) {               // 4.3.3.6 (2008/11/15) Generics警告対応
232                bodyFormats = new TableFormatter[BODYFORMAT_MAX_COUNT];
233
234                bodyFormatsCount = 0;
235                // 7.2.9.4 (2020/11/20) PMD:This for loop can be replaced by a foreach loop
236                for( final TableFormatter format : list ) {
237//              for( int i=0; i<list.size(); i++ ) {
238//                      final TableFormatter format = list.get( i );            // 4.3.3.6 (2008/11/15) Generics警告対応
239                        switch( format.getFormatType() ) {
240                        case TYPE_HEAD : headerFormat = format; break;
241                        case TYPE_BODY : bodyFormats[bodyFormatsCount++] = format; break;
242                        default : final String errMsg = "FormatterType の定義外の値が指定されました。";
243                        // 4.3.4.4 (2009/01/01)
244                                          throw new HybsSystemException( errMsg );
245                        }
246                }
247
248                if( headerFormat == null ) {
249                        final String errMsg = "og:thead タグの、フォーマットの指定は必須です。";
250                        throw new HybsSystemException( errMsg );
251                }
252        }
253
254        /**
255         * フォーマットメソッドを使用できるかどうかを問い合わせます。
256         *
257         * @return  使用可能(true)/ 使用不可能 (false)
258         */
259        @Override
260        public boolean canUseFormat() {
261                return true;
262        }
263
264        /**
265         * ビューで表示したカラムの一覧をCSV形式で返します。
266         *
267         * @og.rev 5.1.6.0 (2010/05/01) 新規追加
268         * @og.rev 6.2.0.1 (2015/03/06) TableFormatter#getLocation(int)の有効判定
269         * @og.rev 6.4.3.4 (2016/03/11) getViewClms(TableFormatter) を使用して表示されたカラム一覧を求めます。
270         *
271         * @return      ビューで表示したカラムの一覧
272         * @og.rtnNotNull
273         */
274        @Override
275        public String getViewClms() {
276                return getViewClms( headerFormat );
277        }
278}