View Javadoc

1   /**
2    * - Digitalis Internal Framework v2.0 - (C) 2007, Digitalis Informatica. Distribuicao e Gestao de Informatica, Lda.
3    * Estrada de Paco de Arcos num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999
4    * http://www.digitalis.pt
5    */
6   package pt.digitalis.dif.presentation.entities.system.error;
7   
8   import java.sql.SQLException;
9   import java.util.Map;
10  
11  import org.hibernate.HibernateException;
12  import org.hibernate.JDBCException;
13  import org.hibernate.LazyInitializationException;
14  import org.hibernate.MappingException;
15  import org.hibernate.QueryException;
16  import org.hibernate.TransactionException;
17  import org.hibernate.exception.SQLGrammarException;
18  import org.hibernate.jdbc.BatchFailedException;
19  
20  import pt.digitalis.dif.controller.ExceptionHandlers;
21  import pt.digitalis.dif.controller.interfaces.IDIFContext;
22  import pt.digitalis.dif.dem.annotations.entities.StageDefinition;
23  import pt.digitalis.dif.dem.annotations.stage.Context;
24  import pt.digitalis.dif.dem.annotations.stage.ExceptionHandler;
25  import pt.digitalis.dif.dem.annotations.stage.InjectMessages;
26  import pt.digitalis.dif.dem.annotations.stage.View;
27  import pt.digitalis.dif.dem.annotations.stage.controller.DispatcherMode;
28  
29  /**
30   * The default DiF Hibernate Error page
31   * 
32   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
33   * @created Dec 20, 2007
34   */
35  @StageDefinition(name = "Database Error Stage", service = "errorhandlingservice")
36  @ExceptionHandler(exceptions = "org.hibernate.HibernateException,java.sql.SQLException")
37  @View(target = "internal/databaseError.jsp")
38  @DispatcherMode(authenticate = false, authorize = false)
39  public class HibernateErrorStage extends AbstractErrorHandler {
40  
41      /** the current stage context */
42      @Context
43      protected IDIFContext context;
44  
45      /** the message text */
46      private String messageContent = null;
47  
48      /** the current stage messages */
49      @InjectMessages
50      protected Map<String, String> messages;
51  
52      /** the message title */
53      private String messageTitle = null;
54  
55      /** the message type */
56      private String messageType = null;
57  
58      /**
59       * Inspector for the 'messageContent' attribute.
60       * 
61       * @return the messageContent value
62       */
63      public String getMessageContent()
64      {
65          if (messageContent == null)
66              setMessage();
67  
68          return messageContent;
69      }
70  
71      /**
72       * Inspector for the 'messageTitle' attribute.
73       * 
74       * @return the messageTitle value
75       */
76      public String getMessageTitle()
77      {
78          if (messageTitle == null)
79              setMessage();
80  
81          return messageTitle;
82      }
83  
84      /**
85       * Inspector for the 'messageType' attribute.
86       * 
87       * @return the messageType value
88       */
89      public String getMessageType()
90      {
91          if (messageType == null)
92              setMessage();
93  
94          return messageType;
95      }
96  
97      /**
98       * Analises que given exception and determines the message to show
99       */
100     private void setMessage()
101     {
102         Exception originalException = (Exception) context.getRequest().getAttribute(
103                 ExceptionHandlers.RAISED_EXCEPTION_ATTRIBUTE);
104         Exception exception = null;
105 
106         // Defaults
107         messageType = "warn";
108         messageContent = messages.get("page.desc");
109         messageTitle = messages.get("messageTitle");
110 
111         exception = getExceptionWithinStack(HibernateException.class, originalException);
112         if (exception == null)
113             exception = getExceptionWithinStack(SQLException.class, originalException);
114 
115         // Found a HibernateException. Analize it...
116         if (exception != null)
117         {
118             if (exception instanceof MappingException)
119             {
120                 messageTitle = messages.get("messageTitleOutOfDate");
121                 messageContent = messages.get("messageContentOutOfDate");
122             }
123             else if (exception instanceof LazyInitializationException || exception instanceof SQLGrammarException
124                     || exception instanceof QueryException || exception instanceof TransactionException)
125             {
126                 messageTitle = messages.get("messageTitleReadingError");
127                 messageContent = messages.get("messageContentReadingError");
128             }
129             else if (exception instanceof BatchFailedException || exception instanceof JDBCException
130                     || exception instanceof JDBCException || exception instanceof SQLException)
131             {
132                 String error = (exception.getCause() == null) ? exception.toString() : exception.getCause().toString();
133                 if (error.contains("ORA-20000:"))
134                 {
135                     messageTitle = messages.get("messageTitleORA20000");
136                     error = error.substring(error.indexOf("ORA-20000:") + 10);
137                     if (error.contains("ORA-"))
138                     {
139                         String[] errorSplit = error.split("ORA-");
140                         error = errorSplit[0];
141                     }
142 
143                     messageContent = error;
144                 }
145                 else if (error.contains("ORA-"))
146                 {
147                     String errorOra = "";
148                     String descError;
149                     boolean firstError = true;
150                     for (String errorSplit: error.split("ORA-"))
151                     {
152                         if (!firstError)
153                             errorOra += "-";
154                         descError = errorSplit.substring(errorSplit.indexOf(":") + 1).trim();
155 
156                         if (!"".equals(descError))
157                         {
158                             errorOra += " <span class='bold'> " + errorSplit.substring(errorSplit.indexOf(":") + 1)
159                                     + "</span><br>";
160                             firstError = false;
161                         }
162                     }
163                     messageTitle = messages.get("messageTitleSaveError");
164                     messageContent = errorOra + "<br>" + messages.get("messageContentSaveError");
165                 }
166                 else
167                 {
168                     messageTitle = messages.get("messageTitleSaveError");
169                     messageContent = messages.get("messageContentSaveError");
170                 }
171             }
172             else
173             {
174                 messageTitle = messages.get("messageTitleErroBD");
175                 messageContent = messages.get("messageContentErroBD");
176             }
177         }
178     }
179 }