001/* 002 * Copyright 2011 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.survey.answer; 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.apache.commons.lang.StringUtils; 029import org.xml.sax.SAXException; 030 031import org.ametys.plugins.survey.repository.Survey; 032import org.ametys.plugins.survey.repository.SurveyQuestion; 033import org.ametys.runtime.i18n.I18nizableText; 034 035/** 036 * Action that processes the user submitted data on a survey. 037 */ 038public class SurveyErrorsGenerator extends AbstractGenerator 039{ 040 041 @Override 042 public void generate() throws IOException, SAXException, ProcessingException 043 { 044 Request request = ObjectModelHelper.getRequest(objectModel); 045 046 Survey survey = (Survey) request.getAttribute("survey"); 047 SurveyErrors errors = (SurveyErrors) request.getAttribute("survey-errors"); 048 049 Map<String, List<I18nizableText>> errorMap = errors.getErrors(); 050 051 contentHandler.startDocument(); 052 053 AttributesImpl atts = new AttributesImpl(); 054 atts.addCDATAAttribute("id", survey.getId()); 055 atts.addCDATAAttribute("title", survey.getTitle()); 056 atts.addCDATAAttribute("label", survey.getLabel()); 057 058 XMLUtils.startElement(contentHandler, "survey-errors", atts); 059 060 for (Map.Entry<String, List<I18nizableText>> fieldErrors : errorMap.entrySet()) 061 { 062 String questionName = fieldErrors.getKey(); 063 List<I18nizableText> messages = fieldErrors.getValue(); 064 065 if (StringUtils.isEmpty(questionName) || "survey-access".equals(questionName)) 066 { 067 068 XMLUtils.startElement(contentHandler, "survey-access"); 069 for (I18nizableText message : messages) 070 { 071 message.toSAX(contentHandler, "error"); 072 } 073 XMLUtils.endElement(contentHandler, "survey-access"); 074 } 075 else 076 { 077 SurveyQuestion question = survey.getQuestion(questionName); 078 079 atts.clear(); 080 atts.addCDATAAttribute("id", question.getId()); 081 atts.addCDATAAttribute("name", questionName); 082 atts.addCDATAAttribute("label", question.getLabel()); 083 atts.addCDATAAttribute("title", question.getTitle()); 084 atts.addCDATAAttribute("type", question.getType().toString()); 085 086 XMLUtils.startElement(contentHandler, "question", atts); 087 088 for (I18nizableText message : messages) 089 { 090 message.toSAX(contentHandler, "error"); 091 } 092 093 XMLUtils.endElement(contentHandler, "question"); 094 } 095 } 096 097 XMLUtils.endElement(contentHandler, "survey-errors"); 098 099 contentHandler.endDocument(); 100 } 101 102}