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     */
016    package org.opengion.fukurou.model;
017    
018    
019    /**
020     * [PN],[OYA] などの [] でæŒ?®šされたカラãƒ?�§表されたフォーマットデータに対してã€?
021     * DBTableModelオブジェクトを適用して å�?‚«ラãƒ?�«実データを割り当てるオブジェクトですã?
022     *
023     * @og.group 画面表示
024     *
025     * @version  4.0
026     * @author   Kazuhiko Hasegawa
027     * @since    JDK5.0,
028     */
029    public class ArrayDataModel implements DataModel<String> {
030            private final String[] names ;
031            private String[] values = null;
032    
033            /**
034             * 引数に名前配å?を指定したコンストラクター
035             *
036             * @param       nms     名前配å?
037             * @throws  IllegalArgumentException 引数の名前配å?ã�?null の場å�?
038             */
039            public ArrayDataModel( final String[] nms ) {
040                    if( nms == null ) {
041                            String errMsg = "引数の名前配å?に、null は設定できませんã€?;
042                            throw new IllegalArgumentException( errMsg );
043                    }
044    
045                    int size = nms.length ;
046                    names = new String[size] ;
047                    System.arraycopy( nms,0,names,0,size );
048            }
049    
050            /**
051             * row にあるセルの設定å?を置き換えますã?
052             *
053             * @param   vals  新しい配å?値ã€?
054             * @param   row   値が変更されるè¡?無視されまã�?
055             */
056            public void setValues( final String[] vals, final int row ) {
057                    int size = vals.length;
058                    values = new String[size];
059                    System.arraycopy( vals,0,values,0,size );
060            }
061    
062            /**
063             * カラãƒ?��に対応すã‚?カラãƒ?•ª号を返しますã?
064             *
065             * 特殊なカラãƒ?�ŒæŒ?®šされた場合ã?、è²??値を返しますã?
066             * 例えば、[KEY.カラãƒ?��]、[I]、[ROW.ID] など、特定ã?è²??値を返しますã?
067             * またã?カラãƒ?��がå?のãƒ??タモãƒ?ƒ«に存在しなã�??合も、è²??値かã?
068             * Exception を返しますã?è²??値なのかã?Exception なのかã?ã€?
069             * 実è£?�«依存しますã?
070             *
071             * @param       columnName      値が参照されるカラãƒ?��
072             *
073             * @return  æŒ?®šされたセルのカラãƒ?•ª号。存在しなければã€?1
074             * @throws  IllegalArgumentException 引数のカラãƒ?��ã�?null の場å�?
075             */
076            public int getColumnNo( final String columnName ) {
077                    if( columnName == null ) {
078                            String errMsg = "引数のカラãƒ?��に、null は設定できませんã€?;
079                            throw new IllegalArgumentException( errMsg );
080                    }
081    
082                    int address = -1;
083                    for( int i=0; i<names.length; i++ ) {
084                            if( columnName.equalsIgnoreCase( names[i] ) ) {
085                                    address = i;
086                                    break;
087                            }
088                    }
089    
090                    return address;
091            }
092    
093            /**
094             * カラãƒ?��配å?に対応すã‚?カラãƒ?•ª号配å?を返しますã?
095             *
096             * これはã€?getColumnNo( String ) に対する è¤?•°のカラãƒ?��を検索した
097             * 場合と同じですã?
098             *
099             * @param       clmNms  値が参照されるカラãƒ?��配å?
100             *
101             * @return  æŒ?®šされたセルのカラãƒ?•ª号配å?ã€?
102             */
103            public int[] getColumnNos( final String[] clmNms ) {
104                    if( clmNms == null ) {
105                            return new int[0];
106                    }
107    
108                    int[] clmNos = new int[clmNms.length];
109                    for( int j=0; j<clmNms.length; j++ ) {
110                            int address = -1;
111                            for( int i=0; i<names.length; i++ ) {
112                                    if( clmNms[j].equalsIgnoreCase( names[i] ) ) {
113                                            address = i;
114                                            break;
115                                    }
116                            }
117                            clmNos[j] = address;
118                    }
119    
120                    return clmNos;
121            }
122    
123            /**
124             * カラãƒ?��配å?を返しますã?
125             *
126             * @return      カラãƒ?��配å?
127             */
128            public String[] getNames() {
129                    return names.clone();
130            }
131    
132            /**
133             * row にあるセルの属æ?値をé?列で返しますã?
134             *
135             * @param   row     値が参照されるè¡?無視されまã�?
136             *
137             * @return  æŒ?®šされたセルの属æ?値
138             */
139            public String[] getValues( final int row ) {
140                    return values.clone();
141            }
142    
143            /**
144             * row および clm にあるセルの属æ?値をStringに変換して返しますã?
145             *
146             * @param   row     値が参照されるè¡?無視されまã�?
147             * @param   clm     値が参照されるå?
148             *
149             * @return  æŒ?®šされたセルの値
150             *
151             */
152            public String getValue( final int row, final int clm) {
153                    return values[clm];
154            }
155    
156            /**
157             * clm のNativeタイプを返しますã?
158             * Nativeタイプã?org.opengion.fukurou.model.NativeTypeで定義されてã�?�¾すã?
159             *
160             * @og.rev 4.1.1.2 (2008/02/28) 新規追åŠ?
161             * @og.rev 5.1.8.0 (2010/07/01) NativeType#getType(String) のメソãƒ?ƒ‰を使用するように変更ã€?
162             *
163             * @param  clm      値が参照されるå?
164             *
165             * @return Nativeタイãƒ?
166             * @see org.opengion.fukurou.model.NativeType
167             */
168            public NativeType getNativeType( final int clm ) {
169    //              return StringUtil.getNativeType( values[clm] );
170                    return NativeType.getType( values[clm] );
171            }
172    }