View Javadoc
1   package uk.ac.ebi.intenz.webapp.controller;
2   
3   import java.io.IOException;
4   import java.sql.SQLException;
5   import java.util.List;
6   import java.util.Map;
7   import java.util.PropertyResourceBundle;
8   
9   import javax.servlet.ServletContext;
10  import javax.servlet.ServletException;
11  
12  import org.apache.log4j.Logger;
13  
14  import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry;
15  import uk.ac.ebi.intenz.domain.exceptions.DomainException;
16  import uk.ac.ebi.intenz.webapp.utilities.IntEnzMessenger;
17  import uk.ac.ebi.rhea.mapper.MapperException;
18  
19  /**
20   * This command handles requests about proposed entries.
21   * <p/>
22   * At the moment only requests for the whole list are processed.
23   * 
24   * @author Michael Darsow
25   * @version 0.9 - 21-July-2003
26   */
27  public class SearchProposedCommand extends DatabaseCommand {
28  
29  	public static final Logger LOGGER = Logger
30  			.getLogger(SearchProposedCommand.class);
31  
32  	/**
33  	 * TBD
34  	 * 
35  	 * @throws ServletException
36  	 * @throws IOException
37  	 */
38  	public void process() throws ServletException, IOException {
39  		String id = request.getParameter("id");
40  
41  		if (id == null) {
42  			List<?> proposedEntries = findProposedList();
43  			if (proposedEntries.size() == 0) {
44  				request.setAttribute("message",
45  						"There are no proposed entries stored in the database at the moment.");
46  			}
47  			request.setAttribute("proposed", proposedEntries);
48  			forward("/proposed.jsp");
49  		} else {
50  			EnzymeEntry proposedEntry = findProposedEntry(new Long(id));
51  			if (proposedEntry != null) {
52  				request.setAttribute("result", proposedEntry);
53  				forward("/entry.jsp");
54  			} else {
55  				if (request.getAttribute("message") != null) {
56  					forward("/error.jsp");
57  				} else {
58  					request.setAttribute("message",
59  							"The requested proposed entry does not exist.");
60  					forward("/error.jsp"); // No result found.
61  				}
62  			}
63  		}
64  	}
65  
66  	// ------------------- PRIVATE METHODS ------------------------
67  
68  	/**
69  	 * Returns the list of all proposed entries.
70  	 * <p/>
71  	 * The method returns <code>null</code> if an error occured or no entries
72  	 * have been found.
73  	 * 
74  	 * @return the list of proposed entries or <code>null</code> if an error
75  	 *         occured.
76  	 */
77  	private List<EnzymeEntry> findProposedList() {
78  		ServletContext application = request.getSession().getServletContext();
79  		@SuppressWarnings("unchecked")
80  		List<EnzymeEntry> proposedList = (List<EnzymeEntry>) application
81  				.getAttribute("proposedList");
82  
83  		if (proposedList == null) {
84  			try {
85  				proposedList = enzymeEntryMapper.findProposedList(con);
86  				if (proposedList != null) {
87  					application.setAttribute("proposedList", proposedList);
88  				}
89  			} catch (DomainException e) {
90  				LOGGER.error("Finding proposed list", e);
91  				PropertyResourceBundle intenzMessages = (PropertyResourceBundle) request
92  						.getSession().getServletContext()
93  						.getAttribute("intenz.messages");
94  				IntEnzMessenger.sendError(this.getClass().toString(),
95  						intenzMessages.getString(e.getMessageKey()),
96  						(String) request.getSession().getAttribute("user"));
97  				request.setAttribute("message", e.getMessage());
98  				return null;
99  			} catch (Exception e) {
100 				LOGGER.error("Finding proposed list", e);
101 				IntEnzMessenger.sendError(this.getClass().toString(), e
102 						.getMessage(), (String) request.getSession()
103 						.getAttribute("user"));
104 				request.setAttribute("message", e.getMessage());
105 				return null;
106 			}
107 		}
108 
109 		return proposedList;
110 	}
111 
112 	/**
113 	 * Returns the proposed entry requested by the given ID.
114 	 * 
115 	 * @param id
116 	 *            The enzyme ID of the proposed entry.
117 	 * @return The proposed entry or <code>null</code> if an error occured.
118 	 */
119 	private EnzymeEntry findProposedEntry(Long id) {
120 		EnzymeEntry enzymeEntry = null;
121 		ServletContext application = request.getSession().getServletContext();
122 		@SuppressWarnings("unchecked")
123 		Map<Long, EnzymeEntry> proposedEntries = (Map<Long, EnzymeEntry>) application
124 				.getAttribute("proposedEntries");
125 
126 		// Check if object already stored in the cache.
127 		if (proposedEntries.containsKey(id)) {
128 			enzymeEntry = (EnzymeEntry) proposedEntries.get(id);
129 		} else {
130 			try {
131 				enzymeEntry = enzymeEntryMapper.findById(id, con);
132 				if (enzymeEntry != null) {
133 					proposedEntries.put(id, enzymeEntry);
134 				}
135 			} catch (SQLException e) {
136 				IntEnzMessenger.sendError(this.getClass().toString(), e
137 						.getMessage(), (String) request.getSession()
138 						.getAttribute("user"));
139 				request.setAttribute("message", e.getMessage());
140 			} catch (MapperException e) {
141 				IntEnzMessenger.sendError(this.getClass().toString(), e
142 						.getMessage(), (String) request.getSession()
143 						.getAttribute("user"));
144 				request.setAttribute("message", e.getMessage());
145 			} catch (DomainException e) {
146 				LOGGER.error("Finding proposed entry", e);
147 				PropertyResourceBundle intenzMessages = (PropertyResourceBundle) request
148 						.getSession().getServletContext()
149 						.getAttribute("intenz.messages");
150 				IntEnzMessenger.sendError(this.getClass().toString(),
151 						intenzMessages.getString(e.getMessageKey()),
152 						(String) request.getSession().getAttribute("user"));
153 				request.setAttribute("message", e.getMessage());
154 			} finally {
155 				try {
156 					con.close();
157 				} catch (SQLException e) {
158 					IntEnzMessenger.sendError(this.getClass().toString(), e
159 							.getMessage(), (String) request.getSession()
160 							.getAttribute("user"));
161 					request.setAttribute("message", e.getMessage());
162 					return null;
163 				}
164 			}
165 		}
166 		return enzymeEntry;
167 	}
168 }