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.html;
017
018import org.opengion.hayabusa.db.DBTableModel;
019import org.opengion.hayabusa.db.DBColumn;
020import org.opengion.fukurou.util.StringUtil;
021import org.opengion.fukurou.util.Attributes;
022import org.opengion.fukurou.model.Formatter;
023import static org.opengion.fukurou.system.HybsConst.BUFFER_LARGE;               // 6.1.0.0 (2014/12/26) refactoring
024
025import java.util.concurrent.ConcurrentMap;                                                              // 6.4.3.3 (2016/03/04)
026import java.util.concurrent.ConcurrentHashMap;                                                  // 6.4.3.1 (2016/02/12) refactoring
027import java.util.List;
028import java.util.ArrayList;
029import java.util.Arrays ;
030
031/**
032 * ViewMarker インターフェース の実装オブジェクトです。
033 * これを,共通のスーパークラスとして 各種表示フォーム(例:HTML表示等)に使います。
034 *
035 * このクラスは、setter/getterメソッドのデフォルト実装を提供しています。
036 * 各種表示フォームに対応したサブクラス上で, create() をオーバーライドして下さい。
037 *
038 * @og.group 画面表示
039 *
040 * @version  4.0
041 * @author   Kazuhiko Hasegawa
042 * @since    JDK5.0,
043 */
044public class ViewMarker_MARKER implements ViewMarker {
045
046        private static final int        MARK_NULL   = -1;       // マーカー未設定
047        private static final int        MARK_TRUE   = 1;        // マーカー作成
048        private static final int        MARK_FALSE  = 0;        // マーカー作成せず
049
050        private List<Attributes>                markData        ;               // 4.0.0 (2005/08/31)
051        /** 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。  */
052        private final ConcurrentMap<Integer,Formatter>  formMap         = new ConcurrentHashMap<>();    // 6.4.3.1 (2016/02/12)
053        private DBTableModel            table                   ;
054        private int[]                           markCmlNo               ;
055        private int[]                           isMark                  ;
056        // 3.5.2.0 (2003/10/20)
057        private String[]                        markKey                 ;
058        private String[]                        markLists               ;
059        private String[]                        instrVals               ;               // 3.8.8.1 (2007/01/06)
060        private int[]                           markListNo              ;
061        private boolean[]                       useFmtDeco              ;               // 5.6.3.0 (2013/04/01) [$XXXX],[#XXXX]機能を有効にするかどうか(true:有効)
062
063        /** 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。  */
064        private final ConcurrentMap<Integer,List<Integer>>      clmMap  = new ConcurrentHashMap<>();    // 6.4.3.1 (2016/02/12)
065
066        /**
067         * デフォルトコンストラクター
068         *
069         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
070         */
071        public ViewMarker_MARKER() { super(); }         // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
072
073        /**
074         * 内容をクリア(初期化)します。
075         *
076         * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。
077         * @og.rev 3.5.2.0 (2003/10/20) markLists,markListNo,markKey属性を追加
078         * @og.rev 3.5.6.1 (2004/06/25) formMap属性を追加
079         * @og.rev 3.8.8.1 (2007/01/06) instrVals属性を追加
080         * @og.rev 5.6.3.0 (2013/04/01) useFmtDeco属性を追加
081         * @og.rev 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。
082         *
083         */
084        public void clear() {
085                markData        = null;         // 4.0.0 (2005/08/31)
086                formMap.clear();                // 6.4.3.3 (2016/03/04)
087                table           = null;
088                isMark          = null;
089                markKey         = null;
090                markLists       = null;
091                instrVals       = null;         // 3.8.8.1 (2007/01/06)
092                markListNo      = null;
093                clmMap.clear();                 // 6.4.3.3 (2016/03/04)
094                useFmtDeco      = null;         // 5.6.3.0 (2013/04/01) [$XXXX],[#XXXX]機能を有効にするかどうか(true:有効)
095        }
096
097        /**
098         * カラムに対するマーカーアトリビュートをセットします。
099         *
100         * @og.rev 3.1.0.0 (2003/03/20) Hashtable を使用している箇所で、非同期でも構わない箇所を、HashMap に置換え。
101         * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。
102         *
103         * @param       attri   アトリビュート
104         */
105        public void addAttribute( final Attributes attri ) {
106                if( markData == null ) { markData = new ArrayList<>(); }
107                markData.add( attri );
108        }
109
110        /**
111         * このマーカーが、初期化されているかどうかを判定します。
112         *
113         * 使用できる状態の場合は、true , 初期化が出来ていない場合は、false を返します。
114         *
115         * @og.rev 6.7.2.0 (2017/01/16) caseKey,caseVal等で未使用のときの対応。
116         *
117         * @return      初期化状況 [true:初期化済み/false:未初期化]
118         */
119        public boolean isUsable() {
120//              return markData != null && markData.size() > 0 ;                // 本当は、ゼロということは無いはず。
121                return markData != null && !markData.isEmpty() ;                // 本当は、ゼロということは無いはず。    // 6.9.7.0 (2018/05/14) PMD
122        }
123
124        /**
125         * 内部に DBTableModel をセットします。
126         *
127         * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。
128         * @og.rev 3.5.2.0 (2003/10/20) markLists,markListNo,markKey属性を追加
129         * @og.rev 3.5.6.1 (2004/06/25) DBTableModel の再設定に対応。
130         * @og.rev 3.8.8.1 (2007/01/06) instrVals属性を追加
131         * @og.rev 5.6.3.0 (2013/04/01) useFmtDeco属性を追加
132         * @og.rev 6.4.3.3 (2016/03/04) Map#computeIfAbsent で対応する。
133         * @og.rev 6.4.3.4 (2016/03/11) Formatterに新しいコンストラクターを追加する。
134         * @og.rev 6.7.6.0 (2017/03/17) strictCheck 追加。
135         *
136         * @param  tbl DBTableModelオブジェクト
137         */
138        public void setDBTableModel( final DBTableModel tbl ) {
139                table = tbl;
140                final int count = markData.size();                      // 4.0.0 (2005/08/31)
141
142                isMark          = new int[count];
143                markKey         = new String[count];
144                markCmlNo       = new int[count];
145                markLists       = new String[count];
146                instrVals       = new String[count];
147                markListNo      = new int[count];
148                useFmtDeco      = new boolean[count];   // 5.6.3.0 (2013/04/01) [$XXXX],[#XXXX]機能を有効にするかどうか(true:有効)
149
150                Arrays.fill( isMark,MARK_FALSE );       // マーカーの表示可否
151                Arrays.fill( markCmlNo,-1 );            // マーカーの可否を判断するカラム番号
152                Arrays.fill( useFmtDeco,false );        // [$XXXX],[#XXXX]機能を無効にする。(互換性のため)
153
154                for( int intKey=0; intKey<count; intKey++ ) {
155                        final Attributes attri = markData.get( intKey );
156
157                        final String column = attri.get( "column" );
158
159                        // 6.7.6.0 (2017/03/17) カラムのDBTableModel存在チェック。初期値が true なので、attri に無い場合も、true になる。
160                        final String strChk = attri.get( "strictCheck" );
161                        final boolean strictCheck = ! "false".equalsIgnoreCase( strChk );
162
163                        // 6.4.3.1 (2016/02/12) ConcurrentMap 系は、key,val ともに not null 制限です。
164                        final int clm = table.getColumnNo( column,strictCheck );        // 6.7.6.0 (2017/03/17) strictCheck で、true の場合は、clm番号が見つからないときは、Exception発生
165
166                        if( clm < 0 ) { continue; }             // 6.7.6.0 (2017/03/17) 存在しない場合、以下の処理を行わない。= clmMap に、カラムが登録されない。
167
168                        // 6.4.3.3 (2016/03/04) Map#computeIfAbsent で対応する。
169                        // Map#computeIfAbsent : 戻り値は、既存の、または計算された値。追加有り、置換なし、削除なし
170                        clmMap.computeIfAbsent( clm,k -> new ArrayList<>() ).add( intKey );
171
172                        final String body = attri.get( "body" );
173                        final Formatter formatter = new Formatter( table,body );                // 6.4.3.4 (2016/03/11)
174                        // 6.4.3.1 (2016/02/12) ConcurrentMap 系は、key,val ともに not null 制限です。
175                        formMap.put( intKey, formatter );
176
177                        makeOnMarkFormat( intKey,attri );
178
179                        useFmtDeco[intKey] = "true".equalsIgnoreCase( attri.get( "useFormatDeco" ) );   // 5.6.3.0 (2013/04/01)
180                }
181        }
182
183        /**
184         * 指定の行列に対するマーカー文字列を返します。
185         * この値は,すでにマーカー文字列処理されている為, RendererValue で
186         * 変換する必要はありません。
187         * 引数の value はそのカラムの値として利用されます。この値は、修飾済みの
188         * 値を与えることが可能です。
189         *
190         * @og.rev 3.5.6.1 (2004/06/25) formMap属性を使用します。
191         * @og.rev 3.8.8.1 (2007/01/06) instrVals属性を追加
192         * @og.rev 5.3.9.0 (2011/09/01) カラム名の先頭に'$'を付加した場合に、URLEncodeされた値を返すように対応
193         * @og.rev 5.6.3.0 (2013/04/01) useFmtDeco属性を追加([$XXXX],[#XXXX]機能を有効にするかどうか)
194         * @og.rev 6.2.4.0 (2015/05/15) useFmtDeco属性とは無関係に、[!XXXX](値)を対応します。
195         * @og.rev 6.8.3.1 (2017/12/01) [$XXXX param] で、RendererValueのパラメータを動的に変更できます。
196         *
197         * @param   row 指定の行
198         * @param   clm 指定の列
199         * @param   value カラムの値(マーカー文字列処理済)
200         *
201         * @return  row行,colum列 のマーカー文字列
202         */
203        public String getMarkerString( final int row,final int clm,final String value ) {
204                final int intKey = isOnMark(row,clm) ;
205                if( intKey < 0 ) { return value; }
206
207                final Formatter formatter = formMap.get( intKey );
208                final int[]    clmNo  = formatter.getClmNos();
209                final String[] format = formatter.getFormat();
210                final char[]   types  = formatter.getType();
211
212                final StringBuilder buf = new StringBuilder( BUFFER_LARGE );
213                int j=0;
214                String val ;
215                for( ; j<clmNo.length; j++ ) {
216                        // 6.2.4.0 (2015/05/15) [!XXXX](値)を対応
217                        if( clm == clmNo[j] && types[j] != '!' ) {
218                                val = value;                                                            // 一致する場合の valueは、通常レンデラー
219                        }
220                        else {
221                                val = formatter.getValue(row,clmNo[j]);         // 生の値
222                        }
223
224                        // 5.6.3.0 (2013/04/01) useFmtDeco属性を追加(trueの場合は、[$XXXX],[#XXXX]機能を有効にする)
225                        if( useFmtDeco[intKey] ) {
226                                final DBColumn dbClm = table.getDBColumn( clmNo[j] );
227                                final String   prm   = formatter.getClmParam(j);        // 6.8.3.1 (2017/12/01)
228                                if( types[j] == '$' ) {                 // レンデラー
229                                        // 6.8.3.1 (2017/12/01) [$XXXX param] で、RendererValueのパラメータを動的に変更できます。
230                                        val = formatter.getValue(row,clmNo[j]);                 // ※ 互換性の関係で、useFmtDecoで、'$' のときのみ、生の値を再取得します。
231                                        val = prm == null || prm.isEmpty()
232                                                                        ? dbClm.getRendererValue( row,val )
233                                                                        : dbClm.getRendererValue( row,val,prm );
234                                }
235                                else if( types[j] == '#' ) {    // ラベル
236                                        if( prm == null || prm.isEmpty() ) {
237                                                val = dbClm.getLabel();
238                                        }
239                                        else {
240                                                // 6.8.3.1 (2017/12/01) [#XXXX param] で、RendererValueのパラメータを動的に変更できます。
241                                                switch( prm.charAt(0) ) {               // 先頭の文字(文字列のswitchでもよい)
242                                                        case 'L': val = dbClm.getLabel();                       break;
243                                                        case 'S': val = dbClm.getShortLabel();          break;
244                                                        case 'T': val = dbClm.getLongLabel();           break;
245                                                        case 'D': val = dbClm.getDescription();         break;
246                                                        default : val = dbClm.getLabel();                       break;
247                                                }
248                                        }
249                                }
250                        }
251                        // false が以前と同じ処理(互換処理)ただし、view などのフォーマット処理とは異なる。
252                        else {
253                                // 5.3.9.0 (2011/09/01) カラム名の先頭に'$'を付加した場合URLEncodeされた値を返すように対応
254                                if( types[j] == '$' ) {
255                                        val = StringUtil.urlEncode( val );
256                                }
257                        }
258
259                        buf.append( format[j] ).append( val );
260                }
261                if( j < format.length ) { buf.append( format[j] ); }
262                String rtn = StringUtil.replace( buf.toString(),"{I}",String.valueOf( row ) );
263
264                // 3.8.8.1 (2007/01/06) instrVals属性を追加
265                if( instrVals[intKey] != null ) {
266                        final String[] vals = StringUtil.csv2Array( instrVals[intKey],' ' );
267                        for( int i=0; i<vals.length; i++ ) {
268                                final String css = "<span class=\"instr" + i + "\">" + vals[i] + "</span>";
269                                rtn = StringUtil.replace( rtn,vals[i],css );
270                        }
271                }
272                return rtn ;
273        }
274
275        /**
276         * マーカーを作成する/作成しないの指定カラム番号を求めます。
277         * また、int[列番号] isMark を初期化します。
278         *
279         * @og.rev 3.5.2.0 (2003/10/20) markLists,markListNo,markKey属性を追加
280         * @og.rev 3.8.8.1 (2007/01/06) instrVals属性を追加
281         *
282         * @param       intKey  カラムキーの番号
283         * @param       attri   アトリビュート
284         */
285        private void makeOnMarkFormat( final int intKey,final Attributes attri ) {
286                final String onMark   = attri.get( "onMark" );
287                final String markList = attri.get( "markList" );
288                instrVals[intKey] = attri.get( "instrVals" );   // 3.8.8.1 (2007/01/06)
289
290                // 3.5.6.0 (2004/06/18) nullポインタの参照外しバグの対応
291                // このロジックで値が設定済みであれば、以下の処理は不要である。
292                isMark[intKey] = MARK_NULL;
293                if( onMark == null || onMark.isEmpty() ||
294                        markList == null || markList.isEmpty() ) {
295                                isMark[intKey] = MARK_FALSE;
296                                return ;        // 3.5.6.0 (2004/06/18)
297                }
298                else if( onMark.charAt(0) != '[' && markList.charAt(0) != '[' ) {
299                        isMark[intKey] = markList.indexOf( onMark ) >= 0 ? MARK_TRUE : MARK_FALSE;
300                        return ;        // 3.5.6.0 (2004/06/18)
301                }
302
303                if( onMark.charAt(0) == '[' ) {
304                        markCmlNo[intKey] = table.getColumnNo( onMark.substring( 1,onMark.length()-1 ));
305                }
306                else {
307                        markCmlNo[intKey]  = -1;
308                        markKey[intKey]    = onMark ;
309                }
310
311                if( markList.charAt(0) == '[' ) {
312                        markListNo[intKey] = table.getColumnNo( markList.substring( 1,markList.length()-1 ));
313                }
314                else {
315                        markListNo[intKey] = -1;
316                        markLists[intKey] = markList;
317                }
318        }
319
320        /**
321         * マーカーを作成するかどうかを判断します。
322         * int[列番号] isMark には、 未設定 FALSE TRUE の状態を持っており、
323         * 列でマーカーを作成する状態が固定の場合(例えば,onMark属性がデフォルト "true" の場合)
324         * カラムに関係なく、同じ値を返すときに、使用します。
325         *
326         * @og.rev 3.5.2.0 (2003/10/20) markLists,markListNo,markKey属性を追加
327         * @og.rev 3.5.4.0 (2003/11/25) onMark ,markList が null(またはゼロストリング)の場合は、false とする。
328         * @og.rev 4.0.0.0 (2005/08/31) 同一カラムの複数登録を許可します。
329         * @og.rev 6.7.2.0 (2017/01/16) markListの先頭が、"?" の場合、正規表現で判定します。
330         *
331         * @param       row     列番号
332         * @param       clm     カラムキーの名称
333         *
334         * @return      処理するリスト番号、-1 の場合は、該当なし
335         */
336        private int isOnMark( final int row,final int clm ) {
337                final List<Integer> list = clmMap.get( clm );
338                if( list == null ) { return -1; }
339
340                for( int i=0; i<list.size(); i++ ) {
341                        final int intKey = list.get( i );
342                        if( isMark[intKey] != MARK_NULL ) {
343                                if( isMark[intKey] == MARK_TRUE ) { return intKey; }
344                                else { continue; }
345                        }
346
347                        final String onMark ;
348                        if( markCmlNo[intKey] < 0 ) { onMark = markKey[intKey] ; }
349                        else { onMark = table.getValue( row,markCmlNo[intKey] ); }
350
351                        // 3.5.4.0 (2003/11/25) 追加
352                        if( onMark == null || onMark.isEmpty() ) { continue; }
353
354                        final String markList ;
355                        if( markListNo[intKey] < 0 ) { markList = markLists[intKey] ; }
356                        else { markList = table.getValue( row,markListNo[intKey] ); }
357
358                        // 3.5.4.0 (2003/11/25) 修正
359                        if( markList == null || markList.isEmpty() ) { continue; }
360
361                        // 6.7.2.0 (2017/01/16) markListの先頭が、"?" の場合、正規表現で判定します。
362                        if( markList.charAt(0) == '?' ) {
363                                if( onMark.matches( markList.substring(1) ) ) {
364                                        return intKey;
365                                }
366                        }
367                        else if( markList.indexOf( onMark ) >= 0 ) { return intKey; }
368                }
369                return -1;
370        }
371
372        /**
373         * マーカーされたカラム番号の配列を返します。
374         *
375         * これは特殊処理で、Edit機能で、カラム列をキャッシュしているときに、
376         * JSPのソース等の変更時に、変更が反映されない対応を行う場合、
377         * 通常の ViewFormのサブクラスから、Edit専用の ViewForm_HTMLSeqClmTable で
378         * 制御する場合、ViewMarkerのEditMarkerでは、通常非表示(検索の場合)ですが
379         * Editのポップアップ画面に、表示されてしまうのを避けるため、noDisplay に
380         * 強制的にするカラム番号が必要です。
381         * あくまで、暫定処置です。Edit機能を改修するときに、この機能は削除します。
382         *
383         * ※ この処理は、EditMarkerでのみ有効にします。
384         *
385         * @og.rev 6.0.3.0 (2014/11/13) Edit機能で、JSPソース変更時の対応
386         *
387         * @return  マーカーされたカラム番号の配列
388         */
389        public int[] getColumnNos() {
390                final int[] rtn = new int[clmMap.size()];
391                int i=0;
392                for( final Integer obj : clmMap.keySet() ) {
393                        rtn[i++] = obj.intValue();
394                }
395
396                return rtn;
397        }
398}