001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.forms.processing;
017
018import java.io.File;
019import java.io.IOException;
020import java.sql.Types;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.ProcessingException;
026import org.apache.cocoon.environment.ObjectModelHelper;
027import org.apache.cocoon.generation.ServiceableGenerator;
028import org.apache.cocoon.xml.AttributesImpl;
029import org.apache.cocoon.xml.XMLUtils;
030import org.apache.commons.lang.StringUtils;
031import org.xml.sax.SAXException;
032
033import org.ametys.core.util.URIUtils;
034import org.ametys.plugins.forms.Field;
035import org.ametys.plugins.forms.Form;
036import org.ametys.plugins.forms.data.FieldValue;
037import org.ametys.plugins.forms.jcr.FormPropertiesManager;
038
039/**
040 * Generates the results.
041 */
042public class FormResultsGenerator extends ServiceableGenerator
043{
044    private FormPropertiesManager _formPropManager;
045    
046    @Override
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        super.service(smanager);
050        _formPropManager = (FormPropertiesManager) smanager.lookup(FormPropertiesManager.ROLE);
051    }
052    
053    @Override
054    public void generate() throws IOException, SAXException, ProcessingException
055    {
056        @SuppressWarnings("unchecked")
057        Map<String, Object> params = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
058        
059        Form form = (Form) params.get("form");
060        @SuppressWarnings("unchecked")
061        Map<String, FieldValue> input = (Map<String, FieldValue>) params.get("input");
062        
063        contentHandler.startDocument();
064        
065        AttributesImpl atts = new AttributesImpl();
066        atts.addCDATAAttribute("id", form.getId());
067        atts.addCDATAAttribute("label", URIUtils.decode(form.getLabel()));
068        
069        XMLUtils.startElement(contentHandler, "form-results", atts);
070        
071        for (FieldValue entry : input.values())
072        {
073            Field field = entry.getField();
074            String value = null;
075            
076            switch (entry.getType())
077            {
078                case Types.VARCHAR:
079                case Types.LONGVARCHAR:
080                    value = _formPropManager.getDisplayValue(field, StringUtils.defaultString((String) entry.getValue()));
081                    break;
082                case Types.BOOLEAN:
083                    value = Boolean.toString((Boolean) entry.getValue());
084                    break;
085                case Types.BLOB:
086                    // File will be joined by the action.
087                    File file = (File) entry.getValue();
088                    value = file == null ? "" : file.getName();
089                    break;
090                default:
091                    break;
092            }
093            
094            if (value != null)
095            {
096                atts.clear();
097                atts.addCDATAAttribute("label", field.getLabel());
098                atts.addCDATAAttribute("name", field.getName());
099                atts.addCDATAAttribute("type", field.getType().toString());
100                
101                Map<String, String> properties = field.getProperties();
102                if (properties.containsKey("regexptype"))
103                {
104                    atts.addCDATAAttribute("regexptype", properties.get("regexptype"));
105                }
106                
107                XMLUtils.createElement(contentHandler, "entry", atts, value);
108            }
109        }
110        
111        XMLUtils.endElement(contentHandler, "form-results");
112        
113        contentHandler.endDocument();
114    }
115}