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.observation.Event; 028import org.ametys.core.observation.ObservationManager; 029import org.ametys.core.ui.Callable; 030import org.ametys.core.user.CurrentUserProvider; 031import org.ametys.core.util.DateUtils; 032import org.ametys.plugins.forms.FormEvents; 033import org.ametys.plugins.forms.dao.FormDAO; 034import org.ametys.plugins.forms.repository.Form; 035import org.ametys.plugins.repository.AmetysObjectResolver; 036import org.ametys.runtime.plugin.component.AbstractLogEnabled; 037 038/** 039 * The helper to schedule opening of form 040 */ 041public class ScheduleOpeningHelper extends AbstractLogEnabled implements Serviceable, Component 042{ 043 /** Avalon ROLE. */ 044 public static final String ROLE = ScheduleOpeningHelper.class.getName(); 045 046 /** Ametys object resolver. */ 047 protected AmetysObjectResolver _resolver; 048 049 /** The form DAO */ 050 protected FormDAO _formDAO; 051 052 /** The current user provider */ 053 protected CurrentUserProvider _currentUserProvider; 054 055 /** Observer manager. */ 056 protected ObservationManager _observationManager; 057 058 /** 059 * The form status 060 */ 061 public enum FormStatus 062 { 063 /** The form is not opened yet */ 064 COMING, 065 /** The form is opened */ 066 OPEN, 067 /** The form is closed */ 068 OVER; 069 } 070 071 public void service(ServiceManager manager) throws ServiceException 072 { 073 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 074 _formDAO = (FormDAO) manager.lookup(FormDAO.ROLE); 075 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 076 _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE); 077 } 078 079 /** 080 * Get the scheduled dates of a form 081 * @param formId the id of the form 082 * @return a map with start date and end date 083 */ 084 @Callable 085 public Map<String, Object> getScheduledDates(String formId) 086 { 087 Map<String, Object> dates = new HashMap<> (); 088 089 Form form = _resolver.resolveById(formId); 090 _formDAO.checkHandleFormRight(form); 091 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 Form form = _resolver.resolveById(formId); 118 _formDAO.checkHandleFormRight(form); 119 120 LocalDate startDate = startDateAsStr != null ? LocalDate.parse(startDateAsStr) : null; 121 LocalDate endDate = endDateAsStr != null ? LocalDate.parse(endDateAsStr) : null; 122 123 form.setStartDate(startDate); 124 form.setEndDate(endDate); 125 form.saveChanges(); 126 127 Map<String, Object> eventParams = new HashMap<>(); 128 eventParams.put("form", form); 129 _observationManager.notify(new Event(FormEvents.FORM_MODIFIED, _currentUserProvider.getUser(), eventParams)); 130 131 return true; 132 } 133 134 /** 135 * Get the opening status of the form 136 * @param form the form 137 * @return 'open', 'over', or 'coming' depending on today's date 138 */ 139 public FormStatus getStatus(Form form) 140 { 141 LocalDate startDate = form.getStartDate(); 142 LocalDate endDate = form.getEndDate(); 143 144 LocalDate now = LocalDate.now(); 145 146 if (startDate != null && startDate.isAfter(now)) 147 { 148 return FormStatus.COMING; 149 } 150 151 if (endDate != null && endDate.isBefore(now)) 152 { 153 return FormStatus.OVER; 154 } 155 156 return FormStatus.OPEN; 157 } 158}