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.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 */
029public 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}