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.hayabusa.common.HybsSystemException;
019
020import java.util.Map;
021import java.util.concurrent.ConcurrentMap;                                                      // 6.4.3.3 (2016/03/04)
022import java.util.concurrent.ConcurrentHashMap;                                          // 6.4.3.1 (2016/02/12) refactoring
023
024/**
025 * ViewFormTag にパラメーターを渡す為のスーパークラスです。
026 *
027 * ViewForm 関連の各クラスは、特殊・専用化の傾向が強くなりつつあり、
028 * 設定するパラメーターも増えています。これらのパラメータを、共通の
029 * ViewFormインターフェースに設定することは、得策とは考えられない為、
030 * パラメーターを一括して渡すようにします。
031 * ただし、key1=**** val2=**** 的な渡し方では、エラーチェックや自動ドキュメント化
032 * が難しいため、各ViewFormのサブクラスごとに、パラメータクラスを作成し、
033 * それらのスーパークラスとして、最終的には、同一方法で、パラメータオブジェクト
034 * として渡すことにします。
035 *
036 * @og.rev 3.5.4.8 (2004/02/23) 新規作成
037 * @og.rev 6.3.4.0 (2015/08/01) ViewParamTag.java → ViewParamImpl.java
038 * @og.group 画面表示
039 *
040 * @version  4.0
041 * @author   Kazuhiko Hasegawa
042 * @since    JDK5.0,
043 */
044class ViewParamImpl extends CommonTagSupport {
045        /** このプログラムのVERSION文字列を設定します。   {@value} */
046        private static final String VERSION = "6.4.8.2 (2016/07/08)" ;
047        private static final long serialVersionUID = 648220160708L ;
048
049        /** 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。  */
050        private final ConcurrentMap<String,String> paramMap = new ConcurrentHashMap<>();                // 6.4.3.3 (2016/03/04)
051        /**
052         * デフォルトコンストラクター
053         *
054         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
055         */
056        public ViewParamImpl() { super(); }             // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
057
058        /**
059         * Taglibの終了タグが見つかったときに処理する doEndTag() を オーバーライドします。
060         *
061         * @og.rev 6.4.4.1 (2016/03/18) 設定元のMapをクリアすると、データが消えてしまうので、新規に作る。
062         * @og.rev 6.4.8.2 (2016/07/08) debug 属性を、パラメータにセットします。
063         *
064         * @return      後続処理の指示
065         */
066        @Override
067        public int doEndTag() {
068                debugPrint();           // 4.0.0 (2005/02/28)
069
070                putParam( "debug"        , String.valueOf( isDebug() ) );               // 6.4.8.2 (2016/07/08)
071
072                final ViewFormTag viewform = (ViewFormTag)findAncestorWithClass( this,ViewFormTag.class );
073                if( viewform == null ) {
074                        final String errMsg = "<b>" + getTagName() + "タグは、ViewFormTagの内側(要素)に記述してください。</b>";
075                        throw new HybsSystemException( errMsg );
076                }
077
078                // 6.4.4.1 (2016/03/18) 
079                viewform.setParam( new ConcurrentHashMap<>( paramMap ) );
080
081                return EVAL_PAGE ;
082        }
083
084        /**
085         * タグリブオブジェクトをリリースします。
086         * キャッシュされて再利用されるので、フィールドの初期設定を行います。
087         *
088         * @og.rev 6.4.3.3 (2016/03/04) Mapの初期化を、clear()メソッドで行う。
089         */
090        @Override
091        protected void release2() {
092                super.release2();               // 3.5.6.0 (2004/06/18) 追加(抜けていた)
093                paramMap.clear();
094        }
095
096        /**
097         * パラメータのMapを初期設定します。
098         *
099         * パラメータのキーと値の初期値をセットしたMapを初期設定します。
100         * 処理のタイミングとして、すでにパラメータ変数は、設定されています。
101         * すでに、登録されている場合は、キーが存在しているため、キーの存在しない
102         * データのみ、初期値マップからコピーします。
103         *
104         * @og.rev 5.5.5.6 (2012/08/31) 新規追加
105         * @og.rev 6.0.2.5 (2014/10/31) entrySet イテレータを使用するように変更。
106         * @og.rev 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。
107         * @og.rev 6.4.3.3 (2016/03/04) Mapの初期化を、clear()メソッドで行う。
108         *
109         * @param   map         パラメータのMap(null値を許容する)
110         */
111        protected void initParam( final Map<String,String> map ) {
112                // 6.4.3.3 (2016/03/04) キーが存在しなければ、初期化情報を登録する処理を、Map#putIfAbsent を使用して実現します。
113                // putIfAbsent → 追加有り、置換なし、削除なし
114                if( map != null ) {
115                        map.forEach( (k,v) -> { if( v != null ) { paramMap.putIfAbsent(k,v); } } );
116                }
117        }
118
119        /**
120         * パラメータのキーと値をセットします。
121         *
122         * @og.rev 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。
123         * @og.rev 6.4.3.3 (2016/03/04) Mapの初期化を、clear()メソッドで行う。
124         *
125         * @param   key         キー
126         * @param   value       値
127         */
128        protected void putParam( final String key, final String value ) {
129                if( key != null && value != null ) {
130                        // 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。
131                        paramMap.put( key,value );
132                }
133        }
134
135        /**
136         * このオブジェクトの文字列表現を返します。
137         * 基本的にデバッグ目的に使用します。
138         *
139         * @og.rev 5.2.1.0 (2010/10/01) Map の内容表示方法を変更
140         * @og.rev 6.4.3.3 (2016/03/04) Mapの初期化を、clear()メソッドで行う。
141         *
142         * @return このクラスの文字列表現
143         * @og.rtnNotNull
144         */
145        @Override
146        public String toString() {
147                final StringBuilder rtn = new StringBuilder( BUFFER_MIDDLE );
148
149                // 6.0.2.5 (2014/10/31) char を append する。
150                rtn.append( '[' ).append( this.getClass().getName() ).append( ']' ).append( CR );
151                paramMap.forEach( (k,v) -> { rtn.append( k ).append( '=' ).append( v ).append( CR ); } );
152
153                return rtn.toString();
154        }
155}