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;
019import java.util.regex.Pattern;                                                                 // 6.4.3.3 (2016/03/04)
020
021import org.opengion.fukurou.system.OgBuilder ;                                  // 6.4.4.2 (2016/04/01)
022import org.opengion.fukurou.util.StringUtil;
023import org.opengion.hayabusa.common.HybsSystemException;
024import org.opengion.hayabusa.html.TableFormatter;
025
026/**
027 * ヘッダー部分のフォーマットに応じたテーブルを自動作成する、フォーマットテーブル作成クラスです。
028 *
029 * AbstractViewForm により、setter/getterメソッドのデフォルト実装を提供しています。
030 * 各HTMLのタグに必要な setter/getterメソッドのみ、追加定義しています。
031 * [XXXX]は、カラムを指定します。ラベル+入力フィールドをそれぞれtdで囲います。
032 * [#XXXX]は、対応するカラムのラベルを出力します。
033 * [$XXXX]は、対応するカラムのレンデラーを出力します。
034 * [!XXXX]は、対応するカラムの値を出力します。
035 * 特殊記号の解釈は、HTMLFormatTextField系とHTMLFormatTable系で異なりますので
036 * ご注意ください。
037 *
038 * AbstractViewForm を継承している為、ロケールに応じたラベルを出力させる事が出来ます。
039 *
040 * @og.group 画面表示
041 *
042 * @version  4.0
043 * @author   Kazuhiko Hasegawa
044 * @since    JDK5.0,
045 */
046public class ViewForm_HTMLFormatTable extends ViewForm_HTMLTable  {
047        /** このプログラムのVERSION文字列を設定します。   {@value} */
048        private static final String VERSION = "7.3.0.0 (2021/01/06)" ;
049
050        // 6.4.3.3 (2016/03/04) class属性(ColumnのDBType)置換で、td属性に[カラム]があると、誤ってそちらがセットされてしまう対応。
051        // ※ 正規表現の「.」は、改行を除く任意の文字なので、改行を含みあい場合は、「[\\s\\S]」とします。
052        private static final Pattern TD_BODY = Pattern.compile( "[\\s\\S]*<td[^>]*>[\\s]*" );           // 6.4.4.2 (2016/04/01) tdタグが完了して、BODY部分の解析に入る。
053        private static final Pattern TD_END  = Pattern.compile( "[^<]*>[^<>]*" );                                       // 何らかのタグが完了して、BODY部分の解析に入る。
054
055        // 3.5.4.0 (2003/11/25) TableFormatter クラス追加
056        private TableFormatter format   ;
057
058        /**
059         * デフォルトコンストラクター
060         *
061         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
062         */
063        public ViewForm_HTMLFormatTable() { super(); }          // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
064
065        /**
066         * DBTableModel から HTML文字列を作成して返します。
067         * startNo(表示開始位置)から、pageSize(表示件数)までのView文字列を作成します。
068         * 表示残りデータが pageSize 以下の場合は、残りのデータをすべて出力します。
069         *
070         * @og.rev 3.5.0.0 (2003/09/17) BODY要素の noClass 属性を追加。
071         * @og.rev 3.5.0.0 (2003/09/17) &lt;tr&gt;属性は、元のフォーマットのまま使用します。
072         * @og.rev 3.5.2.0 (2003/10/20) ヘッダー繰り返し属性( headerSkipCount )を採用
073         * @og.rev 3.5.3.1 (2003/10/31) skip属性を採用。headerLine のキャッシュクリア
074         * @og.rev 3.5.4.0 (2003/11/25) TableFormatter クラスを使用するように変更。
075         * @og.rev 3.5.5.0 (2004/03/12) systemFormat(例:[KEY.カラム名]形式等)の対応
076         * @og.rev 3.5.5.0 (2004/03/12) No 欄そのものの作成判断ロジックを追加
077         * @og.rev 3.5.5.7 (2004/05/10) [#カラム名] , [$カラム名] に対応
078         * @og.rev 3.5.6.0 (2004/06/18) '!' 値のみ 追加 既存の '$' は、レンデラー
079         * @og.rev 3.5.6.4 (2004/07/16) ヘッダーとボディー部をJavaScriptで分離
080         * @og.rev 3.7.0.3 (2005/03/01) getBgColorCycleClass に、選択行マーカーを採用
081         * @og.rev 4.0.0.0 (2005/01/31) 新規作成(getColumnClassName ⇒ getColumnDbType)
082         * @og.rev 4.3.1.0 (2008/09/08) フォーマットが設定されていない場合のエラー追加・編集行のみを表示する属性(isSkipNoEdit)追加
083         * @og.rev 4.3.3.0 (2008/10/01) noTransition属性対応
084         * @og.rev 4.3.7.4 (2009/07/01) tbodyタグの入れ子を解消(FireFox対応)
085         * @og.rev 6.2.0.0 (2015/02/27) フォーマット系の noDisplay 対応
086         * @og.rev 6.4.3.3 (2016/03/04) class属性(ColumnのDBType)置換で、td属性に[カラム]があると、誤ってそちらがセットされてしまう対応。
087         * @og.rev 6.4.3.4 (2016/03/11) tdに、[カラム]が無いケースで、次の[カラム]のクラス属性が、前方すべてのtdにセットされてしまう対応。
088         * @og.rev 6.4.4.2 (2016/04/01) TableFormatterのタイプ別値取得処理の共通部をまとめる。
089         * @og.rev 6.4.5.0 (2016/04/08) メソッド変更( getColumnDbType(int) → getClassName(int) )
090         * @og.rev 6.8.1.1 (2017/07/22) ckboxTD変数は、&lt;td&gt; から &lt;td に変更します(タグの最後が記述されていない状態でもらう)。
091         * @og.rev 6.8.1.1 (2017/07/22) 行番号のtdに、個別に class="S9" を追加するための useS9 変数を追加。
092         *
093         * @param  startNo    表示開始位置
094         * @param  pageSize   表示件数
095         *
096         * @return  DBTableModelから作成された HTML文字列
097         * @og.rtnNotNull
098         */
099        @Override
100        public String create( final int startNo, final int pageSize )  {
101                if( getRowCount() == 0 ) { return ""; } // 暫定処置
102
103                // 4.3.1.0 (2008/09/08)
104                if( format == null ) {
105                        final String errMsg = "ViewTagで canUseFormat() = true の場合、Formatter は必須です。";
106                        throw new HybsSystemException( errMsg );
107                }
108                // 6.2.0.0 (2015/02/27) フォーマット系の noDisplay 対応
109                // ※ makeFormat の直後に setFormatNoDisplay を実行する順番を他のクラスと合わせます。
110                // 元の場所では、setColumnDisplay のタイミングが後なので、うまく動きません
111                format.makeFormat( getDBTableModel() );         // 6.2.0.0 (2015/02/27) #setFormatterList( List<TableFormatter> ) から移動
112                setFormatNoDisplay( format );
113
114                headerLine       = null;                // 3.5.3.1 (2003/10/31) キャッシュクリア
115                final int lastNo = getLastNo( startNo, pageSize );
116                final int blc = getBackLinkCount();
117                final int hsc = getHeaderSkipCount();           // 3.5.2.0 (2003/10/20)
118                int hscCnt = 1;                                         // 3.5.2.0 (2003/10/20)
119
120                final StringBuilder out = new StringBuilder( BUFFER_LARGE )
121                        .append( getCountForm( startNo,pageSize ) )
122                        .append( getHeader() );
123
124                final String ckboxTD = "<td " + format.getRowspan();                            // 6.8.1.1 (2017/07/22)
125                int bgClrCnt = 0;
126                for( int row=startNo; row<lastNo; row++ ) {
127                        if( isSkip( row ) || isSkipNoEdit( row ) ) { continue; }                // 4.3.1.0 (2008/09/08)
128                        if( ! format.isUse( row,getDBTableModel() ) ) { continue; }             // 3.5.4.0 (2003/11/25)
129                        out.append("<tbody").append( getBgColorCycleClass( bgClrCnt++,row ) );
130                        if( isNoTransition() ) {        // 4.3.3.0 (2008/10/01)
131                                out.append( getHiddenRowValue( row ) );
132                        }
133                        out.append('>')                 // 6.0.2.5 (2014/10/31) char を append する。
134                                .append( format.getTrTag() );
135
136                        // 3.5.5.0 (2004/03/12) No 欄そのものの作成判断追加
137                        if( isNumberDisplay() ) {
138                                out.append( makeCheckbox( ckboxTD,row,blc,true ) );                     // 6.8.1.1 (2017/07/22) 行番号のtdに、個別に class="S9" を追加
139                        }
140
141                        int cl = 0;
142                        boolean isTdBody = false;                                                                               // 6.4.3.3 (2016/03/04) tdBody処理を行ってよいかどうかのフラグ
143                        for( ; cl<format.getLocationSize(); cl++ ) {
144                                String fmt = format.getFormat(cl);                              // 3.5.0.0 (2003/09/17)
145                                final int loc = format.getLocation(cl);                 // 3.5.5.0 (2004/03/12)
146                                if( ! format.isNoClass() && loc >= 0 ) {                // 3.5.5.7 (2004/05/10)
147                                        if( isTdBody && TD_END.matcher( fmt ).matches() ) {             // 6.4.3.3 (2016/03/04) tdBody許可があり、TDが閉じた場合。
148                                                isTdBody = false;
149                                                final int idx = out.lastIndexOf( "<td" );
150                                                if( idx >= 0 ) {
151                                                        final String tdclass = " class=\"" + getClassName(loc) + "\" ";                 // 6.4.5.0 (2016/04/08)
152                                                        out.insert( idx+3, tdclass );                                   // +3 は、"<td" の後ろという意味。fmtでなくそれ以前に戻る必要がある。
153                                                }
154                                        }
155                                        if( TD_BODY.matcher( fmt ).matches() ) {                                // 6.4.3.3 (2016/03/04) TDが閉じた場合。
156                                                // 6.4.3.4 (2016/03/11) tdに、[カラム]が無いケースで、次の[カラム]のクラス属性が、前方すべてのtdにセットされてしまう対応。
157                                                final int idx = fmt.lastIndexOf( "<td" );
158                                                if( idx >= 0 ) {        // matchしてるので、あるはず
159                                                        final String tdclass = " class=\"" + getClassName(loc) + "\" ";                 // 6.4.5.0 (2016/04/08)
160                                                        fmt = fmt.substring( 0,idx+3 ) + tdclass + fmt.substring( idx+3 ) ;
161                                                }
162                                                isTdBody = false;                                                                       // 6.4.3.3 (2016/03/04) これは、要らない・・・はず。
163                                        }
164                                        else {
165                                                isTdBody = true;                                                                        // TDが閉じていない。
166                                        }
167                                }
168                                out.append( fmt );                      // 3.5.0.0
169
170                                // 3.5.5.7 (2004/05/10) #,$ 対応
171                                if( loc >= 0 ) {
172                                        // 6.4.4.2 (2016/04/01) 処理の共通部をまとめる。
173                                        out.append( getTypeCaseValue( format.getType(cl),row,loc ) );
174                                }
175                                else {
176                                        out.append( format.getSystemFormat(row,loc) );
177                                }
178                        }
179                        out.append( format.getFormat(cl) )
180                                .append("</tbody>").append( CR );
181
182                // 3.5.2.0 (2003/10/20) ヘッダー繰り返し属性( headerSkipCount )を採用
183                        if( hsc > 0 && hscCnt % hsc == 0 ) {
184                                out.append("<tbody class=\"row_h\" >")
185                                        .append( getHeadLine() )
186                                        .append("</tbody>");
187                                hscCnt = 1;
188                        }
189                        else {
190                                hscCnt ++ ;
191                        }
192                }
193                out.append("</table>").append( CR )
194                        .append( getScrollBarEndDiv() );        // 3.8.0.3 (2005/07/15)
195
196                return out.toString();
197        }
198
199        /**
200         * 内容をクリア(初期化)します。
201         *
202         * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。
203         * @og.rev 3.5.0.0 (2003/09/17) Noカラムに、表示を全て消せるように、class 属性を追加。
204         * @og.rev 3.5.4.0 (2003/11/25) TableFormatter クラスを使用するように変更。
205         *
206         */
207        @Override
208        public void clear() {
209                super.clear();
210                format          = null;
211        }
212
213        /**
214         * DBTableModel から テーブルのタグ文字列を作成して返します。
215         *
216         * @og.rev 3.2.4.0 (2003/06/12) makeFormat() する位置を移動。
217         * @og.rev 3.5.0.0 (2003/09/17) &lt;tr&gt;属性は、元のフォーマットのまま使用します。
218         * @og.rev 3.5.1.0 (2003/10/03) Noカラムに、numberType 属性を追加
219         * @og.rev 3.5.2.0 (2003/10/20) ヘッダー繰り返し部をgetHeadLine()へ移動
220         * @og.rev 3.5.3.1 (2003/10/31) VERCHAR2 を VARCHAR2 に修正。
221         * @og.rev 3.5.5.0 (2004/03/12) No 欄そのものの作成判断ロジックを追加
222         * @og.rev 3.5.6.5 (2004/08/09) thead に、id="header" を追加
223         * @og.rev 4.0.0.0 (2005/01/31) DBColumn の 属性(CLS_NM)から、DBTYPEに変更
224         * @og.rev 5.9.1.2 (2015/10/23) 自己終了警告対応
225         * @og.rev 6.4.4.1 (2016/03/18) NUMBER_DISPLAYを、static final 定数化します。
226         * @og.rev 6.4.4.2 (2016/04/01) StringBuilderの代わりに、OgBuilderを使用する。
227         * @og.rev 6.4.9.0 (2016/07/23) colgroupのHTML5対応(No欄)
228         * @og.rev 6.4.9.1 (2016/08/05) colgroupのHTML5対応(No欄)時の対応ミス修正
229         * @og.rev 6.8.1.0 (2017/07/14) HTML5対応ヘッダー出力設定時に、ブラウザを互換設定したときの対応。
230         * @og.rev 6.8.2.0 (2017/10/13) makeNthChildの廃止と、makeCheckboxで、個別にclass指定するように変更。
231         * @og.rev 7.0.4.0 (2019/05/31) colgroup 廃止
232         *
233         * @return  テーブルのタグ文字列
234         * @og.rtnNotNull
235         */
236        @Override
237        protected String getTableHead() {
238                // 6.4.9.0 (2016/07/23) colgroupのHTML5対応(No欄)
239                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
240
241//              // 7.0.4.0 (2019/05/31) colgroup 廃止
242//              if( isNumberDisplay() ) {
243//                      // 6.4.9.0 (2016/07/23) colgroupのHTML5対応(No欄)
244//                              buf.append( NUMBER_DISPLAY );           // 6.8.1.0 (2017/07/14) HTML5ネイティブ時でも、出力します。
245//                      // 6.8.1.0 (2017/07/14) HTML5対応ヘッダー出力設定時に、ブラウザを互換設定したときの対応。
246//                      // 6.8.2.0 (2017/10/13) makeNthChildの廃止と、makeCheckboxで、個別にclass指定するように変更。
247//      //              if( !useIE7Header ) {
248//      //                      buf.append( "<style type=\"text/css\">" ).append( CR );
249//      //                      makeNthChild( buf,2,"BIT" );
250//      //                      makeNthChild( buf,3,"S9"  );
251//      //                      buf.append( "</style>" ).append( CR );          // 6.4.9.1 (2016/08/05)
252//      //              }
253//              }
254
255                // 6.4.9.0 (2016/07/23) colgroupのHTML5対応(No欄)の関係で、修正します。
256                return new OgBuilder()
257                                        .appendIf( isNumberDisplay() , buf.toString() )
258                                        .appendCR( "<thead id=\"header\">" )                            // 3.5.6.5 (2004/08/09)
259                                        .append(   getHeadLine() )
260                                        .appendCR( "</thead>" )
261                                        .toString();
262        }
263
264        /**
265         * ヘッダー繰り返し部を、getTableHead()メソッドから分離。
266         *
267         * @og.rev 6.1.2.0 (2015/01/24) キャッシュを返すのを、#getHeadLine() に移動。
268         * @og.rev 7.3.0.0 (2021/01/06) SpotBugs:null チェックなしで null 値を利用対策
269         *
270         * @return      テーブルのタグ文字列
271         * @og.rtnNotNull
272         */
273        @Override
274        protected String getHeadLine() {
275                // 7.3.0.0 (2021/01/06) SpotBugs:null チェックなしで null 値を利用対策
276                if( format == null ) {
277                        final String errMsg = "ViewTagで canUseFormat() = true の場合、Formatter は必須です。";
278                        throw new HybsSystemException( errMsg );
279                }
280
281                // 6.9.8.0 (2018/05/28) FindBugs:コンストラクタで初期化されていないフィールドを null チェックなしで null 値を利用している
282                // フレームワークとして、create メソッドからしか、呼ばれないため、nullチェック済みです。
283                if( headerLine == null ) {                                      // キャッシュになければ、設定する。
284                        headerLine = getHeadLine( "<th" + format.getRowspan() ) ;
285                }
286
287                return headerLine ;
288        }
289
290        /**
291         * ヘッダー繰り返し部を、getTableHead()メソッドから分離。
292         *
293         * @og.rev 3.5.2.0 (2003/10/20) 新規作成
294         * @og.rev 3.5.4.0 (2003/11/25) TableFormatter クラスを使用するように変更。
295         * @og.rev 3.5.4.3 (2004/01/05) useCheckControl 属性の機能を追加
296         * @og.rev 3.5.4.6 (2004/01/30) numberType="none" 時の処理を追加(Noラベルを出さない)
297         * @og.rev 3.5.4.7 (2004/02/06) ヘッダーにソート機能用のリンクを追加します。
298         * @og.rev 3.5.5.0 (2004/03/12) systemFormat(例:[KEY.カラム名]形式等)の対応
299         * @og.rev 3.5.5.0 (2004/03/12) No 欄そのものの作成判断ロジックを追加
300         * @og.rev 3.7.0.1 (2005/01/31) 全件チェックコントロール処理変更
301         * @og.rev 6.1.2.0 (2015/01/24) キャッシュを返すのを、#getHeadLine() に移動。
302         * @og.rev 6.4.3.4 (2016/03/11) ヘッダーでもTableFormatterのType(#,$,!)に対応した値を出すようにする。
303         * @og.rev 6.4.4.2 (2016/04/01) TableFormatterのタイプ別値取得処理の共通部をまとめる。
304         * @og.rev 7.3.0.0 (2021/01/06) SpotBugs:null チェックなしで null 値を利用対策
305         *
306         * @param       thTag タグの文字列
307         *
308         * @return      テーブルのタグ文字列
309         * @og.rtnNotNull
310         */
311        @Override
312        protected String getHeadLine( final String thTag ) {
313                // 7.3.0.0 (2021/01/06) SpotBugs:null チェックなしで null 値を利用対策
314                if( format == null ) {
315                        final String errMsg = "ViewTagで canUseFormat() = true の場合、Formatter は必須です。";
316                        throw new HybsSystemException( errMsg );
317                }
318
319                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE )
320                        .append( format.getTrTag() ).append( CR );
321
322                // 3.5.5.0 (2004/03/12) No 欄そのものの作成判断追加
323                if( isNumberDisplay() ) {
324                        // 6.1.2.0 (2015/01/24) thTag に、headerFormat.getRowspan() を含ませて受け取る。
325                        // 3.5.4.3 (2004/01/05) 追加分
326                        if( isUseCheckControl() && "checkbox".equals( getSelectedType() ) ) {
327                                buf.append( thTag ).append( "></th>" )
328                                        .append( thTag ).append( '>' ).append( getAllCheckControl() ).append( "</th>" )
329                                        .append( thTag ).append( '>' ).append( getNumberHeader()    ).append( "</th>" );
330                        }
331                        else {
332                                buf.append( thTag ).append(" colspan=\"3\">").append( getNumberHeader() ).append( "</th>" );    // 6.0.2.5 (2014/10/31) char を append する。
333                        }
334                }
335
336                int cl = 0;
337                // 6.9.8.0 (2018/05/28) FindBugs:コンストラクタで初期化されていないフィールドを null チェックなしで null 値を利用している
338                // フレームワークとして、create メソッドからしか、呼ばれないため、nullチェック済みです。
339                for( ; cl<format.getLocationSize(); cl++ ) {
340                        buf.append( StringUtil.replace( format.getFormat(cl) ,"td","th" ));
341                        final int loc = format.getLocation(cl);
342                        // 6.4.3.4 (2016/03/11) ヘッダーでもTableFormatterのType(#,$,!)に対応した値を出すようにする。
343                        if( loc >= 0 ) {
344                                // 6.4.4.2 (2016/04/01) 処理の共通部をまとめる。
345                                buf.append( getTypeCaseValue( format.getType(cl),-1,loc ) );
346                        }
347                }
348                buf.append( StringUtil.replace( format.getFormat(cl) ,"td","th" ) ).append( CR );
349
350                return buf.toString();                          // 6.1.2.0 (2015/01/24)
351        }
352
353        /**
354         * フォーマットを設定します。
355         *
356         * @og.rev 3.5.4.0 (2003/11/25) 新規作成
357         * @og.rev 6.2.0.0 (2015/02/27) フォーマット系の noDisplay 対応。ロジックを共通にするため、処理を移動
358         *
359         * @param       list    TableFormatterのリスト
360         */
361        @Override
362        public void setFormatterList( final List<TableFormatter> list ) {               // 4.3.3.6 (2008/11/15) Generics警告対応
363                format = list.get(0);           // 4.3.3.6 (2008/11/15) Generics警告対応
364                // 6.2.0.0 (2015/02/27) フォーマット系の noDisplay 対応。ロジックを共通にするため、処理を移動
365        }
366
367        /**
368         * フォーマットメソッドを使用できるかどうかを問い合わせます。
369         *
370         * @return  使用可能(true)/ 使用不可能 (false)
371         */
372        @Override
373        public boolean canUseFormat() {
374                return true;
375        }
376
377        /**
378         * ビューで表示したカラムの一覧をCSV形式で返します。
379         *
380         * @og.rev 5.1.6.0 (2010/05/01) 新規追加
381         * @og.rev 6.2.0.1 (2015/03/06) TableFormatter#getLocation(int)の有効判定
382         * @og.rev 6.4.3.4 (2016/03/11) getViewClms(TableFormatter) を使用して表示されたカラム一覧を求めます。
383         *
384         * @return      ビューで表示したカラムの一覧
385         * @og.rtnNotNull
386         */
387        @Override
388        public String getViewClms() {
389                return getViewClms( format );
390        }
391}