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.plugin.column;
017
018import org.opengion.hayabusa.common.HybsSystem;                                                                 // 8.0.2.0 (2021/11/30) Add
019import org.opengion.hayabusa.db.AbstractEditor;
020import org.opengion.hayabusa.db.CellEditor;
021import org.opengion.hayabusa.db.DBColumn;
022import org.opengion.fukurou.util.TagBuffer;                                                                             // 8.0.2.0 (2021/11/30) Add
023import org.opengion.fukurou.util.StringUtil;                                                                    // 8.0.2.0 (2021/11/30) Add
024import org.opengion.fukurou.util.XHTMLTag;
025
026import static org.opengion.fukurou.util.StringUtil.isNull;                                              // 8.0.2.0 (2021/11/30) Add
027
028/**
029 * YM エディターは、カラムのデータを日付(年/月)編集する場合に使用するクラスです。
030 * 元の値が8桁の場合は先頭6桁にsubstringされます。
031 *
032 *  カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
033 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
034 *
035 * 8.0.2.0 (2021/11/30)
036 * カラムのパラメータの情報より、日付送り戻しの機能が使えます。
037 * 例:M-1,M0,M+1 or M-1,M0,M+1,true
038 *
039 * 第一引数は、日付戻しを指定します。
040 * 第二引数は、初期化します。
041 * 第三引数は、日付送りを指定します。
042 * 第四引数は、検索ボタンを押すか押さないか[true/false]を指定します。 (初期値:false)
043 *
044 * 日付についての加減算処理を行うためのコマンドを指定します。
045 *  ・SYXX :年の最初の日付を指定の分だけ進めます。(SY-1なら先年の1月1日、SY1なら翌年の1月1日)
046 *  ・SDXX :月の最初の日付を指定の分だけ進めます。(SD-1なら先月の1日、SD1なら翌月の1日)
047 *  ・SWXX :週初め(月曜日)を指定の分だけ進めます。(SW-1なら先週の月曜日、SW1なら翌週の月曜日)
048 *  ・EYXX :年の最後の日付を指定の分だけ進めます。(EY-1なら先年の年末、EY1なら翌年の年末)
049 *  ・EDXX :月の最後の日付を指定の分だけ進めます。(ED-1なら先月の月末、ED1なら翌月の月末)
050 *  ・EWXX :週末(日曜日)を指定の分だけ進めます。(EW-1なら先週の日曜日、EW1なら翌週の日曜日)
051 *  ・YXX  :年を指定の分だけ進めます。(Y-1なら1年前、Y1なら1年後)
052 *  ・MXX  :月を指定の分だけ進めます。(M-1なら1月前、M1なら1月後)
053 *  ・DXX  :日を指定の分だけ進めます。(D-1なら1日前、D1なら1日後)
054 *  ※ 数字がゼロのコマンドは初期化します。
055 *  ※ 数字がないコマンドはサーバー上のシステム日付をセットします。
056 *
057 * @og.rev 5.4.3.6 (2012/01/19) コメント修正
058 * @og.rev 8.0.2.0 (2021/11/30) 日付送り戻し対応
059 *
060 * @og.group データ編集
061 *
062 * @version  4.0
063 * @author   Kazuhiko Hasegawa
064 * @since    JDK5.0,
065 */
066public class Editor_YM extends AbstractEditor {
067        /** このプログラムのVERSION文字列を設定します。   {@value} */
068        private static final String VERSION = "8.0.2.0 (2021/11/30)" ;
069
070        // 8.0.2.0 (2021/11/30) 日付送り戻し対応
071        private static final int CNT_ARY                = 3;
072        private static final String JSP_ICON    = HybsSystem.sys( "JSP_ICON" ) ;
073        private static final String FR_STR              = "<a href=\"#\" onClick=\"dateFeedRtn('%1$s','%2$s','%3$s','%4$s','%5$s');\" style=\"margin:0 5px 0 3px\" >";
074        private static final String FR_END              = "<img src=\"" + JSP_ICON + "/%6$s\" alt=\"%4$s\" title=\"%4$s\" /></a>";
075        private static final String[] FR_IMG    = { "FR_PREV.png", "FR_CIRCLE.png", "FR_NEXT.png" };
076
077        private String isSubm                                   = "false";                                                              // 検索ボタンを押すか押さないか
078        private String errMsg;                                                                                                                  // エラーメッセージ
079        private String[] prmAry;                                                                                                                // 編集パラメータ
080
081        private final boolean hidden;
082
083        /**
084         * デフォルトコンストラクター。
085         * このコンストラクターで、基本オブジェクトを作成します。
086         *
087         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
088         * @og.rev 8.0.2.0 (2021/11/30) 日付送り戻し対応
089         *
090         */
091//      public Editor_YM() { super(); }         // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
092        // 8.0.2.0 (2021/11/30) Modify
093        public Editor_YM() {
094                super();
095                isSubm  = "false";
096                errMsg  = null;
097                prmAry  = null;
098                hidden  = true;
099        }
100
101        /**
102         * DBColumnオブジェクトを指定したprivateコンストラクター。
103         *
104         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
105         * @og.rev 3.5.6.0 (2004/06/18) XHTMLTag の 内部配列 INPUT_KEY を隠蔽します。
106         * @og.rev 8.0.2.0 (2021/11/30) 日付送り戻し対応
107         *
108         * @param       clm     DBColumnオブジェクト
109         */
110        private Editor_YM( final DBColumn clm ) {
111                super( clm );
112                tagBuffer.add( XHTMLTag.inputAttri( attributes ) );
113
114                final boolean disabled = "disabled".equalsIgnoreCase( attributes.get( "disabled" ) );
115                final boolean readonly = "readonly".equalsIgnoreCase( attributes.get( "readonly" ) );   // 8.0.2.0 (2021/11/30)
116
117                // 8.0.2.0 (2021/11/30) Add 日付送り戻し対応
118                hidden = disabled || readonly ;
119                if( !hidden ) {
120                        // 例:M-1,M0,M+1 or M-1,M0,M+1,true
121                        final String prmStr = clm.getEditorParam();
122                        if( prmStr != null ) {
123                                prmAry = StringUtil.csv2Array( prmStr );
124                                // パラメータの第四引数がある場合
125                                if( prmAry.length > CNT_ARY ) {
126                                        isSubm = prmAry[CNT_ARY];
127                                } else if( prmAry.length < CNT_ARY ) {
128                                        errMsg = "editorParam の設定が不足です。"
129                                                                                + " name="  + name
130                                                                                + " label=" + clm.getLabel()
131                                                                                + " editorParam=" + clm.getEditorParam();
132                                        System.out.println( errMsg );
133                                }
134                        }
135                }
136        }
137
138        /**
139         * 各オブジェクトから自分のインスタンスを返します。
140         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
141         * まかされます。
142         *
143         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
144         * @og.rev 3.1.2.1 (2003/04/10) synchronized を、削除します。
145         *
146         * @param       clm     DBColumnオブジェクト
147         *
148         * @return      CellEditorオブジェクト
149         * @og.rtnNotNull
150         */
151        public CellEditor newInstance( final DBColumn clm ) {
152                return new Editor_YM( clm );
153        }
154
155        /**
156         * データの編集用文字列を返します。
157         *
158         * @og.rev 8.0.2.0 (2021/11/30) 日付送り戻し対応
159         *
160         * @param       value 入力値
161         *
162         * @return      データの編集用文字列
163         */
164        @Override
165        public String getValue( final String value ) {
166                // 8.0.2.0 (2021/11/30) Add
167                if( prmAry != null && prmAry.length < CNT_ARY ) {
168                        return "<span class=\"error\">" + errMsg + "</span>";
169                }
170
171                // 6.4.1.1 (2016/01/16) PMD refactoring. A method should have only one exit point, and that should be the last statement in the method
172                // 8.0.2.0 (2021/11/30) Delete
173//              return value != null && value.length() == 8
174//                                      ? super.getValue( value.substring( 0,6 ) )
175//                                      : super.getValue( value );
176
177                // 8.0.2.0 (2021/11/30) Modify
178                final String val = ( value != null && value.length() == 8 ) ? value.substring( 0,6 ) : value ;
179
180                final String tag = new TagBuffer( "input" )
181                                                .add( "name"    , name )
182                                                .add( "id"              , name , isNull( attributes.get( "id" ) ) )
183                                                .add( "value"   , val )
184                                                .add( "size"    , size1 )
185                                                .add( tagBuffer.makeTag() )
186                                                .makeTag();
187
188                if( hidden ) {
189                        return tag;
190                } else {
191                        final StringBuilder buf = new StringBuilder(BUFFER_MIDDLE);
192                        buf.append( tag );
193
194                        // 日付送り戻し対応
195                        if( prmAry != null && prmAry.length > 0 ) {
196                                final String nmid = isNull( attributes.get( "id" ) ) ? name : attributes.get( "id" );
197                                // 第一回目の処理は、日付戻しを指定します。
198                                // 第二回目の処理は、初期化します。
199                                // 第三回目の処理は、日付送りを指定します。
200                                for( int i=0; i<CNT_ARY; i++ ) {
201                                        buf.append( String.format( FR_STR + FR_END, "YM", nmid, val, prmAry[i], isSubm, FR_IMG[i] ) );
202                                }
203                        }
204                        return buf.toString();
205                }
206        }
207
208        /**
209         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
210         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し,
211         * リクエスト情報を1つ毎のフィールドで処理できます。
212         *
213         * @og.rev 7.2.9.4 (2020/11/20) spotbugs:メソッド呼び出しは非 null パラメータに null を渡している
214         * @og.rev 8.0.2.0 (2021/11/30) 日付送り戻し対応
215         *
216         * @param       row             行番号
217         * @param       value   入力値
218         *
219         * @return  データ表示/編集用の文字列
220         */
221        @Override
222        public String getValue( final int row,final String value ) {
223                // 8.0.2.0 (2021/11/30) Add
224                if( prmAry != null && prmAry.length < CNT_ARY ) {
225                        return "<span class=\"error\">" + errMsg + " row=" + row + "</span>";
226                }
227
228                // 7.2.9.4 (2020/11/20) spotbugs:メソッド呼び出しは非 null パラメータに null を渡している
229                final String val = value == null ? ""
230                                                                                 : value.length() > 6 ? value.substring( 0,6 )
231                                                                                                                          : value ;
232
233//              return super.getValue( row,val );                                                                               // 8.0.2.0 (2021/11/30) Delete
234
235//              // 6.4.1.1 (2016/01/16) PMD refactoring. A method should have only one exit point, and that should be the last statement in the method
236//              return value != null && value.length() == 8
237//                                      ? super.getValue( row,value.substring( 0,6 ) )
238//                                      : super.getValue( row,value );
239
240                // 8.0.2.0 (2021/11/30) Modify
241                final String name2 = name + HybsSystem.JOINT_STRING + row ;
242
243                final String tag = new TagBuffer( "input" )
244                                                .add( "name"    , name2 )
245                                                .add( "id"              , name2 , isNull( attributes.get( "id" ) ) )
246                                                .add( "value"   , val )
247                                                .add( "size"    , size2 )
248                                                .add( tagBuffer.makeTag() )
249                                                .makeTag( row,val );
250
251                if( hidden ) {
252                        return tag;
253                } else {
254                        final StringBuilder buf = new StringBuilder(BUFFER_MIDDLE);
255                        buf.append( tag );
256
257                        // 日付送り戻し対応
258                        if( prmAry != null && prmAry.length > 0 ) {
259                                final String nmid = isNull( attributes.get( "id" ) ) ? name2 : attributes.get( "id" );
260                                // 第一回目の処理は、日付戻しを指定します。
261                                // 第二回目の処理は、初期化します。
262                                // 第三回目の処理は、日付送りを指定します。
263                                for( int i=0; i<CNT_ARY; i++ ) {
264                                        buf.append( String.format( FR_STR + FR_END, "YM", nmid, val, prmAry[i], isSubm, FR_IMG[i] ) );
265                                }
266                        }
267                        return buf.toString();
268                }
269        }
270}