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