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.util;
017
018import java.util.List;
019import java.util.ArrayList;
020
021
022/**
023 * エラーメッセージを受け渡すときに使用するクラスです。
024 * 結果値として、0:正常 1:警告 2:異常 8:EXCEPTION 9:ORACLEエラー を持っています。
025 *
026 * @og.group エラー処理
027 *
028 * @version  4.0
029 * @author   Kazuhiko Hasegawa
030 * @since    JDK5.0,
031 */
032public final class ErrorMessage {
033        /** 改行コード */
034        public static final String CR = System.getProperty("line.separator");   // 5.1.9.0 (2010/08/01) 追加
035
036        /** バッファの初期容量を通常より多い目に設定します。  {@value}  */
037        public static final int BUFFER_MIDDLE = 200;                                                    // 5.1.9.0 (2010/08/01) 追加
038
039        /** 結果値 0:正常 {@value} */
040        public static final int OK        = 0;
041        /** 結果値 1:警告 {@value} */
042        public static final int WARNING   = 1;
043        /** 結果値 2:異常 {@value} */
044        public static final int NG        = 2;
045        /** 結果値 8:EXCEPTION {@value} */
046        public static final int EXCEPTION = 8;
047        /** 結果値 9:ORACLEエラー {@value} */
048        public static final int ORCL_ERR  = 9;
049
050        private int                     maxKekka        = OK;
051        private String          title           = "";
052        private final List<ErrMsg> list   = new ArrayList<ErrMsg>();
053
054        private boolean  setPgStep = false; // 3.8.9.5 (2007/09/12)
055
056        /**
057         * デフォルトコンストラクター
058         * 詳細メッセージを指定しないで ErrorMessage を構築します。
059         * (明示しないと、引き通付きコンストラクタのみのクラスになってしまいます。)
060         */
061        public ErrorMessage() {
062                setTitle( "NO TITLE" );
063        }
064
065        /**
066         * タイトルを指定して ErrorMessage を構築します。
067         *
068         * @param       title   タイトル
069         */
070        public ErrorMessage( final String title ) {
071                setTitle( title );
072        }
073
074        /**
075         * 指定されたエラー情報を追加登録します。
076         * これは、行番号0、結果:NG IDは無し(ゼロ文字列)です。
077         *
078         * @param    args String... メッセージの引数(可変引数)
079         */
080        public void addMessage( final String... args ) {
081                addMessage( 0,NG,"",args );
082        }
083
084        /**
085         * 指定されたエラー情報を追加登録します。
086         *
087         * @param       no      行番号
088         * @param       kekka   結果 0:正常 1:警告 2:異常
089         * @param       id      メッセージID
090         * @param    args String... メッセージの引数(可変引数)
091         */
092        public void addMessage( final int no,final int kekka,final String id,final String... args ) {
093                if( id != null ) {
094                        ErrMsg msg = new ErrMsg( no,kekka,null,null,id,args );
095                        list.add( msg );
096                        if( maxKekka < kekka ) {  maxKekka = kekka; }
097                }
098        }
099
100        /**
101         * 指定されたエラーオブジェクトを追加登録します。
102         * 追加するErrMsgが、内部の結果値より大きい場合は、その結果値にセットします。
103         * つまり、内部結果値は、登録されたすべてのエラーオブジェクトの最大値です。
104         *
105         * @param       msg     エラーオブジェクト
106         */
107        public void addMessage( final ErrMsg msg ) {
108                list.add( msg );
109                if( maxKekka < msg.getKekka() ) {  maxKekka = msg.getKekka(); }
110                if( msg.getPg() != null || msg.getStep() != null ) { setPgStep = true; }  // 3.8.9.5 (2007/09/12)
111        }
112
113        /**
114         * 指定された ErrorMessageオブジェクトを追加登録します。
115         * タイトルは元のまま変わりません。
116         * 現状の ErrorMessage の続きに、追加していきます。
117         * 引数の ErrorMessageオブジェクト が null の場合は,何もしません。
118         *
119         * @param   msg ErrorMessageオブジェクト
120         */
121        public void append( final ErrorMessage msg ) {
122                if( msg != null ) {
123                        if( maxKekka < msg.getKekka() ) {  maxKekka = msg.getKekka(); }
124
125                        ErrMsg[] emsg = msg.toArray();
126                        for( int i=0; i<emsg.length; i++ ) {
127                                list.add( emsg[i] );
128                        }
129                }
130        }
131
132        /**
133         * 指定された ErrorMessageオブジェクトを行番号指定で追加登録します。
134         * タイトルは元のまま変わりません。
135         * 現状の ErrorMessage の続きに、追加していきます。
136         * 引数の ErrorMessageオブジェクト が null の場合は,何もしません。
137         *
138         * @param       no      行番号
139         * @param   msg ErrorMessageオブジェクト
140         */
141        public void append( final int no,final ErrorMessage msg ) {
142                if( msg != null ) {
143                        if( maxKekka < msg.getKekka() ) {  maxKekka = msg.getKekka(); }
144
145                        ErrMsg[] emsg = msg.toArray();
146                        for( int i=0; i<emsg.length; i++ ) {
147                                list.add( emsg[i].copy( no ) );
148                        }
149                }
150        }
151
152        /**
153         *  このリスト内の要素を適切な順序で繰り返し処理する反復子を返します。
154         *
155         * @og.rev 4.0.0.0 (2004/12/31) 新規追加
156         * @og.rev 4.3.2.0 (2008/09/11) private ⇒ public に変更。
157         *
158         * @return  すべての要素を正しい順序で保持するErrMsg配列
159         */
160//      private ErrMsg[] toArray() {
161        public ErrMsg[] toArray() {
162                return list.toArray( new ErrMsg[list.size()] ) ;
163        }
164
165        /**
166         * リスト内のキーと値のマッピングの数を返します。
167         *
168         * @return   リスト内の size
169         */
170        public int size() {
171                return list.size() ;
172        }
173
174        /**
175         *  タイトルを返します。
176         *
177         * @return   タイトル
178         */
179        public String getTitle() {
180                return title;
181        }
182
183        /**
184         *  タイトルをセットします。
185         *
186         * @param       title   タイトル
187         */
188        public void setTitle( final String title ) {
189                this.title = title;
190        }
191
192        /**
193         *  指定の行の位置のエラー行番号を返します。
194         *
195         * @og.rev 4.3.2.0 (2008/09/11) 廃止
196         *
197         * @param    row 行の位置
198         *
199         * @return   行番号
200         */
201//      public int getNo( final int row ) {
202//              return list.get(row).getNo();
203//      }
204
205        /**
206         *  指定の行の位置のエラーオブジェクトを返します。
207         *
208         * @og.rev 4.0.0.0 (2004/12/31) 新規追加
209         * @og.rev 4.3.2.0 (2008/09/11) 廃止
210         *
211         * @param    row 行の位置
212         *
213         * @return   エラーオブジェクト
214         */
215//      public ErrMsg getErrMsg( final int row ) {
216//              return list.get(row);
217//      }
218
219        /**
220         *  このエラーメッセージの中で、最大の結果値(エラーの最大レベル)を返します。
221         *
222         * @return   結果   OK, WARNING, NG, ORCL_ERR
223         */
224        public int getKekka() {
225                return maxKekka;
226        }
227
228        /**
229         *  指定の行の位置の結果を返します。
230         *
231         * @og.rev 4.3.2.0 (2008/09/11) 廃止
232         *
233         * @param    row 行の位置
234         *
235         * @return   結果  OK, WARNING, NG, EXCEPTION , ORCL_ERR
236         */
237//      public int getKekka( final int row ) {
238//              return list.get(row).getKekka();
239//      }
240
241        /**
242         *  すべてのメッセージが 正常(OK)かを返します。
243         *
244         * @return   結果 すべてOK:true / それ以外 false
245         */
246        public boolean isOK() {
247                return ( maxKekka == OK );
248        }
249
250        /**
251         *  指定の行の位置のエラーコードIDを返します。
252         *
253         * @og.rev 4.3.2.0 (2008/09/11) 廃止
254         *
255         * @param    row 行の位置
256         *
257         * @return   エラーコードID
258         */
259//      public String getId( final int row ) {
260//              return list.get(row).getId();
261//      }
262
263        /**
264         *  指定の行の位置のPG名を返します。
265         *
266         * @og.rev 3.8.9.5 (2007/09/12) 新規作成
267         * @og.rev 4.3.2.0 (2008/09/11) 廃止
268         *
269         * @param    row 行の位置
270         *
271         * @return   PG名
272         */
273//      public String getPg( final int row ) {
274////            return ((ErrMsg)list.get(row)).getPg();
275//              return (list.get(row)).getPg();         // 4.0.0.0 (2007/09/25)
276//      }
277
278        /**
279         *  指定の行の位置のステップ名を返します。
280         *
281         * @og.rev 3.8.9.5 (2007/09/12) 新規作成
282         * @og.rev 4.3.2.0 (2008/09/11) 廃止
283         *
284         * @param    row 行の位置
285         *
286         * @return   ステップ名
287         */
288//      public String getStep( final int row ) {
289////            return ((ErrMsg)list.get(row)).getStep();
290//              return (list.get(row)).getStep();               // 4.0.0.0 (2007/09/25)
291//      }
292
293        /**
294         *  配列中にPG名またはステップ名が設定されているかを返します。
295         *
296         * @og.rev 3.8.9.5 (2007/09/12) 新規作成
297         *
298         * @return   PG名またはステップ名が設定されているか
299         */
300        public boolean isSetPgStep() {
301                return setPgStep;
302        }
303
304        /**
305         *  メッセージの連結リストを返します。
306         *
307         * @return   メッセージの連結リスト
308         */
309        @Override
310        public String toString() {
311//              StringBuilder rtn = new StringBuilder( HybsSystem.BUFFER_MIDDLE );
312                StringBuilder rtn = new StringBuilder( BUFFER_MIDDLE );
313//              rtn.append( getTitle() ).append( HybsSystem.CR );
314                rtn.append( getTitle() ).append( CR );
315                ErrMsg[] msg = list.toArray( new ErrMsg[list.size()] );
316                for( int i=0; i<msg.length; i++ ) {
317                        rtn.append( msg[i] );
318//                      rtn.append( HybsSystem.CR );
319                        rtn.append( CR );
320                }
321                return rtn.toString();
322        }
323}