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.db;
017
018import org.opengion.hayabusa.common.HybsSystem;
019import org.opengion.fukurou.util.StringFormat;
020import org.opengion.fukurou.util.StringUtil;                    // 5.9.0.1 (2015/09/11)
021
022/**
023 * 動的プルダウンなどで利用されるイベントカラムの各種情報を保持するための
024 * 管理クラスです。
025 *
026 * 内容を置き換えるカラム(子カラム)の名前をキーに、イベントカラム(親カラム)や、
027 * イベント発行時の処理URL等を管理します。
028 *
029 * これらの情報は、ColumnTagやSelectTag、ViewFormTagなどで登録され、その結果を
030 * JavaScriptのコードとして出力します。(common/eventColumn.jsp)
031 *
032 * ここで出力された情報をイベント発行時に、JavaScriptが参照し、処理URLに渡す
033 * ことで、動的な項目の入れ替えを実現しています。
034 *
035 * @og.rev 5.1.7.0 (2010/06/01) 新規追加
036 * @og.rev 5.9.0.1 (2015/09/11) アンダースコア対応
037 *
038 * @version  4.0
039 * @author   Hiroki Nakamura
040 * @since    JDK5.0,
041 */
042public class DBEventColumn {
043
044        private static final String EVENT_COLUMN_URL = HybsSystem.sys( "JSP" ) + "/" + HybsSystem.sys( "EVENT_COLUMN_URL" );
045
046        private final String name;                      // 内容を置き換えるカラム(子カラム)
047        private final String eventColumn;       // イベントカラム(親カラム・CSV形式)
048        private final String eventValue;        // 6.3.3.0 (2015/07/25) eventValue 追加
049        private final String eventUrl;          // イベント発行時の処理URL
050        private final String renderer;          // 子カラムのレンデラー
051        private final String editor;            // 子カラムのエディター
052        private final String rendParam;         // 子カラムの表示パラメーター
053        private final String editParam;         // 子カラムの編集パラメーター
054        private final String valParam;          // 6.3.3.0 (2015/07/25) rendParam,editParam で使用する変数化された親カラム
055
056        /**
057         * 初期情報を含んだ新規オブジェクトを作成します。
058         *
059         * @og.rev 6.3.3.0 (2015/07/25) eventValue 追加
060         * @og.rev 5.9.0.1 (2015/09/11) アンダースコア対応
061         *
062         * @param name 内容を置き換えるカラム(子カラム)
063         * @param eventColumn イベントカラム(親カラム・CSV形式)
064         * @param eventValue 子カラムの値SQL
065         * @param eventUrl イベント発行時の処理URL
066         * @param renderer 子カラムのレンデラー
067         * @param editor 子カラムのエディター
068         * @param rendParam 子カラムの表示パラメーター
069         * @param editParam 子カラムの編集パラメーター
070         */
071        public DBEventColumn( final String name, final String eventColumn, final String eventValue, final String eventUrl,
072                                                        final String renderer, final String editor, final String rendParam, final String editParam ) {
073                this.name                       = name;
074                this.eventColumn        = eventColumn;
075                this.eventValue         = eventValue;                                                           // 6.3.3.0 (2015/07/25)
076                // 6.4.2.1 (2016/02/05) PMD refactoring. Useless parentheses.
077                this.eventUrl           = eventUrl != null && eventUrl.length() > 0 ? eventUrl : EVENT_COLUMN_URL;       // 5.1.9.0 (2010/08/01) 動的プルダウン不具合対応
078                this.renderer           = renderer;
079                this.editor                     = editor;
080                this.rendParam          = rendParam;
081                this.editParam          = editParam;
082
083                valParam = "{@" + evColReplace( eventColumn ).replace( ",", "}:{@" ) + "}" ;    // 5.9.0.1 (2015/09/11)
084        }
085
086        /**
087         * 内容を置き換えるカラム(子カラム)を返します。
088         *
089         * @return 内容を置き換えるカラム(子カラム)
090         */
091        public String getName() { return name; }
092
093        /**
094         * イベントカラム(親カラム・CSV形式)を返します。
095         *
096         * @return イベントカラム(親カラム・CSV形式)
097         */
098        public String getEventColumn() { return eventColumn; }
099
100        /**
101         * イベント発行時の処理URLを返します。
102         *
103         * @og.rev 5.1.8.0 (2010/07/01) getEventUrl ⇒ getEventURL に変更
104         *
105         * @return イベント発行時の処理URL
106         */
107        public String getEventURL() { return eventUrl; }
108
109        /**
110         * 子カラムのレンデラーを返します。
111         *
112         * @return 子カラムのレンデラー
113         */
114        public String getRenderer() { return renderer; }
115
116        /**
117         * 子カラムのエディターを返します。
118         *
119         * @return 子カラムのエディター
120         */
121        public String getEditor() { return editor; }
122
123        /**
124         * 子カラムの表示パラメーターを返します。
125         *
126         * @return 子カラムの表示パラメーター
127         * @og.rtnNotNull
128         */
129        public String getRendParam() {
130                final StringFormat sf = new StringFormat(
131                                                        rendParam
132                                                        ,valParam               // 6.3.3.0 (2015/07/25)
133                                                        ,name );
134                return sf.format();
135        }
136
137        /**
138         * 子カラムの編集パラメーターを返します。
139         *
140         * @return 子カラムの編集パラメーター
141         * @og.rtnNotNull
142         */
143        public String getEditParam() {
144                final StringFormat sf = new StringFormat(
145                                                        editParam
146                                                        ,valParam               // 6.3.3.0 (2015/07/25)
147                                                        ,name );
148                return sf.format();
149        }
150
151        /**
152         * 子カラムの値SQLを返します。
153         *
154         * @og.rev 6.3.3.0 (2015/07/25) eventValue 追加
155         *
156         * @return 子カラムの値SQL
157         * @og.rtnNotNull
158         */
159        public String getEventValue() {
160                final StringFormat sf = new StringFormat(
161                                                        eventValue
162                                                        ,valParam               // 6.3.3.0 (2015/07/25)
163                                                        ,name );
164                return sf.format();
165        }
166
167        /**
168         * カンマ区切りのカラム名から先頭のアンダースコアを外します。
169         *
170         * @og.rev 5.9.0.1 (2015/09/11) 新規作成
171         * @og.rev 6.8.5.0 (2018/01/09) StringUtil.csv2Array のデフォルトメソッドを使用します。
172         *
173         * @param inStr カンマ区切りイベントカラム
174         * @return 先頭アンダースコアを外したカンマ区切り文字列
175         */
176        private String evColReplace( final String inStr ){
177                final String[] to;
178                if( inStr != null && inStr.indexOf( ',' ) >= 0 ) {
179                        to = StringUtil.csv2Array( inStr );                                     // 6.8.5.0 (2018/01/09)
180                }
181                else {
182                        to = new String[] { inStr };
183                }
184
185                for( int i=0; i<to.length; i++ ) {
186                        if( to[i].charAt( 0 ) == '_'){
187                                to[i] = to[i].substring( 1 );
188                        }
189                }
190
191                return StringUtil.array2csv( to );
192        }
193}