5236N/A/*
5236N/A * CDDL HEADER START
5236N/A *
5236N/A * The contents of this file are subject to the terms of the
5236N/A * Common Development and Distribution License, Version 1.0 only
5236N/A * (the "License"). You may not use this file except in compliance
5236N/A * with the License.
5236N/A *
6983N/A * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
6983N/A * or http://forgerock.org/license/CDDLv1.0.html.
5236N/A * See the License for the specific language governing permissions
5236N/A * and limitations under the License.
5236N/A *
5236N/A * When distributing Covered Code, include this CDDL HEADER in each
6983N/A * file and include the License file at legal-notices/CDDLv1_0.txt.
6983N/A * If applicable, add the following below this CDDL HEADER, with the
6983N/A * fields enclosed by brackets "[]" replaced with your own identifying
6983N/A * information:
5236N/A * Portions Copyright [yyyy] [name of copyright owner]
5236N/A *
5236N/A * CDDL HEADER END
5236N/A *
5236N/A *
5236N/A * Copyright 2010 Sun Microsystems, Inc.
5236N/A */
5236N/Apackage org.opends.server.tools.tasks;
5236N/A
5236N/Aimport java.util.ArrayList;
5236N/Aimport java.util.Collection;
5236N/Aimport java.util.Collections;
5236N/Aimport java.util.Date;
5236N/Aimport java.util.List;
5236N/A
5236N/Aimport org.opends.server.admin.client.cli.TaskScheduleArgs;
5236N/Aimport org.opends.server.backends.task.FailedDependencyAction;
5236N/Aimport org.opends.server.util.StaticUtils;
5236N/Aimport org.opends.server.util.args.ArgumentException;
5236N/Aimport org.opends.server.util.args.StringArgument;
5236N/Aimport org.opends.server.util.cli.CommandBuilder;
5236N/A
5236N/A/**
5236N/A * A generic data structure that contains the data that the user provided to
5236N/A * schedule a task.
5236N/A * <br>
5236N/A * The main difference with {@link TaskScheduleInformation} is that this class
5236N/A * is completely agnostic of the execution.
5236N/A */
5236N/Apublic class TaskScheduleUserData
5236N/A{
5236N/A private boolean startNow;
5236N/A private Date startDate;
5236N/A private String recurringDateTime;
5236N/A private final List<String> dependencyIds =
5236N/A new ArrayList<String>();
5236N/A private FailedDependencyAction failedDependencyAction;
5236N/A private final List<String> notifyUponCompletionEmailAddresses =
5236N/A new ArrayList<String>();
5236N/A private final List<String> notifyUponErrorEmailAddresses =
5236N/A new ArrayList<String>();
5236N/A
5236N/A /**
5236N/A * Whether the arguments provided by the user, indicate that the task should
5236N/A * be executed immediately.
5236N/A * @return {@code true} if the task must be executed immediately and
5236N/A * {@code false} otherwise.
5236N/A */
5236N/A public boolean isStartNow()
5236N/A {
5236N/A return startNow;
5236N/A }
5236N/A
5236N/A /**
5236N/A * Sets whether the arguments provided by the user, indicate that the task
5236N/A * should be executed immediately.
5236N/A * @param startNow {@code true} if the task must be executed immediately and
5236N/A * {@code false} otherwise.
5236N/A */
5236N/A public void setStartNow(boolean startNow)
5236N/A {
5236N/A this.startNow = startNow;
5236N/A }
5236N/A
5236N/A /**
5236N/A * Gets the date at which this task should be scheduled to start.
5236N/A *
5236N/A * @return date/time at which the task should be scheduled
5236N/A */
5236N/A public Date getStartDate()
5236N/A {
5236N/A return startDate;
5236N/A }
5236N/A
5236N/A /**
5236N/A * Sets the date at which this task should be scheduled to start.
5236N/A *
5236N/A * @param startDate the date/time at which the task should be scheduled
5236N/A */
5236N/A public void setStartDate(Date startDate)
5236N/A {
5236N/A this.startDate = startDate;
5236N/A }
5236N/A
5236N/A /**
5236N/A * Gets the date/time pattern for recurring task schedule.
5236N/A *
5236N/A * @return recurring date/time pattern at which the task
5236N/A * should be scheduled.
5236N/A */
5236N/A public String getRecurringDateTime()
5236N/A {
5236N/A return recurringDateTime;
5236N/A }
5236N/A
5236N/A /**
5236N/A * Sets the date/time pattern for recurring task schedule.
5236N/A *
5236N/A * @param recurringDateTime recurring date/time pattern at which the task
5236N/A * should be scheduled.
5236N/A */
5236N/A public void setRecurringDateTime(String recurringDateTime)
5236N/A {
5236N/A this.recurringDateTime = recurringDateTime;
5236N/A }
5236N/A
5236N/A /**
5236N/A * Gets a list of task IDs upon which this task is dependent.
5236N/A *
5236N/A * @return list of task IDs
5236N/A */
5236N/A public List<String> getDependencyIds()
5236N/A {
5236N/A return dependencyIds;
5236N/A }
5236N/A
5236N/A /**
5236N/A * Sets the list of task IDs upon which this task is dependent.
5236N/A *
5236N/A * @param dependencyIds list of task IDs
5236N/A */
5236N/A public void setDependencyIds(List<String> dependencyIds)
5236N/A {
5236N/A this.dependencyIds.clear();
5236N/A this.dependencyIds.addAll(dependencyIds);
5236N/A }
5236N/A
5236N/A /**
5236N/A * Gets the action to take should one of the dependent task fail.
5236N/A *
5236N/A * @return action to take
5236N/A */
5236N/A public FailedDependencyAction getFailedDependencyAction()
5236N/A {
5236N/A return failedDependencyAction;
5236N/A }
5236N/A
5236N/A /**
5236N/A * Sets the action to take should one of the dependent task fail.
5236N/A *
5236N/A * @param failedDependencyAction the action to take
5236N/A */
5236N/A public void setFailedDependencyAction(
5236N/A FailedDependencyAction failedDependencyAction)
5236N/A {
5236N/A this.failedDependencyAction = failedDependencyAction;
5236N/A }
5236N/A
5236N/A /**
5236N/A * Gets a list of email address to which an email will be sent when this
5236N/A * task completes.
5236N/A *
5236N/A * @return list of email addresses
5236N/A */
5236N/A public List<String> getNotifyUponCompletionEmailAddresses()
5236N/A {
5236N/A return notifyUponCompletionEmailAddresses;
5236N/A }
5236N/A
5236N/A /**
5236N/A * Sets the list of email address to which an email will be sent when this
5236N/A * task completes.
5236N/A *
5236N/A * @param notifyUponCompletionEmailAddresses the list of email addresses
5236N/A */
5236N/A public void setNotifyUponCompletionEmailAddresses(
5236N/A List<String> notifyUponCompletionEmailAddresses)
5236N/A {
5236N/A this.notifyUponCompletionEmailAddresses.clear();
5236N/A this.notifyUponCompletionEmailAddresses.addAll(
5236N/A notifyUponCompletionEmailAddresses);
5236N/A }
5236N/A
5236N/A /**
5236N/A * Gets the list of email address to which an email will be sent if this
5236N/A * task encounters an error during execution.
5236N/A *
5236N/A * @return list of email addresses
5236N/A */
5236N/A public List<String> getNotifyUponErrorEmailAddresses()
5236N/A {
5236N/A return notifyUponErrorEmailAddresses;
5236N/A }
5236N/A
5236N/A /**
5236N/A * Sets the list of email address to which an email will be sent if this
5236N/A * task encounters an error during execution.
5236N/A *
5236N/A * @param notifyUponErrorEmailAddresses the list of email addresses
5236N/A */
5236N/A public void setNotifyUponErrorEmailAddresses(
5236N/A List<String> notifyUponErrorEmailAddresses)
5236N/A {
5236N/A this.notifyUponErrorEmailAddresses.clear();
5236N/A this.notifyUponErrorEmailAddresses.addAll(notifyUponErrorEmailAddresses);
5236N/A }
5236N/A
5236N/A
5236N/A /**
5236N/A * An static utility method that can be used to update the object used to
5236N/A * display the equivalent command-line with the contents of a given
5236N/A * task schedule object.
5236N/A * @param commandBuilder the command builder.
5236N/A * @param taskSchedule the task schedule.
5236N/A */
5236N/A public static void updateCommandBuilderWithTaskSchedule(
5236N/A CommandBuilder commandBuilder,
5236N/A TaskScheduleUserData taskSchedule)
5236N/A {
5236N/A TaskScheduleArgs argsToClone = new TaskScheduleArgs();
5236N/A String sDate = null;
5236N/A String recurringDateTime = null;
5236N/A if (!taskSchedule.isStartNow())
5236N/A {
5236N/A Date date = taskSchedule.getStartDate();
5236N/A if (date != null)
5236N/A {
5236N/A sDate = StaticUtils.formatDateTimeString(date);
5236N/A }
5236N/A recurringDateTime = taskSchedule.getRecurringDateTime();
5236N/A }
5236N/A
5236N/A String sFailedDependencyAction = null;
5236N/A FailedDependencyAction fAction = taskSchedule.getFailedDependencyAction();
5236N/A if (fAction != null)
5236N/A {
5236N/A sFailedDependencyAction = fAction.name();
5236N/A }
5236N/A String[] sValues = {sDate, recurringDateTime, sFailedDependencyAction};
5236N/A StringArgument[] args = {argsToClone.startArg,
5236N/A argsToClone.recurringArg, argsToClone.failedDependencyActionArg};
5236N/A for (int i=0; i<sValues.length; i++)
5236N/A {
5236N/A if (sValues[i] != null)
5236N/A {
5236N/A commandBuilder.addArgument(getArgument(args[i],
5236N/A Collections.singleton(sValues[i])));
5236N/A }
5236N/A }
5236N/A
5236N/A List<?>[] values = {taskSchedule.getDependencyIds(),
5236N/A taskSchedule.getNotifyUponCompletionEmailAddresses(),
5236N/A taskSchedule.getNotifyUponErrorEmailAddresses()};
5236N/A args = new StringArgument[]{argsToClone.dependencyArg,
5236N/A argsToClone.completionNotificationArg,
5236N/A argsToClone.errorNotificationArg};
5236N/A
5236N/A for (int i=0; i<values.length; i++)
5236N/A {
5236N/A if (!values[i].isEmpty())
5236N/A {
5236N/A commandBuilder.addArgument(getArgument(args[i],
5236N/A values[i]));
5236N/A }
5236N/A }
5236N/A }
5236N/A
5236N/A private static StringArgument getArgument(
5236N/A StringArgument argToClone, Collection<?> values)
5236N/A {
5236N/A StringArgument arg;
5236N/A try
5236N/A {
5236N/A arg = new StringArgument(argToClone.getName(),
5236N/A argToClone.getShortIdentifier(), argToClone.getLongIdentifier(),
5236N/A argToClone.isRequired(), argToClone.isMultiValued(),
5236N/A argToClone.needsValue(),
5236N/A argToClone.getValuePlaceholder(),
5236N/A argToClone.getDefaultValue(),
5236N/A argToClone.getPropertyName(),
5236N/A argToClone.getDescription());
5236N/A }
5236N/A catch (ArgumentException e)
5236N/A {
5236N/A // This is a bug.
5236N/A throw new RuntimeException("Unexpected error: "+e, e);
5236N/A }
5236N/A for (Object v : values)
5236N/A {
5236N/A arg.addValue(String.valueOf(v));
5236N/A }
5236N/A return arg;
5236N/A }
5236N/A}