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.generators;
017
018import java.io.IOException;
019import java.util.List;
020
021import org.apache.cocoon.ProcessingException;
022import org.apache.cocoon.environment.ObjectModelHelper;
023import org.apache.cocoon.environment.Request;
024import org.apache.cocoon.xml.AttributesImpl;
025import org.apache.cocoon.xml.XMLUtils;
026import org.xml.sax.SAXException;
027
028import org.ametys.plugins.survey.data.SurveySession;
029import org.ametys.plugins.survey.repository.Survey;
030
031/**
032 * Generate all the user sessions with their answers for a given survey.
033 */
034public class SurveySessionsGenerator extends SurveySessionGenerator
035{
036
037    @Override
038    public void generate() throws IOException, SAXException, ProcessingException
039    {
040        Request request = ObjectModelHelper.getRequest(objectModel);
041        
042        String surveyId = parameters.getParameter("id", request.getParameter("id"));
043        
044        Survey survey = _resolver.resolveById(surveyId);
045        
046        List<SurveySession> sessions = _surveyDao.getSessionsWithAnswers(surveyId);
047        
048        contentHandler.startDocument();
049        
050        AttributesImpl attrs = new AttributesImpl();
051        attrs.addCDATAAttribute("surveyId", surveyId);
052        attrs.addCDATAAttribute("surveyTitle", survey.getTitle());
053        
054        XMLUtils.startElement(contentHandler, "survey-sessions", attrs);
055        
056        for (SurveySession surveySession : sessions)
057        {
058            saxSession(surveySession, survey, true);
059        }
060        
061        XMLUtils.endElement(contentHandler, "survey-sessions");
062        
063        contentHandler.endDocument();
064    }
065
066}