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