001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2010 Sun Microsystems, Inc. 025 * Portions Copyright 2014-2015 ForgeRock AS 026 */ 027package org.opends.server.tools.tasks; 028 029import java.util.ArrayList; 030import java.util.Collection; 031import java.util.Collections; 032import java.util.Date; 033import java.util.List; 034 035import org.opends.server.admin.client.cli.TaskScheduleArgs; 036import org.opends.server.backends.task.FailedDependencyAction; 037import org.opends.server.util.StaticUtils; 038import com.forgerock.opendj.cli.ArgumentException; 039import com.forgerock.opendj.cli.StringArgument; 040import com.forgerock.opendj.cli.CommandBuilder; 041 042/** 043 * A generic data structure that contains the data that the user provided to 044 * schedule a task. 045 * <br> 046 * The main difference with {@link TaskScheduleInformation} is that this class 047 * is completely agnostic of the execution. 048 */ 049public class TaskScheduleUserData 050{ 051 private boolean startNow; 052 private Date startDate; 053 private String recurringDateTime; 054 private final List<String> dependencyIds = new ArrayList<>(); 055 private FailedDependencyAction failedDependencyAction; 056 private final List<String> notifyUponCompletionEmailAddresses = new ArrayList<>(); 057 private final List<String> notifyUponErrorEmailAddresses = new ArrayList<>(); 058 059 /** 060 * Whether the arguments provided by the user, indicate that the task should 061 * be executed immediately. 062 * @return {@code true} if the task must be executed immediately and 063 * {@code false} otherwise. 064 */ 065 public boolean isStartNow() 066 { 067 return startNow; 068 } 069 070 /** 071 * Sets whether the arguments provided by the user, indicate that the task 072 * should be executed immediately. 073 * @param startNow {@code true} if the task must be executed immediately and 074 * {@code false} otherwise. 075 */ 076 public void setStartNow(boolean startNow) 077 { 078 this.startNow = startNow; 079 } 080 081 /** 082 * Gets the date at which this task should be scheduled to start. 083 * 084 * @return date/time at which the task should be scheduled 085 */ 086 public Date getStartDate() 087 { 088 return startDate; 089 } 090 091 /** 092 * Sets the date at which this task should be scheduled to start. 093 * 094 * @param startDate the date/time at which the task should be scheduled 095 */ 096 public void setStartDate(Date startDate) 097 { 098 this.startDate = startDate; 099 } 100 101 /** 102 * Gets the date/time pattern for recurring task schedule. 103 * 104 * @return recurring date/time pattern at which the task 105 * should be scheduled. 106 */ 107 public String getRecurringDateTime() 108 { 109 return recurringDateTime; 110 } 111 112 /** 113 * Sets the date/time pattern for recurring task schedule. 114 * 115 * @param recurringDateTime recurring date/time pattern at which the task 116 * should be scheduled. 117 */ 118 public void setRecurringDateTime(String recurringDateTime) 119 { 120 this.recurringDateTime = recurringDateTime; 121 } 122 123 /** 124 * Gets a list of task IDs upon which this task is dependent. 125 * 126 * @return list of task IDs 127 */ 128 public List<String> getDependencyIds() 129 { 130 return dependencyIds; 131 } 132 133 /** 134 * Sets the list of task IDs upon which this task is dependent. 135 * 136 * @param dependencyIds list of task IDs 137 */ 138 public void setDependencyIds(List<String> dependencyIds) 139 { 140 this.dependencyIds.clear(); 141 this.dependencyIds.addAll(dependencyIds); 142 } 143 144 /** 145 * Gets the action to take should one of the dependent task fail. 146 * 147 * @return action to take 148 */ 149 public FailedDependencyAction getFailedDependencyAction() 150 { 151 return failedDependencyAction; 152 } 153 154 /** 155 * Sets the action to take should one of the dependent task fail. 156 * 157 * @param failedDependencyAction the action to take 158 */ 159 public void setFailedDependencyAction( 160 FailedDependencyAction failedDependencyAction) 161 { 162 this.failedDependencyAction = failedDependencyAction; 163 } 164 165 /** 166 * Gets a list of email address to which an email will be sent when this 167 * task completes. 168 * 169 * @return list of email addresses 170 */ 171 public List<String> getNotifyUponCompletionEmailAddresses() 172 { 173 return notifyUponCompletionEmailAddresses; 174 } 175 176 /** 177 * Sets the list of email address to which an email will be sent when this 178 * task completes. 179 * 180 * @param notifyUponCompletionEmailAddresses the list of email addresses 181 */ 182 public void setNotifyUponCompletionEmailAddresses( 183 List<String> notifyUponCompletionEmailAddresses) 184 { 185 this.notifyUponCompletionEmailAddresses.clear(); 186 this.notifyUponCompletionEmailAddresses.addAll( 187 notifyUponCompletionEmailAddresses); 188 } 189 190 /** 191 * Gets the list of email address to which an email will be sent if this 192 * task encounters an error during execution. 193 * 194 * @return list of email addresses 195 */ 196 public List<String> getNotifyUponErrorEmailAddresses() 197 { 198 return notifyUponErrorEmailAddresses; 199 } 200 201 /** 202 * Sets the list of email address to which an email will be sent if this 203 * task encounters an error during execution. 204 * 205 * @param notifyUponErrorEmailAddresses the list of email addresses 206 */ 207 public void setNotifyUponErrorEmailAddresses( 208 List<String> notifyUponErrorEmailAddresses) 209 { 210 this.notifyUponErrorEmailAddresses.clear(); 211 this.notifyUponErrorEmailAddresses.addAll(notifyUponErrorEmailAddresses); 212 } 213 214 215 /** 216 * An static utility method that can be used to update the object used to 217 * display the equivalent command-line with the contents of a given 218 * task schedule object. 219 * @param commandBuilder the command builder. 220 * @param taskSchedule the task schedule. 221 */ 222 public static void updateCommandBuilderWithTaskSchedule( 223 CommandBuilder commandBuilder, 224 TaskScheduleUserData taskSchedule) 225 { 226 TaskScheduleArgs argsToClone = new TaskScheduleArgs(); 227 String sDate = null; 228 String recurringDateTime = null; 229 if (!taskSchedule.isStartNow()) 230 { 231 Date date = taskSchedule.getStartDate(); 232 if (date != null) 233 { 234 sDate = StaticUtils.formatDateTimeString(date); 235 } 236 recurringDateTime = taskSchedule.getRecurringDateTime(); 237 } 238 239 String sFailedDependencyAction = null; 240 FailedDependencyAction fAction = taskSchedule.getFailedDependencyAction(); 241 if (fAction != null) 242 { 243 sFailedDependencyAction = fAction.name(); 244 } 245 String[] sValues = {sDate, recurringDateTime, sFailedDependencyAction}; 246 StringArgument[] args = {argsToClone.startArg, 247 argsToClone.recurringArg, argsToClone.failedDependencyActionArg}; 248 for (int i=0; i<sValues.length; i++) 249 { 250 if (sValues[i] != null) 251 { 252 commandBuilder.addArgument(getArgument(args[i], 253 Collections.singleton(sValues[i]))); 254 } 255 } 256 257 List<?>[] values = {taskSchedule.getDependencyIds(), 258 taskSchedule.getNotifyUponCompletionEmailAddresses(), 259 taskSchedule.getNotifyUponErrorEmailAddresses()}; 260 args = new StringArgument[]{argsToClone.dependencyArg, 261 argsToClone.completionNotificationArg, 262 argsToClone.errorNotificationArg}; 263 264 for (int i=0; i<values.length; i++) 265 { 266 if (!values[i].isEmpty()) 267 { 268 commandBuilder.addArgument(getArgument(args[i], 269 values[i])); 270 } 271 } 272 } 273 274 private static StringArgument getArgument( 275 StringArgument argToClone, Collection<?> values) 276 { 277 StringArgument arg; 278 try 279 { 280 arg = new StringArgument(argToClone.getName(), 281 argToClone.getShortIdentifier(), argToClone.getLongIdentifier(), 282 argToClone.isRequired(), argToClone.isMultiValued(), 283 argToClone.needsValue(), 284 argToClone.getValuePlaceholder(), 285 argToClone.getDefaultValue(), 286 argToClone.getPropertyName(), 287 argToClone.getDescription()); 288 } 289 catch (ArgumentException e) 290 { 291 // This is a bug. 292 throw new RuntimeException("Unexpected error: "+e, e); 293 } 294 for (Object v : values) 295 { 296 arg.addValue(String.valueOf(v)); 297 } 298 return arg; 299 } 300}