1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts;
20
21 import java.io.Serializable;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import javax.servlet.http.HttpServletRequest;
27
28 import org.dom4j.Document;
29 import org.dom4j.DocumentException;
30 import org.dom4j.DocumentHelper;
31 import org.dom4j.Element;
32 import org.json.JSONArray;
33 import org.json.JSONException;
34 import org.json.JSONObject;
35 import org.springframework.extensions.surf.FrameworkUtil;
36 import org.springframework.extensions.surf.RequestContext;
37 import org.springframework.extensions.webscripts.Description;
38 import org.springframework.extensions.webscripts.ScriptModelObject;
39
40
41
42
43
44 public final class ScriptWizard implements Serializable
45 {
46 protected RequestContext context = null;
47
48 public ScriptWizard(RequestContext context)
49 {
50 this.context = context;
51 }
52
53 protected HttpServletRequest getHttpServletRequest()
54 {
55 return this.context.getRequest();
56 }
57
58
59
60 protected JSONObject request = null;
61 protected JSONObject response = null;
62 protected String id = null;
63 protected String currentPageId = null;
64 protected String previousPageId = null;
65 protected boolean isCurrentPageStart = false;
66 protected boolean isCurrentPageEnd = false;
67
68 public void setResponse(String key, String value)
69 {
70 try
71 {
72 response.put(key, value);
73 }
74 catch (JSONException je)
75 {
76 FrameworkUtil.getLogger().error(je);
77 }
78 }
79
80 public void setResponse(String key, boolean value)
81 {
82 try
83 {
84 response.put(key, value);
85 }
86 catch (JSONException je)
87 {
88 FrameworkUtil.getLogger().error(je);
89 }
90 }
91
92 public void setResponse(String key, Object value)
93 {
94 try
95 {
96 response.put(key, value);
97 }
98 catch (JSONException je)
99 {
100 FrameworkUtil.getLogger().error(je);
101 }
102 }
103
104 public String getId()
105 {
106 return id;
107 }
108
109 public void setId(String id)
110 {
111 this.id = id;
112 }
113
114 public void init() throws JSONException, DocumentException
115 {
116
117 Map args = (Map) getModel().get("args");
118 String json = (String) args.get("json");
119
120
121 String debug = (String) args.get("debug");
122 if ("true".equals(debug))
123 {
124 JSONObject o = new JSONObject();
125 o.put("schema", "webstudio32");
126 o.put("windowId", "test-window-id");
127 json = o.toString();
128 }
129 init(json);
130 }
131
132 public void init(String jsonRequestString) throws JSONException,
133 DocumentException
134 {
135
136 request = new JSONObject(jsonRequestString);
137 response = new JSONObject();
138
139
140 setResponse("schema", "webstudio32");
141
142
143 response.put("windowId", request.get("windowId"));
144
145
146 try
147 {
148 String _previousPageId = (String) request.get("currentPageId");
149 if (_previousPageId != null)
150 {
151 previousPageId = _previousPageId;
152 }
153 }
154 catch (Exception ex)
155 {
156 }
157 try
158 {
159 String _currentPageId = (String) request.get("requestedPageId");
160 if (_currentPageId != null)
161 {
162 currentPageId = _currentPageId;
163 }
164 }
165 catch (Exception ex)
166 {
167 }
168
169
170 processRequest();
171 }
172
173 protected void processRequest() throws DocumentException, JSONException
174 {
175
176
177 Description webscript = (Description) getModel().get("webscript");
178
179
180 ScriptConfigModel config = (ScriptConfigModel) getModel().get("config");
181 String xml = (String) config.getScript();
182
183
184 Document doc = DocumentHelper.parseText(xml);
185 Element root = doc.getRootElement();
186 String wizardId = root.attribute("id").getStringValue();
187 setId(wizardId);
188
189
190 Element currentPageElement = null;
191
192
193 String title = root.elementText("title");
194 setResponseTitle(title);
195 String uri = null;
196 String[] uris = webscript.getURIs();
197 if (uris != null)
198 {
199 uri = uris[0];
200 setResponseURI(uri);
201 }
202
203
204 try
205 {
206 if (request.getBoolean("refreshSession"))
207 {
208 this.sessionRemove(getId());
209 }
210 }
211 catch (Exception ex)
212 {
213 }
214
215
216 List pages = root.elements("page");
217 for (int i = 0; i < pages.size(); i++)
218 {
219 Element page = (Element) pages.get(i);
220 String pageId = page.attributeValue("id");
221 String start = page.attributeValue("start");
222 String end = page.attributeValue("end");
223
224
225
226 if (currentPageId == null && "true".equals(start))
227 currentPageId = pageId;
228
229 boolean finish = false;
230 if ("true".equals(end))
231 finish = true;
232
233
234 addPage(pageId, uri, finish);
235
236
237 if (currentPageId != null && currentPageId.equals(pageId))
238 {
239
240 currentPageElement = page;
241
242
243 if ("true".equals(end))
244 isCurrentPageEnd = true;
245 if ("true".equals(start))
246 isCurrentPageStart = true;
247
248
249 Element buttonsElement = page.element("buttons");
250 if (buttonsElement != null)
251 {
252 List buttons = buttonsElement.elements("button");
253 for (int j = 0; j < buttons.size(); j++)
254 {
255 Element button = (Element) buttons.get(j);
256 String buttonId = (String) button.attributeValue("id");
257 String buttonLabel = (String) button
258 .attributeValue("label");
259 String buttonAction = (String) button
260 .attributeValue("action");
261 String buttonData = (String) button
262 .attributeValue("data");
263 String buttonEnabledString = (String) button
264 .attributeValue("enabled");
265 String buttonHiddenString = (String) button
266 .attributeValue("hidden");
267
268 boolean buttonEnabled = true;
269 if ("false".equals(buttonEnabledString))
270 buttonEnabled = false;
271
272 boolean buttonHidden = false;
273 if ("true".equals(buttonHiddenString))
274 buttonHidden = true;
275
276
277 addButton(buttonId, buttonLabel, buttonAction,
278 buttonData, buttonEnabled, buttonHidden);
279 }
280 }
281 }
282
283
284 if (previousPageId != null && previousPageId.equals(pageId))
285 {
286
287 Element dataElement = page.element("data");
288 if (dataElement != null)
289 {
290 List elements = dataElement.elements("element");
291 for (int k = 0; k < elements.size(); k++)
292 {
293 Element element = (Element) elements.get(k);
294 String elementId = (String) element
295 .attributeValue("id");
296
297
298 JSONArray requestElements = null;
299 try
300 {
301 requestElements = request.getJSONArray("elements");
302 }
303 catch (JSONException jsonEx)
304 {
305 }
306 if (requestElements != null)
307 {
308 for (int z = 0; z < requestElements.length(); z++)
309 {
310 JSONObject requestElement = (JSONObject) requestElements
311 .get(z);
312 String requestElementName = (String) requestElement
313 .get("name");
314 if (requestElementName != null
315 && requestElementName.equals(elementId))
316 {
317 String requestElementValue = (String) requestElement
318 .get("value");
319
320 this.sessionPut(requestElementName,
321 requestElementValue);
322 }
323 }
324 }
325 }
326 }
327 }
328 }
329
330
331 current().put("id", currentPageId);
332 current().put("isStart", isCurrentPageStart);
333 current().put("isEnd", isCurrentPageEnd);
334
335
336 if (currentPageElement != null)
337 {
338
339 String currentPageType = currentPageElement.attributeValue("type");
340 if (currentPageType != null && currentPageType.length() != 0)
341 {
342 this.setDialogType(currentPageType);
343 }
344
345
346 String currentPageTitle = currentPageElement
347 .attributeValue("title");
348 if (currentPageTitle != null && currentPageTitle.length() != 0)
349 {
350 this.setResponseTitle(currentPageTitle);
351 }
352
353
354 Element dataEl = currentPageElement.element("data");
355 if (dataEl != null)
356 {
357 List elementList = dataEl.elements("element");
358 for (int u = 0; u < elementList.size(); u++)
359 {
360 Element el = (Element) elementList.get(u);
361
362 String elName = (String) el.attributeValue("id");
363 String elType = (String) el.attributeValue("type");
364 String elDefaultValue = (String) el
365 .attributeValue("defaultValue");
366
367 if ("String".equals(elType))
368 {
369 String elValue = (String) sessionGet(elName,
370 elDefaultValue);
371 getModel().put(elName, elValue);
372 }
373 }
374 }
375 }
376 }
377
378 public String getCurrentPageId()
379 {
380 return this.currentPageId;
381 }
382
383 public boolean isCurrentPageStart()
384 {
385 return this.isCurrentPageStart;
386 }
387
388 public boolean isCurrentPageEnd()
389 {
390 return this.isCurrentPageEnd;
391 }
392
393 public void setCurrentPageId(String currentPageId)
394 {
395 this.currentPageId = currentPageId;
396 }
397
398 public void finalize()
399 {
400 Map model = (Map) getModel().get("model");
401 model.put("json", response.toString());
402 }
403
404 public String request(String name) throws JSONException
405 {
406 return (String) request.getString(name);
407 }
408
409
410
411
412
413
414
415 public void setCacheInvalidateAll() throws JSONException
416 {
417 current().put("cacheInvalidateAll", true);
418 }
419
420 public void setBrowserReload(boolean reload) throws JSONException
421 {
422 current().put("reload", reload);
423 }
424
425 public void setDialogType(String dialogType) throws JSONException
426 {
427 current().put("dialogtype", dialogType);
428 }
429
430 public void setDialogForm() throws JSONException
431 {
432 setDialogType("form");
433 }
434
435 public void setDialogHTML(String html) throws JSONException
436 {
437 setDialogType("html");
438 current().put("html", html);
439 }
440
441 public void setDialogURL(String url) throws JSONException
442 {
443 setDialogType("url");
444 current().put("url", url);
445 }
446
447 public void setResponseCode(String code) throws JSONException
448 {
449 current().put("code", code);
450 }
451
452 public void setResponseCodeOK() throws JSONException
453 {
454 setResponseCode("ok");
455 }
456
457 public void setResponseCodeFinish() throws JSONException
458 {
459 setCacheInvalidateAll();
460 setResponseCode("finish");
461 }
462
463 public void setResponseMessage(String message) throws JSONException
464 {
465 current().put("message", message);
466 }
467
468 public void setResponseTitle(String title) throws JSONException
469 {
470 current().put("title", title);
471 }
472
473 public void setResponseURI(String uri) throws JSONException
474 {
475 current().put("uri", uri);
476 }
477
478 public void setFormFieldFocus(String id, String selectText, String focusCls)
479 throws JSONException
480 {
481 JSONObject focus = new JSONObject();
482 focus.put("id", id);
483 if (selectText != null)
484 focus.put("selectText", selectText);
485 if (focusCls != null)
486 focus.put("focusCls", focusCls);
487 current().put("focus", focus);
488 }
489
490 public void setRedirectToPage(String pageId) throws JSONException
491 {
492 setResponseCode("redirect");
493 current().put("redirect-type", "page");
494 current().put("redirect-target", pageId);
495 }
496
497
498
499
500
501
502
503 protected JSONObject data() throws JSONException
504 {
505 return ensureJSONObject(response, "data");
506 }
507
508 protected JSONArray elements() throws JSONException
509 {
510 return ensureJSONArray(response, "elements");
511 }
512
513 protected JSONArray elementFormats() throws JSONException
514 {
515 return ensureJSONArray(response, "elementformats");
516 }
517
518 protected JSONObject elementValues() throws JSONException
519 {
520 return ensureJSONObject(response, "elementvalues");
521 }
522
523 protected JSONArray buttons() throws JSONException
524 {
525 return ensureJSONArray(response, "buttons");
526 }
527
528 protected JSONArray pages() throws JSONException
529 {
530 return ensureJSONArray(response, "pages");
531 }
532
533 protected JSONObject grid() throws JSONException
534 {
535 return ensureJSONObject(response, "grid");
536 }
537
538 protected JSONObject current() throws JSONException
539 {
540 return ensureJSONObject(response, "current");
541 }
542
543
544
545 protected JSONObject ensureJSONObject(JSONObject obj, String objectName)
546 throws JSONException
547 {
548 JSONObject o = null;
549 try
550 {
551 o = obj.getJSONObject(objectName);
552 }
553 catch (Exception ex)
554 {
555 }
556 if (o == null)
557 {
558 o = new JSONObject();
559 obj.put(objectName, o);
560 }
561 return o;
562 }
563
564 protected JSONArray ensureJSONArray(JSONObject obj, String arrayName)
565 throws JSONException
566 {
567 JSONArray a = null;
568 try
569 {
570 a = obj.getJSONArray(arrayName);
571 }
572 catch (Exception ex)
573 {
574 }
575 if (a == null)
576 {
577 a = new JSONArray();
578 obj.put(arrayName, a);
579 }
580 return a;
581 }
582
583
584
585
586
587
588
589 public void addElement(String name, String value) throws JSONException
590 {
591 JSONObject element = new JSONObject();
592 element.put("name", name);
593 element.put("value", value);
594 elements().put(element);
595 }
596
597 public void addHiddenElement(String name, String value)
598 throws JSONException
599 {
600 addElement(name, value);
601 addElementFormat(name, null, null);
602 addElementFormatKeyPair(name, "hidden", true);
603 }
604
605 public void updateElement(String name, String value) throws JSONException
606 {
607 if (name == null)
608 return;
609
610 JSONArray elements = elements();
611 for (int i = 0; i < elements.length(); i++)
612 {
613 JSONObject element = (JSONObject) elements.get(i);
614 String _name = (String) element.get("name");
615 if (name.equals(_name))
616 element.put("value", value);
617 }
618 }
619
620 public void addElementFormat(String name, String label, String type)
621 throws JSONException
622 {
623 addElementFormat(name, label, type, -1, -1);
624 }
625
626 public void addElementFormat(String name, String label, String type,
627 int width) throws JSONException
628 {
629 addElementFormat(name, label, type, width, -1);
630 }
631
632 public void addElementFormat(String name, String label, String type,
633 int width, int height) throws JSONException
634 {
635 JSONArray elementFormats = elementFormats();
636
637 JSONObject element = new JSONObject();
638 element.put("name", name);
639
640 if (label != null)
641 element.put("label", label);
642 if (type != null)
643 element.put("type", type);
644 if (width != -1)
645 element.put("width", width);
646 if (height != -1)
647 element.put("height", height);
648
649 elementFormats.put(element);
650 }
651
652 public void addElementFormatKeyPair(String name, String key, String value)
653 throws JSONException
654 {
655 JSONArray elementFormats = elementFormats();
656
657 for (int i = 0; i < elementFormats.length(); i++)
658 {
659 JSONObject elementFormat = (JSONObject) elementFormats.get(i);
660 String _name = (String) elementFormat.get("name");
661 if (name.equals(_name))
662 elementFormat.put(key, value);
663 }
664 }
665
666 public void addElementFormatKeyPair(String name, String key, boolean value)
667 throws JSONException
668 {
669 JSONArray elementFormats = elementFormats();
670
671 for (int i = 0; i < elementFormats.length(); i++)
672 {
673 JSONObject elementFormat = (JSONObject) elementFormats.get(i);
674 String _name = (String) elementFormat.get("name");
675 if (name.equals(_name))
676 elementFormat.put(key, value);
677 }
678 }
679
680 public void addElementSelectionValue(String name, String selectionId,
681 String selectionValue) throws JSONException
682 {
683 JSONObject elementValues = elementValues();
684 JSONArray values = ensureJSONArray(elementValues, name);
685
686
687 JSONArray ourValue = new JSONArray();
688 ourValue.put(selectionId);
689 ourValue.put(selectionValue);
690
691
692 values.put(ourValue);
693 }
694
695 public Object getElementValue(String name) throws JSONException
696 {
697 JSONArray elements = elements();
698 for (int i = 0; i < elements.length(); i++)
699 {
700 JSONObject element = (JSONObject) elements.get(i);
701 String _name = (String) element.get("name");
702 if (name.equals(_name))
703 return element.get("value");
704 }
705 return null;
706 }
707
708 public String getElementStringValue(String name) throws JSONException
709 {
710 return (String) getElementValue(name);
711 }
712
713
714
715
716
717
718
719 public void addButton(String id, String text, String action, String data,
720 boolean enabled, boolean hidden) throws JSONException
721 {
722 JSONObject button = new JSONObject();
723 button.put("id", id);
724 if (text != null)
725 button.put("text", text);
726 if (action != null)
727 button.put("action", action);
728 if (data != null)
729 button.put("data", data);
730 button.put("enabled", enabled);
731 button.put("hidden", hidden);
732
733
734 buttons().put(button);
735 }
736
737 public JSONObject getButton(String id) throws JSONException
738 {
739 JSONObject button = null;
740
741 for (int i = 0; i < buttons().length(); i++)
742 {
743 JSONObject obj = buttons().getJSONObject(i);
744
745 if (id.equalsIgnoreCase(obj.getString("id")))
746 {
747 button = obj;
748 }
749 }
750
751 return button;
752 }
753
754 public void enableButton(String id) throws JSONException
755 {
756 JSONObject button = getButton(id);
757 if (button != null)
758 {
759 button.put("enabled", true);
760 }
761 }
762
763 public void disableButton(String id) throws JSONException
764 {
765 JSONObject button = getButton(id);
766 if (button != null)
767 {
768 button.put("enabled", false);
769 }
770 }
771
772 public void hideButton(String id) throws JSONException
773 {
774 JSONObject button = getButton(id);
775 if (button != null)
776 {
777 button.put("hidden", true);
778 }
779 }
780
781 public void showButton(String id) throws JSONException
782 {
783 JSONObject button = getButton(id);
784 if (button != null)
785 {
786 button.put("hidden", false);
787 }
788 }
789
790
791
792
793
794
795
796 public void addPage(String id, String uri, boolean finish)
797 throws JSONException
798 {
799 JSONObject page = new JSONObject();
800 page.put("id", id);
801 if (uri != null)
802 page.put("uri", uri);
803 page.put("finish", finish);
804 pages().put(page);
805 }
806
807
808
809
810
811
812
813
814
815
816
817
818 public void addGridData(Object[] array) throws JSONException
819 {
820 JSONArray griddata = ensureJSONArray(grid(), "griddata");
821
822 JSONArray data = new JSONArray();
823 for (int i = 0; i < array.length; i++)
824 data.put(array[i]);
825
826 griddata.put(data);
827 }
828
829 public void addGridColumn(String id, String text) throws JSONException
830 {
831 JSONArray columns = ensureJSONArray(grid(), "columns");
832
833 JSONObject column = new JSONObject();
834 column.put("id", id);
835 column.put("text", text);
836 column.put("dataIndex", id);
837 columns.put(column);
838 }
839
840 public void addGridColumnFormat(String id, String width, String sortable)
841 throws JSONException
842 {
843 JSONArray columnformats = ensureJSONArray(grid(), "columnformats");
844
845 JSONObject column = new JSONObject();
846 column.put("id", id);
847 if (width != null)
848 column.put("width", width);
849 if (sortable != null)
850 column.put("sortable", sortable);
851 columnformats.put(column);
852 }
853
854 public void addGridToolbar(String id, String text, String tooltip,
855 String iconCls) throws JSONException
856 {
857 JSONArray toolbar = ensureJSONArray(grid(), "toolbar");
858
859 JSONObject item = new JSONObject();
860 item.put("id", id);
861 if (text != null)
862 item.put("text", text);
863 if (tooltip != null)
864 item.put("tooltip", tooltip);
865 if (iconCls != null)
866 item.put("iconCls", iconCls);
867 toolbar.put(item);
868 }
869
870 public void addGridToolbarSpacer() throws JSONException
871 {
872 JSONArray toolbar = ensureJSONArray(grid(), "toolbar");
873 toolbar.put("-");
874 }
875
876 public void addGridNoDataMessage(String message) throws JSONException
877 {
878 grid().put("nodatamessage", message);
879 }
880
881
882
883
884
885
886
887 protected Map ensureMap()
888 {
889 HttpServletRequest r = getHttpServletRequest();
890 if (r != null)
891 {
892 Map map = (Map) r.getSession().getAttribute(
893 "scriptWizard-state-" + getId());
894 if (map == null)
895 {
896 map = new HashMap(32, 1.0f);
897 r.getSession().setAttribute("scriptWizard-state-" + getId(),
898 map);
899 }
900 return map;
901 }
902 return null;
903 }
904
905 public void sessionPut(String name, String value)
906 {
907 Map map = ensureMap();
908 map.put(name, value);
909 }
910
911 public String sessionGet(String name)
912 {
913 Map map = ensureMap();
914 return (String) map.get(name);
915 }
916
917 public String sessionGet(String name, String defaultValue)
918 {
919 Map map = ensureMap();
920 String result = (String) map.get(name);
921 if (result == null)
922 result = defaultValue;
923 return result;
924 }
925
926 public void sessionRemove(String name)
927 {
928 HttpServletRequest r = getHttpServletRequest();
929 if (r != null)
930 {
931 r.getSession().removeAttribute("scriptWizard-state-" + getId());
932 }
933 }
934
935
936
937
938
939
940
941 public String getResponse()
942 {
943 return response.toString();
944 }
945
946 public void putResponse(String str) throws JSONException
947 {
948 response = new JSONObject(str);
949 }
950
951 public String getSafeProperty(ScriptModelObject obj, String propertyName)
952 {
953 String value = obj.getProperty(propertyName);
954 if (value == null)
955 value = "";
956 return value;
957 }
958
959 public String getSafeElementValue(String elementName) throws JSONException
960 {
961 String value = this.getElementStringValue(elementName);
962 if (value == null)
963 value = "";
964 return value;
965 }
966
967 public void setModel(Map<String, Object> model)
968 {
969 this.model = model;
970 }
971
972 public Map<String, Object> getModel()
973 {
974 return this.model;
975 }
976
977 protected Map<String, Object> model;
978 }