001    package com.khubla.pragmatach.plugin.jasper;
002    
003    import java.util.HashMap;
004    import java.util.Map;
005    
006    import com.khubla.pragmatach.framework.api.PragmatachException;
007    import com.khubla.pragmatach.framework.api.Response;
008    import com.khubla.pragmatach.framework.controller.BeanBoundController;
009    import com.khubla.pragmatach.framework.controller.ControllerBeanUtil;
010    import com.khubla.pragmatach.framework.controller.impl.template.AbstractTemplateEngineController;
011    import com.khubla.pragmatach.framework.form.Form;
012    import com.khubla.pragmatach.framework.form.FormItem;
013    
014    /**
015     * @author tome
016     */
017    public class JasperController extends AbstractTemplateEngineController implements BeanBoundController {
018       /**
019        * ctor
020        */
021       public JasperController() {
022       }
023    
024       private Map<String, String> getPostFieldValues() throws PragmatachException {
025          try {
026             final Form form = Form.parse(getRequest().getHttpServletRequest());
027             if (null != form) {
028                final Map<String, String> ret = new HashMap<String, String>();
029                for (final FormItem formItem : form.getItems().values()) {
030                   ret.put(formItem.getName(), formItem.getValue());
031                }
032                return ret;
033             } else {
034                return null;
035             }
036          } catch (final Exception e) {
037             throw new PragmatachException("Exception in getPostFieldValues", e);
038          }
039       }
040    
041       /**
042        * render
043        */
044       public Response render() throws PragmatachException {
045          try {
046             final String template = getTemplate();
047             return new JasperResponse(getCacheHeaders(), getTemplateName(), template, getTemplateContext());
048          } catch (final Exception e) {
049             throw new PragmatachException("Exception in render", e);
050          }
051       }
052    
053       @Override
054       public void populateController() throws PragmatachException {
055          ControllerBeanUtil.populateController(this, this.getPostFieldValues());
056       }
057    }