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        @Override
057        public void setValues( final String[] vals, final int row ) {
058                int size = vals.length;
059                values = new String[size];
060                System.arraycopy( vals,0,values,0,size );
061        }
062
063        /**
064         * カラム名に対応する カラム番号を返します。
065         *
066         * 特殊なカラムが指定された場合は、負の値を返します。
067         * 例えば、[KEY.カラム名]、[I]、[ROW.ID] など、特定の負の値を返します。
068         * また、カラム名が元のデータモデルに存在しない場合も、負の値か、
069         * Exception を返します。負の値なのか、Exception なのかは、
070         * 実装に依存します。
071         *
072         * @param       columnName      値が参照されるカラム名
073         *
074         * @return  指定されたセルのカラム番号。存在しなければ、-1
075         * @throws  IllegalArgumentException 引数のカラム名が null の場合
076         */
077        @Override
078        public int getColumnNo( final String columnName ) {
079                if( columnName == null ) {
080                        String errMsg = "引数のカラム名に、null は設定できません。";
081                        throw new IllegalArgumentException( errMsg );
082                }
083
084                int address = -1;
085                for( int i=0; i<names.length; i++ ) {
086                        if( columnName.equalsIgnoreCase( names[i] ) ) {
087                                address = i;
088                                break;
089                        }
090                }
091
092                return address;
093        }
094
095        /**
096         * カラム名配列に対応する カラム番号配列を返します。
097         *
098         * これは、#getColumnNo( String ) に対する 複数のカラム名を検索した
099         * 場合と同じです。
100         *
101         * @param       clmNms  値が参照されるカラム名配列
102         *
103         * @return  指定されたセルのカラム番号配列。
104         */
105        public int[] getColumnNos( final String[] clmNms ) {
106                if( clmNms == null ) {
107                        return new int[0];
108                }
109
110                int[] clmNos = new int[clmNms.length];
111                for( int j=0; j<clmNms.length; j++ ) {
112                        int address = -1;
113                        for( int i=0; i<names.length; i++ ) {
114                                if( clmNms[j].equalsIgnoreCase( names[i] ) ) {
115                                        address = i;
116                                        break;
117                                }
118                        }
119                        clmNos[j] = address;
120                }
121
122                return clmNos;
123        }
124
125        /**
126         * カラム名配列を返します。
127         *
128         * @return      カラム名配列
129         */
130        @Override
131        public String[] getNames() {
132                return names.clone();
133        }
134
135        /**
136         * row にあるセルの属性値を配列で返します。
137         *
138         * @param   row     値が参照される行(無視されます)
139         *
140         * @return  指定されたセルの属性値
141         */
142        @Override
143        public String[] getValues( final int row ) {
144                return values.clone();
145        }
146
147        /**
148         * row および clm にあるセルの属性値をStringに変換して返します。
149         *
150         * @param   row     値が参照される行(無視されます)
151         * @param   clm     値が参照される列
152         *
153         * @return  指定されたセルの値
154         *
155         */
156        @Override
157        public String getValue( final int row, final int clm) {
158                return values[clm];
159        }
160
161        /**
162         * clm のNativeタイプを返します。
163         * Nativeタイプはorg.opengion.fukurou.model.NativeTypeで定義されています。
164         *
165         * @og.rev 4.1.1.2 (2008/02/28) 新規追加
166         * @og.rev 5.1.8.0 (2010/07/01) NativeType#getType(String) のメソッドを使用するように変更。
167         *
168         * @param  clm      値が参照される列
169         *
170         * @return Nativeタイプ
171         * @see org.opengion.fukurou.model.NativeType
172         */
173        @Override
174        public NativeType getNativeType( final int clm ) {
175                return NativeType.getType( values[clm] );
176        }
177}