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.IntEnzValidations;
13 import uk.ac.ebi.rhea.domain.Database;
14 import uk.ac.ebi.rhea.domain.Reaction;
15 import uk.ac.ebi.rhea.domain.Status;
16 import uk.ac.ebi.xchars.SpecialCharacters;
17
18
19
20
21
22 public class ReactionDTO extends ActionForm {
23
24 private long id = Reaction.NO_ID_ASSIGNED;
25 private String textualRepresentation;
26 private String xmlTextualRepresentation;
27 private String orderIn;
28 private String source;
29 private String view;
30 private String iubmb;
31 private String sourceDisplay;
32 private String viewDisplayString;
33 private String viewDisplayImage;
34 private String status;
35
36 public ReactionDTO() {
37 textualRepresentation = "";
38 xmlTextualRepresentation = "";
39 orderIn = "0";
40 source = Database.INTENZ.getDbCode();
41 view = "INTENZ";
42 setIubmb("true");
43 sourceDisplay = Database.INTENZ.getName();
44 viewDisplayString = "all views";
45 viewDisplayImage = "\"<img src=\\\"images/blue_bullet.gif\\\"/><img src=\\\"images/green_bullet.gif\\\"/><img src=\\\"images/red_bullet.gif\\\"/>\"";
46 status = Status.NO.toString();
47 }
48
49 public ReactionDTO(ReactionDTO reactionDTO) {
50 setTextualRepresentation(reactionDTO.getTextualRepresentation());
51 setXmlTextualRepresentation(reactionDTO.getXmlTextualRepresentation());
52 setSource(reactionDTO.getSource());
53 setView(reactionDTO.getView());
54 setIubmb(reactionDTO.getIubmb());
55 setSourceDisplay(reactionDTO.getSourceDisplay());
56 setViewDisplayString(reactionDTO.getViewDisplayString());
57 setViewDisplayImage(reactionDTO.getViewDisplayImage());
58 }
59
60 @Override
61 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
62 ActionErrors errors = new ActionErrors();
63 SpecialCharacters encoding = (SpecialCharacters) request.getSession().getAttribute("characters");
64 updateTextualRepresentation(encoding);
65 updateSource();
66 updateView();
67 if (!SyntaxValidator.getInstance().validate(xmlTextualRepresentation, SyntaxValidator.BRACKETS))
68 errors.add("reaction", new ActionMessage("errors.form.brackets", "reaction", xmlTextualRepresentation));
69
70
71
72
73 if (!IntEnzValidations.hasNoUnicode(xmlTextualRepresentation))
74 errors.add("reaction", new ActionMessage("errors.form.unicode", "reaction", xmlTextualRepresentation));
75 return errors.isEmpty() ? null : errors;
76 }
77
78 @Override
79 public boolean equals(Object o) {
80 if (this == o) return true;
81 if (!(o instanceof ReactionDTO)) return false;
82
83 final ReactionDTO reactionDTO = (ReactionDTO) o;
84
85
86 if (xmlTextualRepresentation != null ?
87 !xmlTextualRepresentation.equals(reactionDTO.xmlTextualRepresentation) :
88 reactionDTO.xmlTextualRepresentation != null)
89 return false;
90
91 return true;
92 }
93
94 @Override
95 public int hashCode() {
96 int result;
97 result = (xmlTextualRepresentation != null ? xmlTextualRepresentation.hashCode() : 0);
98
99 return result;
100 }
101
102
103
104
105
106
107
108 private void updateTextualRepresentation(SpecialCharacters encoding) {
109 if (xmlTextualRepresentation != null && !xmlTextualRepresentation.equals("")) {
110 textualRepresentation = encoding.xml2Display(xmlTextualRepresentation.trim());
111 }
112 }
113
114
115
116
117 private void updateSource() {
118 if (source != null && !source.equals("")) {
119 sourceDisplay = Database.valueOf(source).getName();
120 }
121 }
122
123
124
125
126 private void updateView() {
127 if(view != null && !view.equals("")) {
128 viewDisplayImage = EnzymeViewConstant.toDisplayImage(view);
129 viewDisplayString = EnzymeViewConstant.toDisplayString(view);
130 }
131 }
132
133
134
135 public long getId() {
136 return id;
137 }
138
139 public void setId(long id) {
140 this.id = id;
141 }
142
143 public String getTextualRepresentation() {
144 return textualRepresentation;
145 }
146
147 public void setTextualRepresentation(String textualRepresentation) {
148 this.textualRepresentation = textualRepresentation
149 .replaceAll("[\r\n]+(.)", " $1")
150 .replaceAll("[\r\n]+$", "");
151 }
152
153 public String getXmlTextualRepresentation() {
154 return xmlTextualRepresentation;
155 }
156
157 public void setXmlTextualRepresentation(String xmlTextualRepresentation) {
158 this.xmlTextualRepresentation = xmlTextualRepresentation
159 .replaceAll("[\r\n]+(.)", " $1")
160 .replaceAll("[\r\n]+$", "");
161 }
162
163 public String getOrderIn() {
164 return orderIn;
165 }
166
167 public void setOrderIn(String orderIn) {
168 this.orderIn = orderIn;
169 }
170
171 public String getSource() {
172 return source;
173 }
174
175 public void setSource(String source) {
176 this.source = source;
177 }
178
179 public String getView() {
180 return view;
181 }
182
183 public void setView(String view) {
184 this.view = view;
185 }
186
187 public String getIubmb() {
188 return iubmb;
189 }
190
191 public void setIubmb(String iubmb) {
192 this.iubmb = iubmb;
193 }
194
195 public String getSourceDisplay() {
196 return sourceDisplay;
197 }
198
199 public void setSourceDisplay(String sourceDisplay) {
200 this.sourceDisplay = sourceDisplay;
201 }
202
203 public String getViewDisplayString() {
204 return viewDisplayString;
205 }
206
207 public void setViewDisplayString(String viewDisplayString) {
208 this.viewDisplayString = viewDisplayString;
209 }
210
211 public String getViewDisplayImage() {
212 return viewDisplayImage;
213 }
214
215 public void setViewDisplayImage(String viewDisplayImage) {
216 this.viewDisplayImage = viewDisplayImage;
217 }
218
219 public String getStatus() {
220 return status;
221 }
222
223 public void setStatus(String status) {
224 this.status = status;
225 }
226
227 }