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 * @og.rev 8.1.2.3 (2022/05/20) 日付送り戻し不具合対応(useDateFeed 属性追加)
060 *
061 * @og.group データ編集
062 *
063 * @version  4.0
064 * @author   Kazuhiko Hasegawa
065 * @since    JDK5.0,
066 */
067public class Editor_YM extends AbstractEditor {
068        /** このプログラムのVERSION文字列を設定します。   {@value} */
069        private static final String VERSION = "8.1.2.3 (2022/05/20)" ;
070
071        // 8.0.2.0 (2021/11/30) 日付送り戻し対応
072        private static final int CNT_ARY                = 3;
073        private static final String JSP_ICON    = HybsSystem.sys( "JSP_ICON" ) ;
074        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\" >";
075        private static final String FR_END              = "<img src=\"" + JSP_ICON + "/%6$s\" alt=\"%4$s\" title=\"%4$s\" /></a>";
076        private static final String[] FR_IMG    = { "FR_PREV.png", "FR_CIRCLE.png", "FR_NEXT.png" };
077
078        private String isSubm                                   = "false";                                                      // 検索ボタンを押すか押さないか
079        private String errMsg;                                                                                                          // エラーメッセージ
080        private String[] prmAry;                                                                                                        // 編集パラメータ
081
082        private final boolean hidden;
083        private final boolean useDateFeed;                                                                                      // 日付送り戻し機能の有効/無効 8.1.2.3 (2022/05/20)
084
085        /**
086         * デフォルトコンストラクター。
087         * このコンストラクターで、基本オブジェクトを作成します。
088         *
089         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
090         * @og.rev 8.0.2.0 (2021/11/30) 日付送り戻し対応
091         * @og.rev 8.1.2.3 (2022/05/20) 日付送り戻し不具合対応(useDateFeed 属性追加)
092         *
093         */
094//      public Editor_YM() { super(); }         // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
095        // 8.0.2.0 (2021/11/30) Modify
096        public Editor_YM() {
097                super();
098                isSubm          = "false";
099                errMsg          = null;
100                prmAry          = null;
101                hidden          = true;
102                useDateFeed     = true;                                                                                                         // 8.1.2.3 (2022/05/20)
103        }
104
105        /**
106         * DBColumnオブジェクトを指定したprivateコンストラクター。
107         *
108         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
109         * @og.rev 3.5.6.0 (2004/06/18) XHTMLTag の 内部配列 INPUT_KEY を隠蔽します。
110         * @og.rev 8.0.2.0 (2021/11/30) 日付送り戻し対応
111         * @og.rev 8.1.2.3 (2022/05/20) 日付送り戻し不具合対応(useDateFeed 属性追加)
112         *
113         * @param       clm     DBColumnオブジェクト
114         */
115        private Editor_YM( final DBColumn clm ) {
116                super( clm );
117                tagBuffer.add( XHTMLTag.inputAttri( attributes ) );
118
119                final boolean disabled = "disabled".equalsIgnoreCase( attributes.get( "disabled" ) );
120                final boolean readonly = "readonly".equalsIgnoreCase( attributes.get( "readonly" ) );   // 8.0.2.0 (2021/11/30)
121                useDateFeed = clm.isDateFeed();                                                                                                                 // 8.1.2.3 (2022/05/20)
122
123                // 8.0.2.0 (2021/11/30) Add 日付送り戻し対応
124                hidden = disabled || readonly ;
125//              if( !hidden ) {                                                                                                                 // 8.1.2.3 (2022/05/20) Modify
126                if( !hidden && useDateFeed ) {
127                        // 例:M-1,M0,M+1 or M-1,M0,M+1,true
128                        final String prmStr = clm.getEditorParam();
129                        if( prmStr != null ) {
130                                prmAry = StringUtil.csv2Array( prmStr );
131                                // パラメータの第四引数がある場合
132                                if( prmAry.length > CNT_ARY ) {
133                                        isSubm = prmAry[CNT_ARY];
134                                } else if( prmAry.length < CNT_ARY ) {
135                                        errMsg = "editorParam の設定が不足です。"
136                                                                                + " name="  + name
137                                                                                + " label=" + clm.getLabel()
138                                                                                + " editorParam=" + clm.getEditorParam();
139                                        System.out.println( errMsg );
140                                }
141                        }
142                }
143        }
144
145        /**
146         * 各オブジェクトから自分のインスタンスを返します。
147         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
148         * まかされます。
149         *
150         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
151         * @og.rev 3.1.2.1 (2003/04/10) synchronized を、削除します。
152         *
153         * @param       clm     DBColumnオブジェクト
154         *
155         * @return      CellEditorオブジェクト
156         * @og.rtnNotNull
157         */
158        public CellEditor newInstance( final DBColumn clm ) {
159                return new Editor_YM( clm );
160        }
161
162        /**
163         * データの編集用文字列を返します。
164         *
165         * @og.rev 8.0.2.0 (2021/11/30) 日付送り戻し対応
166         * @og.rev 8.0.3.0 (2021/12/17) イメージにname属性を付けるため、spanタグで囲います。
167         * @og.rev 8.1.2.3 (2022/05/20) 日付送り戻し不具合対応(useDateFeed 属性追加)
168         *
169         * @param       value 入力値
170         *
171         * @return      データの編集用文字列
172         */
173        @Override
174        public String getValue( final String value ) {
175                // 8.0.2.0 (2021/11/30) Add
176                if( prmAry != null && prmAry.length < CNT_ARY ) {
177                        return "<span class=\"error\">" + errMsg + "</span>";
178                }
179
180                // 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
181                // 8.0.2.0 (2021/11/30) Delete
182//              return value != null && value.length() == 8
183//                                      ? super.getValue( value.substring( 0,6 ) )
184//                                      : super.getValue( value );
185
186                // 8.0.2.0 (2021/11/30) Modify
187                final String val = ( value != null && value.length() == 8 ) ? value.substring( 0,6 ) : value ;
188
189                final String tag = new TagBuffer( "input" )
190                                                .add( "name"    , name )
191                                                .add( "id"              , name , isNull( attributes.get( "id" ) ) )
192                                                .add( "value"   , val )
193                                                .add( "size"    , size1 )
194                                                .add( tagBuffer.makeTag() )
195                                                .makeTag();
196
197                if( hidden ) {
198                        return tag;
199                } else {
200                        final StringBuilder buf = new StringBuilder(BUFFER_MIDDLE)
201                                .append( tag );
202
203                        // 日付送り戻し対応
204//                      if( prmAry != null && prmAry.length > 0 ) {                                                     // 8.1.2.3 (2022/05/20) Modify
205                        if( useDateFeed && prmAry != null && prmAry.length > 0 ) {
206                                final String nmid = isNull( attributes.get( "id" ) ) ? name : attributes.get( "id" );
207                                // 第一回目の処理は、日付戻しを指定します。
208                                // 第二回目の処理は、初期化します。
209                                // 第三回目の処理は、日付送りを指定します。
210                                buf.append( "<span name=\"img" ).append( name ).append( "\">" );        // 8.0.3.0 (2021/12/17)
211                                for( int i=0; i<CNT_ARY; i++ ) {
212                                        buf.append( String.format( FR_STR + FR_END, "YM", nmid, val, prmAry[i], isSubm, FR_IMG[i] ) );
213                                }
214                                buf.append( "</span>" );        // 8.0.3.0 (2021/12/17)
215                        }
216                        return buf.toString();
217                }
218        }
219
220        /**
221         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
222         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し、
223         * リクエスト情報を1つ毎のフィールドで処理できます。
224         *
225         * @og.rev 7.2.9.4 (2020/11/20) spotbugs:メソッド呼び出しは非 null パラメータに null を渡している
226         * @og.rev 8.0.2.0 (2021/11/30) 日付送り戻し対応
227         * @og.rev 8.0.3.0 (2021/12/17) イメージにname属性を付けるため、spanタグで囲います。
228         * @og.rev 8.1.2.3 (2022/05/20) 日付送り戻し不具合対応(useDateFeed 属性追加)
229         *
230         * @param       row             行番号
231         * @param       value   入力値
232         *
233         * @return  データ表示/編集用の文字列
234         */
235        @Override
236        public String getValue( final int row,final String value ) {
237                // 8.0.2.0 (2021/11/30) Add
238                if( prmAry != null && prmAry.length < CNT_ARY ) {
239                        return "<span class=\"error\">" + errMsg + " row=" + row + "</span>";
240                }
241
242                // 7.2.9.4 (2020/11/20) spotbugs:メソッド呼び出しは非 null パラメータに null を渡している
243                final String val = value == null ? ""
244                                                                                 : value.length() > 6 ? value.substring( 0,6 )
245                                                                                                                          : value ;
246
247//              return super.getValue( row,val );                                                                               // 8.0.2.0 (2021/11/30) Delete
248
249//              // 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
250//              return value != null && value.length() == 8
251//                                      ? super.getValue( row,value.substring( 0,6 ) )
252//                                      : super.getValue( row,value );
253
254                // 8.0.2.0 (2021/11/30) Modify
255                final String name2 = name + HybsSystem.JOINT_STRING + row ;
256
257                final String tag = new TagBuffer( "input" )
258                                                .add( "name"    , name2 )
259                                                .add( "id"              , name2 , isNull( attributes.get( "id" ) ) )
260                                                .add( "value"   , val )
261                                                .add( "size"    , size2 )
262                                                .add( tagBuffer.makeTag() )
263                                                .makeTag( row,val );
264
265                if( hidden ) {
266                        return tag;
267                } else {
268                        final StringBuilder buf = new StringBuilder(BUFFER_MIDDLE)
269                                .append( tag );
270
271                        // 日付送り戻し対応
272//                      if( prmAry != null && prmAry.length > 0 ) {                                                     // 8.1.2.3 (2022/05/20) Modify
273                        if( useDateFeed && prmAry != null && prmAry.length > 0 ) {
274                                final String nmid = isNull( attributes.get( "id" ) ) ? name2 : attributes.get( "id" );
275                                // 第一回目の処理は、日付戻しを指定します。
276                                // 第二回目の処理は、初期化します。
277                                // 第三回目の処理は、日付送りを指定します。
278                                buf.append( "<span name=\"img" ).append( name2 ).append( "\">" );       // 8.0.3.0 (2021/12/17)
279                                for( int i=0; i<CNT_ARY; i++ ) {
280                                        buf.append( String.format( FR_STR + FR_END, "YM", nmid, val, prmAry[i], isSubm, FR_IMG[i] ) );
281                                }
282                                buf.append( "</span>" );        // 8.0.3.0 (2021/12/17)
283                        }
284                        return buf.toString();
285                }
286        }
287}