View Javadoc
1   package uk.ac.ebi.intenz.webapp.dtos;
2   
3   import java.io.IOException;
4   import java.net.HttpURLConnection;
5   import java.net.InetSocketAddress;
6   import java.net.Proxy;
7   import java.net.URL;
8   import java.net.URLConnection;
9   import java.sql.Connection;
10  import java.sql.SQLException;
11  import java.util.ArrayList;
12  import java.util.HashSet;
13  import java.util.Iterator;
14  import java.util.List;
15  import java.util.Set;
16  
17  import javax.servlet.http.HttpServletRequest;
18  
19  import org.apache.commons.collections.list.SetUniqueList;
20  import org.apache.struts.action.ActionErrors;
21  import org.apache.struts.action.ActionMapping;
22  import org.apache.struts.action.ActionMessage;
23  import org.apache.struts.action.ActionMessages;
24  import org.apache.struts.taglib.html.Constants;
25  import org.apache.struts.validator.ValidatorForm;
26  
27  import uk.ac.ebi.biobabel.validator.DbIdentifierValidator;
28  import uk.ac.ebi.intenz.domain.constants.EnzymeNameTypeConstant;
29  import uk.ac.ebi.intenz.domain.constants.EnzymeSourceConstant;
30  import uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant;
31  import uk.ac.ebi.intenz.domain.constants.Status;
32  import uk.ac.ebi.intenz.domain.constants.XrefDatabaseConstant;
33  import uk.ac.ebi.intenz.domain.enzyme.EnzymeCommissionNumber;
34  import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry;
35  import uk.ac.ebi.intenz.domain.enzyme.EnzymeLink;
36  import uk.ac.ebi.intenz.domain.exceptions.DomainException;
37  import uk.ac.ebi.intenz.domain.exceptions.EcException;
38  import uk.ac.ebi.intenz.mapper.EnzymeEntryMapper;
39  import uk.ac.ebi.intenz.webapp.utilities.AutoGrowingList;
40  import uk.ac.ebi.rhea.mapper.MapperException;
41  
42  /**
43   * This ActionForm stores all enzyme properties in a <code>Map</code> since the number of properties needs to
44   * be dynamically adjustable.
45   * <p/>
46   * The enzyme properties will be stored as follows:
47   * <p/>
48   * <table border="1" style="border-collapse:collapse;" cellpadding="5">
49   * <tr>
50   * <td align="center" style="border-width:1px;"><b><code>KEY</code></b></td>
51   * <td align="center" style="border-width:1px;"><b><code>VALUE TYPE</code></b></td>
52   * <td align="center" style="border-width:1px;"><b><code>Description</code></b></td>
53   * </tr>
54   * <tr>
55   * <td align="center" style="border-width:1px;"><code>ec</code></td>
56   * <td align="center" style="border-width:1px;"><code>String</code></td>
57   * <td style="border-width:1px;"><code>The enzyme's EC number.</code></td>
58   * </tr>
59   * <tr>
60   * <td align="center" style="border-width:1px;"><code>commonNames</code></td>
61   * <td align="center" style="border-width:1px;"><code>String[][]</code></td>
62   * <td style="border-width:1px;"><code>String[][]</code> containing [index]{name, source}.<br/>
63   * See also {@link uk.ac.ebi.intenz.domain.enzyme.EnzymeName}.</code></td>
64   * </tr>
65   * <tr>
66   * <td align="center" style="border-width:1px;"><code>reactions</code></td>
67   * <td align="center" style="border-width:1px;"><code>String[][]</code></td>
68   * <td style="border-width:1px;"><code>String[][]</code> containing [index]{textualRepresentation, source}.<br/>
69   * See also {@link uk.ac.ebi.intenz.domain.enzyme.Reaction}.</code></td>
70   * </tr>
71   * <tr>
72   * <td align="center" style="border-width:1px;"><code>systematicName</code></td>
73   * <td align="center" style="border-width:1px;"><code>String</code></td>
74   * <td style="border-width:1px;"><code>The enzyme's systematic name.</code></td>
75   * </tr>
76   * <tr>
77   * <td align="center" style="border-width:1px;"><code>synonyms</code></td>
78   * <td align="center" style="border-width:1px;"><code>String[][]</code></td>
79   * <td style="border-width:1px;"><code>String[][]</code> containing [index]{name, qualifier, source, orderIn}.<br/>
80   * See also {@link uk.ac.ebi.intenz.domain.enzyme.EnzymeName}.</code></td>
81   * </tr>
82   * <tr>
83   * <td align="center" style="border-width:1px;"><code>cofactors</code></td>
84   * <td align="center" style="border-width:1px;"><code>String[]</code></td>
85   * <td style="border-width:1px;"><code>String[]</code> of cofactors (Strings containing the 'cofactorValue').<br/>
86   * See also {@link uk.ac.ebi.intenz.domain.enzyme.Cofactor}.</code></td>
87   * </tr>
88   * <tr>
89   * <td align="center" style="border-width:1px;"><code>links</code></td>
90   * <td align="center" style="border-width:1px;"><code>String[][]</code></td>
91   * <td style="border-width:1px;"><code>String[][]</code> containing [index]{source, specificUrl, accession, name}.<br/>
92   * See also {@link uk.ac.ebi.intenz.domain.enzyme.EnzymeLink}.</code></td>
93   * </tr>
94   * <tr>
95   * <td align="center" style="border-width:1px;"><code>comments</code></td>
96   * <td align="center" style="border-width:1px;"><code>String[][]</code></td>
97   * <td style="border-width:1px;"><code>String[][]</code> containing [index]{commentText, source}.<br/>
98   * See also {@link uk.ac.ebi.intenz.domain.enzyme.EnzymeComment}.</code></td>
99   * </tr>
100  * <tr>
101  * <td align="center" style="border-width:1px;"><code>references</code></td>
102  * <td align="center" style="border-width:1px;"><code>String[][]</code></td>
103  * <td style="border-width:1px;"><code>String[][]</code> containing [index]{pubId, authors, title, year,
104  * pubName, firstPage, lastPage, edition, editor, volume, publisher, publisherPlace, pubMedId, medlineId, patentNumber}.<br/>
105  * See also {@link uk.ac.ebi.intenz.domain.reference}.</code></td>
106  * </tr>
107  * <tr>
108  * <td align="center" style="border-width:1px;"><code>note</code></td>
109  * <td align="center" style="border-width:1px;"><code>String</code></td>
110  * <td style="border-width:1px;"><code>A note which might have been added by a curator.</code></td>
111  * </tr>
112  * <tr>
113  * <td align="center" style="border-width:1px;"><code>historyLine</code></td>
114  * <td align="center" style="border-width:1px;"><code>String</code></td>
115  * <td style="border-width:1px;"><code>The enzyme's history line.</code></td>
116  * </tr>
117  * <tr>
118  * <td align="center" style="border-width:1px;"><code>status</code></td>
119  * <td align="center" style="border-width:1px;"><code>String</code></td>
120  * <td style="border-width:1px;"><code>The enzyme's status (code).</code></td>
121  * </tr>
122  * <tr>
123  * <td align="center" style="border-width:1px;"><code>source</code></td>
124  * <td align="center" style="border-width:1px;"><code>String</code></td>
125  * <td style="border-width:1px;"><code>The enzyme's source (code).</code></td>
126  * </tr>
127  * </table>
128  *
129  * @author Michael Darsow
130  * @version $Revision: 1.3 $ $Date: 2008/03/12 12:29:16 $
131  */
132 public class EnzymeDTO extends ValidatorForm {
133   // ------------------------------ FIELDS ------------------------------
134 
135   private Integer uowId;
136 
137   private boolean isActive;
138   private String id;
139   private String ec;
140   private String transferredEc;
141   private String transferredToEc;
142   private EnzymeNameDTO systematicName;
143   private String note;
144   private String historyLine;
145   private String latestHistoryEventNote;
146   private String latestHistoryEventClass;
147   private String latestHistoryEventGroupId;
148   private String latestHistoryEventId;
149   private String latestHistoryBeforeId;
150   private String latestHistoryAfterId;
151   private String statusCode;
152   private String statusText;
153   private String source;
154 
155   private String className;
156   private String classEc;
157   private String subclassName;
158   private String subclassEc;
159   private String subSubclassName;
160   private String subSubclassEc;
161 
162   private List commonNames;
163   private List<ReactionDTO> reactionDtos;
164   private List synonyms;
165   private List<CofactorDTO> cofactors;
166   private List links;
167   private List uniProtLinks;
168   private List comments;
169   private List<ReferenceDTO> references;
170 
171    private String xcharsView;
172 
173   // --------------------------- CONSTRUCTORS ---------------------------
174 
175   public String getXcharsView () {
176       return xcharsView;
177    }
178 
179    public void setXcharsView (String xcharsView) {
180       this.xcharsView = xcharsView;
181    }
182 
183   public boolean isActive() {
184     return isActive;
185   }
186 
187   public void setActive(boolean active) {
188     isActive = active;
189   }
190 
191   public EnzymeDTO() {
192     isActive = true;
193     id = "";
194     ec = "";
195     systematicName = new EnzymeNameDTO();
196     systematicName.setName("");
197     systematicName.setXmlName("");
198     systematicName.setSource(EnzymeSourceConstant.INTENZ.toString());
199     systematicName.setSourceDisplay(EnzymeSourceConstant.INTENZ.toDisplayString());
200     systematicName.setView(EnzymeViewConstant.IUBMB_INTENZ.toString());
201     systematicName.setViewDisplayImage(EnzymeViewConstant.IUBMB_INTENZ.toDisplayImage());
202     systematicName.setViewDisplayString(EnzymeViewConstant.IUBMB_INTENZ.toDisplayString());
203     systematicName.setType(EnzymeNameTypeConstant.SYSTEMATIC_NAME.toString());
204     note = "";
205     historyLine = "";
206     latestHistoryEventNote = "";
207     latestHistoryEventClass = "";
208     latestHistoryEventGroupId = "";
209     latestHistoryEventId = "";
210     latestHistoryBeforeId = "";
211     latestHistoryAfterId = "";
212     statusCode = "SU";
213     statusText = "suggested";
214     source = "INTENZ";
215 
216     className = "";
217     classEc = "";
218     subclassName = "";
219     subclassEc = "";
220     subSubclassName = "";
221     subSubclassEc = "";
222     xcharsView = "false";
223 
224     commonNames = new AutoGrowingList(EnzymeNameDTO.class);
225     reactionDtos = new AutoGrowingList(ReactionDTO.class);
226     synonyms = new AutoGrowingList(EnzymeNameDTO.class);
227     cofactors = new AutoGrowingList(CofactorDTO.class);
228     links = new AutoGrowingList(EnzymeLinkDTO.class);
229     uniProtLinks = new AutoGrowingList(EnzymeLinkDTO.class);
230     comments = new AutoGrowingList(CommentDTO.class);
231     references = new AutoGrowingList(ReferenceDTO.class);
232   }
233 
234   public EnzymeDTO(EnzymeDTO enzymeDTO) {
235     setUowId(enzymeDTO.getUowId());
236     setId(enzymeDTO.getId());
237     setEc(enzymeDTO.getEc());
238     setTransferredEc(enzymeDTO.getTransferredEc());
239     setSystematicName(new EnzymeNameDTO(enzymeDTO.getSystematicName()));
240     setNote(enzymeDTO.getNote());
241     setHistoryLine(enzymeDTO.getHistoryLine());
242     setLatestHistoryEventNote(enzymeDTO.getLatestHistoryEventNote());
243     setLatestHistoryEventClass(enzymeDTO.getLatestHistoryEventClass());
244     setLatestHistoryEventGroupId(enzymeDTO.getLatestHistoryEventGroupId());
245     setLatestHistoryEventId(enzymeDTO.getLatestHistoryEventId());
246     setStatusCode(enzymeDTO.getStatusCode());
247     setStatusText(enzymeDTO.getStatusText());
248     setSource(enzymeDTO.getSource());
249     setClassName(enzymeDTO.getClassName());
250     setClassEc(enzymeDTO.getClassEc());
251     setSubclassName(enzymeDTO.getSubclassName());
252     setSubclassEc(enzymeDTO.getSubclassEc());
253     setSubSubclassName(enzymeDTO.getSubSubclassName());
254     setSubSubclassEc(enzymeDTO.getSubSubclassEc());
255     setXcharsView(enzymeDTO.getXcharsView());
256 
257     List commonNames = enzymeDTO.getCommonNames();
258     List newCommonNames = new AutoGrowingList(EnzymeNameDTO.class);
259     for (int iii = 0; iii < commonNames.size(); iii++) {
260       EnzymeNameDTO commonName = (EnzymeNameDTO) commonNames.get(iii);
261       newCommonNames.add(new EnzymeNameDTO(commonName));
262     }
263     setCommonNames(newCommonNames);
264 
265     List reactions = enzymeDTO.getReactionDtos();
266     List newReactions = new AutoGrowingList(ReactionDTO.class);
267     for (int iii = 0; iii < reactions.size(); iii++) {
268       ReactionDTO reaction = (ReactionDTO) reactions.get(iii);
269       newReactions.add(new ReactionDTO(reaction));
270     }
271     setReactionDtos(newReactions);
272 
273     List synonyms = enzymeDTO.getSynonyms();
274     List newSynonyms = new AutoGrowingList(EnzymeNameDTO.class);
275     for (int iii = 0; iii < synonyms.size(); iii++) {
276       EnzymeNameDTO synonym = (EnzymeNameDTO) synonyms.get(iii);
277       newSynonyms.add(new EnzymeNameDTO(synonym));
278     }
279     setSynonyms(newSynonyms);
280 
281     List<CofactorDTO> cofactors = enzymeDTO.getCofactors();
282     List<CofactorDTO> newCofactors = new AutoGrowingList(CofactorDTO.class);
283     for (int iii = 0; iii < cofactors.size(); iii++) {
284       CofactorDTO cofactor = cofactors.get(iii);
285       newCofactors.add(new CofactorDTO(cofactor));
286     }
287     setCofactors(newCofactors);
288 
289     List links = enzymeDTO.getLinks();
290     List newLinks = new AutoGrowingList(EnzymeLinkDTO.class);
291     for (int iii = 0; iii < links.size(); iii++) {
292       EnzymeLinkDTO link = (EnzymeLinkDTO) links.get(iii);
293       newLinks.add(new EnzymeLinkDTO(link));
294     }
295     setLinks(newLinks);
296 
297     List uniProtLinks = enzymeDTO.getUniProtLinks();
298     List newUniprotLinks = new AutoGrowingList(EnzymeLinkDTO.class);
299     for (int iii = 0; iii < uniProtLinks.size(); iii++) {
300       EnzymeLinkDTO link = (EnzymeLinkDTO) uniProtLinks.get(iii);
301       newUniprotLinks.add(new EnzymeLinkDTO(link));
302     }
303     setUniProtLinks(newUniprotLinks);
304 
305     List comments = enzymeDTO.getComments();
306     List newComments = new AutoGrowingList(CommentDTO.class);
307     for (int iii = 0; iii < comments.size(); iii++) {
308       CommentDTO comment = (CommentDTO) comments.get(iii);
309       newComments.add(new CommentDTO(comment));
310     }
311     setComments(newComments);
312 
313     List<ReferenceDTO> references = enzymeDTO.getReferences();
314     List newReferences = new AutoGrowingList(ReferenceDTO.class);
315     for (int iii = 0; iii < references.size(); iii++) {
316       ReferenceDTO reference = (ReferenceDTO) references.get(iii);
317       newReferences.add(new ReferenceDTO(reference));
318     }
319     setReferences(newReferences);
320   }
321 
322   // --------------------- GETTER / SETTER METHODS ---------------------
323 
324   public List getAllLinks() {
325     List allLinks = new AutoGrowingList(EnzymeLinkDTO.class);
326     allLinks.addAll(links);
327     allLinks.addAll(uniProtLinks);
328     return allLinks;
329   }
330 
331   public String getClassEc() {
332     return classEc;
333   }
334 
335   public void setClassEc(String classEc) {
336     this.classEc = classEc;
337   }
338 
339   public String getClassName() {
340     return className;
341   }
342 
343   public void setClassName(String className) {
344     this.className = className;
345   }
346 
347   public List<CofactorDTO> getCofactors() {
348     return cofactors;
349   }
350 
351   public void setCofactors(List<CofactorDTO> cofactors) {
352     this.cofactors = cofactors;
353   }
354 
355   public List getComments() {
356     return comments;
357   }
358 
359   public void setComments(List comments) {
360     this.comments = comments;
361   }
362 
363   public List getCommonNames() {
364     return commonNames;
365   }
366 
367   public void setCommonNames(List commonNames) {
368     this.commonNames = commonNames;
369   }
370 
371   public String getEc() {
372     return ec;
373   }
374 
375   public void setEc(String ec) {
376     this.ec = ec;
377   }
378 
379   public boolean isPreliminaryEc(){
380     return EnzymeCommissionNumber.isPreliminary(ec);
381   }
382 
383   public String getHistoryLine() {
384     return historyLine;
385   }
386 
387   public void setHistoryLine(String historyLine) {
388     this.historyLine = historyLine;
389   }
390 
391   public String getId() {
392     return id;
393   }
394 
395   public void setId(String id) {
396     this.id = id;
397   }
398 
399   public String getLatestHistoryAfterId() {
400     return latestHistoryAfterId;
401   }
402 
403   public void setLatestHistoryAfterId(String latestHistoryAfterId) {
404     this.latestHistoryAfterId = latestHistoryAfterId;
405   }
406 
407   public String getLatestHistoryBeforeId() {
408     return latestHistoryBeforeId;
409   }
410 
411   public void setLatestHistoryBeforeId(String latestHistoryBeforeId) {
412     this.latestHistoryBeforeId = latestHistoryBeforeId;
413   }
414 
415   public String getLatestHistoryEventClass() {
416     return latestHistoryEventClass;
417   }
418 
419   public void setLatestHistoryEventClass(String latestHistoryEventClass) {
420     this.latestHistoryEventClass = latestHistoryEventClass;
421   }
422 
423   public String getLatestHistoryEventGroupId() {
424     return latestHistoryEventGroupId;
425   }
426 
427   public void setLatestHistoryEventGroupId(String latestHistoryEventGroupId) {
428     this.latestHistoryEventGroupId = latestHistoryEventGroupId;
429   }
430 
431   public String getLatestHistoryEventId() {
432     return latestHistoryEventId;
433   }
434 
435   public void setLatestHistoryEventId(String latestHistoryEventId) {
436     this.latestHistoryEventId = latestHistoryEventId;
437   }
438 
439   public String getLatestHistoryEventNote() {
440     return latestHistoryEventNote;
441   }
442 
443   public void setLatestHistoryEventNote(String latestHistoryEventNote) {
444     this.latestHistoryEventNote = latestHistoryEventNote;
445   }
446 
447   public List getLinks() {
448     return links;
449   }
450 
451   public void setLinks(List links) {
452     this.links = links;
453   }
454 
455   public String getNote() {
456     return note;
457   }
458 
459   public void setNote(String note) {
460     this.note = note;
461   }
462 
463   public List<ReactionDTO> getReactionDtos() {
464     return reactionDtos;
465   }
466 
467   public void setReactionDtos(List<ReactionDTO> reactions) {
468     this.reactionDtos = reactions;
469   }
470 
471   public List<ReferenceDTO> getReferences() {
472     return references;
473   }
474 
475   public void setReferences(List<ReferenceDTO> references) {
476     this.references = references;
477   }
478 
479   public String getSource() {
480     return source;
481   }
482 
483   public void setSource(String source) {
484     this.source = source;
485   }
486 
487   public String getStatusCode() {
488     return statusCode;
489   }
490 
491   public void setStatusCode(String statusCode) {
492     this.statusCode = statusCode;
493   }
494 
495   public String getStatusText() {
496     return statusText;
497   }
498 
499   public void setStatusText(String statusText) {
500     this.statusText = statusText;
501   }
502 
503   public String getSubSubclassEc() {
504     return subSubclassEc;
505   }
506 
507   public void setSubSubclassEc(String subSubclassEc) {
508     this.subSubclassEc = subSubclassEc;
509   }
510 
511   public String getSubSubclassName() {
512     return subSubclassName;
513   }
514 
515   public void setSubSubclassName(String subSubclassName) {
516     this.subSubclassName = subSubclassName;
517   }
518 
519   public String getSubclassEc() {
520     return subclassEc;
521   }
522 
523   public void setSubclassEc(String subclassEc) {
524     this.subclassEc = subclassEc;
525   }
526 
527   public String getSubclassName() {
528     return subclassName;
529   }
530 
531   public void setSubclassName(String subclassName) {
532     this.subclassName = subclassName;
533   }
534 
535   public List getSynonyms() {
536     return synonyms;
537   }
538 
539   public void setSynonyms(List synonyms) {
540     this.synonyms = synonyms;
541   }
542 
543   public EnzymeNameDTO getSystematicName() {
544     return systematicName;
545   }
546 
547   public void setSystematicName(EnzymeNameDTO systematicName) {
548     this.systematicName = systematicName;
549   }
550 
551   public String getTransferredEc() {
552     return transferredEc;
553   }
554 
555   public void setTransferredEc(String transferredEc) {
556     this.transferredEc = transferredEc;
557   }
558 
559   public String getTransferredToEc() {
560     return transferredToEc;
561   }
562 
563   public void setTransferredToEc(String transferredToEc) {
564     this.transferredToEc = transferredToEc;
565   }
566 
567   public List getUniProtLinks() {
568     return uniProtLinks;
569   }
570 
571   public void setUniProtLinks(List uniProtLinks) {
572     this.uniProtLinks = uniProtLinks;
573   }
574 
575   // ------------------------------- GETTER & SETTER --------------------------------
576 
577   public Integer getUowId() {
578     return uowId;
579   }
580 
581   public void setUowId(Integer uowId) {
582     this.uowId = uowId;
583   }
584 
585   // -------------------------- OTHER METHODS --------------------------
586 
587   public Object getCofactor(int index) {
588     return cofactors.toArray()[index];
589   }
590 
591   public CommentDTO getComment(int index) {
592     return (CommentDTO) comments.get(index);
593   }
594 
595   public EnzymeNameDTO getCommonName(int index) {
596     return (EnzymeNameDTO) commonNames.get(index);
597   }
598 
599   public EnzymeLinkDTO getLink(int index) {
600     return (EnzymeLinkDTO) links.get(index);
601   }
602 
603   public ReactionDTO getReactionDto(int index) {
604     return reactionDtos.get(index);
605   }
606 
607   public ReferenceDTO getReference(int index) {
608     return references.get(index);
609   }
610 
611   public EnzymeNameDTO getSynonym(int index) {
612     return (EnzymeNameDTO) synonyms.get(index);
613   }
614 
615   public EnzymeLinkDTO getUniProtLink(int index) {
616     return (EnzymeLinkDTO) uniProtLinks.get(index);
617   }
618 
619   public List getLinks(String dbCode){
620       List selectedLinks = new ArrayList();
621       for (int i = 0; i < links.size(); i++){
622           EnzymeLinkDTO link = (EnzymeLinkDTO) links.get(i);
623           if (link.getDatabaseCode().equals(dbCode))
624               selectedLinks.add(link);
625       }
626       return selectedLinks;
627   }
628 
629   public List getGoLinks(){
630       return getLinks(XrefDatabaseConstant.GO.getDatabaseCode());
631   }
632 
633   public List getCasLinks(){
634       return getLinks(XrefDatabaseConstant.CAS.getDatabaseCode());
635   }
636 
637     @Override
638   public void reset(ActionMapping mapping, HttpServletRequest request) {
639 //    id = "";
640   }
641 
642 	@Override
643 	public ActionErrors validate(ActionMapping mapping,
644 			HttpServletRequest request) {
645 		ActionErrors errors = null;
646 		if (isActive) {
647 			// use Validator framework first:
648 			errors = super.validate(mapping, request);
649 
650 			errors.add(systematicName.validate(mapping, request));
651 
652 			// Validate common names.
653 			validateCommonNames(errors, mapping, request);
654 
655 			// Validate synonyms.
656 			validateSynonyms(errors, mapping, request);
657 
658 			// Checks all names for uniqueness.
659 			validateAllNames(errors, request);
660 
661 			// Validate reactions.
662 			validateReactions(errors, mapping, request);
663 
664 			// Validate cofactors.
665 			// validateCofactors(errors, mapping, request);
666 
667 			// Validate comments.
668 			validateComments(errors, mapping, request);
669 
670 			// Validate links.
671 			validateLinks(errors, mapping, request);
672 
673 			// Validate UniProt links.
674 			validateUniProtLinks(errors, mapping, request);
675 
676 			// Validate references.
677 			validateReferences(errors, mapping, request);
678 
679 			updateStatus();
680 		} else {
681 			errors = validateInactive(request, errors);
682 		}
683 
684 		// Keep token when an error occurs.
685 		if (request.getParameter(Constants.TOKEN_KEY) != null) {
686 			request.setAttribute(Constants.TOKEN_KEY,
687 					request.getParameter(Constants.TOKEN_KEY));
688 		}
689 
690 		return (errors == null || errors.isEmpty()) ? null : errors;
691 	}
692 
693 	/**
694 	 * Validate an inactive (deleted or transferred) entry.
695 	 * @param request
696 	 * @param errors
697 	 * @return
698 	 */
699 	private ActionErrors validateInactive(HttpServletRequest request,
700 			ActionErrors errors) {
701 		// If transferred, target EC must exist and be active:
702 		if (transferredToEc != null && transferredToEc.length() > 0) {
703 			if (errors == null) errors = new ActionErrors();
704 			Connection con = (Connection)
705 					request.getSession().getAttribute("connection");
706 			EnzymeCommissionNumber targetEc = null;
707 			try {
708 				targetEc = EnzymeCommissionNumber.valueOf(transferredToEc);
709 				EnzymeEntry targetEntry = null;
710 				try {
711 					targetEntry = new EnzymeEntryMapper().findByEc(
712 							targetEc.getEc1(), targetEc.getEc2(),
713 							targetEc.getEc3(), targetEc.getEc4(), Status.APPROVED,
714 							con);
715 					if (targetEntry == null) {
716 						errors.add("transferredToEc", new ActionMessage(
717 								"errors.application.ec.nonexisting", targetEc));
718 					} else if (!targetEntry.isActive()) {
719 						errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
720 								"errors.application.ec.inactive", targetEc));
721 					}
722 				} catch (Exception e) {
723 					errors.add("transferredToEc", new ActionMessage(
724 							"errors.application.database", transferredToEc));
725 				}
726 			} catch (Exception e) {
727 				errors.add("transferredToEc", new ActionMessage(
728 						"errors.application.ec.detail", transferredToEc));
729 			}
730 		}
731 		return errors;
732 	}
733 
734   /**
735    * Validates the list of common names.
736    * <p/>
737    * <ol>
738    * <li>removes empty items</li>
739    * <li>removes duplicates</li>
740    * <li>checks the common name independently from the others (using its <code>validate()</code> method)</li>
741    * <li>checks whether two common names have the view flag
742    * {@link uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant#INTENZ} attached.</li>
743    * </ol>
744    *
745    * @param errors  Stores errors.
746    * @param mapping Action mapping object.
747    * @param request Request object.
748    */
749   private void validateCommonNames(ActionErrors errors, ActionMapping mapping, HttpServletRequest request) {
750     assert errors != null : "Parameter 'errors' must not be null.";
751     assert mapping != null : "Parameter 'mapping' must not be null.";
752     assert request != null : "Parameter 'request' must not be null.";
753 
754     removeEmptyNameItems(commonNames);
755     removeDuplicates(commonNames);
756     boolean intenzViewUsed = false;
757     for (int iii = 0; iii < commonNames.size(); iii++) {
758       // Validate common names individually.
759       EnzymeNameDTO enzymeNameDTO = (EnzymeNameDTO) commonNames.get(iii);
760       errors.add(enzymeNameDTO.validate(mapping, request));
761       // Validate dependencies.
762       if (EnzymeViewConstant.isInIntEnzView(enzymeNameDTO.getView()) &&
763           ((enzymeNameDTO.getName() != null && !enzymeNameDTO.getName().equals("")) ||
764            (enzymeNameDTO.getXmlName() != null && !enzymeNameDTO.getXmlName().equals("")))) {
765         if (intenzViewUsed) {
766           ActionMessage message = new ActionMessage("errors.form.common_name.invalidView");
767           errors.add("commonName", message);
768         } else
769           intenzViewUsed = true;
770       }
771     }
772   }
773 
774   /**
775    * Validates the list of synonyms.
776    * <p/>
777    * <ol>
778    * <li>removes empty items</li>
779    * <li>removes duplicates</li>
780    * <li>checks the synonym independently from the others (using its <code>validate()</code> method)</li>
781    * </ol>
782    *
783    * @param errors  Stores errors.
784    * @param mapping Action mapping object.
785    * @param request Request object.
786    */
787   private void validateSynonyms(ActionErrors errors, ActionMapping mapping, HttpServletRequest request) {
788     assert errors != null : "Parameter 'errors' must not be null.";
789     assert mapping != null : "Parameter 'mapping' must not be null.";
790     assert request != null : "Parameter 'request' must not be null.";
791 
792     removeEmptyNameItems(synonyms);
793     removeDuplicates(synonyms);
794     for (int iii = 0; iii < synonyms.size(); iii++) {
795       EnzymeNameDTO enzymeNameDTO = (EnzymeNameDTO) synonyms.get(iii);
796       errors.add(enzymeNameDTO.validate(mapping, request));
797     }
798   }
799 
800   /**
801    * Checks whether the list of all names contains only unique names.
802    *
803    * @param errors  Stores errors.
804    * @param request Request object.
805    */
806   private void validateAllNames(ActionErrors errors, HttpServletRequest request) {
807     assert errors != null : "Parameter 'errors' must not be null.";
808     assert request != null : "Parameter 'request' must not be null.";
809 
810     for (int iii = 0; iii < commonNames.size(); iii++) {
811       EnzymeNameDTO commonName = (EnzymeNameDTO) commonNames.get(iii);
812 //      if (systematicName.getXmlName().equals(commonName.getXmlName())) {
813 //        ActionMessage message = new ActionMessage("errors.form.common_name.duplicate_sys_name", commonName.getName());
814 //        errors.add("commonName", message);
815 //        return;
816 //      }
817       for (int jjj = 0; jjj < synonyms.size(); jjj++) {
818         EnzymeNameDTO synonym = (EnzymeNameDTO) synonyms.get(jjj);
819         if (systematicName.getXmlName().equals(synonym.getXmlName())) {
820           ActionMessage message = new ActionMessage("errors.form.synonym.duplicate_sys_name", synonym.getName());
821           errors.add("otherNames", message);
822           return;
823         }
824         if (commonName.getXmlName().equals(synonym.getXmlName())) {
825           ActionMessage message = new ActionMessage("errors.form.common_name.duplicate_synonym", commonName.getName());
826           errors.add("commonName", message);
827           return;
828         }
829       }
830     }
831 
832   }
833 
834   /**
835    * Validates the list of reactions.
836    * <p/>
837    * <ol>
838    * <li>removes empty items</li>
839    * <li>removes duplicates</li>
840    * <li>check if at least one reaction is present</li>
841    * <li>checks the reaction independently from the others (using its <code>validate()</code> method)</li>
842    * </ol>
843    *
844    * @param errors  Stores errors.
845    * @param mapping Action mapping object.
846    * @param request Request object.
847    */
848   private void validateReactions(ActionErrors errors, ActionMapping mapping, HttpServletRequest request) {
849     assert errors != null : "Parameter 'errors' must not be null.";
850     assert mapping != null : "Parameter 'mapping' must not be null.";
851     assert request != null : "Parameter 'request' must not be null.";
852 
853     removeEmptyReactionItems();
854     removeDuplicates(reactionDtos);
855     if (reactionDtos.size() == 0) {
856       ActionMessage message = new ActionMessage("errors.form.reaction.notExisting");
857       errors.add("reaction", message);
858       return;
859     }
860     for (int iii = 0; iii < reactionDtos.size(); iii++) {
861       ReactionDTO reactionDTO = reactionDtos.get(iii);
862       errors.add(reactionDTO.validate(mapping, request));
863     }
864   }
865 
866   /**
867    * Validates the list of cofactors.
868    * <p/>
869    * <ol>
870    * <li>removes empty items</li>
871    * <li>removes duplicates</li>
872    * <li>checks the cofactor independently from the others (using its <code>validate()</code> method)</li>
873    * </ol>
874    *
875    * @param errors  Stores errors.
876    * @param mapping Action mapping object.
877    * @param request Request object.
878    */
879 //  private void validateCofactors(ActionErrors errors, ActionMapping mapping, HttpServletRequest request) {
880 //    assert errors != null : "Parameter 'errors' must not be null.";
881 //    assert mapping != null : "Parameter 'mapping' must not be null.";
882 //    assert request != null : "Parameter 'request' must not be null.";
883 //
884 //    removeEmptyCofactorItems();
885 ////    removeDuplicates(cofactors);
886 //    for (int iii = 0; iii < cofactors.size(); iii++) {
887 //      CofactorDTO cofactorDTO = (CofactorDTO) cofactors.get(iii);
888 //      errors.add(cofactorDTO.validate(mapping, request));
889 //    }
890 //  }
891 
892   /**
893    * Validates the list of comments.
894    * <p/>
895    * <ol>
896    * <li>removes empty items</li>
897    * <li>removes duplicates</li>
898    * <li>checks whether a comment ends with a full stop</li>
899    * <li>checks the comment independently from the others (using its <code>validate()</code> method)</li>
900    * </ol>
901    *
902    * @param errors  Stores errors.
903    * @param mapping Action mapping object.
904    * @param request Request object.
905    */
906   private void validateComments(ActionErrors errors, ActionMapping mapping, HttpServletRequest request) {
907     assert errors != null : "Parameter 'errors' must not be null.";
908     assert mapping != null : "Parameter 'mapping' must not be null.";
909     assert request != null : "Parameter 'request' must not be null.";
910 
911     removeEmptyCommentItems();
912     removeDuplicates(comments);
913     checkForPeriod(errors);
914     for (int iii = 0; iii < comments.size(); iii++) {
915       CommentDTO commentDTO = (CommentDTO) comments.get(iii);
916       errors.add(commentDTO.validate(mapping, request));
917     }
918   }
919 
920   /**
921    * Validates the list of links.
922    * <p/>
923    * <ol>
924    * <li>removes empty items</li>
925    * <li>removes duplicates</li>
926    * <li>updates static link values</li>
927    * <li>checks the link independently from the others (using its <code>validate()</code> method)</li>
928    * <li>validates CAS Registry number for existance (if available) and for correct format usage</li>
929    * <li>validates if the MEROPS URL is working</li>
930    * <li>validates if required OMIM link values (accession and name) are present</li>
931    * <li>validates if the required PROSITE accession is present</li>
932    * <li>validates if the required DIAGRAM name is present and whether the given link is working</li>
933    * </ol>
934    *
935    * @param errors  Stores errors.
936    * @param mapping Action mapping object.
937    * @param request Request object.
938    */
939   private void validateLinks(ActionErrors errors, ActionMapping mapping, HttpServletRequest request) {
940     assert errors != null : "Parameter 'errors' must not be null.";
941     assert mapping != null : "Parameter 'mapping' must not be null.";
942     assert request != null : "Parameter 'request' must not be null.";
943 
944     removeEmptyLinkItems();
945     removeLinkDuplicates();
946     try {
947       updateStaticLinks();
948     } catch (DomainException e) {
949       ActionMessage message = new ActionMessage(e.getMessageKey(), e.getProperty());
950       errors.add("links", message);
951     }
952     for (int iii = 0; iii < links.size(); iii++) {
953       EnzymeLinkDTO enzymeLinkDTO = (EnzymeLinkDTO) links.get(iii);
954       errors.add(enzymeLinkDTO.validate(mapping, request));
955       if (enzymeLinkDTO.getDatabaseName().equals(XrefDatabaseConstant.CAS.getDisplayName())) {
956         if (enzymeLinkDTO.getAccession() == null || enzymeLinkDTO.getAccession().equals("")) {
957           ActionMessage error = new ActionMessage("errors.form.link.invalidCasValue");
958           errors.add("links", error);
959         }
960 //        if (!IntEnzValidations.validateCASRegistryNumberFormat(enzymeLinkDTO)) {
961         if (!DbIdentifierValidator.getInstance().validate(enzymeLinkDTO.getAccession(), DbIdentifierValidator.CAS_REGISTRY_NUMBER)){
962           ActionMessage error = new ActionMessage("errors.form.link.invalidCasFormat");
963           errors.add("links", error);
964         }
965       }
966       if (enzymeLinkDTO.getDatabaseName().equals(XrefDatabaseConstant.MEROPS.getDisplayName())) {
967         try {
968           URL url = new URL(enzymeLinkDTO.getUrl());
969           URLConnection connection = url.openConnection();
970           connection.connect();
971         } catch (IOException e) {
972           ActionMessage message = new ActionMessage("errors.form.link.invalidMeropsUrl");
973           errors.add("links", message);
974         }
975       }
976       if (enzymeLinkDTO.getDatabaseName().equals(XrefDatabaseConstant.MIM.getDisplayName())) {
977         if (enzymeLinkDTO.getAccession() == null || enzymeLinkDTO.getAccession().equals("") ||
978             enzymeLinkDTO.getName() == null || enzymeLinkDTO.getName().equals("")) {
979           ActionMessage message = new ActionMessage("errors.form.link.MimIncomplete");
980           errors.add("links", message);
981         }
982       }
983       if (enzymeLinkDTO.getDatabaseName().equals(XrefDatabaseConstant.PROSITE.getDisplayName())) {
984         if (enzymeLinkDTO.getAccession() == null || enzymeLinkDTO.getAccession().equals("")) {
985           ActionMessage message = new ActionMessage("errors.form.link.invalidPrositeValue");
986           errors.add("links", message);
987         } else {
988         	enzymeLinkDTO.setUrl(XrefDatabaseConstant.PROSITE.getUrl() + enzymeLinkDTO.getAccession());
989         }
990       }
991       if (enzymeLinkDTO.getDatabaseName().equals(XrefDatabaseConstant.DIAGRAM.getDisplayName())) {
992         if (enzymeLinkDTO.getName() == null || enzymeLinkDTO.getName().equals("")) {
993           ActionMessage message = new ActionMessage("errors.form.link.invalidDiagramName");
994           errors.add("links", message);
995         }
996         try {
997           URL url = new URL(enzymeLinkDTO.getUrl());
998           Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("www-proxy.ebi.ac.uk", 8080));
999           HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
1000           if (connection.getResponseCode() != HttpURLConnection.HTTP_OK){
1001               ActionMessage message = new ActionMessage("errors.form.link.invalidDiagramUrl");
1002               errors.add("links", message);        	  
1003           }
1004         } catch (IOException e) {
1005           ActionMessage message = new ActionMessage("errors.form.link.invalidDiagramUrl");
1006           errors.add("links", message);
1007         }
1008       }
1009     }
1010   }
1011 
1012   /**
1013    * Validates the list of UniProt links.
1014    * <p/>
1015    * NB: UniProt links are generated automatically and do not contain any empty or duplicated values.
1016    * <p/>
1017    * <ol>
1018    * <li>checks the link independently from the others (using its <code>validate()</code> method)</li>
1019    * </ol>
1020    *
1021    * @param errors  Stores errors.
1022    * @param mapping Action mapping object.
1023    * @param request Request object.
1024    */
1025   private void validateUniProtLinks(ActionErrors errors, ActionMapping mapping, HttpServletRequest request) {
1026     assert errors != null : "Parameter 'errors' must not be null.";
1027     assert mapping != null : "Parameter 'mapping' must not be null.";
1028     assert request != null : "Parameter 'request' must not be null.";
1029 
1030     for (int iii = 0; iii < uniProtLinks.size(); iii++) {
1031       EnzymeLinkDTO enzymeLinkDTO = (EnzymeLinkDTO) uniProtLinks.get(iii);
1032       errors.add(enzymeLinkDTO.validate(mapping, request));
1033     }
1034   }
1035 
1036   /**
1037    * Validates the list of references.
1038    * <p/>
1039    * <ol>
1040    * <li>removes empty items</li>
1041    * <li>removes duplicates</li>
1042    * <li>checks the reference independently from the others (using its <code>validate()</code> method)</li>
1043    * </ol>
1044    *
1045    * @param errors  Stores errors.
1046    * @param mapping Action mapping object.
1047    * @param request Request object.
1048    */
1049   private void validateReferences(ActionErrors errors, ActionMapping mapping, HttpServletRequest request) {
1050     assert errors != null : "Parameter 'errors' must not be null.";
1051     assert mapping != null : "Parameter 'mapping' must not be null.";
1052     assert request != null : "Parameter 'request' must not be null.";
1053 
1054     removeEmptyReferenceItems();
1055     removeDuplicates(references);
1056     for (int iii = 0; iii < references.size(); iii++) {
1057       ReferenceDTO referenceDTO = (ReferenceDTO) references.get(iii);
1058       errors.add(referenceDTO.validate(mapping, request));
1059     }
1060   }
1061 
1062   private void removeEmptyNameItems(List names) {
1063     assert names != null : "Parameter 'names' must not be null.";
1064     for (int iii = 0; iii < names.size(); iii++) {
1065       EnzymeNameDTO enzymeNameDTO = (EnzymeNameDTO) names.get(iii);
1066       if (enzymeNameDTO.getXmlName() == null || enzymeNameDTO.getXmlName().equals("")) {
1067         names.remove(iii);
1068         iii--;
1069       }
1070     }
1071   }
1072 
1073   private void removeEmptyReactionItems() {
1074     for (int iii = 0; iii < reactionDtos.size(); iii++) {
1075       ReactionDTO reactionDTO = (ReactionDTO) reactionDtos.get(iii);
1076       if (reactionDTO.getXmlTextualRepresentation() == null || reactionDTO.getXmlTextualRepresentation().equals("")) {
1077         reactionDtos.remove(iii);
1078         iii--;
1079       }
1080     }
1081   }
1082 
1083 //  private void removeEmptyCofactorItems() {
1084 //    for (int iii = 0; iii < cofactors.size(); iii++) {
1085 //      CofactorDTO cofactorDTO = (CofactorDTO) cofactors.get(iii);
1086 //      if (cofactorDTO.getXmlCofactorValue() == null || cofactorDTO.getXmlCofactorValue().equals("")) {
1087 //        cofactors.remove(iii);
1088 //        iii--;
1089 //      }
1090 //    }
1091 //  }
1092 
1093   /**
1094    * All database links which name value is <code>NONE</code> will be removed.
1095    * <p/>
1096    * All the others will be kept and checked in the updating Action.
1097    */
1098   private void removeEmptyLinkItems() {
1099     assert links != null : "Parameter 'links' must not be null.";
1100     for (int iii = 0; iii < links.size(); iii++) {
1101       EnzymeLinkDTO enzymeLinkDTO = (EnzymeLinkDTO) links.get(iii);
1102       if (enzymeLinkDTO.getDatabaseName().equals(XrefDatabaseConstant.UNDEF.getDisplayName())) {
1103         links.remove(iii);
1104         iii--;
1105       }
1106     }
1107   }
1108 
1109   private void removeEmptyCommentItems() {
1110     for (int iii = 0; iii < comments.size(); iii++) {
1111       CommentDTO commentDTO = (CommentDTO) comments.get(iii);
1112       if (commentDTO.getXmlComment() == null || commentDTO.getXmlComment().equals("")) {
1113         comments.remove(iii);
1114         iii--;
1115       }
1116     }
1117   }
1118 
1119   private void removeEmptyReferenceItems() {
1120     assert references != null : "Parameter 'references' must not be null.";
1121     for (int iii = 0; iii < references.size(); iii++) {
1122       ReferenceDTO referenceDTO = references.get(iii);
1123       if (referenceDTO.isEmpty()) {
1124         references.remove(iii);
1125         iii--;
1126       }
1127     }
1128   }
1129 
1130   private void checkForPeriod(ActionMessages errors) {
1131     for (int iii = 0; iii < comments.size(); iii++) {
1132       CommentDTO commentDTO = (CommentDTO) comments.get(iii);
1133       if (!commentDTO.getXmlComment().endsWith(".")) {
1134         ActionMessage error = new ActionMessage("errors.form.comment.missing_period", "" + (iii + 1));
1135         errors.add("comments", error);
1136         return;
1137       }
1138     }
1139   }
1140 
1141   /**
1142    * Removes duplicates from the list of links.
1143    */
1144   private void removeLinkDuplicates() {
1145     assert links != null : "Parameter 'links' must not be null.";
1146     Set databaseNames = new HashSet();
1147     Set checkedLinks = new HashSet();
1148     for (int iii = 0; iii < links.size(); iii++) {
1149       EnzymeLinkDTO enzymeLinkDTO = (EnzymeLinkDTO) links.get(iii);
1150       if (databaseNames.contains(enzymeLinkDTO.getDatabaseName())) {
1151         // If the given name is a valid xref name and it is a unique xref then remove it from the list.
1152         if (XrefDatabaseConstant.isUniqueXrefDatabaseName(enzymeLinkDTO.getDatabaseName())) {
1153           links.remove(enzymeLinkDTO);
1154         } else { // Remove xref only if all xref values are the same.
1155           for (Iterator it = checkedLinks.iterator(); it.hasNext();) {
1156             EnzymeLinkDTO checkedLink = (EnzymeLinkDTO) it.next();
1157             if (checkedLink.equals(enzymeLinkDTO)) links.remove(enzymeLinkDTO);
1158           }
1159         }
1160       } else {
1161         databaseNames.add(enzymeLinkDTO.getDatabaseName());
1162         checkedLinks.add(enzymeLinkDTO);
1163       }
1164     }
1165   }
1166 
1167   private void removeDuplicates(List list) {
1168     list.retainAll(SetUniqueList.decorate(list));
1169   }
1170 
1171   /**
1172    * If a user added a static link the values such as the correct URL will be set here.
1173    *
1174    * @throws DomainException if the database code of this link is unknown.
1175    */
1176   private void updateStaticLinks() throws DomainException {
1177     assert links != null : "Parameter 'links' must not be null.";
1178     for (int iii = 0; iii < links.size(); iii++) {
1179       EnzymeLinkDTO enzymeLinkDTO = (EnzymeLinkDTO) links.get(iii);
1180       EnzymeLink staticLink = EnzymeLink.getStaticLink(XrefDatabaseConstant.valueOf(enzymeLinkDTO.getDatabaseCode()));
1181       if (staticLink != null) {
1182         enzymeLinkDTO.setDatabaseName(staticLink.getXrefDatabaseConstant().getDisplayName());
1183         enzymeLinkDTO.setName(staticLink.getName());
1184         enzymeLinkDTO.setUrl(staticLink.getFullUrl(
1185         		EnzymeCommissionNumber.isPreliminary(ec)?
1186         				enzymeLinkDTO.getAccession() : ec));
1187       }
1188     }
1189   }
1190 
1191   private void updateStatus() {
1192     if (statusCode != null && !statusCode.equals("")) {
1193       statusText = Status.fromCode(statusCode).toString();
1194     }
1195   }
1196 }
1197