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.IOException;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.cocoon.ProcessingException;
023import org.apache.cocoon.environment.ObjectModelHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.cocoon.generation.AbstractGenerator;
026import org.apache.cocoon.xml.AttributesImpl;
027import org.apache.cocoon.xml.XMLUtils;
028import org.xml.sax.SAXException;
029
030import org.ametys.plugins.forms.Field;
031import org.ametys.plugins.forms.Form;
032import org.ametys.runtime.i18n.I18nizableText;
033
034/**
035 * Action that processes the user submitted data on a form.
036 */
037public class FormErrorsGenerator extends AbstractGenerator
038{
039
040    @Override
041    public void generate() throws IOException, SAXException, ProcessingException
042    {
043        Request request = ObjectModelHelper.getRequest(objectModel);
044        
045        FormErrors errors = (FormErrors) request.getAttribute("form-errors");
046        Form form = errors.getForm();
047        
048        Map<String, Field> fields = form.getFieldMap();
049        
050        Map<String, List<I18nizableText>> errorMap = errors.getErrors();
051        
052        contentHandler.startDocument();
053        
054        AttributesImpl atts = new AttributesImpl();
055        atts.addCDATAAttribute("id", form.getId());
056        atts.addCDATAAttribute("label", form.getLabel());
057//        atts.addCDATAAttribute("contentId", form.getContentId());
058        
059        XMLUtils.startElement(contentHandler, "form-errors", atts);
060        
061        for (Map.Entry<String, List<I18nizableText>> fieldErrors : errorMap.entrySet())
062        {
063            String fieldId = fieldErrors.getKey();
064            Field field = fields.get(fieldId);
065            List<I18nizableText> messages = fieldErrors.getValue();
066            
067            atts.clear();
068            atts.addCDATAAttribute("id", fieldId);
069            atts.addCDATAAttribute("name", field.getName());
070            atts.addCDATAAttribute("label", field.getLabel());
071            atts.addCDATAAttribute("type", field.getType().toString());
072            
073            XMLUtils.startElement(contentHandler, "field", atts);
074            
075            for (I18nizableText message : messages)
076            {
077                message.toSAX(contentHandler, "error");
078            }
079            
080            XMLUtils.endElement(contentHandler, "field");
081        }
082        
083        XMLUtils.endElement(contentHandler, "form-errors");
084        
085        contentHandler.endDocument();
086    }
087
088}