001/* 002 * Copyright 2017 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.web.publication; 017 018import java.time.Instant; 019import java.time.LocalDateTime; 020import java.time.ZoneId; 021import java.util.Date; 022import java.util.HashMap; 023import java.util.Map; 024 025import org.ametys.core.schedule.Runnable; 026import org.ametys.core.user.UserIdentity; 027 028/** 029 * A {@link Runnable} which schedules a {@link PublishOrUnpublishPageSchedulable} task for (un)publishing a page. 030 */ 031public abstract class AbstractPublishOrUnpublishPageRunnable implements Runnable 032{ 033 /** The id of the page to (un)publish */ 034 protected String _pageId; 035 /** The name of the page to (un)publish */ 036 protected String _pageName; 037 /** The {@link UserIdentity} to launch the runnable task */ 038 protected UserIdentity _userIdentity; 039 /** The date when to (un)publish the page */ 040 protected Date _date; 041 042 /** 043 * Constructor 044 * @param pageId The id of the page to (un)publish 045 * @param pageName The name of the page to (un)publish 046 * @param userIdentity The {@link UserIdentity} to launch the runnable task 047 * @param date The date when to (un)publish the page 048 */ 049 public AbstractPublishOrUnpublishPageRunnable(String pageId, String pageName, UserIdentity userIdentity, Date date) 050 { 051 _pageId = pageId; 052 _date = date; 053 _pageName = pageName; 054 _userIdentity = userIdentity; 055 } 056 057 @Override 058 public String getId() 059 { 060 return this.getClass().getName() + "." + _pageId; 061 } 062 063 @Override 064 public FireProcess getFireProcess() 065 { 066 return FireProcess.CRON; 067 } 068 069 @Override 070 public String getCronExpression() 071 { 072 // Convert java.util.Date to java.time.LocalDateTime 073 Instant instant = Instant.ofEpochMilli(_date.getTime()); 074 LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); 075 // Then tranform it to a Cron Expression 076 return ldt.getSecond() + " " + ldt.getMinute() + " " + ldt.getHour() + " " + ldt.getDayOfMonth() + " " + ldt.getMonthValue() + " ? " + ldt.getYear(); 077 } 078 079 @Override 080 public String getSchedulableId() 081 { 082 return "org.ametys.web.publication.schedule.PublishOrUnpublish"; 083 } 084 085 @Override 086 public boolean isRemovable() 087 { 088 return false; 089 } 090 091 @Override 092 public boolean isModifiable() 093 { 094 return false; 095 } 096 097 @Override 098 public boolean isDeactivatable() 099 { 100 return false; 101 } 102 103 @Override 104 public MisfirePolicy getMisfirePolicy() 105 { 106 return MisfirePolicy.FIRE_ONCE; 107 } 108 109 @Override 110 public boolean isVolatile() 111 { 112 return false; 113 } 114 115 @Override 116 public Map<String, Object> getParameterValues() 117 { 118 Map<String, Object> values = new HashMap<>(); 119 values.put(PublishOrUnpublishPageSchedulable.PAGE_ID_KEY, _pageId); 120 return values; 121 } 122 123 @Override 124 public UserIdentity getUserIdentity() 125 { 126 return _userIdentity; 127 } 128}