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.resource;
017
018import org.opengion.fukurou.util.StringUtil ;
019import org.opengion.hayabusa.common.HybsSystemException;                        // 6.4.7.0 (2016/06/03)
020
021import static org.opengion.fukurou.system.HybsConst.BUFFER_MIDDLE;      // 6.1.0.0 (2014/12/26) refactoring
022import static org.opengion.fukurou.system.HybsConst.CR ;                        // 6.1.0.0 (2014/12/26)
023
024import java.text.MessageFormat;
025import java.util.Arrays;                                                                                        // 6.4.7.0 (2016/06/03)
026
027/**
028 * systemId , lang に対応したラベルデータを作成します。
029 *
030 * ラベルデータは、項目(CLM)に対して、各種ラベル情報を持っています。
031 * 従来のラベルは、表示名称として、一種類しか持っていませんでしたが、
032 * ラベルデータは、3種類の名称と、1種類の概要を持っています。
033 *
034 *   label       : 名称(長)      従来のラベルと同じで定義された文字そのものです。
035 *   shortLabel  : 名称(HTML短)  概要説明をバルーン表示する短い名称です。
036 *   longLabel   : 名称(HTML長)  概要説明をバルーン表示する長い名称です。
037 *   description : 概要説明      カラムの説明やバルーンに使用します。
038 *
039 * 名称(HTML長)は、従来の表示名称にあたります。これは、一般的なラベルとして
040 * 使用されます。名称(HTML短)は、テーブル一覧のヘッダーの様に、特殊なケースで、
041 * 簡略化された名称を使用するときに利用されます。この切り替えは、自動で判断されます。
042 * 名称(HTML短)に、なにも設定されていない場合は、名称(HTML長)が自動的に使用されますので
043 * 初期データ移行時には、そのまま、通常時もテーブルヘッダー時も同じ文字列が
044 * 使用されます。
045 * ただし、一覧表示のうち、EXCEL出力などのデータには、名称(長)が使用されます。
046 * これは、名称(HTML短)や名称(HTML長)は、Tips表示を行う為のHTML構文を採用している為
047 * テキスト等に出力するには不適切だからです。また、EXCEL等のツールでは、ラベル名が
048 * 長くてもセル幅等で調整できる為、簡略化された名称よりも正式名称で出力します。
049 *
050 * ラベルデータを作成する場合は、同一ラベルで、作成区分(KBSAKU)違いの場合は、
051 * 最も大きな作成区分を持つコードを使用します。
052 * 作成区分(KBSAKU)は、0:システム予約、1:アプリ設定、2:ユーザー設定 という具合に
053 * カスタマイズの度合いに応じて大きな数字を割り当てることで、キー情報を上書き修正
054 * することが可能になります。(削除することは出来ません。)
055 *
056 * @og.rev 4.0.0.0 (2004/12/31) 新規作成
057 * @og.group リソース管理
058 *
059 * @version  4.0
060 * @author   Kazuhiko Hasegawa
061 * @since    JDK5.0,
062 */
063public final class LabelData implements LabelInterface {
064
065        /** 内部データのカラム番号 {@value}        */
066        public static final int CLM                     = 0 ;
067        /** 内部データのカラム番号 {@value}        */
068        public static final int SNAME           = 1 ;
069        /** 内部データのカラム番号 {@value}        */
070        public static final int LNAME           = 2 ;
071        /** 内部データのカラム番号 {@value}        */
072        public static final int DESCRIPTION     = 3 ;
073        /** 内部データのカラム数 {@value} */
074        public static final int DATA_SIZE       = 4 ;
075
076        /** リソース読み込みのために一時利用 4.3.5.7 (2009/03/22) */
077        /* default */ static final int FG_LOAD          = 4 ;
078        /** FGLOAD の読み込み済み設定用のUNIQ 6.3.1.1 (2015/07/10) */
079        /* default */ static final int UNIQ                     = 5 ;
080        /** FGLOAD の読み込み済み設定用のSYSTEM_ID 6.3.1.1 (2015/07/10) */
081        /* default */ static final int SYSTEM_ID                = 6 ;
082
083        private final String    key                     ;               // 項目
084        private final String    label           ;               // 名称(長)
085        private final String    shortLabel      ;               // 名称(HTML短)
086        private final String    longLabel       ;               // 名称(HTML長)
087        private final String    description     ;               // 概要説明
088        private final boolean   official        ;               // リソースDBから作成されたかどうか
089        private final boolean   isFormat        ;               // メッセージフォーマット対象かどうか 4.0.0.0 (2007/10/17)
090        private final String    rawShortLabel;          // 名称(未変換短) 4.3.8.0 (2009/08/01)
091        private final boolean   isFormatDesc;           // 概要がフォーマット対象か 4.3.7.6 (2009/07/15)
092        private final String    rawLongLabel;           // 名称(未変換長) 5.6.8.2 (2013/09/20)
093
094        /**
095         * null LabelData オブジェクトを作成します。
096         * このオブジェクトは、DBリソース上に存在しない場合に使用される
097         * null 情報を表す、LabelData オブジェクトです。
098         * 
099         * @og.rev 5.6.8.2 (2013/09/20) rawLongLabel対応
100         * @og.rev 5.7.3.0 (2014/02/07) public に格上げします。
101         *
102         * @param       inkey   キー情報
103         */
104        public LabelData( final String inkey ) {
105                key                     = inkey.intern() ;
106                label           = key ;         // 名称(長)
107                shortLabel      = key ;         // 名称(HTML短)
108                longLabel       = key ;         // 名称(HTML長)
109                description     = ""  ;         // 概要説明
110                official        = false;        // 非正式
111                isFormat        = false;        // 非フォーマット 4.0.0.0 (2007/10/17)
112                rawShortLabel = key;    // 名称(未変換短) 4.3.8.0 (2009/08/01)
113                isFormatDesc = false;   // 概要フォーマット 4.3.7.6 (2009/07/15)
114                rawLongLabel = key;             // 名称(未変換長)5.6.8.2 (2013/09/20)
115        }
116
117        /**
118         * 配列文字列のデータを元に、LabelDataオブジェクトを構築します。
119         * このコンストラクタは、他のパッケージから呼び出せないように、
120         * パッケージプライベートにしておきます。
121         * このコンストラクタは、DBリソースファイルを想定しています。
122         *
123         * @og.rev 5.4.0.1 (2011/11/01) SNAME、概要説明、rawShortLabel 関係の処理を修正
124         * @og.rev 5.6.8.2 (2013/09/20) rawLongLabel対応
125         * @og.rev 6.2.2.0 (2015/03/27) BRと\nを相互に変換する処理を追加
126         * @og.rev 6.2.2.3 (2015/04/10) htmlフィルターに、BR→改行処理機能を追加。
127         * @og.rev 6.3.9.0 (2015/11/06) StringUtil.yenN2br は、null を返しません。
128         *
129         * @param       data    CLM,SNAME,LNAME,DESCRIPTION
130         */
131        LabelData( final String[] data ) {
132                key                     = data[CLM].intern() ;                                                          // 項目
133                label           = StringUtil.yenN2br( data[LNAME] ) ;                           // 6.2.2.3 (2015/04/10) 名称(HTML長)
134                description     = StringUtil.yenN2br( data[DESCRIPTION] ) ;                     // 6.2.2.3 (2015/04/10) 概要説明
135                official        = true;                                                                                         // 正式
136                isFormat        = label.indexOf( '{' ) >= 0 ;                                           // 4.0.0.0 (2007/10/17)
137                String title = null;
138
139                rawLongLabel = label;           // 名称(未変換長)5.6.8.2 (2013/09/20)
140                if( description.isEmpty() ) {                                                                   // 6.3.9.0 (2015/11/06) StringUtil.yenN2br は、null を返しません。
141                        isFormatDesc = false;
142                        // 5.4.0.1 (2011/11/01) title と label が間違っている。(SNAME が存在する場合)
143                        title     = StringUtil.htmlFilter( label,true ) ;                       // 6.2.2.0 (2015/03/27) 
144
145                        // 概要説明がない場合は、そのままラベルを使用する。
146                        longLabel = label;
147                }
148                else {
149                        isFormatDesc = description.indexOf( '{' ) >= 0 ;                // 5.1.8.0 (2010/07/01) nullポインタの参照外し対策
150                        title        = StringUtil.htmlFilter( description,true ) ;      // 6.2.2.0 (2015/03/27) 
151
152                        // 概要説明がある場合は、ツールチップにDESCRIPTIONを表示する。
153                        longLabel = "<span title=\""
154                                                        + title
155                                                        + "\">"
156                                                        + label
157                                                        + "</span>" ;
158                }
159
160                final String sname = data[SNAME];               // 名称(HTML短)
161                if( sname == null || sname.isEmpty() ) {
162                        // SNAME がない場合は、longLabel を使用する。
163                        shortLabel    = longLabel;
164                        rawShortLabel = label;          // 5.4.0.1 (2011/11/01) longLabel を使うと、ツールチップが加味されるため。
165                }
166                else {
167                        // SNAME が存在する場合、ツールチップにdescriptionかlabelを使用する。
168                        shortLabel = "<span title=\""
169                                                        + title
170                                                        + "\">"
171                                                        + sname
172                                                        + "</span>" ;
173                        rawShortLabel = sname; // 4.3.8.0 (2009/08/01)
174                }
175        }
176
177        /**
178         * ラベルオブジェクトのキーを返します。
179         *
180         * @return      ラベルオブジェクトのキー
181         */
182        public String getKey() { return key; }
183
184        /**
185         * ラベルオブジェクトの名称を返します。
186         * これは、DB上の LNAME(名称(長))に該当します。
187         *
188         * @return      ラベルオブジェクトの名称(短)
189         */
190        public String getLabel() { return label; }
191
192        /**
193         * ラベルオブジェクトの名称(短)を返します。
194         * 概要説明がない場合でかつDB上のSNAMEが未設定の場合は、
195         * LNAME が返されます。SNAMEが設定されている場合は、
196         * ツールチップにLNAME が表示されます。
197         * 概要説明が存在する場合は、ツールチップに概要説明が
198         * 表示されます。
199         *
200         * @return      ラベルオブジェクトの名称(短)
201         */
202        public String getShortLabel() { return shortLabel; }
203
204        /**
205         * ラベルオブジェクトの名称(長)を返します。
206         * 概要説明が存在する場合は、ツールチップに概要説明が
207         * 表示されます。
208         *
209         * @return      ラベルオブジェクトの名称(長)
210         * @see #getLongLabel( String )
211         */
212        public String getLongLabel() { return longLabel; }
213
214        /**
215         * ラベルインターフェースの名称(長)を返します。
216         * ツールチップに表示するタイトル属性(概要説明)を置き換えます。
217         * null の場合は、既存のgetLongLabel()を返します。
218         *
219         * @og.rev 6.2.2.0 (2015/03/27) BRと\nを相互に変換する処理を追加
220         * @og.rev 6.2.2.3 (2015/04/10) htmlフィルターに、BR→改行処理機能を追加。
221         *
222         * @param       title   ツールチップに表示するタイトル属性
223         *
224         * @return      ラベルインターフェースの名称(長)
225         * @see #getLongLabel()
226         */
227        public String getLongLabel( final String title ) {
228                final String tipsLabel ;
229                if( title == null ) {
230                        tipsLabel = longLabel;
231                }
232                else {
233                        tipsLabel = "<span title=\""
234                                                        + StringUtil.htmlFilter( title,true )   // 6.2.2.3 (2015/04/10)
235                                                        + "\">"
236                                                        + label
237                                                        + "</span>" ;
238                }
239                return tipsLabel ;
240        }
241
242        /**
243         * ラベルインターフェースの引数付きメッセージを返します。
244         * メッセージの引数部分に、文字列配列を適用して、MessageFormat
245         * で変換した結果を返します。(MessageData でのみ有効です。)
246         *
247         * @og.rev 4.0.0.0 (2007/10/17) メッセージリソース統合に伴い、MessageDataより移行
248         * @og.rev 4.3.8.0 (2009/08/01) 引数にHTMLサニタイジング処理
249         * @og.rev 5.0.0.2 (2009/09/15) サニタイジング処理をやめる
250         * @og.rev 6.4.3.4 (2016/03/11) ラベル {n} が存在しない場合は、引数を連結せず、元のラベルを返す。
251         * @og.rev 6.4.5.0 (2016/04/08) リソースから作成された場合は、引数を連結せず、そうでない場合は連結します。
252         * @og.rev 6.4.7.0 (2016/06/03) IllegalArgumentException が発生した場合に、見えるようにする。
253         *
254         * @param       vals    メッセージ引数の文字列配列(可変長引数)
255         *
256         * @return      ラベルインターフェースの引数付きメッセージ
257         */
258        public String getMessage( final String... vals ) {              // 6.1.1.0 (2015/01/17) 可変長引数でもnullは来る。
259                final String rtn ;
260
261                final String[] args = ( vals == null ) ? new String[0] : vals ;
262                if( isFormat ) {
263                        // 6.4.7.0 (2016/06/03) IllegalArgumentException が発生した場合に、見えるようにする。
264                        try {
265                                rtn = MessageFormat.format( label,(Object[])args );
266                        }
267                        catch( final IllegalArgumentException ex ) {
268                                final String errMsg = "MessageFormatエラー:"
269                                                        + " Key [" + key + "]"
270                                                        + " Pattern [" + label + "]"
271                                                        + " Arguments [" + Arrays.toString( (Object[])args ) + "]" ;
272                                throw new HybsSystemException( errMsg,ex );
273                        }
274                }
275                else {
276                        // 6.4.5.0 (2016/04/08) リソースから作成された場合は、引数を連結せず、そうでない場合は連結します。
277                        if( official ) {
278                                rtn = label ;                   // 6.4.3.4 (2016/03/11)
279                        }
280                        else {
281                                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE )
282                                        .append( label );
283                                for( final String val : args ) {
284                                        if( val != null && ! val.equals( label ) ) {
285                                                buf.append( ' ' ).append( val );                                        // 6.0.2.5 (2014/10/31) char を append する。
286                                        }
287                                }
288                                rtn = buf.toString();
289                        }
290
291                }
292                return rtn ;
293        }
294
295        /**
296         * ラベルオブジェクトの概要説明を返します。
297         * 概要説明が存在する場合は、ラベルのツールチップに
298         * 概要説明が表示されます。
299         *
300         * @return      ラベルオブジェクトの概要説明
301         */
302        public String getDescription() { return description; }
303
304        /**
305         * ラベルオブジェクトの概要説明を返します。
306         * このメソッドでは{0},{1}...をパラメータで置換します。
307         *
308         * @og.rev 4.3.7.6 (2009/07/15) 新規作成
309         * @og.rev 4.3.8.0 (2009/08/01) 引数にHTMLサニタイジング処理
310         * @og.rev 5.0.0.2 (2009/09/15) サニタイジング処理をやめる
311         * @og.rev 5.4.0.1 (2011/11/01) {}が存在しない場合は単に概要を出力
312         * @og.rev 6.4.7.0 (2016/06/03) IllegalArgumentException が発生した場合に、見えるようにする。
313         *
314         * @param       vals    メッセージ引数の文字列配列(可変長引数)
315         *
316         * @return      ラベルオブジェクトの概要説明
317         */
318        public String getDescription( final String... vals ) {          // 6.1.1.0 (2015/01/17) 可変長引数でもnullは来る。
319                final String rtn ;
320
321                // 6.1.1.0 (2015/01/17) 可変長引数でもnullは来る。
322                final String[] args = ( vals == null ) ? new String[0] : vals ;
323                if( isFormatDesc ) {
324                        // 6.4.7.0 (2016/06/03) IllegalArgumentException が発生した場合に、見えるようにする。
325                        try {
326                                rtn = MessageFormat.format( description,(Object[])args );
327                        }
328                        catch( final IllegalArgumentException ex ) {
329                                final String errMsg = "MessageFormatエラー:"
330                                                        + " Key [" + key + "]"
331                                                        + " Pattern [" + description + "]"
332                                                        + " Arguments [" + Arrays.toString( (Object[])args ) + "]" ;
333                                throw new HybsSystemException( errMsg,ex );
334                        }
335                }
336                else {
337                        // 5.4.0.1 (2011/11/01) {}が存在しない場合は単に概要を出力 ・・・ なら、直接セットに変更
338                        rtn = description;
339                }
340                return rtn ;
341        }
342
343        /**
344         * リソースDBから作成されたかどうかを返します。
345         * 正式な場合は、true / リソースになく、独自に作成された場合は、false になります。
346         *
347         * @return      リソースDBから作成されたかどうか
348         */
349        public boolean isOfficial() { return official; }
350
351        /**
352         * ラベルオブジェクトの名称(長)をそのままの形で返します。
353         * (discription等を付けない)
354         *
355         * @og.rev 5.6.8.2 (2009/08/01) 追加
356         *
357         * @return      ラベルオブジェクトの名称(長)そのままの状態
358         */
359        public String getRawLongLabel() { return rawLongLabel; }
360
361        /**
362         * ラベルオブジェクトの名称(短)をspanタグを付けない状態で返します。
363         * SNAMEが未設定の場合は、LNAME が返されます。
364         *
365         * @og.rev 4.3.8.0 (2009/08/01) 追加
366         *
367         * @return      ラベルオブジェクトの名称(短)にspanタグを付けない状態
368         */
369        public String getRawShortLabel() { return rawShortLabel; }
370
371        /**
372         * オブジェクトの識別子として,詳細なユーザー情報を返します。
373         *
374         * @return  詳細なユーザー情報
375         * @og.rtnNotNull
376         */
377        @Override
378        public String toString() {
379                final StringBuilder rtn = new StringBuilder( BUFFER_MIDDLE )
380                        .append( "CLM :" ).append( key )
381                        .append( " SNAME :" ).append( shortLabel )
382                        .append( " LNAME :" ).append( longLabel )
383                        .append( " DESCRIPTION :" ).append( description ).append( CR );
384                return rtn.toString();
385        }
386}