Salome HOME
Promote/Review/Validate functionality for author of the study is improved.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / exception / AbstractException.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            Id: 
5  * Creation date   02.10.2012
6  * @author         Author: Maria KRUCHININA
7  * @version        Revision: 
8  *****************************************************************************/
9
10 package org.splat.exception; 
11
12 import org.splat.i18n.I18nUtils;
13
14 /**
15  * Superclass for all SIMAN application exceptions.
16  * @author Maria KRUCHININA
17  *
18  */
19 public class AbstractException extends Exception {
20
21         /**
22          * serial id.
23          */
24         private static final long serialVersionUID = -96254753042743175L;
25
26         /**
27          * message key to write to user interface.
28          */
29         private String _messageKey = null;
30
31         /**
32          * message key to write to user interface.
33          */
34         private Object[] _context = new Object[0];
35
36         /**
37          * default constructor.
38          */
39         public AbstractException() {
40                 super();
41         }
42
43         /**
44          * constructor with one parameter :the original throwable.
45          * 
46          * @param throwable
47          *            the original throwable
48          * @see java.lang.Exception#Exception(Throwable)
49          */
50         public AbstractException(final Throwable throwable) {
51                 super(throwable);
52         }
53
54         /**
55          * constructor with two parameters : - the message - a throwable.
56          * 
57          * @param message
58          *            the error message
59          * @param throwable
60          *            the original throwable
61          * @see java.lang.Exception#Exception(String, Throwable)
62          */
63         public AbstractException(final String message, final Throwable throwable) {
64                 super(I18nUtils.getMessageLocaleDefault(message), throwable);
65                 this._messageKey = message;
66         }
67
68         /**
69          * Build a ServiceException with message, user message key, and throwable.
70          * CAUTION: this only works with 1 log-messages.properties 
71          * 
72          * @param messageKey
73          *            the User Interface Message Key
74          * @param throwable
75          *            the original exception
76          * @param context
77          *            the execution context
78          */
79         public AbstractException(final String messageKey,
80                         final Throwable throwable, final Object... context) {
81                 // read the CAUTION above
82                 super(I18nUtils.getMessageLocaleDefault(messageKey, context), throwable);
83                 this._messageKey = messageKey;
84                 this._context = context.clone();
85
86         }
87
88         /**
89          * Build a ServiceException with message, user message key and context.
90          * CAUTION: this only works with 1 log-messages.properties 
91          * 
92          * @param messageKey
93          *            the User Interface Message Key
94          * @param context
95          *            the execution context
96          */
97         public AbstractException(final String messageKey, final Object... context) {
98                 // read the CAUTION above
99                 super(I18nUtils.getMessageLocaleDefault(messageKey, context));
100                 this._messageKey = messageKey;
101                 this._context = context.clone();
102         }
103
104         /**
105          * get the exception message key.
106          * 
107          * @return the messageKey
108          */
109         public String getMessageKey() {
110                 return _messageKey;
111         }
112
113         /**
114          * set the exception message key.
115          * 
116          * @param key
117          *            the messageKey to set
118          */
119         public void setMessageKey(final String key) {
120                 _messageKey = key;
121         }
122
123         /**
124          * get the context.
125          * 
126          * @return the context
127          */
128         public Object[] getContext() {
129                 return _context.clone();
130         }
131
132         /**
133          * Converts the object context array into a string array.
134          * @return a string array
135          */
136         public String[] getContextAsStringArray() {
137                 String[] result = null;
138                 
139                 if (null != _context) {
140                         result = 
141                                 new String[_context.length];
142                         int i = 0;
143                         for (Object o : _context) {
144                                 if (null == o) {
145                                         result[i] = null;
146                                 } else {
147                                         result[i] = o.toString();
148                                 }
149                                 i++;
150                         }
151                 } 
152                 
153                 return result;
154         }
155
156         /**
157          * set the context.
158          * 
159          * @param context
160          *            the context to set
161          */
162         public void setContext(final Object[] context) {
163                 this._context = context.clone();
164         }
165
166 }