001: /*
002: * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
003: *
004: * @version $Id: BaseFormEventImpl.java 6385 2007-10-25 14:02:26Z wehrens $
005: */
006: package org.gridsphere.provider.event.jsr.impl;
007:
008: import org.apache.commons.fileupload.FileItem;
009: import org.apache.commons.fileupload.FileItemFactory;
010: import org.apache.commons.fileupload.FileUpload;
011: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
012: import org.apache.commons.fileupload.servlet.ServletFileUpload;
013: import org.apache.commons.fileupload.servlet.ServletRequestContext;
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016: import org.gridsphere.portlet.impl.SportletProperties;
017: import org.gridsphere.provider.portletui.beans.*;
018: import org.gridsphere.services.core.persistence.QueryFilter;
019:
020: import javax.portlet.PortletRequest;
021: import javax.portlet.PortletResponse;
022: import javax.servlet.http.HttpServletRequest;
023: import java.io.IOException;
024: import java.util.*;
025:
026: /**
027: * The <code>FormEventImpl</code> provides methods for creating/retrieving visual beans
028: * from the <code>PortletRequest</code>
029: */
030: public abstract class BaseFormEventImpl {
031:
032: protected transient static Log log = LogFactory
033: .getLog(BaseFormEventImpl.class);
034:
035: protected PortletRequest portletRequest;
036: protected PortletResponse portletResponse;
037: protected Locale locale = null;
038:
039: protected Map<String, TagBean> tagBeans = null;
040: protected List<FileItem> fileItems = null;
041:
042: protected String cid = null;
043: protected String compId = null;
044:
045: protected BaseFormEventImpl() {
046:
047: }
048:
049: public BaseFormEventImpl(PortletRequest request,
050: PortletResponse response) {
051: this .portletRequest = request;
052: this .portletResponse = response;
053: locale = (Locale) portletRequest.getPortletSession(true)
054: .getAttribute(SportletProperties.LOCALE);
055: if (locale == null)
056: locale = portletRequest.getLocale();
057: if (locale == null)
058: locale = Locale.ENGLISH;
059: cid = (String) portletRequest
060: .getAttribute(SportletProperties.COMPONENT_ID);
061: compId = (String) portletRequest
062: .getAttribute(SportletProperties.GP_COMPONENT_ID);
063: }
064:
065: public PortletRequest getRequest() {
066: return portletRequest;
067: }
068:
069: protected void configureBean(TagBean tagBean) {
070: tagBean.setLocale(locale);
071: if (cid != null)
072: tagBean.addParam(SportletProperties.COMPONENT_ID, cid);
073: if (compId != null)
074: tagBean
075: .addParam(SportletProperties.GP_COMPONENT_ID,
076: compId);
077: }
078:
079: /**
080: * Return an existing <code>ActionLinkBean</code> or create a new one
081: *
082: * @param beanId the bean identifier
083: * @return a ActionLinkBean
084: */
085: public ActionLinkBean getActionLinkBean(String beanId) {
086: String beanKey = getBeanKey(beanId);
087: if (tagBeans.containsKey(beanKey)) {
088: return (ActionLinkBean) tagBeans.get(beanKey);
089: }
090: ActionLinkBean al = new ActionLinkBean(beanId);
091: configureBean(al);
092: tagBeans.put(beanKey, al);
093: return al;
094: }
095:
096: /**
097: * Return an existing <code>ParamBean</code> or create a new one
098: *
099: * @param beanId the bean identifier
100: * @return a ParamBean
101: */
102: public ParamBean getParamBean(String beanId) {
103: String beanKey = getBeanKey(beanId);
104: if (tagBeans.containsKey(beanKey)) {
105: return (ParamBean) tagBeans.get(beanKey);
106: }
107: ParamBean ap = new ParamBean(beanId);
108: configureBean(ap);
109: tagBeans.put(beanKey, ap);
110: return ap;
111: }
112:
113: /**
114: * Return an existing <code>ActionSubmitBean</code> or create a new one
115: *
116: * @param beanId the bean identifier
117: * @return a ActionSubmitBean
118: */
119: public ActionSubmitBean getActionSubmitBean(String beanId) {
120: String beanKey = getBeanKey(beanId);
121: if (tagBeans.containsKey(beanKey)) {
122: return (ActionSubmitBean) tagBeans.get(beanKey);
123: }
124: ActionSubmitBean as = new ActionSubmitBean(beanId);
125: configureBean(as);
126: tagBeans.put(beanKey, as);
127: return as;
128: }
129:
130: /**
131: * Return an existing <code>CheckBoxBean</code> or create a new one
132: *
133: * @param beanId the bean identifier
134: * @return a CheckBoxBean
135: */
136: public CheckBoxBean getCheckBoxBean(String beanId) {
137: String beanKey = getBeanKey(beanId);
138: if (tagBeans.containsKey(beanKey)) {
139: return (CheckBoxBean) tagBeans.get(beanKey);
140: }
141: CheckBoxBean cb = new CheckBoxBean(beanId);
142: configureBean(cb);
143: tagBeans.put(beanKey, cb);
144: return cb;
145: }
146:
147: /**
148: * Return an existing <code>CalendarBean</code> or create a new one
149: *
150: * @param beanId the bean identifier
151: * @return a CalendarBean
152: */
153: public CalendarBean getCalendarBean(String beanId) {
154: String beanKey = getBeanKey(beanId);
155: if (tagBeans.containsKey(beanKey)) {
156: return (CalendarBean) tagBeans.get(beanKey);
157: }
158: CalendarBean ca = new CalendarBean(beanId);
159: configureBean(ca);
160: tagBeans.put(beanKey, ca);
161: return ca;
162: }
163:
164: /**
165: * Return an existing <code>RadioButtonBean</code> or create a new one
166: *
167: * @param beanId the bean identifier
168: * @return a RadioButtonBean
169: */
170: public RadioButtonBean getRadioButtonBean(String beanId) {
171: String beanKey = getBeanKey(beanId);
172: if (tagBeans.containsKey(beanKey)) {
173: return (RadioButtonBean) tagBeans.get(beanKey);
174: }
175: RadioButtonBean rb = new RadioButtonBean(beanId);
176: configureBean(rb);
177: tagBeans.put(beanKey, rb);
178: return rb;
179: }
180:
181: /**
182: * Return an existing <code>PanelBean</code> or create a new one
183: *
184: * @param beanId the bean identifier
185: * @return a PanelBean
186: */
187: public PanelBean getPanelBean(String beanId) {
188: String beanKey = getBeanKey(beanId);
189: if (tagBeans.containsKey(beanKey)) {
190: return (PanelBean) tagBeans.get(beanKey);
191: }
192: PanelBean pb = new PanelBean(beanId);
193: configureBean(pb);
194: tagBeans.put(beanKey, pb);
195: return pb;
196: }
197:
198: /**
199: * Return an existing <code>TextFieldBean</code> or create a new one
200: *
201: * @param beanId the bean identifier
202: * @return a TextFieldBean
203: */
204: public TextFieldBean getTextFieldBean(String beanId) {
205: String beanKey = getBeanKey(beanId);
206: //log.debug("Checking for textfieldbean with bean key=" + beanKey);
207: if (tagBeans.containsKey(beanKey)) {
208: return (TextFieldBean) tagBeans.get(beanKey);
209: }
210: TextFieldBean tf = new TextFieldBean(beanId);
211: configureBean(tf);
212: tagBeans.put(beanKey, tf);
213: return tf;
214: }
215:
216: /**
217: * Return an existing <code>TextEditorBean</code> or create a new one
218: *
219: * @param beanId the bean identifier
220: * @return a TextEditorBean
221: */
222: public TextEditorBean getTextEditorBean(String beanId) {
223: String beanKey = getBeanKey(beanId);
224: //log.debug("Checking for texteditorbean with bean key=" + beanKey);
225: if (tagBeans.containsKey(beanKey)) {
226: return (TextEditorBean) tagBeans.get(beanKey);
227: }
228: TextEditorBean te = new TextEditorBean(beanId);
229: configureBean(te);
230: tagBeans.put(beanKey, te);
231: return te;
232: }
233:
234: /**
235: * Return an existing <code>RichTextEditorBean</code> or create a new one
236: *
237: * @param beanId the bean identifier
238: * @return a RichTextEditorBean
239: */
240: public RichTextEditorBean getRichTextEditorBean(String beanId) {
241: String beanKey = getBeanKey(beanId);
242: if (tagBeans.containsKey(beanKey)) {
243: return (RichTextEditorBean) tagBeans.get(beanKey);
244: }
245: RichTextEditorBean rt = new RichTextEditorBean(beanId);
246: configureBean(rt);
247: tagBeans.put(beanKey, rt);
248: return rt;
249: }
250:
251: /**
252: * Return an existing <code>TreeBean</code> or create a new one.
253: *
254: * @param beanId bendId of the bean Idetifier
255: * @return a TreeBean
256: */
257: public TreeBean getTreeBean(String beanId) {
258: String beanKey = getBeanKey(beanId);
259: if (tagBeans.containsKey(beanKey)) {
260: return (TreeBean) tagBeans.get(beanKey);
261: }
262: TreeBean tr = new TreeBean(beanId);
263: configureBean(tr);
264: tagBeans.put(beanKey, tr);
265: return tr;
266:
267: }
268:
269: /**
270: * Return an existing <code>HiddenFieldBean</code> or create a new one
271: *
272: * @param beanId the bean identifier
273: * @return a HiddenFieldBean
274: */
275: public HiddenFieldBean getHiddenFieldBean(String beanId) {
276: String beanKey = getBeanKey(beanId);
277: if (tagBeans.containsKey(beanKey)) {
278: return (HiddenFieldBean) tagBeans.get(beanKey);
279: }
280: HiddenFieldBean hf = new HiddenFieldBean(beanId);
281: configureBean(hf);
282: tagBeans.put(beanKey, hf);
283: return hf;
284: }
285:
286: /**
287: * Return an existing <code>FileInputBean</code> or create a new one
288: *
289: * @param beanId the bean identifier
290: * @return a FileInputBean
291: * @throws IOException if an error occurs
292: */
293: public FileInputBean getFileInputBean(String beanId)
294: throws IOException {
295: String beanKey = getBeanKey(beanId);
296: if (tagBeans.containsKey(beanKey)) {
297: return (FileInputBean) tagBeans.get(beanKey);
298: }
299: FileInputBean fi = new FileInputBean(beanId);
300: configureBean(fi);
301: tagBeans.put(beanKey, fi);
302: return fi;
303: }
304:
305: /**
306: * Return an existing <code>PasswordBean</code> or create a new one
307: *
308: * @param beanId the bean identifier
309: * @return a PasswordBean
310: */
311: public PasswordBean getPasswordBean(String beanId) {
312: String beanKey = getBeanKey(beanId);
313: if (tagBeans.containsKey(beanKey)) {
314: return (PasswordBean) tagBeans.get(beanKey);
315: }
316: PasswordBean pb = new PasswordBean(beanId);
317: configureBean(pb);
318: tagBeans.put(beanKey, pb);
319: return pb;
320: }
321:
322: /**
323: * Return an existing <code>TextAreaBean</code> or create a new one
324: *
325: * @param beanId the bean identifier
326: * @return a TextAreaBean
327: */
328: public TextAreaBean getTextAreaBean(String beanId) {
329: String beanKey = getBeanKey(beanId);
330: if (tagBeans.containsKey(beanKey)) {
331: return (TextAreaBean) tagBeans.get(beanKey);
332: }
333: TextAreaBean ta = new TextAreaBean(beanId);
334: configureBean(ta);
335: tagBeans.put(beanKey, ta);
336: return ta;
337: }
338:
339: /**
340: * Return an existing <code>FrameBean</code> or create a new one
341: *
342: * @param beanId the bean identifier
343: * @return a FrameBean
344: */
345: public FrameBean getFrameBean(String beanId) {
346: String beanKey = getBeanKey(beanId);
347: if (tagBeans.containsKey(beanKey)) {
348: return (FrameBean) tagBeans.get(beanKey);
349: }
350: FrameBean fb = new FrameBean(beanId);
351: configureBean(fb);
352: //System.err.println("Creating new frame bean" + beanId + " bean key= " + beanKey);
353: tagBeans.put(beanKey, fb);
354: return fb;
355: }
356:
357: /**
358: * Return an existing <code>TextBean</code> or create a new one
359: *
360: * @param beanId the bean identifier
361: * @return a TextBean
362: */
363: public TextBean getTextBean(String beanId) {
364: String beanKey = getBeanKey(beanId);
365: //log.debug("Checking for textbean with bean key=" + beanKey);
366: if (tagBeans.containsKey(beanKey)) {
367: return (TextBean) tagBeans.get(beanKey);
368: }
369: TextBean tb = new TextBean(beanId);
370: configureBean(tb);
371: tagBeans.put(beanKey, tb);
372: return tb;
373: }
374:
375: /**
376: * Return an existing <code>ImageBean</code> or create a new one
377: *
378: * @param beanId the bean identifier
379: * @return a ImageBean
380: */
381: public ImageBean getImageBean(String beanId) {
382: String beanKey = getBeanKey(beanId);
383: if (tagBeans.containsKey(beanKey)) {
384: return (ImageBean) tagBeans.get(beanKey);
385: }
386: ImageBean ib = new ImageBean(beanId);
387: configureBean(ib);
388: tagBeans.put(beanKey, ib);
389: return ib;
390:
391: }
392:
393: /**
394: * Return an existing <code>IncludeBean</code> or create a new one
395: *
396: * @param beanId the bean identifier
397: * @return a IncludeBean
398: */
399: public IncludeBean getIncludeBean(String beanId) {
400: String beanKey = getBeanKey(beanId);
401: if (tagBeans.containsKey(beanKey)) {
402: return (IncludeBean) tagBeans.get(beanKey);
403: }
404: IncludeBean includeBean = new IncludeBean(beanId);
405: configureBean(includeBean);
406: tagBeans.put(beanKey, includeBean);
407: return includeBean;
408: }
409:
410: /**
411: * Return an existing <code>ActionComponentBean</code> or create a new one
412: *
413: * @param beanId the bean identifier
414: * @return a IncludeBean
415: */
416: public ActionComponentBean getActionComponentBean(String beanId) {
417: String beanKey = getBeanKey(beanId);
418: if (tagBeans.containsKey(beanKey)) {
419: return (ActionComponentBean) tagBeans.get(beanKey);
420: }
421: ActionComponentBean bean = new ActionComponentBean(beanId);
422: configureBean(bean);
423: tagBeans.put(beanKey, bean);
424: return bean;
425: }
426:
427: /**
428: * Return an existing <code>TableBean</code> or create a new one
429: *
430: * @param beanId the bean identifier
431: * @return a TableBean
432: */
433: public TableBean getTableBean(String beanId) {
434: String beanKey = getBeanKey(beanId);
435: if (tagBeans.containsKey(beanKey)) {
436: return (TableBean) tagBeans.get(beanKey);
437: }
438: TableBean tb = new TableBean(beanId);
439: configureBean(tb);
440: tagBeans.put(beanKey, tb);
441: return tb;
442: }
443:
444: /**
445: * Return an existing <code>TableRowBean</code> or create a new one
446: *
447: * @param beanId the bean identifier
448: * @return a TableRowBean
449: */
450: public TableRowBean getTableRowBean(String beanId) {
451: String beanKey = getBeanKey(beanId);
452: if (tagBeans.containsKey(beanKey)) {
453: return (TableRowBean) tagBeans.get(beanKey);
454: }
455: TableRowBean tr = new TableRowBean(beanId);
456: configureBean(tr);
457: tagBeans.put(beanKey, tr);
458: return tr;
459: }
460:
461: /**
462: * Return an existing <code>TableCellBean</code> or create a new one
463: *
464: * @param beanId the bean identifier
465: * @return a TableCellBean
466: */
467: public TableCellBean getTableCellBean(String beanId) {
468: String beanKey = getBeanKey(beanId);
469: if (tagBeans.containsKey(beanKey)) {
470: return (TableCellBean) tagBeans.get(beanKey);
471: }
472: TableCellBean tc = new TableCellBean(beanId);
473: configureBean(tc);
474: tagBeans.put(beanKey, tc);
475: return tc;
476: }
477:
478: /**
479: * Return an existing <code>ListBoxBean</code> or create a new one
480: *
481: * @param beanId the bean identifier
482: * @return a ListBoxBean
483: */
484: public ListBoxBean getListBoxBean(String beanId) {
485: String beanKey = getBeanKey(beanId);
486: if (tagBeans.containsKey(beanKey)) {
487: return (ListBoxBean) tagBeans.get(beanKey);
488: }
489: ListBoxBean lb = new ListBoxBean(beanId);
490: configureBean(lb);
491: tagBeans.put(beanKey, lb);
492: return lb;
493: }
494:
495: /**
496: * Return an existing <code>ListBoxItemBean</code> or create a new one
497: *
498: * @param beanId the bean identifier
499: * @return a ListBoxItemBean
500: */
501: public ListBoxItemBean getListBoxItemBean(String beanId) {
502: String beanKey = getBeanKey(beanId);
503: if (tagBeans.containsKey(beanKey)) {
504: return (ListBoxItemBean) tagBeans.get(beanKey);
505: }
506: ListBoxItemBean lb = new ListBoxItemBean(beanId);
507: configureBean(lb);
508: tagBeans.put(beanKey, lb);
509: return lb;
510: }
511:
512: /**
513: * Return an existing <code>MessageBoxBean</code> or create a new one
514: *
515: * @param beanId the bean identifier
516: * @return a IncludeBean
517: */
518: public MessageBoxBean getMessageBoxBean(String beanId) {
519: String beanKey = getBeanKey(beanId);
520: if (tagBeans.containsKey(beanKey)) {
521: return (MessageBoxBean) tagBeans.get(beanKey);
522: }
523: MessageBoxBean messageBoxBean = new MessageBoxBean(beanId);
524: configureBean(messageBoxBean);
525: tagBeans.put(beanKey, messageBoxBean);
526: return messageBoxBean;
527: }
528:
529: /**
530: * Prints the request parameters to stdout. Generally used for debugging
531: */
532: public void logRequestParameters() {
533: StringBuffer sb = new StringBuffer();
534: sb.append("\n\n show request params\n--------------------\n");
535:
536: Enumeration e = null;
537: if (portletRequest != null) {
538: e = portletRequest.getParameterNames();
539: }
540: if (e != null) {
541: while (e.hasMoreElements()) {
542: String name = (String) e.nextElement();
543: sb.append("\t\tname :").append(name);
544: String values[] = null;
545: if (portletRequest != null) {
546: values = portletRequest.getParameterValues(name);
547: }
548: if (values != null) {
549: if (values.length == 1) {
550: String pval = values[0];
551: if (!name.startsWith("ui_pb")) {
552: sb.append("\t\t value : ").append(pval);
553: }
554: } else {
555: sb.append("\t\t value :");
556: for (int i = 0; i < values.length; i++) {
557: sb.append("\t\t - ").append(values[i]);
558: }
559: }
560: }
561: }
562: }
563: sb.append("--------------------\n");
564: log.debug(sb.toString());
565: }
566:
567: /**
568: * Prints the request attributes to stdout. Generally used for debugging
569: */
570: public void logRequestAttributes() {
571: StringBuffer sb = new StringBuffer();
572: sb
573: .append("\n\n show request attributes\n--------------------\n");
574: Enumeration e = null;
575: if (portletRequest != null) {
576: e = portletRequest.getAttributeNames();
577: }
578: if (e != null) {
579: while (e.hasMoreElements()) {
580: String name = (String) e.nextElement();
581: sb.append("name :").append(name);
582: }
583: }
584: sb.append("--------------------\n");
585: log.debug(sb.toString());
586: }
587:
588: /**
589: * Parses all request parameters for visual beans.
590: * A visual bean parameter has the following encoding:
591: * ui_<visual bean element>_<bean Id>_name
592: * where <visual bean element> is a two letter encoding of the kind of
593: * visual bean that it is.
594: */
595: protected void createTagBeans() {
596: if (tagBeans == null)
597: tagBeans = new HashMap<String, TagBean>();
598: Map<String, String[]> paramsMap;
599: // check for file upload
600: paramsMap = parseFileUpload();
601: Enumeration e = portletRequest.getParameterNames();
602: if (e != null) {
603: while (e.hasMoreElements()) {
604: String uiname = (String) e.nextElement();
605: String[] vals = null;
606: vals = portletRequest.getParameterValues(uiname);
607: paramsMap.put(uiname, vals);
608: }
609: }
610: for (String s : paramsMap.keySet()) {
611:
612: String uiname = (String) s;
613: String vb = "";
614: String name;
615: String beanId = "";
616:
617: if (!uiname.startsWith("ui"))
618: continue;
619: //log.debug("found a tag bean: " + uiname);
620:
621: String vbname = uiname.substring(3);
622:
623: int idx = vbname.indexOf("_");
624:
625: if (idx > 0) {
626: vb = vbname.substring(0, idx);
627: }
628:
629: vbname = vbname.substring(idx + 1);
630: idx = vbname.lastIndexOf("_");
631:
632: String beanKey;
633:
634: if (idx > 0) {
635: beanId = vbname.substring(0, idx);
636: //log.debug("Parsing beanId...");
637: int index = beanId.lastIndexOf("%");
638: if (index > -1 && index != beanId.length()) {
639: beanKey = beanId;
640: beanId = beanId.substring(index + 1);
641: } else {
642: beanKey = getBeanKey(beanId);
643: }
644: //log.debug("beanId = " + beanId);
645: //log.debug("beankey = " + beanKey);
646: } else {
647: beanKey = getBeanKey(beanId);
648:
649: }
650:
651: name = vbname.substring(idx + 1);
652: //log.debug("vbname: " + name);
653:
654: String[] vals = (String[]) paramsMap.get(uiname);
655:
656: //log.debug("Adding bean " + beanId + " with bean key " + beanKey);
657:
658: if (vb.equals(TagBean.TEXTFIELD_NAME)) {
659: //log.debug("Creating a textfieldbean bean with id:" + beanId);
660: TextFieldBean bean = new TextFieldBean(beanId);
661: bean.setValue(vals[0]);
662: //log.debug("setting new value" + vals[0]);
663: bean.setName(name);
664: //System.err.println("putting a bean: " + beanId + "into tagBeans with name: " + name);
665: configureBean(bean);
666: tagBeans.put(beanKey, bean);
667: } else if (vb.equals(TagBean.FILEINPUT_NAME)) {
668: //logRequestAttributes();
669: //log.debug("Creating a fileinput bean with id:" + beanId);
670:
671: FileInputBean bean;
672: FileItem fileItem = null;
673: // check whether the fileItems list contains a bean with this name
674: if (fileItems != null) {
675: for (int i = 0; i < fileItems.size(); i++) {
676: FileItem item = (FileItem) fileItems.get(i);
677: // if the item is an inputfile item, and the name matches
678: if (!item.isFormField()
679: && item.getFieldName().equals(uiname)) {
680: // then create a FileInputBean with this fileItem
681: fileItem = item;
682: break;
683: }
684: }
685: }
686: if (fileItem != null) {
687: bean = new FileInputBean(beanId, fileItem);
688: } else {
689: bean = new FileInputBean(beanId);
690: }
691: bean.setName(name);
692: configureBean(bean);
693: tagBeans.put(beanKey, bean);
694:
695: //System.err.println("putting a bean: " + beanId + "into tagBeans with name: " + name);
696: } else if (vb.equals(TagBean.CALENDAR_NAME)) {
697: //log.debug("Creating a calendarbean bean with id:" + beanId);
698: CalendarBean bean = new CalendarBean(beanId);
699: bean.setValue(vals[0]);
700: bean.setName(name);
701: configureBean(bean);
702: //System.err.println("putting a bean: " + beanId + "into tagBeans with name: " + name);
703: tagBeans.put(beanKey, bean);
704: } else if (vb.equals(TagBean.CHECKBOX_NAME)) {
705: CheckBoxBean bean = (CheckBoxBean) tagBeans
706: .get(beanKey);
707: if (bean == null) {
708: //log.debug("Creating a checkbox bean with id:" + beanId);
709: bean = new CheckBoxBean(beanId);
710: bean.setValue(vals[0]);
711: for (int i = 0; i < vals.length; i++) {
712: String val = vals[i];
713: bean.addSelectedValue(val);
714: }
715: bean.setName(name);
716: } else {
717: bean.addSelectedValue(vals[0]);
718: }
719: bean.setSelected(true);
720: configureBean(bean);
721: //System.err.println("putting a bean: " + beanId + "into tagBeans with name: " + name);
722: tagBeans.put(beanKey, bean);
723: } else if (vb.equals(ListBoxBean.NAME)) {
724: //log.debug("Creating a listbox bean with id:" + beanId);
725: ListBoxBean bean = new ListBoxBean(beanId);
726: bean.setName(name);
727: for (int i = 0; i < vals.length; i++) {
728: ListBoxItemBean item = new ListBoxItemBean();
729: item.setName(vals[i]);
730: item.setValue(vals[i]);
731: item.setSelected(true);
732: //log.debug("adding an item bean: " + vals[i]);
733: bean.addBean(item);
734: }
735: //System.err.println("putting a bean: " + beanId + "into tagBeans with name: " + name);
736: configureBean(bean);
737: tagBeans.put(beanKey, bean);
738: } else if (vb.equals(TagBean.RADIOBUTTON_NAME)) {
739: RadioButtonBean bean = (RadioButtonBean) tagBeans
740: .get(beanKey);
741: if (bean == null) {
742: //log.debug("Creating a new radiobutton bean with id:" + beanId);
743: bean = new RadioButtonBean(beanId);
744: bean.setValue(vals[0]);
745: bean.addSelectedValue(vals[0]);
746: bean.setName(name);
747: } else {
748: //log.debug("Using existing radiobutton bean with id:" + beanId);
749: bean.addSelectedValue(vals[0]);
750: }
751: bean.setSelected(true);
752: configureBean(bean);
753: //System.err.println("putting a bean: " + beanId + "into tagBeans with name: " + name);
754: tagBeans.put(beanKey, bean);
755: } else if (vb.equals(TagBean.PASSWORD_NAME)) {
756: //log.debug("Creating a passwordbean bean with id:" + beanId);
757: PasswordBean bean = new PasswordBean(beanId);
758: bean.setValue(vals[0]);
759: bean.setName(name);
760: configureBean(bean);
761: //System.err.println("putting a bean: " + beanId + "into tagBeans with name: " + name);
762: tagBeans.put(beanKey, bean);
763: } else if (vb.equals(TagBean.TEXTAREA_NAME)) {
764: //log.debug("Creating a textareabean bean with id:" + beanId);
765: TextAreaBean bean = new TextAreaBean(beanId);
766: bean.setValue(vals[0]);
767: bean.setName(name);
768: configureBean(bean);
769: //System.err.println("putting a bean: " + beanId + "into tagBeans with name: " + name);
770: tagBeans.put(beanKey, bean);
771: } else if (vb.equals(TagBean.TEXTEDITOR_NAME)) {
772: //log.debug("Creating a textareabean bean with id:" + beanId);
773: TextEditorBean bean = new TextEditorBean(beanId);
774: bean.setValue(vals[0]);
775: bean.setName(name);
776: configureBean(bean);
777: //System.err.println("putting a bean: " + beanId + "into tagBeans with name: " + name);
778: tagBeans.put(beanKey, bean);
779:
780: } else if (vb.equals(TagBean.HIDDENFIELD_NAME)) {
781: //log.debug("Creating a hidden bean bean with id:" + beanId);
782: HiddenFieldBean bean = new HiddenFieldBean(beanId);
783: bean.setValue(vals[0]);
784: bean.setName(name);
785: configureBean(bean);
786: //System.err.println("putting a bean: " + beanId + "into tagBeans with name: " + name);
787: tagBeans.put(beanKey, bean);
788: } else if (vb.equals(TagBean.RICHTEXTEDITOR_NAME)) {
789: RichTextEditorBean bean = new RichTextEditorBean(beanId);
790: bean.setValue(vals[0]);
791: bean.setName(name);
792: configureBean(bean);
793: tagBeans.put(beanKey, bean);
794: } else {
795: log.error("unable to find suitable bean type for : "
796: + uiname);
797: }
798:
799: /*
800: String values[] = request.getParameterValues(name);
801: if (values.length == 1) {
802: String pval = values[0];
803: if (pval.length() == 0) {
804: pval = "no value";
805: }
806: System.out.println(" value : " + pval);
807: } else {
808: System.out.println(" value :");
809: for (int i = 0; i < values.length; i++) {
810: System.out.println(" - " + values[i]);
811: }
812: }
813: */
814:
815: }
816: }
817:
818: protected Map<String, String[]> parseFileUpload() {
819: //log.debug("parseFileUpload");
820: Map<String, String[]> parameters = new Hashtable<String, String[]>();
821: if (portletRequest instanceof HttpServletRequest) {
822: HttpServletRequest hreq = (HttpServletRequest) portletRequest;
823: //logRequestParameters();
824: //logRequestAttributes();
825: ServletRequestContext ctx = new ServletRequestContext(hreq);
826: if (FileUpload.isMultipartContent(ctx)) {
827: FileItemFactory factory = new DiskFileItemFactory();
828: // Create a new file upload handler
829: ServletFileUpload upload = new ServletFileUpload(
830: factory);
831: try {
832: fileItems = upload.parseRequest(hreq);
833: } catch (Exception e) {
834: log.error("Unable to parse multi part form!!!", e);
835: }
836: if (fileItems != null) {
837: //log.debug("File items has size " + fileItems.size());
838: for (int i = 0; i < fileItems.size(); i++) {
839: FileItem item = (FileItem) fileItems.get(i);
840: String[] tmpstr = new String[1];
841: if (item.isFormField()) {
842: tmpstr[0] = item.getString();
843: } else {
844: tmpstr[0] = "fileinput";
845: }
846: //log.debug("File item " + item.getFieldName() + "->" + tmpstr[0]);
847: parameters.put(item.getFieldName(), tmpstr);
848: }
849: }
850: }
851: }
852: return parameters;
853: }
854:
855: /**
856: * Returns a bean key identifier using the component identifier
857: *
858: * @param beanId the bean identifier
859: * @return the bean key identifier
860: */
861: protected String getBeanKey(String beanId) {
862: String beanKey;
863: if (compId == null) {
864: beanKey = beanId + '_' + cid;
865: } else {
866: beanKey = compId + '%' + beanId + '_' + cid;
867: }
868: //log.debug("BaseFormEventImpl.getBeanKey(" + beanId + ") = " + beanKey);
869: return beanKey;
870: }
871:
872: /**
873: * Returns the collection of visual tag beans contained by this form event
874: *
875: * @return the collection of visual tag beans
876: */
877: public Map getTagBeans() {
878: return tagBeans;
879: }
880:
881: /**
882: * Stores any created beans into the request
883: */
884: public void store() {
885: Iterator it = tagBeans.keySet().iterator();
886: TagBean tagBean;
887: while (it.hasNext()) {
888: String beanKey = (String) it.next();
889: tagBean = (TagBean) tagBeans.get(beanKey);
890: //log.debug("storing bean in attribute: " + beanKey);
891: if (portletRequest != null)
892: portletRequest.setAttribute(beanKey, tagBean);
893: }
894: //logRequestAttributes();
895: }
896:
897: /**
898: * Logs all tag bean identifiers, primarily used for debugging
899: */
900: public void logTagBeans() {
901: //log.debug("in print tag beans:");
902: for (TagBean tagBean : tagBeans.values()) {
903: log.debug("tag bean id: " + tagBean.getBeanId());
904: }
905: }
906:
907: public ActionMenuItemBean getActionMenuItemBean(String beanId) {
908: String beanKey = getBeanKey(beanId);
909: if (tagBeans.containsKey(beanKey)) {
910: return (ActionMenuItemBean) tagBeans.get(beanKey);
911: }
912: ActionMenuItemBean ami = new ActionMenuItemBean(beanId);
913: configureBean(ami);
914: tagBeans.put(beanKey, ami);
915: return ami;
916: }
917:
918: public ActionMenuBean getActionMenuBean(String beanId) {
919: String beanKey = getBeanKey(beanId);
920: if (tagBeans.containsKey(beanKey)) {
921: return (ActionMenuBean) tagBeans.get(beanKey);
922: }
923: ActionMenuBean am = new ActionMenuBean(beanId);
924: configureBean(am);
925: tagBeans.put(beanKey, am);
926: return am;
927: }
928:
929: public QueryFilter getQueryFilter(int maxResults, int totalItems) {
930: if (portletRequest.getParameter(TableBean.SHOW_ALL) != null)
931: return null;
932: int firstResult = 0;
933: QueryFilter queryFilter = new QueryFilter();
934: String curPage = portletRequest
935: .getParameter(TableBean.CURRENT_PAGE);
936: if (curPage != null) {
937: firstResult = Integer.valueOf(curPage).intValue()
938: * maxResults;
939: }
940: queryFilter.setFirstResult(firstResult);
941: queryFilter.setMaxResults(maxResults);
942: queryFilter.setTotalItems(totalItems);
943: return queryFilter;
944: }
945: }
|