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