1 package uk.ac.ebi.intenz.webapp.dtos;
2
3 import javax.servlet.http.HttpServletRequest;
4
5 import org.apache.struts.action.ActionErrors;
6 import org.apache.struts.action.ActionForm;
7 import org.apache.struts.action.ActionMapping;
8 import org.apache.struts.action.ActionMessage;
9
10 import uk.ac.ebi.biobabel.validator.SyntaxValidator;
11 import uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant;
12 import uk.ac.ebi.intenz.webapp.utilities.IntEnzUtilities;
13 import uk.ac.ebi.intenz.webapp.utilities.IntEnzValidations;
14 import uk.ac.ebi.xchars.SpecialCharacters;
15 import uk.ac.ebi.xchars.XMLErrorReport;
16 import uk.ac.ebi.xchars.utilities.XCharsValidator;
17
18
19
20
21
22 public class CommentDTO extends ActionForm {
23
24
25 private String comment;
26 private String xmlComment;
27 private String orderIn;
28 private String source;
29 private String view;
30 private String sourceDisplay;
31 private String viewDisplayString;
32 private String viewDisplayImage;
33
34
35
36 public CommentDTO() {
37 comment = "";
38 xmlComment = "";
39 orderIn = "0";
40 source = "INTENZ";
41 view = "INTENZ";
42 sourceDisplay = "IntEnz";
43 viewDisplayString = "all views";
44 viewDisplayImage = "\"<img src=\\\"images/blue_bullet.gif\\\"/><img src=\\\"images/green_bullet.gif\\\"/><img src=\\\"images/red_bullet.gif\\\"/>\"";
45 }
46
47 public CommentDTO(CommentDTO commentDTO) {
48 setComment(commentDTO.getComment());
49 setXmlComment(commentDTO.getXmlComment());
50 setOrderIn(commentDTO.getOrderIn());
51 setSource(commentDTO.getSource());
52 setView(commentDTO.getView());
53 setSourceDisplay(commentDTO.getSourceDisplay());
54 setViewDisplayString(commentDTO.getViewDisplayString());
55 setViewDisplayImage(commentDTO.getViewDisplayImage());
56 }
57
58
59
60 public String getComment() {
61 return comment;
62 }
63
64 public void setComment(String comment) {
65 this.comment = comment;
66 }
67
68 public String getOrderIn() {
69 return orderIn;
70 }
71
72 public void setOrderIn(String orderIn) {
73 this.orderIn = orderIn;
74 }
75
76 public String getSource() {
77 return source;
78 }
79
80 public void setSource(String source) {
81 this.source = source;
82 }
83
84 public String getSourceDisplay() {
85 return sourceDisplay;
86 }
87
88 public void setSourceDisplay(String sourceDisplay) {
89 this.sourceDisplay = sourceDisplay;
90 }
91
92 public String getView() {
93 return view;
94 }
95
96 public void setView(String view) {
97 this.view = view;
98 }
99
100 public String getViewDisplayImage() {
101 return viewDisplayImage;
102 }
103
104 public void setViewDisplayImage(String viewDisplayImage) {
105 this.viewDisplayImage = viewDisplayImage;
106 }
107
108 public String getViewDisplayString() {
109 return viewDisplayString;
110 }
111
112 public void setViewDisplayString(String viewDisplayString) {
113 this.viewDisplayString = viewDisplayString;
114 }
115
116 public String getXmlComment() {
117 return xmlComment;
118 }
119
120 public void setXmlComment(String xmlComment) {
121 xmlComment = IntEnzUtilities.linkMarkedEC(xmlComment.trim(), false);
122 this.xmlComment = xmlComment;
123 }
124
125
126
127 public boolean equals(Object o) {
128 if (this == o) return true;
129 if (!(o instanceof CommentDTO)) return false;
130
131 final CommentDTO commentDTO = (CommentDTO) o;
132 if (source != null ? !source.equals(commentDTO.source) : commentDTO.source != null) return false;
133 if (xmlComment != null ? !xmlComment.equals(commentDTO.xmlComment) : commentDTO.xmlComment != null) return false;
134
135 return true;
136 }
137
138 public int hashCode() {
139 int result;
140 result = (xmlComment != null ? xmlComment.hashCode() : 0);
141 result = 29 * result + (source != null ? source.hashCode() : 0);
142 return result;
143 }
144
145
146
147 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
148 ActionErrors errors = new ActionErrors();
149
150 updateView();
151 updateComment(request);
152 if (!SyntaxValidator.getInstance().validate(xmlComment, SyntaxValidator.BRACKETS))
153 errors.add("comments", new ActionMessage("errors.form.brackets", "comment", xmlComment));
154 XMLErrorReport xmlErrors = XCharsValidator.validate(xmlComment);
155 if (xmlErrors != null)
156 errors.add("comments", new ActionMessage("errors.form.xchars", "comment", xmlComment, xmlErrors.getErrorMessage()));
157 if (!IntEnzValidations.hasNoUnicode(xmlComment))
158 errors.add("comments", new ActionMessage("errors.form.unicode", "comment", xmlComment));
159 return errors.isEmpty() ? null : errors;
160 }
161
162
163
164
165 private void updateView() {
166 if (view != null && !view.equals("")) {
167 viewDisplayImage = EnzymeViewConstant.toDisplayImage(view);
168 viewDisplayString = EnzymeViewConstant.toDisplayString(view);
169 }
170 }
171
172 private void updateComment(HttpServletRequest request) {
173 if (xmlComment != null && !xmlComment.equals("")) {
174 SpecialCharacters encoding = (SpecialCharacters) request.getSession().getAttribute("characters");
175 comment = encoding.xml2Display(xmlComment.trim());
176 }
177 }
178 }
179