View Javadoc
1   package uk.ac.ebi.intenz.webapp.controller;
2   
3   import uk.ac.ebi.intenz.mapper.EnzymeEntryMapper;
4   import uk.ac.ebi.intenz.webapp.utilities.IntEnzMessenger;
5   import uk.ac.ebi.rhea.mapper.MapperException;
6   
7   import javax.servlet.RequestDispatcher;
8   import javax.servlet.ServletContext;
9   import javax.servlet.ServletException;
10  import javax.servlet.http.HttpServletRequest;
11  import javax.servlet.http.HttpServletResponse;
12  import java.io.IOException;
13  import java.sql.Connection;
14  import java.sql.SQLException;
15  
16  import org.apache.log4j.Logger;
17  
18  /**
19   * A database command always relies on a connection to the database.
20   *
21   * @author Michael Darsow
22   * @version 0.9 - 21-July-2003
23   */
24  public abstract class DatabaseCommand extends Command {
25  
26     Logger LOGGER = Logger.getLogger(DatabaseCommand.class);
27  
28    protected final String databaseErrorMessage = "\nIt was not possible to retrieve any information from the database. " +
29            "\nIt would be very kind of you, if you could report this error to " +
30            "<a href=\"http://www.ebi.ac.uk/support/\">EBI Support</a>.\n" +
31            "Sorry for any inconvenience this error might have caused.";
32  
33    protected ServletContext context;
34  
35    protected HttpServletRequest request;
36  
37    protected HttpServletResponse response;
38  
39    protected Connection con;
40  
41  	protected final EnzymeEntryMapper enzymeEntryMapper =
42  			new EnzymeEntryMapper();
43  
44    /**
45     * Initialises the attributes which will be used to extract information for the command execution.
46     *
47     * @param context  The servlet context.
48     * @param request  The request object.
49     * @param response The response object.
50     * @param con      A logical connection.
51     */
52    public void init(ServletContext context, HttpServletRequest request, HttpServletResponse response, Connection con) {
53      this.context = context;
54      this.request = request;
55      this.response = response;
56      this.con = con;
57    }
58  
59    /**
60     * Forwards to the given target.
61     *
62     * @param target A valid URI.
63     * @throws ServletException
64     * @throws IOException
65     */
66    protected void forward(String target) throws ServletException, IOException {
67      closeConnection();
68      RequestDispatcher dispatcher = context.getRequestDispatcher(target);
69      dispatcher.forward(request, response);
70    }
71  
72    /**
73     * Closes the database connection or forwards to an error page if any error occured.
74     * 
75     * @throws ServletException
76     * @throws IOException
77     */
78    protected void closeConnection() throws ServletException, IOException {
79      try {
80  
81         if (con != null && !con.isClosed()){
82            LOGGER.debug("Closing connection");
83           con.close();
84         }
85      } catch (SQLException e) {
86         LOGGER.error("Closing connection", e);
87        IntEnzMessenger.sendError(this.getClass().toString(), e.getMessage(), (String) request.getSession().getAttribute("user"));
88        request.setAttribute("message", "The following database error occured:\n" + e.getMessage() +
89                this.databaseErrorMessage);
90        RequestDispatcher dispatcher = context.getRequestDispatcher("/error.jsp");
91        dispatcher.forward(request, response);
92      }
93    }
94    
95  	protected void close() {
96  		enzymeEntryMapper.close();
97  	}
98  
99  	@Override
100 	protected void finalize() throws Throwable {
101 		close();
102 	}
103 
104 }