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