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.view;
017
018import org.opengion.hayabusa.common.HybsSystem;
019import org.opengion.fukurou.util.StringUtil;
020// import org.opengion.fukurou.util.XHTMLTag;
021import org.opengion.hayabusa.html.AbstractViewForm;
022import org.opengion.hayabusa.html.ViewCalendarParam;
023import static org.opengion.fukurou.system.HybsConst.BR; // 6.1.0.0 (2014/12/26) refactoring
024
025import java.util.Calendar;
026
027/**
028 * 1ヶ月分のカレンダー形式で、検索結果を表示する、カレンダー表示クラスです。
029 *
030 * AbstractViewForm により、setter/getterメソッドのデフォルト実装を提供しています。
031 * 各HTMLのタグに必要な setter/getterメソッドのみ、追加定義しています。
032 *
033 * AbstractViewForm を継承している為、ロケールに応じたラベルを出力させる事が出来ます。
034 *
035 * @og.group 画面表示
036 *
037 * @version  4.0
038 * @author   Kazuhiko Hasegawa
039 * @since    JDK5.0,
040 */
041public class ViewForm_HTMLCalendar extends AbstractViewForm {
042        /** このプログラムのVERSION文字列を設定します。   {@value} */
043        private static final String VERSION = "8.2.0.2 (2022/06/24)" ;
044
045        private static final String[] WEEK_JA = { "日" ,"月" ,"火" ,"水" ,"木" ,"金" ,"土" ,"日" ,"月" };        // 6.4.1.1 (2016/01/16) WEEK_ja  → WEEK_JA  refactoring
046        private static final String[] WEEK_EN = { "SUN","MON","TUE","WED","THU","FRI","SAT","SUN","MON" };      // 6.4.1.1 (2016/01/16) WEEK_en  → WEEK_EN  refactoring
047        private static final String[] WEEK_MK = { "SUN","",   "",   "",   "",   "",   "SAT","SUN",""    };
048
049        private String[] week              ;
050        private String[] viewKeys          ;            // カレンダを指定するキー配列        // 3.6.0.0 (2004/09/17)
051        private String   ymKey             ;            // 年月のカラム名
052        private String   dayKey            ;            // 休日(0:通常、1:休日)のカラム名
053        private String   valKey            ;            // カレンダに表示する値のカラム名
054        private boolean  valueBrFlag   ;                // 日付けと値の間に、BRタグを入れるかどうか
055        private int              firstWeek         ;            // 曜日の開始(0:日曜 1:月曜)
056        private int              columnSize        ;            // カレンダの横方向の繰り返し数       // 3.6.0.0 (2004/09/17)
057
058        /**
059         * デフォルトコンストラクター
060         *
061         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
062         */
063        public ViewForm_HTMLCalendar() { super(); }             // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
064
065        /**
066         * DBTableModel から HTML文字列を作成して返します。
067         * startNo(表示開始位置)から、pageSize(表示件数)までのView文字列を作成します。
068         * 表示残りデータが pageSize 以下の場合は、残りのデータをすべて出力します。
069         *
070         * @og.rev 3.5.2.1 (2003/10/27) 属性値の指定のシングルクオートをダブルクオートに変更。
071         * @og.rev 3.6.0.0 (2004/09/17) 複数カレンダに対応
072         * @og.rev 3.6.0.5 (2004/10/18) calenderParam の viewKeys バグ対応。
073         * @og.rev 3.7.0.1 (2005/01/31) 全件チェックコントロール処理変更
074         * @og.rev 7.0.1.0 (2018/10/15) XHTML → HTML5 対応(空要素の、"/>" 止めを、">" に変更します)。
075         * @og.rev 5.10.7.1 (2019/01/18) 1件の場合でもチェックボックスを表示
076         * @og.rev 8.2.0.2 (2022/06/24) HTML5廃止対応
077         *
078         * @param  startNo    表示開始位置
079         * @param  pageSize   表示件数
080         *
081         * @return  DBTableModelから作成された HTML文字列
082         * @og.rtnNotNull
083         */
084        public String create( final int startNo, final int pageSize )  {
085                if( getRowCount() == 0 ) { return ""; } // 暫定処置
086
087                paramInit();
088
089                final int lastNo = getLastNo( startNo, pageSize );
090
091                final StringBuilder out = new StringBuilder( BUFFER_LARGE )
092                        .append( "<table><tr>" ).append( CR );
093
094//              final boolean onlyOne = ( lastNo - startNo ) == 1 ;     // 互換性のため、1件の処理のみ変更
095                final boolean onlyOne = lastNo - startNo == 1 ;         // 互換性のため、1件の処理のみ変更             // 6.9.7.0 (2018/05/14) PMD Useless parentheses.
096
097                // 3.7.0.1 (2005/01/31) 全件チェックコントロール処理変更
098                if( ! onlyOne && isUseCheckControl() && "checkbox".equals( getSelectedType() ) ) {
099                        out.append( "<tr><td>" ).append( getAllCheckControl() ).append( "</td></tr>");
100                }
101
102                for( int row=startNo; row<lastNo; row++ ) {
103//                      out.append( "<td valign=\"top\">" );
104                        out.append( "<td style=\"vertical-align:top;\">" );                                     // 8.2.0.2 (2022/06/24) Modify
105
106                        if( isWritable( row ) ) {
107                                // 5.10.7.1 (2019/01/18) 1件でもチェックボックス表示に変更
108        //                      if( onlyOne ) {
109        //                              out.append( XHTMLTag.hidden( HybsSystem.ROW_SEL_KEY,String.valueOf( row ) ) )
110        //                                      .append( CR );
111        //                      }
112        //                      else {
113                                        out.append( "<input type=\"" ).append( getSelectedType() )
114                                                .append( "\" name=\"" ).append( HybsSystem.ROW_SEL_KEY ).append( '"' );
115                                        if( isChecked( row ) ) { out.append( " checked=\"checked\"" ); }
116//                                      out.append( " value=\"" ).append( row ).append( "\" />" );
117                                        out.append( " value=\"" ).append( row ).append( "\" >" );                       // 7.0.1.0 (2018/10/15)
118        //                      }
119                        }
120
121                        // 3.6.0.5 (2004/10/18)
122                        // 7.2.9.4 (2020/11/20) PMD:This for loop can be replaced by a foreach loop
123                        for( final String vkey : viewKeys ) {
124                                if( "_".equals( vkey ) ) { continue; }
125                                final int newCol = getColumnNo( vkey );
126//                      for( int col=0; col<viewKeys.length; col++ ) {
127//                              if( "_".equals( viewKeys[col] ) ) { continue; }
128//                              final int newCol = getColumnNo( viewKeys[col] );
129                                out.append("<span id=\"label\">")
130                                        .append( getColumnLabel( newCol ) )
131                                        .append( ":</span>" )
132                                        .append( getValueLabel(row,newCol) )
133                                        .append( "&nbsp;" );
134                        }
135                        out.append( BR )
136                                .append( makeCalendarData( row ) )
137                                .append( "</td>" );
138                        if( (row+1) % columnSize == 0 ) {
139                                out.append( "</tr><tr>" ).append( CR );
140                        }
141                }
142                out.append( "</tr></table>" ).append( CR );
143
144                return out.toString();
145        }
146
147        /**
148         * DBTableModel の指定の行番号より、カレンダのHTML文字列を作成して返します。
149         *
150         * @og.rev 3.6.0.0 (2004/09/17) create( int startNo, int pageSize ) のロジックを移動
151         * @og.rev 3.6.0.5 (2004/10/18) 印刷時の罫線出力関連機能の追加。id 属性を出力します。
152         * @og.rev 7.0.1.0 (2018/10/15) XHTML → HTML5 対応(空要素の、"/>" 止めを、">" に変更します)。
153         * @og.rev 8.2.0.2 (2022/06/24) HTML5廃止対応
154         *
155         * @param  row    指定の行番号
156         *
157         * @return  指定の行番号のカレンダのHTML文字列
158         * @og.rtnNotNull
159         */
160        private String makeCalendarData( final int row ) {
161                final String yyyymm = getValue( row,getColumnNo( ymKey ));
162                final Calendar currentCal = getCalendar( yyyymm );
163
164                final StringBuilder out = new StringBuilder( BUFFER_LARGE )
165//                      .append( "<table id=\"viewCalendar\" border=\"0\" cellpadding=\"1\" cellspacing=\"2\">" )
166                        .append( "<table id=\"viewCalendar\" border=\"0\" cellpadding=\"1\" >" )        // 8.2.0.2 (2022/06/24) Modify
167                        .append( CR )                                                                           // 3.6.0.5 (2004/10/18)
168                        .append( " <tr><td class=\"titleStyle\" colspan=\"7\">" )
169                        .append( getValueLabel( row,getColumnNo( ymKey )) )
170                        .append( "</td></tr>" ).append( CR );
171
172                // 今月の最終日
173                final int daysInMonth = currentCal.getActualMaximum(Calendar.DAY_OF_MONTH);
174                // カレンダーの週の初めを設定する。月曜開始のカレンダーを作成するときに利用
175                currentCal.setFirstDayOfWeek( Calendar.SUNDAY + firstWeek );
176
177                // 週を表す番号  0~6 の間
178                int culDay = ( currentCal.get(Calendar.DAY_OF_WEEK)
179                                                + ( 7 - currentCal.getFirstDayOfWeek() ) ) % 7 ;
180
181                // 曜日欄を記述します。
182                out.append(" <tr>").append( CR );
183                for( int i=0; i<7; i++ ) {
184//                      out.append("  <td width=\"14%\" class=\"dayHead" )
185                        out.append("  <td class=\"dayHead" )                                                            // 8.2.0.2 (2022/06/24) Modify
186                                .append( WEEK_MK[i+firstWeek] ).append( "\">" )
187                                .append( week[i+firstWeek] ).append( "</td>" )
188                                .append( CR );
189                }
190                out.append(" </tr>").append( CR )
191                        .append(" <tr>").append( CR );                          // 6.1.0.0 (2014/12/26) refactoring
192
193                // 第一週の日付欄の空きスペースの計算
194                if( culDay != 0 ) {
195                        out.append("  <td colspan=\"" ).append( culDay ).append( "\">&nbsp;</td>" ).append( CR );
196                }
197
198                // 日付欄を埋めます。
199//              final String BR = valueBrFlag ? "<br />" : "" ;
200                final String BR = valueBrFlag ? "<br>" : "" ;                           // 7.0.1.0 (2018/10/15)
201                for( int day=1; day <= daysInMonth; day++ ) {
202                        final int    daycol = getColumnNo( dayKey + day );              // 動的日付けカラム番号取得
203                        final String daylbl = getValueLabel( row,daycol,day );  // ローカルの日付けラベルを求めるメソッド
204                        // 6.3.9.1 (2015/11/27) Found 'DD'-anomaly for variable(PMD)
205                        final String vallbl = valKey == null ? "" : BR + getValueLabel( row,getColumnNo( valKey + day ) );              // 動的値カラム番号取得
206
207                        if( "1".equals( getValue( row,daycol ))) {
208                                out.append("  <td class=\"holyday\">" );
209                        } else {
210                                out.append("  <td class=\"day" )
211                                        .append( WEEK_MK[culDay+firstWeek] )
212                                        .append("\">");
213                        }
214                        out.append( daylbl )
215                                .append( vallbl )
216                                .append("</td>")
217                                .append( CR );
218
219                        // 週の終わりで、行を折り返す。
220                        if( culDay == 6 ) {
221                                out.append(" </tr>" ).append( CR )
222                                        .append( " <tr>").append( CR );                 // 6.1.0.0 (2014/12/26) refactoring
223                                culDay = 0;
224                        } else {
225                                culDay++;
226                        }
227                }
228
229                // 最終週の日付欄の空きスペースの計算
230                if( 7 != culDay ) {                             // 6.4.2.1 (2016/02/05) PMD refactoring. Useless parentheses.
231                        out.append( "  <td colspan=\"" ).append( 7-culDay ).append( "\">&nbsp;</td>" );
232                }
233                out.append( CR )
234                        .append( " </tr>" ).append( CR )
235                        .append( "</table>" ).append( CR );
236
237                return out.toString();
238        }
239
240        /**
241         * このビーに対する特別な初期化を行う。
242         *
243         * @og.rev 3.5.4.0 (2003/11/25) TableFormatter クラスを使用するように変更。
244         * @og.rev 3.5.5.9 (2004/06/07) ヘッダーの日付け表示に、Locale を加味できるように変更
245         * @og.rev 3.6.0.0 (2004/09/17) viewKeys , columnSize 属性の追加
246         *
247         */
248        private void paramInit() {
249
250                final String viewKeysCd = getParam( ViewCalendarParam.VIEW_KEYS                 ,ViewCalendarParam.VIEW_VALUES                  );      // 3.6.0.0 (2004/09/17)
251                ymKey                           = getParam( ViewCalendarParam.YM_KEY                    ,ViewCalendarParam.YM_VALUE                             );
252                dayKey                          = getParam( ViewCalendarParam.DAY_KEY                   ,ViewCalendarParam.DAY_VALUE                    );
253                valKey                          = getParam( ViewCalendarParam.VALUE_KEY                 ,null                                                                   );
254                final String valueBrCd  = getParam( ViewCalendarParam.VALUE_BR_FLAG_KEY ,ViewCalendarParam.VALUE_BR_FLAG_VALUE  );
255                final String firstWeekCd        = getParam( ViewCalendarParam.FIRSTWEEK_KEY             ,ViewCalendarParam.FIRSTWEEK_VALUE              );
256                final String headerLocale       = getParam( ViewCalendarParam.HEADER_LOCALE_KEY ,ViewCalendarParam.HEADER_LOCALE_VALUE  );
257                final String columnSizeCd       = getParam( ViewCalendarParam.COLUMN_SIZE_KEY   ,ViewCalendarParam.COLUMN_SIZE_VALUE    );      // 3.6.0.0 (2004/09/17)
258
259                viewKeys    = StringUtil.csv2Array( viewKeysCd );
260                firstWeek   = Integer.parseInt( firstWeekCd );
261                valueBrFlag = Boolean.parseBoolean( valueBrCd );                // 6.1.0.0 (2014/12/26) refactoring
262                columnSize  = Integer.parseInt( columnSizeCd );                 // 3.6.0.0 (2004/09/17)
263
264                // 曜日ヘッダーをコピーして作成します。
265                if( "ja".equals( headerLocale ) ) {
266                        week = WEEK_JA;
267                }
268                else {
269                        week = WEEK_EN;
270                }
271        }
272
273        /**
274         * row行、colum列 のデータの値をHTML文字列に変換して返します。
275         * リソースバンドルが登録されている場合は、リソースに応じた
276         * HTML文字列を作成します。
277         * カレンダーViewに特化した、ラベル値を返します。
278         *
279         * @og.rev 3.8.0.9 (2005/10/17) writableControl 追加による引数変更
280         *
281         * @param       row             行番号
282         * @param       column  カラム番号
283         * @param       day             日付
284         *
285         * @return      row行、colum列 のデータの値
286         * @og.rtnNotNull
287         */
288        private String getValueLabel( final int row, final int column, final int day ) {
289                // 6.3.9.1 (2015/11/27) A method should have only one exit point, and that should be the last statement in the method.(PMD)
290                final String rtn ;
291                if( isColumnWritable( column ) && isWritable( row ) ) {
292                        final String val = getValue( row,column ) ;
293                        rtn = day + getEditorValue( row,column,val );
294                }
295                else {
296                        rtn = String.valueOf( day );
297                }
298                return rtn ;
299        }
300
301        /**
302         * 指定のYYYYMM文字列から、カレンダーオブジェクトを作成して返します。
303         * 日にちは、1日にセットされます。
304         *
305         * @param   ym YYYYMM文字列
306         *
307         * @return  カレンダーオブジェクト
308         */
309        private Calendar getCalendar( final String ym ) {
310                final Calendar cal = Calendar.getInstance();
311
312                if( ym != null && ym.length() == 6 ) {
313                        final int yyyy = Integer.parseInt( ym.substring( 0,4 ) );
314                        final int mm   = Integer.parseInt( ym.substring( 4,6 ) ) - 1;
315                        final int dd   = 1;
316                        cal.set( yyyy,mm,dd,0,0,0 );
317                }
318                else {
319                        // カレンダーを今月の1日に設定する。
320                        cal.set(Calendar.DAY_OF_MONTH, 1);
321                }
322
323                return cal ;
324        }
325
326        /**
327         * フォーマットメソッドを使用できるかどうかを問い合わせます。
328         *
329         * @og.rev 3.6.0.0 (2004/09/17) 親クラス変更に伴なう、追加
330         *
331         * @return  使用可能(true)/ 使用不可能 (false)
332         */
333        public boolean canUseFormat() {
334                return false;
335        }
336
337        /**
338         * 表示項目の編集(並び替え)が可能かどうかを返します。
339         *
340         * @og.rev 5.1.6.0 (2010/05/01) 新規追加
341         *
342         * @return      表示項目の編集(並び替え)が可能かどうか(false:不可能)
343         */
344        @Override
345        public boolean isEditable() {
346                return false;
347        }
348}