001/* 002 * Copyright 2022 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.forms.helper; 017 018import java.time.LocalDate; 019import java.util.HashMap; 020import java.util.Map; 021 022import org.apache.avalon.framework.component.Component; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026 027import org.ametys.core.ui.Callable; 028import org.ametys.core.user.CurrentUserProvider; 029import org.ametys.core.user.UserManager; 030import org.ametys.core.util.DateUtils; 031import org.ametys.plugins.forms.dao.FormDAO; 032import org.ametys.plugins.forms.repository.Form; 033import org.ametys.plugins.repository.AmetysObjectResolver; 034import org.ametys.runtime.plugin.component.AbstractLogEnabled; 035 036/** 037 * The helper to schedule opening of form 038 */ 039public class ScheduleOpeningHelper extends AbstractLogEnabled implements Serviceable, Component 040{ 041 /** Avalon ROLE. */ 042 public static final String ROLE = ScheduleOpeningHelper.class.getName(); 043 044 /** Ametys object resolver. */ 045 protected AmetysObjectResolver _resolver; 046 047 /** The users manager */ 048 protected UserManager _userManager; 049 050 /** The form mail helper */ 051 protected FormMailHelper _formMailHelper; 052 053 /** The form DAO */ 054 protected FormDAO _formDAO; 055 056 /** The current user provider */ 057 protected CurrentUserProvider _currentUserProvider; 058 059 /** 060 * The form status 061 */ 062 public enum FormStatus 063 { 064 /** The form is not opened yet */ 065 COMING, 066 /** The form is opened */ 067 OPEN, 068 /** The form is closed */ 069 OVER; 070 } 071 072 public void service(ServiceManager manager) throws ServiceException 073 { 074 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 075 _userManager = (UserManager) manager.lookup(UserManager.ROLE); 076 _formMailHelper = (FormMailHelper) manager.lookup(FormMailHelper.ROLE); 077 _formDAO = (FormDAO) manager.lookup(FormDAO.ROLE); 078 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 079 } 080 081 /** 082 * Get the scheduled dates of a form 083 * @param formId the id of the form 084 * @return a map with start date and end date 085 */ 086 @Callable 087 public Map<String, Object> getScheduledDates(String formId) 088 { 089 Map<String, Object> dates = new HashMap<> (); 090 091 Form form = _resolver.resolveById(formId); 092 LocalDate startDate = form.getStartDate(); 093 if (startDate != null) 094 { 095 dates.put(Form.START_DATE, DateUtils.localDateToString(startDate)); 096 } 097 098 LocalDate endDate = form.getEndDate(); 099 if (endDate != null) 100 { 101 dates.put(Form.END_DATE, DateUtils.localDateToString(endDate)); 102 } 103 104 return dates; 105 } 106 107 /** 108 * Set date of publication of form 109 * @param formId The form id 110 * @param startDateAsStr The start date. Can be null. 111 * @param endDateAsStr The end date. Can be null. 112 * @return true if operation has succeeded. 113 */ 114 @Callable 115 public boolean setScheduledDate (String formId, String startDateAsStr, String endDateAsStr) 116 { 117 LocalDate startDate = startDateAsStr != null ? LocalDate.parse(startDateAsStr) : null; 118 LocalDate endDate = endDateAsStr != null ? LocalDate.parse(endDateAsStr) : null; 119 120 Form form = _resolver.resolveById(formId); 121 122 form.setStartDate(startDate); 123 form.setEndDate(endDate); 124 form.saveChanges(); 125 126 return true; 127 } 128 129 /** 130 * Get the opening status of the form 131 * @param form the form 132 * @return 'open', 'over', or 'coming' depending on today's date 133 */ 134 public FormStatus getStatus(Form form) 135 { 136 LocalDate startDate = form.getStartDate(); 137 LocalDate endDate = form.getEndDate(); 138 139 LocalDate now = LocalDate.now(); 140 141 if (startDate != null && startDate.isAfter(now)) 142 { 143 return FormStatus.COMING; 144 } 145 146 if (endDate != null && endDate.isBefore(now)) 147 { 148 return FormStatus.OVER; 149 } 150 151 return FormStatus.OPEN; 152 } 153}