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;
020
021import org.opengion.hayabusa.develop.AbstractJspCreate;
022import org.opengion.hayabusa.develop.JspConvertEntity;
023import org.opengion.fukurou.xml.OGElement;
024
025/**
026 * query.jspの<og:select>タグを利用した ORDER BYパラメータを作成します。
027 *
028 * 検索条件のプルダウンは、通常、queryButtonタグ内に記載します。
029 * ただし、場合によっては、表に出すこともある為、name="ORDER_BY" で置き換えを実行します。
030 *
031 * ●使用例
032 *      <og:select name="ORDER_BY">
033 *          <option value = column.getRemarks() lbls = column.getRemarks() selected = "selected" />
034 *          <option value = column.getRemarks() lbls = column.getRemarks() />
035 *             ・・・・
036 *      </og:select>
037 *
038 * @og.rev 5.6.1.2 (2013/02/22) 文字列連結から、XML処理するように変更します。
039 * @author Takeshi.Takada
040 *
041 */
042public class JspCreate_ORDER_BY extends AbstractJspCreate {
043        /** このプログラムのVERSION文字列を設定します。   {@value} */
044        private static final String VERSION = "6.3.9.1 (2015/11/27)" ;
045
046        // 6.3.9.1 (2015/11/27) Variables should start with a lowercase character(PMD)
047        private List<JspConvertEntity> orderROWS ;
048        private boolean isNULL ;
049
050        /**
051         * コンストラクター
052         *
053         * インスタンス構築時に、タグ名(key)とファイル名(names)を指定します。
054         *
055         * @og.rev 6.3.9.1 (2015/11/27) コンストラクタを用意して、KEY,NAME をセットするように変更します。
056         */
057        public JspCreate_ORDER_BY() {
058                super( ":select" , "query" );
059        }
060
061        /**
062         * 初期化メソッド
063         *
064         * 内部で使用する JspConvertEntity の リスト のマップを受け取り、初期化を行います。
065         *
066         * @og.rev 5.2.1.0 (2010/10/01) 名前空間を、og 決め打ちから、名前空間指定無しに変更します。
067         *
068         * @param       master  JspConvertEntityのリストのマップ
069         */
070        @Override
071        protected void init( final Map<String,List<JspConvertEntity>> master ) {
072                orderROWS = master.get("ORDER");                                                // 6.3.9.1 (2015/11/27)
073                isNULL = !isNotEmpty( orderROWS );                                              // 6.3.9.1 (2015/11/27)
074        }
075
076        /**
077         * JSPに出力するタグの内容を作成します。
078         * 引数より作成前のタグの属性内容を確認するする事が出来ます。
079         *
080         * @og.rev 5.2.1.0 (2010/10/01) メソッドの引数を、OGAttributes から OGElement に変更します。
081         * @og.rev 5.2.1.0 (2010/10/01) 名前空間を、og 決め打ちから、引数を使用するように変更します。
082         *
083         * @param ele OGElementエレメントオブジェクト
084         * @param       nameSpace       このドキュメントのnameSpace( og とか mis とか )
085         *
086         * @return      変換された文字列
087         * @og.rtnNotNull
088         * @throws Throwable 変換時のエラー
089         */
090        @Override
091        protected String execute( final OGElement ele , final String nameSpace )  throws Throwable {
092                if( isNULL ) { return ""; }
093
094                // name="ORDER_BY" 以外は、そのまま返す。
095                if( !"ORDER_BY".equalsIgnoreCase( ele.getVal( "name" ) ) ) {
096                        return ele.toString();
097                }
098
099                final String ns = nameSpace.isEmpty() ? "" : nameSpace + ":" ;          // 名前空間
100
101                ele.clearNode();        // 一旦すべてのノードを削除します。
102
103                boolean isFirst = true;
104                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );   // ラベル用に、別名を取り除く , 6.1.0.0 (2014/12/26) refactoring
105                for( final JspConvertEntity column : orderROWS ){                               // 6.3.9.1 (2015/11/27)
106                        final OGElement opt = new OGElement( ns + "option" );
107                        final String remks = column.getRemarks();                       // 属性。ここに、A1.AA,A1.BB,A1.CC,B1.DD desc などのカラム列が入る。
108                        final String[] clms = remks.split( "," );                       // カンマで分解
109                        buf.setLength(0);                                                                       // 6.1.0.0 (2014/12/26) refactoring
110                        for( int i=0; i<clms.length; i++ ) {
111                                if( i>0 ) { buf.append( ',' ); }                                // 最初以外は、カンマを追加していく。
112
113                                final String clm = clms[i].trim();
114                                final int idx = clm.indexOf( '.' );
115                                if( idx >= 0 ) { buf.append( clm.substring( idx+1 ) ); }
116                                else           { buf.append( clm ); }
117                        }
118
119                        opt.addAttr( "value" , remks  );
120                        opt.addAttr( "lbls"  , buf.toString() );
121                        if( isFirst ){
122                                opt.addAttr( "selected"  , "selected" );
123                                isFirst = false;
124                        }
125                        ele.addNode( opt );
126                }
127
128                return ele.getText( 1 );
129        }
130}