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