1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
package pt.digitalis.dif.controller.objects; |
7 | |
|
8 | |
import java.util.HashMap; |
9 | |
import java.util.Map; |
10 | |
|
11 | |
import pt.digitalis.dif.controller.http.HTTPConstants; |
12 | |
import pt.digitalis.dif.controller.interfaces.IDIFContext; |
13 | |
import pt.digitalis.dif.controller.interfaces.IDIFRequest; |
14 | |
import pt.digitalis.dif.controller.interfaces.IDIFSession; |
15 | |
import pt.digitalis.dif.dem.interfaces.IStage; |
16 | |
import pt.digitalis.dif.dem.managers.IDEMManager; |
17 | |
import pt.digitalis.dif.dem.managers.impl.UsageIssuesManagerImpl; |
18 | |
import pt.digitalis.dif.dem.objects.EventType; |
19 | |
import pt.digitalis.dif.dem.objects.ViewObject; |
20 | |
import pt.digitalis.dif.dem.objects.issues.IssueScope; |
21 | |
import pt.digitalis.dif.dem.objects.issues.IssueType; |
22 | |
import pt.digitalis.dif.dem.objects.issues.UsageIssue; |
23 | |
import pt.digitalis.dif.ioc.DIFIoCRegistry; |
24 | |
import pt.digitalis.dif.startup.DIFGeneralConfigurationParameters; |
25 | |
import pt.digitalis.dif.utils.ObjectFormatter; |
26 | |
import pt.digitalis.dif.utils.logging.DIFLogger; |
27 | |
import pt.digitalis.utils.common.CollectionUtils; |
28 | |
import pt.digitalis.utils.common.SystemUtils; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public class DIFContext implements IDIFContext { |
37 | |
|
38 | |
|
39 | 0 | private IDIFRequest difRequest = null; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | private boolean hasRedirection = false; |
45 | |
|
46 | |
|
47 | 0 | private ResultMessage resultMessage = null; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | private String stage = new String(); |
53 | |
|
54 | |
|
55 | 0 | private Map<String, Object> stageResults = new HashMap<String, Object>(); |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 0 | private String stageToRedirect = new String(); |
61 | |
|
62 | |
|
63 | 0 | private Map<String, Object> temporaryAttributes = new HashMap<String, Object>(); |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | 0 | protected ViewObject theView = new ViewObject(); |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public void addResultMessage(String type, String title, String description) |
75 | |
{ |
76 | 0 | addResultMessage(type, title, description, null); |
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public void addResultMessage(String type, String title, String description, boolean popupMode) |
84 | |
{ |
85 | 0 | resultMessage = new ResultMessage(type, title, description, popupMode, null); |
86 | 0 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public void addResultMessage(String type, String title, String description, String mode) |
93 | |
{ |
94 | 0 | resultMessage = new ResultMessage(type, title, description, false, mode); |
95 | 0 | } |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public void addStageResult(String resultName, Object resultValue) |
101 | |
{ |
102 | 0 | this.stageResults.put(resultName, resultValue); |
103 | 0 | } |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public String getLanguage() |
109 | |
{ |
110 | 0 | IDIFSession session = getSession(); |
111 | |
|
112 | 0 | if (session == null) |
113 | 0 | return DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage(); |
114 | |
else |
115 | 0 | return session.getLanguage(); |
116 | |
} |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public IDIFRequest getRequest() |
122 | |
{ |
123 | 0 | return difRequest; |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public ResultMessage getResultMessage() |
130 | |
{ |
131 | 0 | return resultMessage; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public IDIFSession getSession() |
138 | |
{ |
139 | 0 | if (this.difRequest != null) |
140 | |
{ |
141 | 0 | return this.difRequest.getSession(); |
142 | |
} |
143 | |
else |
144 | |
{ |
145 | 0 | return null; |
146 | |
} |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public String getStage() |
153 | |
{ |
154 | 0 | return this.stage; |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public final Map<String, Object> getStageResults() |
161 | |
{ |
162 | 0 | return stageResults; |
163 | |
} |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public Map<String, Object> getTemporaryAttributes() |
169 | |
{ |
170 | 0 | return temporaryAttributes; |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
public ViewObject getView() |
177 | |
{ |
178 | 0 | return this.theView; |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public void handleRedirection() |
188 | |
{ |
189 | 0 | this.stage = this.stageToRedirect; |
190 | 0 | this.stageToRedirect = null; |
191 | 0 | this.hasRedirection = false; |
192 | 0 | } |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
public boolean hasRedirection() |
198 | |
{ |
199 | 0 | return this.hasRedirection; |
200 | |
} |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
public void redirectTo(String newStage) |
206 | |
{ |
207 | 0 | this.redirectTo(newStage, ""); |
208 | 0 | } |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
public void redirectTo(String newStage, boolean allowRedirectToSameStage) |
214 | |
{ |
215 | 0 | this.redirectTo(newStage, "", allowRedirectToSameStage); |
216 | 0 | } |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
public void redirectTo(String newStage, Map<String, Object> redirectionParameters) |
222 | |
{ |
223 | 0 | this.redirectTo(newStage, redirectionParameters, false); |
224 | 0 | } |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
public void redirectTo(String newStage, Map<String, Object> redirectionParameters, boolean allowRedirectToSameStage) |
230 | |
{ |
231 | |
|
232 | |
|
233 | 0 | if (allowRedirectToSameStage || !this.getStage().equals(newStage)) |
234 | |
{ |
235 | 0 | this.stageToRedirect = newStage; |
236 | 0 | this.hasRedirection = true; |
237 | |
|
238 | 0 | if (redirectionParameters != null && !redirectionParameters.isEmpty()) |
239 | |
{ |
240 | 0 | this.difRequest.setParameters(redirectionParameters); |
241 | |
} |
242 | |
} |
243 | |
else |
244 | 0 | DIFLogger.getLogger().warn( |
245 | 0 | "Prevented a redirection to the same stage: \"" + stageToRedirect |
246 | 0 | + "\" (cyclic redirection danger!)."); |
247 | 0 | } |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
public void redirectTo(String newStage, String redirectionParameters) |
253 | |
{ |
254 | 0 | redirectTo(newStage, redirectionParameters, false); |
255 | 0 | } |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
public void redirectTo(String newStage, String redirectionParameters, boolean allowRedirectToSameStage) |
261 | |
{ |
262 | 0 | Map<String, Object> parameterMap = new HashMap<String, Object>(); |
263 | |
|
264 | 0 | if (redirectionParameters != null && !"".equals(redirectionParameters)) |
265 | 0 | parameterMap = CollectionUtils.keyValueStringToMapObject(redirectionParameters); |
266 | |
|
267 | 0 | redirectTo(newStage, parameterMap, allowRedirectToSameStage); |
268 | 0 | } |
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
public void reportIssue(String uID, IssueType type, String issueDescription) |
275 | |
{ |
276 | 0 | this.reportIssue(uID, type, issueDescription, null); |
277 | 0 | } |
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
public void reportIssue(String uID, IssueType type, String issueDescription, Exception exception) |
284 | |
{ |
285 | 0 | this.reportIssue(uID, type, issueDescription, exception, true); |
286 | 0 | } |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
public void reportIssue(String uID, IssueType type, String issueDescription, Exception exception, |
293 | |
boolean showStackTrace) |
294 | |
{ |
295 | 0 | IStage stage = DIFIoCRegistry.getRegistry().getImplementation(IDEMManager.class).getStage(this.getStage()); |
296 | |
|
297 | 0 | Object submitForm = this.getRequest().getParameter(HTTPConstants.FORM_SUBMIT_NAME); |
298 | 0 | Object eventID = this.getRequest().getParameter(HTTPConstants.EVENT_ID); |
299 | 0 | EventType eventType = null; |
300 | |
|
301 | |
|
302 | 0 | if (stage.getEventHandlers().get(EventType.FORM_SUBMIT).contains(submitForm)) |
303 | 0 | eventType = EventType.FORM_SUBMIT; |
304 | 0 | else if (stage.getEventHandlers().get(EventType.FORM_SUBMIT_SAVE_ACTION).contains(submitForm)) |
305 | 0 | eventType = EventType.FORM_SUBMIT_SAVE_ACTION; |
306 | 0 | else if (stage.getEventHandlers().get(EventType.FORM_SUBMIT_AJAX_REQUEST).contains(submitForm)) |
307 | 0 | eventType = EventType.FORM_SUBMIT_AJAX_REQUEST; |
308 | 0 | else if (stage.getEventHandlers().get(EventType.AJAX_REQUEST).contains(eventID)) |
309 | 0 | eventType = EventType.AJAX_REQUEST; |
310 | 0 | else if (stage.getEventHandlers().get(EventType.DOCUMENT_TYPE).contains(eventID)) |
311 | 0 | eventType = EventType.DOCUMENT_TYPE; |
312 | |
|
313 | 0 | if (eventID != null) |
314 | 0 | issueDescription += " | <b>Event:</b> " + eventID + (eventType != null ? " [" + eventType + "]" : ""); |
315 | 0 | if (submitForm != null) |
316 | 0 | issueDescription += " | <b>Form:</b> " + submitForm; |
317 | |
|
318 | 0 | if (showStackTrace) |
319 | |
{ |
320 | 0 | String traceContent = null; |
321 | |
|
322 | 0 | if (showStackTrace) |
323 | |
{ |
324 | 0 | if (exception == null) |
325 | |
{ |
326 | 0 | StackTraceElement[] traceList = Thread.currentThread().getStackTrace(); |
327 | 0 | StackTraceElement[] parsedTraceList = new StackTraceElement[traceList.length - 1]; |
328 | 0 | int totalToRemove = 1; |
329 | |
|
330 | |
|
331 | 0 | while (traceList[totalToRemove].getMethodName().contains("reportIssue")) |
332 | 0 | totalToRemove++; |
333 | 0 | System.arraycopy(traceList, totalToRemove, parsedTraceList, 0, parsedTraceList.length |
334 | 0 | - totalToRemove); |
335 | |
|
336 | 0 | traceContent = SystemUtils.getStackTraceListHTML(parsedTraceList).toString(); |
337 | |
} |
338 | |
else |
339 | 0 | traceContent = SystemUtils.getStackTraceHTML(exception); |
340 | |
} |
341 | |
|
342 | 0 | String requestSum = "<b>Stage:</b> " + stage.getOriginalClassName() + "<br/>"; |
343 | 0 | if (eventID != null) |
344 | 0 | requestSum += "<b>Event:</b> " + eventID + (eventType != null ? " [" + eventType + "]" : "") + "<br/>"; |
345 | 0 | if (submitForm != null) |
346 | 0 | requestSum += "<b>Form:</b> " + submitForm + "<br/>"; |
347 | |
|
348 | 0 | StringBuffer buffer = new StringBuffer(); |
349 | 0 | buffer.append("Ext.create('Ext.window.Window',{"); |
350 | 0 | buffer.append("title: 'Track of the orginating execution of the issue',"); |
351 | 0 | buffer.append("width:800,height:460,laytout:'border',modal:true,"); |
352 | 0 | buffer.append("buttonAlign:'center',buttons:[{ text: 'OK',handler:function(b){b.up('window').close()}}],"); |
353 | 0 | buffer.append("defaults:{autoScroll:true,bodyPadding:5},"); |
354 | 0 | buffer.append("items:[{"); |
355 | 0 | buffer.append("region:'north',height:150,bodyCls:'backgthememedium',html: '" + requestSum + "<br/>" |
356 | 0 | + issueDescription + "'"); |
357 | 0 | buffer.append("},{xtype:'tabpanel',height:270,region:'center',laytout:'fit',defaults:{autoScroll:true,bodyPadding:5},items:[{"); |
358 | 0 | buffer.append("title:'Stack Trace',html: '" |
359 | 0 | + (traceContent == null ? "No stack trace available!" : traceContent.replaceAll("\n", "") |
360 | 0 | .replaceAll("\\\'", "\\\\\'").replaceAll("\\\"", "\\\\\\\\\'")) + "'"); |
361 | 0 | buffer.append("},{title:'Context',html: '<code>" |
362 | 0 | + this.toString().replaceAll("\n", "<br/>").replaceAll("\\\'", "\\\\\'") |
363 | 0 | .replaceAll("\\\"", "\\\\\\\\\'") + "</code>'"); |
364 | 0 | buffer.append("}]}]}).show();"); |
365 | |
|
366 | 0 | issueDescription = issueDescription.split("<br/>")[0].split("<br />")[0].split("<br>")[0].split("\n")[0] |
367 | 0 | + "<a onClick=\"" + buffer.toString() + "\"> | More details</a>"; |
368 | |
} |
369 | |
|
370 | 0 | UsageIssue issue = new UsageIssue(); |
371 | 0 | issue.setUID(uID == null ? null : this.getStage() + uID); |
372 | 0 | issue.setIssueScope(IssueScope.RUNTIME); |
373 | 0 | issue.setIssueType(type); |
374 | 0 | issue.setIssueDescription(issueDescription); |
375 | 0 | issue.setLocation(stage.getOriginalClassName()); |
376 | 0 | issue.setException(exception); |
377 | 0 | issue.setContext(this); |
378 | |
|
379 | 0 | this.reportIssue(issue); |
380 | 0 | } |
381 | |
|
382 | |
/** |
383 | |
* @see pt.digitalis.dif.controller.interfaces.IDIFContext#reportIssue(pt.digitalis.dif.dem.objects.issues.UsageIssue) |
384 | |
*/ |
385 | |
public void reportIssue(UsageIssue issue) |
386 | |
{ |
387 | 0 | UsageIssuesManagerImpl.getInstance().addIssue(issue); |
388 | 0 | } |
389 | |
|
390 | |
/** |
391 | |
* @see pt.digitalis.dif.controller.interfaces.IDIFContext#setRequest(pt.digitalis.dif.controller.interfaces.IDIFRequest) |
392 | |
*/ |
393 | |
public void setRequest(IDIFRequest difRequest) |
394 | |
{ |
395 | 0 | this.difRequest = difRequest; |
396 | 0 | } |
397 | |
|
398 | |
/** |
399 | |
* @see pt.digitalis.dif.controller.interfaces.IDIFContext#setSession(IDIFSession) |
400 | |
*/ |
401 | |
public void setSession(IDIFSession session) |
402 | |
{ |
403 | 0 | this.difRequest.setSession(session); |
404 | 0 | } |
405 | |
|
406 | |
/** |
407 | |
* @see pt.digitalis.dif.controller.interfaces.IDIFContext#setStage(String) |
408 | |
*/ |
409 | |
public void setStage(String stage) |
410 | |
{ |
411 | 0 | this.stage = stage; |
412 | 0 | } |
413 | |
|
414 | |
/** |
415 | |
* @see pt.digitalis.dif.controller.interfaces.IDIFContext#setStageResults(Map) |
416 | |
*/ |
417 | |
public final void setStageResults(Map<String, Object> results) |
418 | |
{ |
419 | 0 | this.stageResults = results; |
420 | 0 | } |
421 | |
|
422 | |
/** |
423 | |
* @see pt.digitalis.dif.controller.interfaces.IDIFContext#setTemporaryAttributes(java.util.Map) |
424 | |
*/ |
425 | |
public void setTemporaryAttributes(Map<String, Object> temporaryAttributes) |
426 | |
{ |
427 | 0 | this.temporaryAttributes = temporaryAttributes; |
428 | 0 | } |
429 | |
|
430 | |
/** |
431 | |
* @see pt.digitalis.dif.controller.interfaces.IDIFContext#setView(ViewObject) |
432 | |
*/ |
433 | |
public void setView(ViewObject newView) |
434 | |
{ |
435 | 0 | this.theView = newView; |
436 | 0 | } |
437 | |
|
438 | |
/** |
439 | |
* Prints the context information on an human-readable form. Overrides java.lang.Object#toString(). |
440 | |
* |
441 | |
* @see java.lang.Object#toString() |
442 | |
*/ |
443 | |
@Override |
444 | |
public String toString() |
445 | |
{ |
446 | 0 | ObjectFormatter formatter = new ObjectFormatter(); |
447 | 0 | formatter.addItem("Request", difRequest); |
448 | 0 | formatter.addItem("Has Redirection", hasRedirection); |
449 | 0 | formatter.addItem("View", theView); |
450 | 0 | formatter.addItem("Stage", stage); |
451 | 0 | formatter.addItem("Stage Results", stageResults); |
452 | 0 | formatter.addItemIfNotNull("Stage to redirect", stageToRedirect); |
453 | |
|
454 | 0 | return formatter.getFormatedObject(); |
455 | |
} |
456 | |
} |