001/* 002 * Copyright 2013 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.repository; 017 018import java.util.Date; 019import java.util.Map; 020 021import org.apache.avalon.framework.component.Component; 022import org.apache.avalon.framework.logger.AbstractLogEnabled; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026import org.apache.avalon.framework.thread.ThreadSafe; 027import org.apache.cocoon.environment.Cookie; 028import org.apache.cocoon.environment.Request; 029import org.apache.commons.lang.StringUtils; 030 031import org.ametys.core.user.UserIdentity; 032import org.ametys.plugins.survey.answer.ProcessInputAction; 033import org.ametys.plugins.survey.data.SurveyAnswerDao; 034import org.ametys.plugins.survey.data.SurveySession; 035 036/** 037 * helper to check survey access 038 * 039 */ 040public class SurveyAccessHelper extends AbstractLogEnabled implements Component, ThreadSafe, Serviceable 041{ 042 /** Avalon role */ 043 public static final String ROLE = SurveyAccessHelper.class.getName(); 044 045 /** The ametys object resolver. */ 046 protected SurveyAnswerDao _answerDao; 047 048 @Override 049 public void service(ServiceManager manager) throws ServiceException 050 { 051 _answerDao = (SurveyAnswerDao) manager.lookup(SurveyAnswerDao.ROLE); 052 } 053 054 /** 055 * Returns the date on which the user answered to the survey or <code>null</code> if he was never answered 056 * @param surveyId the survey ID. 057 * @param user the user. 058 * @return the date on which the survey was taken, or <code>null</code> if the user never took the survey. 059 */ 060 public Date getSubmissionDate(String surveyId, UserIdentity user) 061 { 062 Date alreadyTakenOn = null; 063 064 if (user != null && StringUtils.isNotBlank(user.getLogin()) && StringUtils.isNotBlank(user.getPopulationId())) 065 { 066 SurveySession userSession = _answerDao.getSession(surveyId, user); 067 068 if (userSession != null) 069 { 070 alreadyTakenOn = userSession.getSubmittedAt(); 071 } 072 } 073 074 return alreadyTakenOn; 075 } 076 077 /** 078 * Return the name of cookie if the survey was already taken or <code>null</code> otherwise. 079 * @param request the request. 080 * @param surveyId the survey ID. 081 * @return the name of cookie if the survey was already taken, or <code>null</code> otherwise. 082 */ 083 public String getCookieName (Request request, String surveyId) 084 { 085 Map<String, Cookie> cookieMap = request.getCookieMap(); 086 087 if (cookieMap.containsKey(ProcessInputAction.COOKIE_NAME)) 088 { 089 Cookie cookie = cookieMap.get(ProcessInputAction.COOKIE_NAME); 090 String takenSurveys = cookie.getValue(); 091 if (StringUtils.isNotEmpty(takenSurveys)) 092 { 093 String[] takenSurveyIds = StringUtils.split(takenSurveys, ','); 094 for (int i = 0; i < takenSurveyIds.length; i++) 095 { 096 if (surveyId.equals(takenSurveyIds[i])) 097 { 098 return ProcessInputAction.COOKIE_NAME; 099 } 100 } 101 } 102 } 103 104 return null; 105 } 106 107}