1 package uk.ac.ebi.intenz.webapp.controller.modification;
2
3 import java.sql.Connection;
4 import java.sql.SQLException;
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse;
7
8 import org.apache.log4j.Logger;
9 import org.apache.struts.action.*;
10 import uk.ac.ebi.intenz.domain.constants.Event;
11 import uk.ac.ebi.intenz.domain.constants.Status;
12 import uk.ac.ebi.intenz.domain.enzyme.EnzymeCommissionNumber;
13 import uk.ac.ebi.intenz.domain.enzyme.EnzymeCommissionNumber.Type;
14 import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry;
15 import uk.ac.ebi.intenz.domain.exceptions.EcException;
16 import uk.ac.ebi.intenz.mapper.AuditPackageMapper;
17 import uk.ac.ebi.intenz.mapper.EnzymeEntryMapper;
18 import uk.ac.ebi.intenz.mapper.EventPackageMapper;
19 import uk.ac.ebi.intenz.mapper.HistoryEventMapper;
20 import uk.ac.ebi.intenz.webapp.dtos.EcSearchForm;
21 import uk.ac.ebi.intenz.webapp.dtos.EnzymeDTO;
22 import uk.ac.ebi.intenz.webapp.exceptions.DeregisterException;
23 import uk.ac.ebi.intenz.webapp.utilities.UnitOfWork;
24
25
26
27
28
29
30
31 public class CreateEntryAction extends CurationAction {
32 private static final Logger LOGGER =
33 Logger.getLogger(CreateEntryAction.class.getName());
34 private final static String SEARCH_BY_EC_ACTION_FWD = "searchEc";
35
36 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
37 HttpServletResponse response) throws Exception {
38
39 ActionForward forward = super.execute(mapping, form, request, response);
40 if (forward != null) return forward;
41
42 EnzymeDTO enzymeDTO = (EnzymeDTO) form;
43 Connection con = (Connection) request.getSession().getAttribute("connection");
44 ActionMessages errors = new ActionMessages();
45 UnitOfWork unitOfWork = (UnitOfWork) request.getSession().getAttribute("uow");
46 try {
47 EnzymeCommissionNumber ec = EnzymeCommissionNumber.valueOf(enzymeDTO.getEc());
48 if (!isValidEc(ec, errors, con)) {
49 saveErrors(request, errors);
50 return mapping.getInputForward();
51 }
52 enzymeDTO.setEc(ec.toString());
53 if (ec.getType().equals(Type.PRELIMINARY)){
54
55 enzymeDTO.setStatusCode(Status.PRELIMINARY.getCode());
56 }
57
58
59 AuditPackageMapper auditPackageMapper = new AuditPackageMapper();
60 auditPackageMapper.setRemark(AuditPackageMapper.STANDARD_REMARK, con);
61
62
63 LOGGER.info("Committing form data.");
64 unitOfWork.commit(enzymeDTO, con);
65 LOGGER.info("Data subimtted");
66
67 if (enzymeDTO.isActive()){
68
69 if (!ec.getType().equals(Type.PRELIMINARY)){
70 EventPackageMapper eventPackageMapper = new EventPackageMapper();
71 eventPackageMapper.insertFutureCreationEvent(new Long(enzymeDTO.getId()), con);
72 }
73 } else {
74
75 HistoryEventMapper hem = new HistoryEventMapper();
76 Long enzymeId = Long.valueOf(enzymeDTO.getId());
77 hem.insertEvent(Event.CREATION, null, enzymeId, null, con);
78
79
80 Thread.sleep(1000);
81 if (enzymeDTO.getTransferredToEc() != null
82 && enzymeDTO.getTransferredToEc().length() > 0){
83
84 EnzymeEntry targetEntry = new EnzymeEntryMapper().findByEc(
85 enzymeDTO.getTransferredToEc(), Status.APPROVED, con);
86 hem.insertEvent(Event.TRANSFER, enzymeId, targetEntry.getId(),
87 enzymeDTO.getLatestHistoryEventNote(), con);
88 } else {
89
90 hem.insertEvent(Event.DELETION, enzymeId, null,
91 enzymeDTO.getLatestHistoryEventNote(), con);
92 }
93 }
94
95 con.commit();
96
97 LOGGER.info("Create event finished.");
98 } catch (SQLException e){
99 con.rollback();
100 throw e;
101 } catch (EcException e) {
102 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
103 "errors.application.ec.detail", e.getMessage()));
104 saveErrors(request, errors);
105 keepToken(request);
106 return mapping.getInputForward();
107 } catch (DeregisterException e) {
108 LOGGER.error(e.getMessage());
109
110 throw e;
111 }
112
113 LOGGER.info("Searching for EC " + enzymeDTO.getEc());
114 EcSearchForm ecSearchForm = new EcSearchForm();
115 ecSearchForm.setEc(enzymeDTO.getEc());
116 request.setAttribute("ecSearchForm", ecSearchForm);
117 return mapping.findForward(SEARCH_BY_EC_ACTION_FWD);
118
119 }
120
121
122
123
124
125
126
127
128
129
130 private boolean isValidEc(EnzymeCommissionNumber ec, ActionMessages errors, Connection con) throws SQLException {
131 assert ec != null : "Parameter 'ec' must not be null.";
132 assert errors != null : "Parameter 'errors' must not be null.";
133 assert con != null : "Parameter 'con' must not be null.";
134
135 try {
136
137 if (EnzymeEntryMapper.ecExists(ec, con)) {
138 errors.add("ec", new ActionMessage("errors.application.ec.exists", ec.toString()));
139 return false;
140 }
141
142
143 EnzymeCommissionNumber subclassEc = EnzymeCommissionNumber.valueOf(ec.getEc1(), ec.getEc2());
144 final boolean subSubclassExists = EnzymeEntryMapper.ecExists(subclassEc, con);
145 if (!subSubclassExists) {
146 errors.add("ec", new ActionMessage("errors.application.ec.nosubsubclass", ec.toString()));
147 return subSubclassExists;
148 }
149 } catch (EcException e) {
150 errors.add("ec", new ActionMessage("errors.application.ec.invalid"));
151 }
152 return true;
153 }
154 }