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.develop;
017
018import java.util.List;
019import java.util.Map;
020import java.util.Set;
021import java.util.HashSet;
022
023import org.opengion.hayabusa.develop.AbstractJspCreate;
024import org.opengion.hayabusa.develop.JspConvertEntity;
025import org.opengion.fukurou.xml.OGElement;
026import org.opengion.fukurou.xml.OGNode;
027import org.opengion.fukurou.xml.OGNodeType;
028
029/**
030 * query.jspの<og:column>タグを作成します。
031 * column タグは、部分置換ではなく、table 部分からの全面置換です。(部分置換は難しかったので)
032 * table には、tr を配置し、column タグを、TD_COUNT(初期値=3)の繰り返し数だけ配置します。
033 * それを超えると、新しい tr に書き出していきます。
034 * TR_COUNT(初期値=2)を超える検索条件は、og:hideMenu として、作成します。
035 *
036 * ●使用例
037 *      <table summary = "layout" >
038 *          <tr>
039 *              <og:column
040 *                  name       = column.getColumnName() 
041 *                  defaultVal = column.getDefaultValue()
042 *                  must       = "true"         ("1".equals( column.getMust() ))
043 *                  clazz      = "aimai"        (ope.startsWith( "lk" ))
044 *              />
045 *              <og:column
046 *                  ・・・・
047 *              />
048 *          </tr>
049 *          <tr>
050 *                  ・・・・
051 *          </tr>
052 *      </table>
053 *
054 * @og.rev 5.6.1.2 (2013/02/22) 文字列連結から、XML処理するように変更します。
055 *
056 * @version  5.0
057 * @author       Kazuhiko Hasegawa
058 * @since    JDK7.0,
059 */
060public class JspCreate_COLUMN extends AbstractJspCreate {
061        //* このプログラムのVERSION文字列を設定します。   {@value} */
062        private static final String VERSION = "5.6.4.4 (2013/05/31)" ;
063
064        public static final int TD_COUNT = 3 ;          // tdタグを繰り返す数
065        public static final int TR_COUNT = 2 ;          // trタグを繰り返す数
066
067        private List<JspConvertEntity> QUERY_ROWS ;
068        private boolean IS_NULL ;
069
070        /**
071         * 初期化メソッド
072         *
073         * 内部で使用する JspConvertEntity の リスト のマップを受け取り、初期化を行います。
074         *
075         * @og.rev 5.6.4.4 (2013/05/31) 検索カラムは、名称で重複している場合は、片方だけでよい。
076         *
077         * @param       master  JspConvertEntityのリストのマップ
078         */
079        @Override
080        protected void init( final Map<String,List<JspConvertEntity>> master ) {
081                QUERY_ROWS = master.get("QUERY");
082                IS_NULL = !isNotEmpty( QUERY_ROWS );
083                KEY  = "table";
084                NAME = "query";
085
086                // 検索カラムは、名称で重複している場合は、片方だけでよい。
087                if( !IS_NULL ) {
088                        Set<String> keys = new HashSet<String>();
089                        int size = QUERY_ROWS.size();
090                        int idx  = 0;
091                        while( idx < size ) {
092                                String key = QUERY_ROWS.get(idx).getColumnName();
093                                if( keys.add( key ) ) {                         // 正常にセットできれば、true
094                                        idx++ ;
095                                }
096                                else {                                                          // すでに同じキーが存在する場合
097                                        QUERY_ROWS.remove(idx);
098                                        size-- ;
099                                }
100                        }
101                }
102        }
103
104        /**
105         * JSPに出力するタグの内容を作成します。
106         * 引数より作成前のタグの属性内容を確認するする事が出来ます。
107         *
108         * @og.rev 5.2.1.0 (2010/10/01) メソッドの引数を、OGAttributes から OGElement に変更します。
109         * @og.rev 5.2.1.0 (2010/10/01) 名前空間を、og 決め打ちから、引数を使用するように変更します。
110         * @og.rev 5.6.1.2 (2013/02/22) XML処理するように変更します。
111         * @og.rev 5.6.4.4 (2013/05/31) hideMenu の対応
112         *
113         * @param ele OGElementエレメントオブジェクト
114         * @param       nameSpace       このドキュメントのnameSpace( og とか mis とか )
115         *
116         * @return      変換された文字列
117         * @throws Throwable 変換時のエラー
118         */
119        @Override
120        protected String execute( final OGElement ele , final String nameSpace )  throws Throwable {
121                if( IS_NULL ) { return ""; }
122
123                // table タグの親タグが、hideMenu の場合は、処理しません。
124                OGNode paraNode = ele.getParentNode() ;
125                if( paraNode != null && paraNode.getNodeType() == OGNodeType.Element && "og:hideMenu".equals( ((OGElement)paraNode).getTagName() ) ) {
126                        return "";
127                }
128
129                // 既存の設定値をすべて削除します。ホントは自動登録した分だけを削除すべき。
130                OGElement tblEle  = new OGElement( "table" );
131                tblEle.addAttr( "summary","layout" );
132
133                OGElement tr = null;
134                for( int i=0; i<QUERY_ROWS.size() && i<(TD_COUNT*TR_COUNT); i++ ) {
135                        JspConvertEntity column = QUERY_ROWS.get(i);
136                        if( i%TD_COUNT == 0 ) {
137                                tr = new OGElement( "tr" );
138                                tblEle.addNode( tr );
139                        }
140
141                        tr = trElement( tr,column );
142                }
143
144                return tblEle.getText( 0 );
145        }
146
147        /**
148         * TRエレメントにカラムタグを追加していきます。
149         * 行列の判定処理を上位で行い、ここでは、カラムタグを追加する処理に専念します。
150         *
151         * @og.rev 5.6.4.4 (2013/05/31) hideMenu の対応
152         *
153         * @param       tr              OGElementエレメントオブジェクト
154         * @param       column  カラムタグの元情報
155         *
156         * @return      カラムタグが追加されたTRエレメント
157         */
158        protected OGElement trElement( final OGElement tr , final JspConvertEntity column ) {
159                OGElement clm  = new OGElement( "og:column" );
160                clm.addAttr( "name",column.getColumnName() );
161
162                if ( column.getDefaultValue() != null && column.getDefaultValue().trim().length() > 0) {
163                        clm.addAttr( "defaultVal",column.getDefaultValue() );
164                }
165                if ( "1".equals( column.getMust() )){
166                        clm.addAttr( "must","true" );
167                }
168                String ope = column.getRemarks();
169                if( ope != null && ope.startsWith( "lk" ) ) {
170                        clm.addAttr( "clazz","aimai" );
171                }
172
173                tr.addNode( clm );
174
175                return tr ;
176        }
177}