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        }
122
123        /**
124         * 内部に DBTableModel をセットします。
125         *
126         * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。
127         * @og.rev 3.5.2.0 (2003/10/20) markLists,markListNo,markKey属性を追加
128         * @og.rev 3.5.6.1 (2004/06/25) DBTableModel の再設定に対応。
129         * @og.rev 3.8.8.1 (2007/01/06) instrVals属性を追加
130         * @og.rev 5.6.3.0 (2013/04/01) useFmtDeco属性を追加
131         * @og.rev 6.4.3.3 (2016/03/04) Map#computeIfAbsent で対応する。
132         * @og.rev 6.4.3.4 (2016/03/11) Formatterに新しいコンストラクターを追加する。
133         * @og.rev 6.7.6.0 (2017/03/17) strictCheck 追加。
134         *
135         * @param  tbl DBTableModelオブジェクト
136         */
137        public void setDBTableModel( final DBTableModel tbl ) {
138                table = tbl;
139                final int count = markData.size();                      // 4.0.0 (2005/08/31)
140
141                isMark          = new int[count];
142                markKey         = new String[count];
143                markCmlNo       = new int[count];
144                markLists       = new String[count];
145                instrVals       = new String[count];
146                markListNo      = new int[count];
147                useFmtDeco      = new boolean[count];   // 5.6.3.0 (2013/04/01) [$XXXX],[#XXXX]機能を有効にするかどうか(true:有効)
148
149                Arrays.fill( isMark,MARK_FALSE );       // マーカーの表示可否
150                Arrays.fill( markCmlNo,-1 );            // マーカーの可否を判断するカラム番号
151                Arrays.fill( useFmtDeco,false );        // [$XXXX],[#XXXX]機能を無効にする。(互換性のため)
152
153                for( int intKey=0; intKey<count; intKey++ ) {
154                        final Attributes attri = markData.get( intKey );
155
156                        final String column = attri.get( "column" );
157
158                        // 6.7.6.0 (2017/03/17) カラムのDBTableModel存在チェック。初期値が true なので、attri に無い場合も、true になる。
159                        final String strChk = attri.get( "strictCheck" );
160                        final boolean strictCheck = ! "false".equalsIgnoreCase( strChk );
161
162                        // 6.4.3.1 (2016/02/12) ConcurrentMap 系は、key,val ともに not null 制限です。
163                        final int clm = table.getColumnNo( column,strictCheck );        // 6.7.6.0 (2017/03/17) strictCheck で、true の場合は、clm番号が見つからないときは、Exception発生
164
165                        if( clm < 0 ) { continue; }             // 6.7.6.0 (2017/03/17) 存在しない場合、以下の処理を行わない。= clmMap に、カラムが登録されない。
166
167                        // 6.4.3.3 (2016/03/04) Map#computeIfAbsent で対応する。
168                        // Map#computeIfAbsent : 戻り値は、既存の、または計算された値。追加有り、置換なし、削除なし
169                        clmMap.computeIfAbsent( clm,k -> new ArrayList<>() ).add( intKey );
170
171                        final String body = attri.get( "body" );
172                        final Formatter formatter = new Formatter( table,body );                // 6.4.3.4 (2016/03/11)
173                        // 6.4.3.1 (2016/02/12) ConcurrentMap 系は、key,val ともに not null 制限です。
174                        formMap.put( intKey, formatter );
175
176                        makeOnMarkFormat( intKey,attri );
177
178                        useFmtDeco[intKey] = "true".equalsIgnoreCase( attri.get( "useFormatDeco" ) );   // 5.6.3.0 (2013/04/01)
179                }
180        }
181
182        /**
183         * 指定の行列に対するマーカー文字列を返します。
184         * この値は,すでにマーカー文字列処理されている為, RendererValue で
185         * 変換する必要はありません。
186         * 引数の value はそのカラムの値として利用されます。この値は、修飾済みの
187         * 値を与えることが可能です。
188         *
189         * @og.rev 3.5.6.1 (2004/06/25) formMap属性を使用します。
190         * @og.rev 3.8.8.1 (2007/01/06) instrVals属性を追加
191         * @og.rev 5.3.9.0 (2011/09/01) カラム名の先頭に'$'を付加した場合に、URLEncodeされた値を返すように対応
192         * @og.rev 5.6.3.0 (2013/04/01) useFmtDeco属性を追加([$XXXX],[#XXXX]機能を有効にするかどうか)
193         * @og.rev 6.2.4.0 (2015/05/15) useFmtDeco属性とは無関係に、[!XXXX](値)を対応します。
194         * @og.rev 6.8.3.1 (2017/12/01) [$XXXX param] で、RendererValueのパラメータを動的に変更できます。
195         *
196         * @param   row 指定の行
197         * @param   clm 指定の列
198         * @param   value カラムの値(マーカー文字列処理済)
199         *
200         * @return  row行,colum列 のマーカー文字列
201         */
202        public String getMarkerString( final int row,final int clm,final String value ) {
203                final int intKey = isOnMark(row,clm) ;
204                if( intKey < 0 ) { return value; }
205
206                final Formatter formatter = formMap.get( intKey );
207                final int[]    clmNo  = formatter.getClmNos();
208                final String[] format = formatter.getFormat();
209                final char[]   types  = formatter.getType();
210
211                final StringBuilder buf = new StringBuilder( BUFFER_LARGE );
212                int j=0;
213                String val ;
214                for( ; j<clmNo.length; j++ ) {
215                        // 6.2.4.0 (2015/05/15) [!XXXX](値)を対応
216                        if( clm == clmNo[j] && types[j] != '!' ) {
217                                val = value;                                                            // 一致する場合の valueは、通常レンデラー
218                        }
219                        else {
220                                val = formatter.getValue(row,clmNo[j]);         // 生の値
221                        }
222
223                        // 5.6.3.0 (2013/04/01) useFmtDeco属性を追加(trueの場合は、[$XXXX],[#XXXX]機能を有効にする)
224                        if( useFmtDeco[intKey] ) {
225                                final DBColumn dbClm = table.getDBColumn( clmNo[j] );
226                                final String   prm   = formatter.getClmParam(j);        // 6.8.3.1 (2017/12/01)
227                                if( types[j] == '$' ) {                 // レンデラー
228                                        // 6.8.3.1 (2017/12/01) [$XXXX param] で、RendererValueのパラメータを動的に変更できます。
229                                        val = formatter.getValue(row,clmNo[j]);                 // ※ 互換性の関係で、useFmtDecoで、'$' のときのみ、生の値を再取得します。
230                                        val = prm == null || prm.isEmpty()
231                                                                        ? dbClm.getRendererValue( row,val )
232                                                                        : dbClm.getRendererValue( row,val,prm );
233                                }
234                                else if( types[j] == '#' ) {    // ラベル
235                                        if( prm == null || prm.isEmpty() ) {
236                                                val = dbClm.getLabel();
237                                        }
238                                        else {
239                                                // 6.8.3.1 (2017/12/01) [#XXXX param] で、RendererValueのパラメータを動的に変更できます。
240                                                switch( prm.charAt(0) ) {               // 先頭の文字(文字列のswitchでもよい)
241                                                        case 'L': val = dbClm.getLabel();                       break;
242                                                        case 'S': val = dbClm.getShortLabel();          break;
243                                                        case 'T': val = dbClm.getLongLabel();           break;
244                                                        case 'D': val = dbClm.getDescription();         break;
245                                                        default : val = dbClm.getLabel();                       break;
246                                                }
247                                        }
248                                }
249                        }
250                        // false が以前と同じ処理(互換処理)ただし、view などのフォーマット処理とは異なる。
251                        else {
252                                // 5.3.9.0 (2011/09/01) カラム名の先頭に'$'を付加した場合URLEncodeされた値を返すように対応
253                                if( types[j] == '$' ) {
254                                        val = StringUtil.urlEncode( val );
255                                }
256                        }
257
258                        buf.append( format[j] ).append( val );
259                }
260                if( j < format.length ) { buf.append( format[j] ); }
261                String rtn = StringUtil.replace( buf.toString(),"{I}",String.valueOf( row ) );
262
263                // 3.8.8.1 (2007/01/06) instrVals属性を追加
264                if( instrVals[intKey] != null ) {
265                        final String[] vals = StringUtil.csv2Array( instrVals[intKey],' ' );
266                        for( int i=0; i<vals.length; i++ ) {
267                                final String css = "<span class=\"instr" + i + "\">" + vals[i] + "</span>";
268                                rtn = StringUtil.replace( rtn,vals[i],css );
269                        }
270                }
271                return rtn ;
272        }
273
274        /**
275         * マーカーを作成する/作成しないの指定カラム番号を求めます。
276         * また、int[列番号] isMark を初期化します。
277         *
278         * @og.rev 3.5.2.0 (2003/10/20) markLists,markListNo,markKey属性を追加
279         * @og.rev 3.8.8.1 (2007/01/06) instrVals属性を追加
280         *
281         * @param       intKey  カラムキーの番号
282         * @param       attri   アトリビュート
283         */
284        private void makeOnMarkFormat( final int intKey,final Attributes attri ) {
285                final String onMark   = attri.get( "onMark" );
286                final String markList = attri.get( "markList" );
287                instrVals[intKey] = attri.get( "instrVals" );   // 3.8.8.1 (2007/01/06)
288
289                // 3.5.6.0 (2004/06/18) nullポインタの参照外しバグの対応
290                // このロジックで値が設定済みであれば、以下の処理は不要である。
291                isMark[intKey] = MARK_NULL;
292                if( onMark == null || onMark.isEmpty() ||
293                        markList == null || markList.isEmpty() ) {
294                                isMark[intKey] = MARK_FALSE;
295                                return ;        // 3.5.6.0 (2004/06/18)
296                }
297                else if( onMark.charAt(0) != '[' && markList.charAt(0) != '[' ) {
298                        isMark[intKey] = markList.indexOf( onMark ) >= 0 ? MARK_TRUE : MARK_FALSE;
299                        return ;        // 3.5.6.0 (2004/06/18)
300                }
301
302                if( onMark.charAt(0) == '[' ) {
303                        markCmlNo[intKey] = table.getColumnNo( onMark.substring( 1,onMark.length()-1 ));
304                }
305                else {
306                        markCmlNo[intKey]  = -1;
307                        markKey[intKey]    = onMark ;
308                }
309
310                if( markList.charAt(0) == '[' ) {
311                        markListNo[intKey] = table.getColumnNo( markList.substring( 1,markList.length()-1 ));
312                }
313                else {
314                        markListNo[intKey] = -1;
315                        markLists[intKey] = markList;
316                }
317        }
318
319        /**
320         * マーカーを作成するかどうかを判断します。
321         * int[列番号] isMark には、 未設定 FALSE TRUE の状態を持っており、
322         * 列でマーカーを作成する状態が固定の場合(例えば,onMark属性がデフォルト "true" の場合)
323         * カラムに関係なく、同じ値を返すときに、使用します。
324         *
325         * @og.rev 3.5.2.0 (2003/10/20) markLists,markListNo,markKey属性を追加
326         * @og.rev 3.5.4.0 (2003/11/25) onMark ,markList が null(またはゼロストリング)の場合は、false とする。
327         * @og.rev 4.0.0.0 (2005/08/31) 同一カラムの複数登録を許可します。
328         * @og.rev 6.7.2.0 (2017/01/16) markListの先頭が、"?" の場合、正規表現で判定します。
329         *
330         * @param       row     列番号
331         * @param       clm     カラムキーの名称
332         *
333         * @return      処理するリスト番号、-1 の場合は、該当なし
334         */
335        private int isOnMark( final int row,final int clm ) {
336                final List<Integer> list = clmMap.get( clm );
337                if( list == null ) { return -1; }
338
339                for( int i=0; i<list.size(); i++ ) {
340                        final int intKey = list.get( i );
341                        if( isMark[intKey] != MARK_NULL ) {
342                                if( isMark[intKey] == MARK_TRUE ) { return intKey; }
343                                else { continue; }
344                        }
345
346                        final String onMark ;
347                        if( markCmlNo[intKey] < 0 ) { onMark = markKey[intKey] ; }
348                        else { onMark = table.getValue( row,markCmlNo[intKey] ); }
349
350                        // 3.5.4.0 (2003/11/25) 追加
351                        if( onMark == null || onMark.isEmpty() ) { continue; }
352
353                        final String markList ;
354                        if( markListNo[intKey] < 0 ) { markList = markLists[intKey] ; }
355                        else { markList = table.getValue( row,markListNo[intKey] ); }
356
357                        // 3.5.4.0 (2003/11/25) 修正
358                        if( markList == null || markList.isEmpty() ) { continue; }
359
360                        // 6.7.2.0 (2017/01/16) markListの先頭が、"?" の場合、正規表現で判定します。
361                        if( markList.charAt(0) == '?' ) {
362                                if( onMark.matches( markList.substring(1) ) ) {
363                                        return intKey;
364                                }
365                        }
366                        else if( markList.indexOf( onMark ) >= 0 ) { return intKey; }
367                }
368                return -1;
369        }
370
371        /**
372         * マーカーされたカラム番号の配列を返します。
373         *
374         * これは特殊処理で、Edit機能で、カラム列をキャッシュしているときに、
375         * JSPのソース等の変更時に、変更が反映されない対応を行う場合、
376         * 通常の ViewFormのサブクラスから、Edit専用の ViewForm_HTMLSeqClmTable で
377         * 制御する場合、ViewMarkerのEditMarkerでは、通常非表示(検索の場合)ですが
378         * Editのポップアップ画面に、表示されてしまうのを避けるため、noDisplay に
379         * 強制的にするカラム番号が必要です。
380         * あくまで、暫定処置です。Edit機能を改修するときに、この機能は削除します。
381         *
382         * ※ この処理は、EditMarkerでのみ有効にします。
383         *
384         * @og.rev 6.0.3.0 (2014/11/13) Edit機能で、JSPソース変更時の対応
385         *
386         * @return  マーカーされたカラム番号の配列
387         */
388        public int[] getColumnNos() {
389                final int[] rtn = new int[clmMap.size()];
390                int i=0;
391                for( final Integer obj : clmMap.keySet() ) {
392                        rtn[i++] = obj.intValue();
393                }
394
395                return rtn;
396        }
397}