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.taglib;
017
018import org.opengion.fukurou.util.ErrMsg;
019import org.opengion.fukurou.util.ErrorMessage;
020import org.opengion.fukurou.util.StringUtil;                            // 6.9.2.1 (2018/03/12)
021import org.opengion.hayabusa.html.ViewForm;
022import org.opengion.hayabusa.html.ViewFormFactory;
023import org.opengion.hayabusa.resource.ResourceManager;
024import org.opengion.hayabusa.db.DBTableModel;
025import org.opengion.hayabusa.db.DBTableModelUtil;
026import org.opengion.hayabusa.db.DBColumn;
027
028import java.util.Arrays;
029import java.util.Comparator;
030
031/**
032 *  Taglib で共通的に使用される ユーティリティクラスです。
033 *
034 * 全てのメソッドは、 パッケージプライベートなstatic 宣言されています。
035 *
036 * @og.rev 3.1.1.0 (2003/03/28) 新規作成
037 * @og.group その他部品
038 *
039 * @version  4.0
040 * @author       Kazuhiko Hasegawa
041 * @since    JDK5.0,
042 */
043final class TaglibUtil  {
044        /* default */ static final String VIEW_ID = "HYBS_VIEW" ;               // 3.5.6.4 (2004/07/16)
045        /* default */ static final String LINK_ID = "HYBS_LINK" ;               // 3.5.6.4 (2004/07/16)
046        /* default */ static final String MARK_ID = "HYBS_MARK" ;               // 3.5.6.4 (2004/07/16)
047
048        private static int tagId        ;
049
050        // 4.3.2.0 (2008/09/11) 行番号順ソートのための、Comparator オブジェクト
051        private static final Comparator<? super ErrMsg> ERR_COMP =              // 6.4.1.1 (2016/01/16) errComp  → ERR_COMP  refactoring
052                new Comparator<ErrMsg>() {
053                        /**
054                         * 順序付けのために2つの引数を比較します。
055                         *
056                         * Comparator インタフェースの実装です。
057                         *
058                         * @param       s1      比較対象の最初のErrMsg
059                         * @param       s2      比較対象の2番目のErrMsg
060                         * @return      最初の引数が2番目の引数より小さい場合は負の整数、両方が等しい場合は0、最初の引数が2番目の引数より大きい場合は正の整数。
061                         */
062                        @Override
063                        public int compare( final ErrMsg err1,final ErrMsg err2 ) {
064                                return err1.getNo() - err2.getNo() ;
065                        }
066                } ;
067
068        /**
069         *      デフォルトコンストラクターをprivateにして、
070         *      オブジェクトの生成をさせないようにする。
071         *
072         */
073        private TaglibUtil() {}
074
075        /**
076         * チェックボックスのラベルを関連つけるための id 値を返します。
077         *
078         * InputTag と OrderByTag 全体で、呼び出される都度に連番をつけて返します。
079         * ユニークな番号になる様にする事が目的なので、99999 で、0 にクリアします。
080         * 別スレッドによる同時アクセスで整合性が破綻(同じ番号が返る)しても、
081         * 問題ありません。(単独のスレッド内でユニークになればよい。)
082         *
083         * @return  ユニークな id 値
084         */
085        /* default */ static String getTagId() {
086                final String id = "ID" + ( tagId++ );
087
088                if( tagId >= 100000 ) { tagId = 0; }
089                return id ;
090        }
091
092        /**
093         * ErrorMessage オブジェクトと、ResourceManager より、表示用 HTMLテーブルを作成します。
094         * これは、従来,各Taglibパッケージで使用していました、ErrorMessage オブジェクトを、
095         * HTMLテーブル形式で表示する為に、DBUtilクラスや、ViewFormクラスなど、複数のクラスを
096         * 複雑に組み合わせて使用していましたが、一つの static メソッドにまとめたものです。
097         *
098         * @og.rev 3.5.5.2 (2004/04/02) 新規追加
099         * @og.rev 3.5.6.1 (2004/06/25) lang 言語コード 属性を削除します。
100         * @og.rev 3.5.6.2 (2004/07/05) setBgColorCycle の引数定義を変更
101         * @og.rev 3.8.0.4 (2005/08/08) setUseScrollBar の追加(ヘッダー固定のスクロールバーを出さない)
102         * @og.rev 3.8.9.5 (2007/09/12) PG名、ステップ名追加(開発ガイドライン対応)
103         * @og.rev 4.0.0.0 (2005/01/31) ResourceManager getMessage 関数を使用
104         * @og.rev 4.0.0.0 (2007/10/18) メッセージリソース統合( getResource().getMessage ⇒ getResource().getLabel )
105         * @og.rev 4.3.2.0 (2008/09/11) 行番号順ソート、メッセージのラベル(短)化、コメント出力
106         * @og.rev 4.3.4.5 (2009/01/08) 対応方法(概要説明)が存在すれば表示する
107         * @og.rev 4.3.7.6 (2009/07/15) 対応方法のパラメータ対応
108         * @og.rev 6.0.2.5 (2014/10/31) エラーメッセージに、preタグを付けます。
109         * @og.rev 6.2.2.0 (2015/03/27) interface に変更タイプ と、書込み許可を追加
110         * @og.rev 6.4.5.2 (2016/05/06) Exception: の後ろに改行を入れます(クラス名が長いと、メッセージが見えにくいため)。
111         * @og.rev 6.8.1.1 (2017/07/22) makeNthChild 処理時に、自身のテーブルと区別するために、viewClass を追加します。
112         * @og.rev 6.9.2.1 (2018/03/12) エラーメッセージのタグ情報を、パースします。
113         * @og.rev 6.9.3.1 (2018/04/02) エラーメッセージのタグ情報のパースは、JasperException 以降から、改行2つまでの間のみとする。
114         *
115         * @param   errMsg  ErrorMessageオブジェクト
116         * @param   resource リソースマネージャー
117         *
118         * @return  HTMLのテーブルタグに変換後の文字列(無ければ、ゼロストリング)
119         * @og.rtnNotNull
120         */
121        /* default */ static String makeHTMLErrorTable( final ErrorMessage errMsg,final ResourceManager resource ) {
122                if( errMsg == null ) { return ""; }
123
124                final String[] names ;
125                if( errMsg.isSetPgStep() ) {
126                        // names = new String[] { "ROW_NO","KEKKA","MSGID","MESSAGE","PG","STEP" }; // 3.8.9.5 (2007/09/12)
127                        names = new String[] { "ROW_NO","KEKKA","MSGID","MESSAGE", "PG","STEP" }; // 3.8.9.5 (2007/09/12)
128                }
129                else {
130                        names = new String[] { "ROW_NO","KEKKA","MSGID","MESSAGE" };
131                }
132
133                final int clmSize = names.length;
134
135                final DBTableModel errTable = DBTableModelUtil.newDBTable();
136                errTable.init( clmSize );
137
138                for( int i=0; i<clmSize; i++ ) {
139                        final DBColumn dbColumn = resource.makeDBColumn( names[i] );
140                        errTable.setDBColumn( i,dbColumn );
141                }
142
143                // 4.3.2.0 (2008/09/11) 行番号順ソート
144                final ErrMsg[] errMsgs = errMsg.toArray();
145                Arrays.sort( errMsgs, ERR_COMP );
146
147                for( int i=0; i<errMsg.size(); i++ ) {
148                        final ErrMsg err = errMsgs[i];
149                        String[] clmVals = new String[clmSize];
150                        clmVals[0] = String.valueOf( err.getNo() );
151                        clmVals[1] = String.valueOf( err.getKekka() );
152                        clmVals[2] = err.getId();
153
154                        // 6.9.3.1 (2018/04/02) エラーメッセージのタグ情報のパースは、JasperException 以降から、改行2つまでの間のみとする。
155                        String tmpVals3 = resource.getLabel( err );
156                        if( tmpVals3 != null ) {
157                                tmpVals3 = tmpVals3.trim();
158                                if( tmpVals3.contains( "Exception:" ) ) {
159                                        tmpVals3 = tmpVals3.replaceAll( "Exception: " , "Exception:\n" );
160                                }
161                                final int idx0 = tmpVals3.indexOf( "JasperException" );         // JSP画面のエラー
162                                if( idx0 >= 0 ) {
163                                        final int idx1 = tmpVals3.indexOf( "\n\n",idx0 );       // 改行2つ
164                                        if( idx1 >= 0 ) {
165                                                tmpVals3 = tmpVals3.substring( 0,idx0 ) +
166                                                                        StringUtil.htmlFilter( tmpVals3.substring( idx0,idx1 ) ) +
167                                                                        tmpVals3.substring( idx1 );
168                                        }
169                                        else {
170                                                tmpVals3 = tmpVals3.substring( 0,idx0 ) +
171                                                                        StringUtil.htmlFilter( tmpVals3.substring( idx0 ) );
172                                        }
173                                }
174                                clmVals[3] = "<pre>" + tmpVals3 + "</pre>";
175                        }
176
177//                      // 6.0.2.5 (2014/10/31) エラーメッセージに、preタグを付けます。
178//                      clmVals[3] = "<pre>" + resource.getLabel( err ) + "</pre>";
179//      //              clmVals[3] = "<pre>" + StringUtil.htmlFilter( resource.getLabel( err ) ) + "</pre>";            // 6.9.2.1 (2018/03/12)
180//
181//                      // 6.4.5.2 (2016/05/06) Exception: の後ろに改行を入れます(クラス名が長いと、メッセージが見えにくいため)。
182//                      if( clmVals[3].contains( "Exception:" ) ) {
183//                              clmVals[3] = clmVals[3].replaceAll( "Exception: " , "Exception:\n" );
184//                      }
185
186                        if( errMsg.isSetPgStep() ) {
187                                clmVals[4] = err.getPg();
188                                clmVals[5] = err.getStep();
189                        }
190                        errTable.addColumnValues( clmVals , null , false );             // 6.2.2.0 (2015/03/27)
191
192                        // 対応方法(概要説明)が存在すれば直下に一行追加する 4.3.4.5 (2009/01/08)
193                        // String desc = resource.getDescription( clmVals[2] );
194                        final String desc = resource.getDescription( err );             // 4.3.7.6 (2009/07/15)
195                        if( desc != null && desc.length() > 0 ){
196                                String[] descVals = new String[clmSize];
197                                descVals[3] = desc;
198                                errTable.addColumnValues( descVals , null , false );    // 6.2.2.0 (2015/03/27)
199                        }
200                }
201
202                // ここまでが、DBTableModel の作成。以下は、View での表示
203
204                final ViewForm form = ViewFormFactory.newInstance( "HTMLTable" );
205
206                form.init( errTable );
207                if( ! errMsg.isOK() )  {                                                                // 正常以外
208                        form.setViewClass( "errorView" );                                       // 6.4.6.1 (2016/06/03)
209                        form.setBgColorCycle( -1 * errMsg.getKekka() );         // エラー処理時のゼブラ模様
210                        form.setNumberType( "delete" );                                         // 番号欄を出さない。
211                        form.setUseTableSorter( false );                                        // ソート用リンクは出さない。
212                        form.setUseScrollBar( false );                                          // 4.0.0 (2005/01/31) スクロールバー用のDIV要素を出力しない。
213                }
214
215                return form.create();
216        }
217
218        /**
219         * ErrorMessage オブジェクトと、ResourceManager より、表示用 HTMLテーブルを作成します。
220         * このメソッドでは、エラーメッセージ及びエラーコードを一覧形式で表示します。
221         *
222         * @og.rev 6.2.2.0 (2015/03/27) interface に変更タイプ と、書込み許可を追加
223         *
224         * @param   errMsg  ErrorMessageオブジェクト
225         * @param   resource リソースマネージャー
226         *
227         * @return  HTMLのテーブルタグに変換後の文字列(無ければ、ゼロストリング)
228         * @og.rtnNotNull
229         */
230        /* default */ static String makeHTMLSimpleErrorList( final ErrorMessage errMsg,final ResourceManager resource ) {
231                if( errMsg == null ) { return ""; }
232
233                final String[] names = new String[] { "MESSAGE" };
234
235                final int clmSize = names.length;
236
237                final DBTableModel errTable = DBTableModelUtil.newDBTable();
238                errTable.init( clmSize );
239
240                for( int i=0; i<clmSize; i++ ) {
241                        final DBColumn dbColumn = resource.makeDBColumn( names[i] );
242                        errTable.setDBColumn( i,dbColumn );
243                }
244
245                final ErrMsg[] errMsgs = errMsg.toArray();
246                Arrays.sort( errMsgs, ERR_COMP );
247
248                for( int i=0; i<errMsg.size(); i++ ) {
249                        final ErrMsg err = errMsgs[i];
250                        String[] clmVals = new String[clmSize];
251                        clmVals[0] = resource.getShortLabel( err.getId() ) + "(" + err.getId() + ")";
252                        errTable.addColumnValues( clmVals , null , false );             // 6.2.2.0 (2015/03/27)
253                }
254
255                // ここまでが、DBTableModel の作成。以下は、View での表示
256                final ViewForm form = ViewFormFactory.newInstance( "HTMLSimpleList" );
257                form.init( errTable );
258
259                if( ! errMsg.isOK() )  {        // 正常以外
260                        form.setBgColorCycle( -1 * errMsg.getKekka() ); // エラー処理時のゼブラ模様
261                }
262
263                return form.create();
264        }
265}