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 java.util.List;
019
020import org.opengion.fukurou.util.StringUtil;
021import org.opengion.hayabusa.common.HybsSystem;
022import org.opengion.hayabusa.common.HybsSystemException;
023import org.opengion.hayabusa.db.DBTableModel;
024import org.opengion.hayabusa.html.TableFormatter;
025
026/**
027 * JavaScript のツリー階層を持ったテーブル表示を行う、ツリーテーブル表示クラスです。
028 *
029 * AbstractViewForm により、setter/getterメソッドのデフォルト実装を提供しています。
030 * 各HTMLのタグに必要な setter/getterメソッドのみ,追加定義しています。
031 *
032 * AbstractViewForm を継承している為,ロケールに応じたラベルを出力させる事が出来ます。
033 *
034 * @og.group 画面表示
035 *
036 * @version  4.0
037 * @author   Hiroki Nakamura
038 * @since    JDK5.0,
039 */
040public class ViewForm_HTMLCustomTreeBOM extends ViewForm_HTMLTable  {
041        //* このプログラムのVERSION文字列を設定します。   {@value} */
042        private static final String VERSION = "5.1.6.0 (2010/05/01)" ;
043
044//      public static final String COLUMN_LEVEL_KEY = "COLUMN_LEVEL";   // 5.1.9.0 (2010/08/01) 廃止
045
046        private TableFormatter          headerFormat    = null;
047        private TableFormatter[]        bodyFormats             = null;
048        private int                                     bodyFormatsCount = 0;
049
050        private static final int BODYFORMAT_MAX_COUNT = 10;
051
052        // 4.3.4.4 (2009/01/01)
053//      /**
054//       * デフォルトコンストラクター
055//       *
056//       */
057//      public ViewForm_HTMLCustomTreeBOM() {
058//              super();
059//      }
060
061        /**
062         * DBTableModel から HTML文字列を作成して返します。
063         * startNo(表示開始位置)から、pageSize(表示件数)までのView文字列を作成します。
064         * 表示残りデータが pageSize 以下の場合は,残りのデータをすべて出力します。
065         *
066         * @og.rev 4.3.1.0 (2008/09/08) フォーマットが設定されていない場合のエラー追加
067         *
068         * @param  stNo     表示開始位置
069         * @param  pgSize   表示件数
070         *
071         * @return  DBTableModelから作成された HTML文字列
072         */
073        @Override
074        public String create( final int stNo, final int pgSize )  {
075                // このクラスでは、テーブル全データを使用します。
076                if( getRowCount() == 0 ) { return ""; } // 暫定処置
077
078                // 4.3.1.0 (2008/09/08)
079                if( headerFormat == null ) {
080                        String errMsg = "ViewTagで canUseFormat() = true の場合、Formatter は必須です。";
081                        throw new HybsSystemException( errMsg );
082                }
083
084                int startNo = 0;
085                int pageSize = getRowCount();
086
087                int lastNo = getLastNo( startNo, pageSize );
088
089                StringBuilder out = new StringBuilder( HybsSystem.BUFFER_LARGE );
090
091                headerFormat.makeFormat( getDBTableModel() );
092
093                if( bodyFormatsCount == 0 ) {
094                        bodyFormats[0] = headerFormat ;
095                        bodyFormatsCount ++ ;
096                }
097                else {
098                        for( int i=0; i<bodyFormatsCount; i++ ) {
099                                bodyFormats[i].makeFormat( getDBTableModel() );
100                        }
101                }
102
103                out.append( getHeader() );
104
105                int level;
106                boolean isFld;
107                for( int row=startNo; row<lastNo; row++ ) {
108                        // カラム==0は、レベルを指定する。
109                        level = Integer.parseInt( getValueLabel(row,0) );
110                        isFld = false;
111                        if( row+1<lastNo ) {
112                                int nextLevel = Integer.parseInt( getValueLabel(row+1,0) );
113                                isFld = ( level < nextLevel ) ? true : false ;
114                        }
115                        out.append( getLevelScript( level,isFld ) );
116
117                        // 開始
118                        for( int i=0; i<bodyFormatsCount; i++ ) {
119                                TableFormatter bodyFormat = bodyFormats[i];
120
121                                int cl = 0;
122                                for( ; cl < bodyFormat.getLocationSize(); cl++ ) {
123                                        String fmt = bodyFormat.getFormat(cl);
124                                        int loc = bodyFormat.getLocation(cl);
125                                        if( ! bodyFormat.isNoClass() && loc >= 0 ) {
126                                                StringBuilder newtg = new StringBuilder( HybsSystem.BUFFER_LARGE );
127                                                newtg.append("<td class=\"");
128                                                newtg.append( getColumnDbType(loc) );
129                                                newtg.append("\" ");
130                                                String tdclass = newtg.toString();
131                                                fmt = StringUtil.replace( bodyFormat.getFormat(cl) ,"<td", tdclass );
132                                        }
133                                        out.append( fmt );
134                                        if( loc >= 0 ) {
135                                                switch( bodyFormat.getType(cl) ) {
136                                                case '#' : out.append( getColumnLabel(loc) );           break;
137                                                case '$' : out.append( getRendererValue(row,loc) );     break;
138                                                case '!' : out.append( getValue(row,loc) );                     break;
139                                                default  : out.append( getValueLabel(row,loc) );        break;
140                                                }
141                                        }
142                                }
143                                out.append( StringUtil.replace( bodyFormat.getFormat(cl), "</tr>", "" ) );
144                        }
145                        // 終了
146
147                        out.append( "', '', 'gold')" );
148                        if( level != 0 ) {
149                                out.append( ")" );
150                        }
151                        out.append( HybsSystem.CR );
152                }
153                out.append( getFutter() );
154
155                return out.toString();
156        }
157
158        /**
159         * DBTableModel から テーブルのヘッダータグ文字列を作成して返します。
160         * JavaScript の TreeBody では、JavaScriptに関連する定義もこのヘッダーに
161         * 含めます。
162         *
163         * @return  テーブルのヘッダータグ文字列
164         */
165        @Override
166        protected String getHeader() {
167                StringBuilder buf = new StringBuilder( HybsSystem.BUFFER_MIDDLE );
168
169                buf.append("<table border=\"0\" cellspacing=\"2\" cellpadding=\"0\"  summary=\"bomTable\" id=\"viewTable\">"); // 3.9.0.1 (2007/12/18)
170                buf.append( HybsSystem.CR );
171                buf.append("<script type=\"text/javascript\">");
172                buf.append( HybsSystem.CR );
173                buf.append("<!--");
174                buf.append( HybsSystem.CR );
175                buf.append("aux0 = gFld('");
176                // 開始
177                int cl = 0;
178                for( ; cl < headerFormat.getLocationSize(); cl++ ) {
179                        buf.append( StringUtil.replace( headerFormat.getFormat(cl) ,"td","th" ));
180                        int loc = headerFormat.getLocation(cl);
181                        if( loc >= 0 ) { buf.append( getColumnLabel(loc) ); }
182                        // ヘッダーフォーマット部では、何もしません。
183                }
184                buf.append( StringUtil.replace( StringUtil.replace( headerFormat.getFormat(cl) ,"td","th" ), "</tr>", "" ) );
185                // 終了
186
187                buf.append("', '', 'gold')");
188                buf.append( HybsSystem.CR );
189
190                return buf.toString();
191        }
192
193        /**
194         * DBTableModel から テーブルのフッタータグ文字列を作成して返します。
195         * JavaScript の TreeBody では、JavaScriptに関連する定義もこのフッターに
196         * 含めます。
197         *
198         * @return  テーブルのフッタータグ文字列
199         */
200        protected String getFutter() {
201                StringBuilder buf = new StringBuilder( HybsSystem.BUFFER_MIDDLE );
202
203                buf.append("initializeDocument()").append( HybsSystem.CR );
204                buf.append("//-->").append( HybsSystem.CR );
205                buf.append("</script>").append( HybsSystem.CR );
206                buf.append("</table>").append( HybsSystem.CR );
207
208                return buf.toString();
209        }
210
211        /**
212         * 行のレベルに応じた JavaScript関数のヘッダー部分を返します。
213         *
214         * @og.rev 3.5.2.1 (2003/10/27) JavaScript 内のダブルコーテーションをシングルコーテーションに変更する。
215         *
216         * @param       lvl             ツリーのレベル
217         * @param       isFld   フォルダかどうか[true:フォルダ/false:最下層]
218         *
219         * @return  JavaScript関数のヘッダー部分
220         */
221        private String getLevelScript( final int lvl,final boolean isFld ) {
222
223                String auxX = "\taux" + ( lvl );
224                String auxY = "aux" + ( lvl-1 );
225
226                final String rtn ;
227                if( isFld ) {
228                        rtn = auxX + " = insFld(" + auxY + ", gFld('";
229                }
230                else {
231                        rtn = "\tinsFld(" + auxY + ", gLnk('CONTENTS','";
232                }
233
234                return rtn;
235        }
236
237        /**
238         * フォーマットを設定します。
239         *
240         * @param       list    TableFormatterのリスト
241         */
242        @Override
243        public void setFormatterList( final List<TableFormatter> list ) {         // 4.3.3.6 (2008/11/15) Generics警告対応
244                bodyFormats = new TableFormatter[BODYFORMAT_MAX_COUNT];
245
246                bodyFormatsCount = 0;
247                for( int i=0; i<list.size(); i++ ) {
248                        TableFormatter format = list.get( i );          // 4.3.3.6 (2008/11/15) Generics警告対応
249                        switch( format.getFormatType() ) {
250                        case TYPE_HEAD : headerFormat = format; break;
251                        case TYPE_BODY : bodyFormats[bodyFormatsCount++] = format; break;
252                        default : String errMsg = "FormatterType の定義外の値が指定されました。";
253                        // 4.3.4.4 (2009/01/01)
254                                          throw new HybsSystemException( errMsg );
255                        }
256                }
257
258                if( headerFormat == null ) {
259                        String errMsg = "og:thead タグの、フォーマットの指定は必須です。";
260                        throw new HybsSystemException( errMsg );
261                }
262        }
263
264        /**
265         * フォーマットメソッドを使用できるかどうかを問い合わせます。
266         *
267         * @return  使用可能(true)/ 使用不可能 (false)
268         */
269        @Override
270        public boolean canUseFormat() {
271                return true;
272        }
273
274        /**
275         * ビューで表示したカラムの一覧をカンマ区切りで返します。
276         *
277         * @og.rev 5.1.6.0 (2010/05/01) 新規追加
278         *
279         * @return      ビューで表示したカラムの一覧
280         */
281        @Override
282        public String getViewClms() {
283                DBTableModel table = getDBTableModel();
284                StringBuilder buf = new StringBuilder( HybsSystem.BUFFER_MIDDLE );
285                for( int i=0; i<headerFormat.getLocationSize(); i++ ) {
286                        if( buf.length() > 0 ) { buf.append( ',' ); }
287                        buf.append( table.getColumnName( headerFormat.getLocation( i ) ) );
288                }
289                return buf.toString();
290        }
291}