View Javadoc
1   package uk.ac.ebi.intenz.webapp.controller.helper.dtoinit;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.List;
6   
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpServletResponse;
9   
10  import org.apache.log4j.Logger;
11  import org.apache.struts.action.Action;
12  import org.apache.struts.action.ActionForm;
13  import org.apache.struts.action.ActionForward;
14  import org.apache.struts.action.ActionMapping;
15  
16  import uk.ac.ebi.intenz.domain.constants.EventConstant;
17  import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry;
18  import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntryEcComparator;
19  import uk.ac.ebi.intenz.domain.enzyme.EnzymeName;
20  import uk.ac.ebi.intenz.domain.enzyme.EnzymeSubSubclass;
21  import uk.ac.ebi.intenz.domain.history.HistoryEvent;
22  import uk.ac.ebi.intenz.webapp.dtos.EnzymeSubSubclassDTO;
23  import uk.ac.ebi.intenz.webapp.dtos.GhostEnzymeDTO;
24  import uk.ac.ebi.intenz.webapp.utilities.IntEnzUtilities;
25  import uk.ac.ebi.xchars.SpecialCharacters;
26  import uk.ac.ebi.xchars.domain.EncodingType;
27  
28  /**
29   * This Action ...
30   *
31   * @author Michael Darsow
32   * @version $Revision: 1.2 $ $Date: 2008/01/28 12:33:00 $
33   */
34  public class PopulateSubSubclassDTOAction extends Action {
35  
36    private static final Logger LOGGER =
37  	  Logger.getLogger(PopulateSubSubclassDTOAction.class.getName());
38  
39    public ActionForward execute(ActionMapping mapping,
40                                 ActionForm form,
41                                 HttpServletRequest request,
42                                 HttpServletResponse response) throws Exception {
43      LOGGER.debug("PopulateSubSubclassDTOAction");
44  
45      // Get special characters instance (set in the servlet handler's process method).
46      SpecialCharacters encoding = (SpecialCharacters) request.getSession().getAttribute("characters");
47  
48      // TODO: provide means to choose the view and the corresponding encoding type.
49      EncodingType encodingType = null;
50  
51      // Populate form.
52      final EnzymeSubSubclass enzymeSubSubclass = (EnzymeSubSubclass) request.getAttribute("result");
53      populateForm(enzymeSubSubclass, form, encoding, encodingType);
54  
55      // Save page title.
56      request.setAttribute("title", "EC " + enzymeSubSubclass.getEc());
57  
58      return mapping.findForward("" + request.getAttribute("forward"));
59    }
60  
61    private void populateForm(EnzymeSubSubclass enzymeSubSubclass, ActionForm form, SpecialCharacters encoding,
62                              EncodingType encodingType) {
63      LOGGER.debug("Populating EnzymeSubSubclassDTO ...");
64      EnzymeSubSubclassDTO enzymeSubSubclassDTO = (EnzymeSubSubclassDTO) form;
65  
66      enzymeSubSubclassDTO.setEc(enzymeSubSubclass.getEc().toString());
67      enzymeSubSubclassDTO.setName(encoding.xml2Display(enzymeSubSubclass.getName(), encodingType));
68      enzymeSubSubclassDTO.setDescription(encoding.xml2Display(enzymeSubSubclass.getDescription(), encodingType));
69      enzymeSubSubclassDTO.setClassName(encoding.xml2Display(enzymeSubSubclass.getClassName(), encodingType));
70      enzymeSubSubclassDTO.setClassEc("" + enzymeSubSubclass.getEc().getEc1());
71      enzymeSubSubclassDTO.setSubclassName(encoding.xml2Display(enzymeSubSubclass.getSubclassName(), encodingType));
72      enzymeSubSubclassDTO.setSubclassEc(enzymeSubSubclass.getEc().getEc1() + "." + enzymeSubSubclass.getEc().getEc2());
73  
74      List<EnzymeEntry> enzymeEntries = enzymeSubSubclass.getEntries();
75      Collections.sort(enzymeEntries, new EnzymeEntryEcComparator());
76      List<GhostEnzymeDTO> ghostEntries = new ArrayList<GhostEnzymeDTO>();
77      for (int iii = 0; iii < enzymeEntries.size(); iii++) {
78        EnzymeEntry enzymeEntry = (EnzymeEntry) enzymeEntries.get(iii);
79        GhostEnzymeDTO ghostEnzymeDTO = new GhostEnzymeDTO();
80        ghostEnzymeDTO.setEnzymeId(enzymeEntry.getId().toString());
81        ghostEnzymeDTO.setEc(enzymeEntry.getEc().toString());
82        List commonNames = enzymeEntry.getCommonNames();
83        for (int jjj = 0; jjj < commonNames.size(); jjj++) {
84          EnzymeName commonName = (EnzymeName) commonNames.get(jjj);
85          if (commonName.getView().isInIntEnzView()) {
86            ghostEnzymeDTO.setName(encoding.xml2Display(commonName.getName(), encodingType));
87            break;
88          }
89        }
90        ghostEnzymeDTO.setSource(enzymeEntry.getSource().toString());
91        ghostEnzymeDTO.setStatus(enzymeEntry.getStatus().toString());
92        final HistoryEvent latestHistoryEventOfRoot = enzymeEntry.getHistory().getLatestHistoryEventOfRoot();
93        ghostEnzymeDTO.setEventClass(latestHistoryEventOfRoot.getEventClass().toString());
94        // Check whether this node has been transferred (set event class to 'TRA') or
95        // if it is the new enzyme in a transfer process (set event class NOT to 'TRA' but 'NEW').
96        if (latestHistoryEventOfRoot.getEventClass() == EventConstant.TRANSFER &&
97            !enzymeEntry.getHistory().isTransferredRootNode())
98          ghostEnzymeDTO.setEventClass(EventConstant.CREATION.toString());
99        ghostEnzymeDTO.setEventNote(IntEnzUtilities.linkMarkedEC(enzymeEntry.getHistory().getLatestHistoryEventOfRoot().getNote(), true));
100       ghostEntries.add(ghostEnzymeDTO);
101     }
102     enzymeSubSubclassDTO.setEntries(ghostEntries);
103 
104     LOGGER.debug("... EnzymeSubSubclassDTO populated.");
105   }
106 
107 }