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.time.ZonedDateTime;
020import java.util.Calendar;
021import java.util.Date;
022import java.util.GregorianCalendar;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.ProcessingException;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.environment.Request;
029import org.apache.cocoon.xml.AttributesImpl;
030import org.apache.cocoon.xml.XMLUtils;
031import org.xml.sax.SAXException;
032
033import org.ametys.core.user.CurrentUserProvider;
034import org.ametys.core.util.DateUtils;
035import org.ametys.plugins.survey.SurveyDateUtils;
036import org.ametys.plugins.survey.data.SurveyAnswerDao;
037import org.ametys.plugins.survey.repository.Survey;
038import org.ametys.plugins.survey.repository.SurveyPage;
039
040/**
041 * SAX surveys
042 *
043 */
044public class SurveyGenerator extends SurveysGenerator
045{
046    /** The current user provider. */
047    protected CurrentUserProvider _currentUserProvider;
048    /** The current user provider. */
049    protected SurveyAnswerDao _surveyAnswerDao;
050
051    @Override
052    public void service(ServiceManager serviceManager) throws ServiceException
053    {
054        super.service(serviceManager);
055        _currentUserProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE);
056        _surveyAnswerDao = (SurveyAnswerDao) serviceManager.lookup(SurveyAnswerDao.ROLE);
057    }
058    
059    @Override
060    public void generate() throws IOException, SAXException, ProcessingException
061    {
062        Request request = ObjectModelHelper.getRequest(objectModel);
063        
064        String id = parameters.getParameter("id", request.getParameter("id"));
065        
066        contentHandler.startDocument();
067        
068        Survey survey = _resolver.resolveById(id);
069        
070        Calendar now = new GregorianCalendar();
071        now.set(Calendar.HOUR_OF_DAY, 0);
072        now.set(Calendar.MINUTE, 0);
073        now.set(Calendar.SECOND, 0);
074        now.set(Calendar.MILLISECOND, 0);
075        
076        Date startDate = survey.getStartDate();
077        Date endDate = survey.getEndDate();
078
079        AttributesImpl attrs = new AttributesImpl();
080        attrs.addCDATAAttribute("id", survey.getId());
081        attrs.addCDATAAttribute("name", survey.getName());
082        attrs.addCDATAAttribute("private", String.valueOf(_surveyDAO.isPrivate(survey)));
083        attrs.addCDATAAttribute("validated", String.valueOf(survey.isValidated()));
084        attrs.addCDATAAttribute("status", _getStatus(startDate, endDate));
085        
086        ZonedDateTime reinitDate = survey.getReinitDate();
087        if (reinitDate != null)
088        {
089            attrs.addCDATAAttribute("reinitDate", SurveyDateUtils.zonedDateTimeToString(reinitDate));
090        }
091        
092        XMLUtils.startElement(contentHandler, "survey", attrs);
093        XMLUtils.createElement(contentHandler, "title", survey.getTitle());
094        XMLUtils.createElement(contentHandler, "label", survey.getLabel());
095
096        if (startDate != null)
097        {
098            XMLUtils.createElement(contentHandler, "startDate", DateUtils.dateToString(startDate));
099        }
100        if (endDate != null)
101        {
102            XMLUtils.createElement(contentHandler, "endDate", DateUtils.dateToString(endDate));
103        }
104        
105        String description = survey.getDescription();
106        if (description != null)
107        {
108            XMLUtils.createElement(contentHandler, "description", description);
109        }
110        
111        String endingMessage = survey.getEndingMessage();
112        if (endingMessage != null)
113        {
114            XMLUtils.createElement(contentHandler, "endingMessage", endingMessage);
115        }
116        
117        for (SurveyPage page : survey.getPages())
118        {
119            toSAX(page, true, true);
120        }
121        
122        toSAXPicture(survey);
123        
124        XMLUtils.endElement(contentHandler, "survey");
125        
126        contentHandler.endDocument();
127    }
128    
129    private String _getStatus (Date startDate, Date endDate)
130    {
131        Calendar today = new GregorianCalendar();
132        today.set(Calendar.HOUR_OF_DAY, 0);
133        today.set(Calendar.MINUTE, 0);
134        today.set(Calendar.SECOND, 0);
135        today.set(Calendar.MILLISECOND, 0);
136        
137        if (startDate != null && startDate.after(today.getTime()))
138        {
139            return "coming";
140        }
141        
142        if (endDate != null && endDate.before(today.getTime()))
143        {
144            return "over";
145        }
146        
147        return "open";
148    }
149}