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.remote;
017
018import java.util.Map;
019
020import org.opengion.fukurou.util.StringUtil;
021import org.opengion.hayabusa.resource.ResourceFactory;
022import org.opengion.hayabusa.resource.ResourceManager;
023import static org.opengion.fukurou.system.HybsConst.BUFFER_MIDDLE;      // 6.1.0.0 (2014/12/26) refactoring
024
025/**
026 * RemoteControllableインタフェイスを実装した
027 * サーブレット経由で遠隔リソース更新を行うためのクラスです。
028 *
029 * POSTキーとしてcommandとCLM、langが必要です。
030 * commandは更新リソースの種別(COLUMN,LABEL,CODE,GUI)
031 * CLMはCLM値をカンマで区切ったCSV形式
032 * langは更新対象の言語(例:ja)です。
033 * 詳しくはremoteControlメソッドのJavaDocをご覧下さい。
034 *
035 * @og.rev 4.1.0.0 (2007/12/20) 新規作成
036 *
037 * @version  4.1
038 * @author   Masakazu Takahashi
039 * @since    JDK6.0,
040 *
041 */
042public class ClearResource implements RemoteControllable {
043
044        /**
045         * デフォルトコンストラクター
046         *
047         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
048         */
049        public ClearResource() { super(); }             // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
050
051        /**
052         * RemoteControllableインタフェイスの実装メソッドです。
053         * ClearResourceでは受け取ったパラメータによってコンテキストのリソースの再読込をします。
054         *
055         * POSTキーとして受け取るキーと値は次の通りとなります
056         * <table class="plain">
057         *   <caption>POSTキーとして受け取るキーと値</caption>
058         *   <tr><th>キー         </th><th>内容             </th><th>値                                                               </th></tr>
059         *   <tr><td>command    </td><td>更新種別   </td><td>COLUMN,LABEL,CODE,GUI                   </td></tr>
060         *   <tr><td>lang               </td><td>言語             </td><td>更新リソースの言語                               </td></tr>
061         *   <tr><td>CLM                </td><td>更新キー   </td><td>キーカラムを","で区切ったCSV形式</td></tr>
062         * </table>
063         *
064         * 動作はcommand == "GUI"の場合とそれ以外の場合に分かれます。
065         * 但し、リソースの更新はResourceManagerのメソッドを利用する部分は共通です。
066         * この時、langによって更新対象の言語を選択します(nullの場合はja)
067         * ①commandが"GUI"の場合
068         *  画面リソースを更新する場合はcommand="GUI"で渡します。
069         *  GUIに限ってCLMパラメータは不要です。
070         *  ResourceManager.guiClear()がコールされて画面リソースがクリアされます。
071         *  (同時に画面リソースのラベルリソースも再読込します)
072         * ②commandが"GUI"以外の場合
073         *  commandが"GUI"以外の場合の動作はどれも同じです。
074         *  受け取ったCLMパラメータをCSV分解し、ループで回して
075         *  ResourceManager.clear(key)をコールします。
076         *
077         * 返す値は XML形式の文字列です。
078         * &lt;clear-resource&gt;
079         *   &lt;command&gt;command引数&lt;/command&gt;
080         *   &lt;lang&gt;lang引数&lt;/lang&gt;
081         *   &lt;keys&gt;
082         *     &lt;key&gt;CLM引数の更新キー1&lt;/key&gt;
083         *     &lt;key&gt;CLM引数の更新キー2&lt;/key&gt;
084         *     ...
085         *   &lt;/keys&gt;
086         * &lt;/clear-resource&gt;
087         * となります。
088         *
089         * @param       valMap   サーブレットが受け取ったキーと値のマップ
090         *
091         * @return      XML形式の実行結果
092         * @og.rtnNotNull
093         */
094        @Override       // RemoteControllable
095        public String remoteControl( final Map<String,String> valMap ) {
096                final String                    command = valMap.get( "command" );
097                final String                    clms    = valMap.get( "CLM" );
098                final String                    lang    = valMap.get( "lang" );
099                final ResourceManager rscM      = ResourceFactory.newInstance( lang );
100
101                final StringBuilder rtnStr = new StringBuilder( BUFFER_MIDDLE )
102                        .append( "<clear-resource><command>" ).append( command )
103                        .append( "</command><lang>"     ).append( lang )
104                        .append( "</lang><keys>" );
105
106                if( "GUI".equals(command) ) {
107                        rscM.guiClear();
108                        rtnStr.append( " <key>").append( clms ).append( "</key>" );
109                }
110                else {
111                        final String[] vals = StringUtil.csv2Array( clms );
112                        for( int i=0; i<vals.length; i++ ) {
113                                rscM.clear( vals[i] );
114                                rtnStr.append( " <key>" ).append( vals[i] ).append( "</key>" );
115                        }
116                }
117
118                rtnStr.append( " </keys></clear-resource>" );
119                return rtnStr.toString();
120        }
121}