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 = "6.4.5.0 (2016/04/08)" ;
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 border=\"0\" cellspacing=\"2\" cellpadding=\"0\"  summary=\"bomTable\" id=\"viewTable\">"
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         *
168         * @return  テーブルのヘッダータグ文字列
169         * @og.rtnNotNull
170         */
171        @Override
172        protected String getHeader() {
173                // 6.4.4.1 (2016/03/18) HEADER を、static final 定数化にします。
174                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE ).append( HEADER );
175
176                int cl = 0;
177                // 6.9.8.0 (2018/05/28) FindBugs:コンストラクタで初期化されていないフィールドを null チェックなしで null 値を利用している
178                // フレームワークとして、create メソッドからしか、呼ばれないため、nullチェック済みです。
179                for( ; cl<headerFormat.getLocationSize(); cl++ ) {
180                        buf.append( StringUtil.replace( headerFormat.getFormat(cl) ,"td","th" ));
181                        final int loc = headerFormat.getLocation(cl);
182                        if( loc >= 0 ) { buf.append( getColumnLabel(loc) ); }
183                        // ヘッダーフォーマット部では、何もしません。
184                }
185                buf.append( StringUtil.replace( StringUtil.replace( headerFormat.getFormat(cl) ,"td","th" ), "</tr>", "" ) )
186                        .append("', '', 'gold')")
187                        .append( CR );
188
189                return buf.toString();
190        }
191
192        /**
193         * 行のレベルに応じた JavaScript関数のヘッダー部分を返します。
194         *
195         * @og.rev 3.5.2.1 (2003/10/27) JavaScript 内のダブルコーテーションをシングルコーテーションに変更する。
196         *
197         * @param       lvl             ツリーのレベル
198         * @param       isFld   フォルダかどうか[true:フォルダ/false:最下層]
199         *
200         * @return  JavaScript関数のヘッダー部分
201         */
202        private String getLevelScript( final int lvl,final boolean isFld ) {
203
204                final String auxX = "\taux" + ( lvl );
205                final String auxY = "aux" + ( lvl-1 );
206
207                final String rtn ;
208                if( isFld ) {
209                        rtn = auxX + " = insFld(" + auxY + ", gFld('";
210                }
211                else {
212                        rtn = "\tinsFld(" + auxY + ", gLnk('CONTENTS','";
213                }
214
215                return rtn;
216        }
217
218        /**
219         * フォーマットを設定します。
220         *
221         * @param       list    TableFormatterのリスト
222         */
223        @Override
224        public void setFormatterList( final List<TableFormatter> list ) {               // 4.3.3.6 (2008/11/15) Generics警告対応
225                bodyFormats = new TableFormatter[BODYFORMAT_MAX_COUNT];
226
227                bodyFormatsCount = 0;
228                for( int i=0; i<list.size(); i++ ) {
229                        final TableFormatter format = list.get( i );            // 4.3.3.6 (2008/11/15) Generics警告対応
230                        switch( format.getFormatType() ) {
231                        case TYPE_HEAD : headerFormat = format; break;
232                        case TYPE_BODY : bodyFormats[bodyFormatsCount++] = format; break;
233                        default : final String errMsg = "FormatterType の定義外の値が指定されました。";
234                        // 4.3.4.4 (2009/01/01)
235                                          throw new HybsSystemException( errMsg );
236                        }
237                }
238
239                if( headerFormat == null ) {
240                        final String errMsg = "og:thead タグの、フォーマットの指定は必須です。";
241                        throw new HybsSystemException( errMsg );
242                }
243        }
244
245        /**
246         * フォーマットメソッドを使用できるかどうかを問い合わせます。
247         *
248         * @return  使用可能(true)/ 使用不可能 (false)
249         */
250        @Override
251        public boolean canUseFormat() {
252                return true;
253        }
254
255        /**
256         * ビューで表示したカラムの一覧をCSV形式で返します。
257         *
258         * @og.rev 5.1.6.0 (2010/05/01) 新規追加
259         * @og.rev 6.2.0.1 (2015/03/06) TableFormatter#getLocation(int)の有効判定
260         * @og.rev 6.4.3.4 (2016/03/11) getViewClms(TableFormatter) を使用して表示されたカラム一覧を求めます。
261         *
262         * @return      ビューで表示したカラムの一覧
263         * @og.rtnNotNull
264         */
265        @Override
266        public String getViewClms() {
267                return getViewClms( headerFormat );
268        }
269}