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;
019import org.opengion.hayabusa.db.AbstractEditor;
020import org.opengion.hayabusa.db.CellEditor;
021import org.opengion.hayabusa.db.DBColumn;
022import org.opengion.fukurou.util.XHTMLTag;
023import org.opengion.fukurou.util.TagBuffer;
024import org.opengion.fukurou.util.StringUtil;                                                                    // 8.0.2.0 (2021/11/30) Add
025
026import static org.opengion.fukurou.util.StringUtil.isNull;                                              // 6.1.1.0 (2015/01/17)
027
028/**
029 * YMD エディターは、カラムのデータを日付(年/月/日)編集する場合に使用するクラスです。
030 * YMD2はカレンダーのポップアップボタンが付属するタイプです。
031 *
032 * このエディタはeventColumnに対応していません。
033 *
034 * カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
035 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
036 *
037 * 8.0.2.0 (2021/11/30)
038 * カラムのパラメータの情報より、日付送り戻しの機能が使えます。
039 * 例:D-1,D0,D+1 or D-1,D0,D+1,true
040 *
041 * 第一引数は、日付戻しを指定します。
042 * 第二引数は、初期化します。
043 * 第三引数は、日付送りを指定します。
044 * 第四引数は、検索ボタンを押すか押さないか[true/false]を指定します。 (初期値:false)
045 *
046 * 日付についての加減算処理を行うためのコマンドを指定します。
047 *  ・SYXX :年の最初の日付を指定の分だけ進めます。(SY-1なら先年の1月1日、SY1なら翌年の1月1日)
048 *  ・SDXX :月の最初の日付を指定の分だけ進めます。(SD-1なら先月の1日、SD1なら翌月の1日)
049 *  ・SWXX :週初め(月曜日)を指定の分だけ進めます。(SW-1なら先週の月曜日、SW1なら翌週の月曜日)
050 *  ・EYXX :年の最後の日付を指定の分だけ進めます。(EY-1なら先年の年末、EY1なら翌年の年末)
051 *  ・EDXX :月の最後の日付を指定の分だけ進めます。(ED-1なら先月の月末、ED1なら翌月の月末)
052 *  ・EWXX :週末(日曜日)を指定の分だけ進めます。(EW-1なら先週の日曜日、EW1なら翌週の日曜日)
053 *  ・YXX  :年を指定の分だけ進めます。(Y-1なら1年前、Y1なら1年後)
054 *  ・MXX  :月を指定の分だけ進めます。(M-1なら1月前、M1なら1月後)
055 *  ・DXX  :日を指定の分だけ進めます。(D-1なら1日前、D1なら1日後)
056 *  ※ 数字がゼロのコマンドは初期化します。
057 *  ※ 数字がないコマンドはサーバー上のシステム日付をセットします。
058 *
059 * @og.rev 3.5.6.2 (2004/07/05) 新規作成
060 * @og.rev 5.4.3.6 (2012/01/19) コメント修正
061 * @og.rev 5.6.5.2 (2013/06/21) ポップアップ変更
062 * @og.rev 5.9.4.0 (2016/01/08) type=button追加,背景色transparent
063 * @og.rev 5.9.4.3 (2016/01/15) buttonのpadding:0
064 * @og.rev 7.3.2.3 (2021/04/09) システム定数のICON_DIRやIMAGE_DIRを使用します。
065 * @og.rev 8.0.2.0 (2021/11/30) 日付送り戻し対応
066 * @og.rev 8.1.2.3 (2022/05/20) 日付送り戻し不具合対応(useDateFeed 属性追加)
067 *
068 * @og.group データ編集
069 *
070 * @version  4.0
071 * @author   Kazuhiko Hasegawa
072 * @since    JDK5.0,
073 */
074public class Editor_YMD2 extends AbstractEditor {
075        /** このプログラムのVERSION文字列を設定します。 {@value} */
076        private static final String VERSION = "8.1.2.3 (2022/05/20)" ;
077
078        // 5.6.5.2 (2013/06/21) htmlからjspに変更
079        // 6.0.2.5 (2014/10/31) height,width に、"px"を追加
080        // 5.9.4.0 (2016/01/09) type=button追加
081        // 6.5.0.1 (2016/10/21) サイズ変更 '250px','265px' → '265px','260px'
082        // 4.3.6.7 (2009/05/22) FireFox対応
083        // 5.9.4.0 背景色transparent 5.9.4.3 padding
084        // 6.4.2.0 (2016/01/29) alt属性にtitle属性を追記。
085
086        // 7.3.2.3 (2021/04/09) システム定数のICON_DIRやIMAGE_DIRを使用します。
087        private static final String JSP      =  HybsSystem.sys( "JSP" ) ;
088        private static final String JSP_ICON =  HybsSystem.sys( "JSP_ICON" ) ;
089
090//      private static final String CAL1 = "<button type=\"button\" onclick=\"ogPopup('../common/calendar.jsp','265px','260px',null,this,new Array('" ;
091        private static final String CAL1 = "<button type=\"button\" onclick=\"ogPopup('" + JSP + "/common/calendar.jsp','265px','260px',null,this,new Array('" ;
092
093        // 6.8.5.0 (2018/01/09)
094//      private static final String CAL3 = "<button type=\"button\" onclick=\"ogPopup('../common/calendar_3month.jsp','320px','770px',null,this,new Array('" ;
095        private static final String CAL3 = "<button type=\"button\" onclick=\"ogPopup('" + JSP + "/common/calendar_3month.jsp','320px','770px',null,this,new Array('" ;
096
097        // 6.8.5.0 (2018/01/09) とりあえず、CAL1 と CAL3 しかないので、決め打ち
098        // 6.9.8.0 (2018/05/28) キーワード変更(CAL_ST → CAL_STR)
099        private static final String CAL_STR = "CAL1".equals( HybsSystem.sys( "CALENDAR_POPUP_TYPE" ) ) ? CAL1 : CAL3 ;
100
101//      // 6.8.5.0 (2018/01/09) キーワード変更(CAL2 → CAL_END)
102//      private static final String CAL_END = "'),event); return false;\" style=\"background-color:transparent;border:0px;padding:0;\">"
103//                                                                                              + "<img src=\"../image/calendar.gif\" alt=\"Calendar\" title=\"Calendar\"/></button>";
104        // 5.9.32.3command,p_appendの対応
105        // 7.0.1.0 (2018/10/15) XHTML → HTML5 対応(空要素の、"/>" 止めを、">" に変更します)。
106        // 7.4.2.2 (2021/05/28) タブレット用にボタンを大きくしたので、元に戻します。
107//      private static final String CAL_END = "'),'NEW',false,event); return false;\" style=\"background-color:transparent;border:0px;padding:0;\">"
108        private static final String CAL_END = "'),'NEW',false,event); return false;\" style=\"background-color:transparent;border:0px;padding:0;min-width:0;margin:0;\">"
109//                                                                                              + "<img src=\"../image/calendar.gif\" alt=\"Calendar\"/></button>";
110//                                                                                              + "<img src=\"../image/calendar.gif\" alt=\"Calendar\"></button>";
111                                                                                                + "<img src=\"" + JSP_ICON + "/calendar.gif\" alt=\"Calendar\"></button>";
112
113        // 8.0.2.0 (2021/11/30) 日付送り戻し対応
114        private static final int CNT_ARY                = 3;
115        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\" >";
116        private static final String FR_END              = "<img src=\"" + JSP_ICON + "/%6$s\" alt=\"%4$s\" title=\"%4$s\" /></a>";
117        private static final String[] FR_IMG    = { "FR_PREV.png", "FR_CIRCLE.png", "FR_NEXT.png" };
118
119        private String isSubm                                   = "false";                                                      // 検索ボタンを押すか押さないか
120        private String errMsg;                                                                                                          // エラーメッセージ
121        private String[] prmAry;                                                                                                        // 編集パラメータ
122
123        // 6.9.8.0 (2018/05/28) disabled 時に、日付ピッカーを出さない対応
124        private final boolean hidden;                                                                                           // 8.0.2.0 (2021/11/30) Modify disabled ⇒ hidden
125        private final boolean useDateFeed;                                                                                      // 日付送り戻し機能の有効/無効 8.1.2.3 (2022/05/20)
126
127        /**
128         * デフォルトコンストラクター。
129         * このコンストラクターで、基本オブジェクトを作成します。
130         *
131         * @og.rev 6.9.8.0 (2018/05/28) disabled 時に、日付ピッカーを出さない対応
132         * @og.rev 8.0.2.0 (2021/11/30) 日付送り戻し対応
133         * @og.rev 8.1.2.3 (2022/05/20) 日付送り戻し不具合対応(useDateFeed 属性追加)
134         */
135//      public Editor_YMD2() { super(); }               // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
136        public Editor_YMD2() {
137                super();
138                isSubm          = "false";                                                                                                      // 8.0.2.0 (2021/11/30) Add
139                errMsg          = null;                                                                                                         // 8.0.2.0 (2021/11/30) Add
140                prmAry          = null;                                                                                                         // 8.0.2.0 (2021/11/30) Add
141                hidden          = true;                                                                                                         // 8.0.2.0 (2021/11/30) Modify disabled ⇒ hidden
142                useDateFeed     = true;                                                                                                         // 8.1.2.3 (2022/05/20)
143        }
144
145        /**
146         * DBColumnオブジェクトを指定したprivateコンストラクター。
147         *
148         * @og.rev 6.9.8.0 (2018/05/28) disabled 時に、日付ピッカーを出さない対応
149         * @og.rev 8.0.2.0 (2021/11/30) 日付送り戻し対応
150         * @og.rev 8.1.2.3 (2022/05/20) 日付送り戻し不具合対応(useDateFeed 属性追加)
151         *
152         * @param       clm     DBColumnオブジェクト
153         */
154        private Editor_YMD2( final DBColumn clm ) {
155                super( clm );
156                tagBuffer.add( XHTMLTag.inputAttri( attributes ) );
157
158                final boolean disabled = "disabled".equalsIgnoreCase( attributes.get( "disabled" ) );   // 6.9.8.0 (2018/05/28)
159                final boolean readonly = "readonly".equalsIgnoreCase( attributes.get( "readonly" ) );   // 8.0.2.0 (2021/11/30)
160                useDateFeed = clm.isDateFeed();                                                                                                                 // 8.1.2.3 (2022/05/20)
161
162                // 8.0.2.0 (2021/11/30) Add 日付送り戻し対応
163                hidden = disabled || readonly;
164//              if( !hidden ) {                                                                                                                 // 8.1.2.3 (2022/05/20) Modify
165                if( !hidden && useDateFeed ) {
166                        // 例:D-1,D0,D+1 or D-1,D0,D+1,true
167                        final String prmStr = clm.getEditorParam();
168                        if( prmStr != null ) {
169                                prmAry = StringUtil.csv2Array( prmStr );
170                                // パラメータの第四引数がある場合
171                                if( prmAry.length > CNT_ARY ) {
172                                        isSubm = prmAry[CNT_ARY];
173                                } else if( prmAry.length < CNT_ARY ) {
174                                        errMsg = "editorParam の設定が不足です。"
175                                                                                + " name="  + name
176                                                                                + " label=" + clm.getLabel()
177                                                                                + " editorParam=" + clm.getEditorParam();
178                                        System.out.println( errMsg );
179                                }
180                        }
181                }
182        }
183
184        /**
185         * 各オブジェクトから自分のインスタンスを返します。
186         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
187         * まかされます。
188         *
189         * @param       clm     DBColumnオブジェクト
190         *
191         * @return      CellEditorオブジェクト
192         * @og.rtnNotNull
193         */
194        public CellEditor newInstance( final DBColumn clm ) {
195                return new Editor_YMD2( clm );
196        }
197
198        /**
199         * データの編集用文字列を返します。
200         *
201         * @og.rev 4.3.7.2 (2009/06/15) 属性でidが出力される場合は、idを出力しない
202         * @og.rev 6.8.5.0 (2018/01/09) カレンダポップアップで、3ヶ月ピッカー対応
203         * @og.rev 6.9.8.0 (2018/05/28) disabled 時に、日付ピッカーを出さない対応
204         * @og.rev 8.0.2.0 (2021/11/30) 日付送り戻し対応
205         * @og.rev 8.0.3.0 (2021/12/17) イメージにname属性を付けるため、spanタグで囲います。
206         * @og.rev 8.1.2.3 (2022/05/20) 日付送り戻し不具合対応(useDateFeed 属性追加)
207         *
208         * @param       value   入力値
209         *
210         * @return      データの編集用文字列
211         * @og.rtnNotNull
212         */
213        @Override
214        public String getValue( final String value ) {
215                // 8.0.2.0 (2021/11/30) Add
216                if( prmAry != null && prmAry.length < CNT_ARY ) {
217                        return "<span class=\"error\">" + errMsg + "</span>";
218                }
219
220                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
221                final String tag = new TagBuffer( "input" )
222                                                .add( "name"    , name )
223                                                .add( "id"              , name , isNull( attributes.get( "id" ) ) )             // 4.3.7.2 (2009/06/15)
224                                                .add( "value"   , value )
225                                                .add( "size"    , size1 )
226                                                .add( tagBuffer.makeTag() )
227                                                .makeTag();
228
229//              return tag + CAL_ST + name + CAL_END ;
230//              return disabled ? tag                                                                   // 6.9.8.0 (2018/05/28) disabled 時に、日付ピッカーを出さない対応
231//                                              : tag + CAL_STR + name + CAL_END ;              // 6.9.8.0 (2018/05/28) 変数名を、CAL_ST → CAL_STR に変更。
232                // 8.0.2.0 (2021/11/30) Modify
233                if( hidden ) {
234                        return tag;
235                } else {
236                        final StringBuilder buf = new StringBuilder(BUFFER_MIDDLE)
237                                .append( tag )
238                                .append( "<span name=\"img" ).append( name ).append( "\">" )    // 8.0.3.0 (2021/12/17)
239                                .append( CAL_STR )
240                                .append( name )
241                                .append( CAL_END );
242
243                        // 8.0.2.0 (2021/11/30) 日付送り戻し対応
244//                      if( prmAry != null && prmAry.length > 0 ) {                                                     // 8.1.2.3 (2022/05/20) Modify
245                        if( useDateFeed && prmAry != null && prmAry.length > 0 ) {
246                                final String nmid = isNull( attributes.get( "id" ) ) ? name : attributes.get( "id" );
247                                // 第一回目の処理は、日付戻しを指定します。
248                                // 第二回目の処理は、初期化します。
249                                // 第三回目の処理は、日付送りを指定します。
250                                for( int i=0; i<CNT_ARY; i++ ) {
251                                        buf.append( String.format( FR_STR + FR_END, "YMD", nmid, value, prmAry[i], isSubm, FR_IMG[i] ) );
252                                }
253                        }
254                        buf.append( "</span>" );        // 8.0.3.0 (2021/12/17)
255                        return buf.toString();
256                }
257        }
258
259        /**
260         * name属性を変えた、データ表示/編集用のHTML文字列を作成します。
261         * テーブル上の name に 行番号を付加して、名前_行番号 で登録するキーを作成し,
262         * リクエスト情報を1つ毎のフィールドで処理できます。
263         *
264         * @og.rev 4.3.7.2 (2009/06/15) 属性でidが出力される場合は、idを出力しない
265         * @og.rev 6.8.5.0 (2018/01/09) カレンダポップアップで、3ヶ月ピッカー対応
266         * @og.rev 6.9.8.0 (2018/05/28) disabled 時に、日付ピッカーを出さない対応
267         * @og.rev 8.0.2.0 (2021/11/30) 日付送り戻し対応
268         * @og.rev 8.0.3.0 (2021/12/17) イメージにname属性を付けるため、spanタグで囲います。
269         * @og.rev 8.1.2.3 (2022/05/20) 日付送り戻し不具合対応(useDateFeed 属性追加)
270         *
271         * @param       row             行番号
272         * @param       value   入力値
273         *
274         * @return      データ表示/編集用の文字列
275         * @og.rtnNotNull
276         */
277        @Override
278        public String getValue( final int row,final String value ) {
279                // 8.0.2.0 (2021/11/30) Add
280                if( prmAry != null && prmAry.length < CNT_ARY ) {
281                        return "<span class=\"error\">" + errMsg + " row=" + row + "</span>";
282                }
283
284                final String name2 = name + HybsSystem.JOINT_STRING + row ;
285
286                // 6.1.1.0 (2015/01/17) TagBufferの連結記述
287                final String tag = new TagBuffer( "input" )
288                                                .add( "name"    , name2 )
289                                                .add( "id"              , name2 , isNull( attributes.get( "id" ) ) )            // 4.3.7.2 (2009/06/15)
290                                                .add( "value"   , value )
291                                                .add( "size"    , size2 )
292                                                .add( tagBuffer.makeTag() )
293                                                .makeTag( row,value );
294
295//              return tag + CAL_ST + name2 + CAL_END ;
296//              return disabled ? tag                                                                   // 6.9.8.0 (2018/05/28) disabled 時に、日付ピッカーを出さない対応
297//                                              : tag + CAL_STR + name2 + CAL_END ;             // 6.9.8.0 (2018/05/28) 変数名を、CAL_ST → CAL_STR に変更。
298                // 8.0.2.0 (2021/11/30) Modify
299                if( hidden ) {
300                        return tag;
301                } else {
302                        final StringBuilder buf = new StringBuilder(BUFFER_MIDDLE)
303                                .append( tag )
304                                .append( "<span name=\"img" ).append( name2 ).append( "\">" )   // 8.0.3.0 (2021/12/17)
305                                .append( CAL_STR )
306                                .append( name2 )
307                                .append( CAL_END );
308
309                        // 8.0.2.0 (2021/11/30) 日付送り戻し対応
310//                      if( prmAry != null && prmAry.length > 0 ) {                                                     // 8.1.2.3 (2022/05/20) Modify
311                        if( useDateFeed && prmAry != null && prmAry.length > 0 ) {
312                                final String nmid = isNull( attributes.get( "id" ) ) ? name2 : attributes.get( "id" );
313                                // 第一回目の処理は、日付戻しを指定します。
314                                // 第二回目の処理は、初期化します。
315                                // 第三回目の処理は、日付送りを指定します。
316                                for( int i=0; i<CNT_ARY; i++ ) {
317                                        buf.append( String.format( FR_STR + FR_END, "YMD", nmid, value, prmAry[i], isSubm, FR_IMG[i] ) );
318                                }
319                        }
320                        buf.append( "</span>" );        // 8.0.3.0 (2021/12/17)
321                        return buf.toString();
322                }
323        }
324}