001/**
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: tao $'
006 * '$Date: 2010-06-03 16:45:10 -0700 (Thu, 03 Jun 2010) $' 
007 * '$Revision: 24730 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029package org.kepler.workflowscheduler.gui;
030
031import javax.swing.JOptionPane;
032
033import org.apache.commons.logging.Log;
034import org.apache.commons.logging.LogFactory;
035import org.kepler.objectmanager.repository.Repository;
036
037import edu.ucsb.nceas.schedulerclient.WorkflowSchedulerClient;
038import edu.ucsb.nceas.workflowscheduler.scheduler.WorkflowSchedulerException;
039import ptolemy.actor.gui.TableauFrame;
040
041/**
042 * An action class to perform scheduling a workflow.
043 * @author tao
044 *
045 */
046public class ScheduleWorkflowAction extends SchedulerAbstractAction 
047{
048  
049  private static final Log log = LogFactory.getLog(ScheduleWorkflowAction.class
050      .getName());
051  
052
053  /**
054   * Constructor
055   * @param parent the parent frame of this action
056   * @param scheduleChangeController the GUI controller to handle the schedule change.
057   */
058  public ScheduleWorkflowAction(TableauFrame parent, WorkflowSchedulerParentPanel schedulerParentPanel,
059      ScheduleChangeController scheduleChangeController )
060  {
061    super(parent, schedulerParentPanel, scheduleChangeController);
062  }
063  
064    
065   
066  /**
067   *Schedule the workflow
068   */
069  protected void doAction() throws Exception
070  {
071    super.doAction();
072    if(schedule != null)
073    {
074      String sessionID = null;
075      if(authenDomainForSourceKar != null)
076      {
077        sessionID = domainSessionHash.get(authenDomainForSourceKar);
078      }   
079      //System.out.println("the domain name is =========== "+authenDomainForSourceKar);
080      //System.out.println("the authenticationServiceURl is =========== "+authenticationServiceURLForSourceKar);
081      //System.out.println("The session id is ========="+sessionID);
082      String returnedWorkflowJobName = null;
083      WorkflowSchedulerClient client = new WorkflowSchedulerClient(schedulerURL, sessionID, authenticationServiceURLForSourceKar);
084      try
085      {
086        String interval = (new Integer(schedule.getInterval())).toString();
087        String endTime = schedule.getEndTime();
088        Repository destination = schedule.getWorkflowRunDestination();
089        WorkflowRunEngine engine = schedule.getWorkflowRunEngine();
090        
091        if(destination == null)
092        {
093          JOptionPane.showMessageDialog(parent, "Couldn't schedule the workflow since user didn't sepcify the destination repository", ERROR, JOptionPane.ERROR_MESSAGE);
094          return;
095        }
096        
097        if(engine == null)
098        {
099          JOptionPane.showMessageDialog(parent, "Couldn't schedule the workflow since user didn't specify the workflow run engine", ERROR, JOptionPane.ERROR_MESSAGE);
100          return;
101        }
102        //System.out.println("End time is "+endTime);
103        //System.out.println("the interval is "+interval);
104       /* public String scheduleJob(String workflowId, String workflowName, String karId, 
105            String startTime, String startTimeFormat, String startTimeZoneID,
106            String endTime, String endTimeFormat, String endTimeZoneID,
107            String intervalValue, String intervalUnit,
108            String sourceRepositoryBaseURL, String sourceAuthorizationPath, String sourceQueryPath,
109            String workflowRunEngineName, String workflowRunEngineURL,
110            String destinationRepositoryName, String dataAuthenticationDomain) 
111            throws WorkflowSchedulerException*/
112        String dataAuthenticationDomain = null;
113        String result = client.scheduleJob(schedule.getWorkflowId(), schedule.getWorkflowName(), schedule.getKarLSID(), 
114                                                        schedule.getStartTime(),schedule.getStartTimeFormat(), schedule.getStartTimeZoneID(),
115                                                        endTime, schedule.getEndTimeFormat(), schedule.getEndTimeZoneID(), interval, schedule.getIntervalUnit(),
116                                                        source.getAuthProtocol()+SLASHCONNECTOR+source.getRepository(), source.getAuthorizationPath(),
117                                                        source.getQueryPath(), engine.getName(), engine.getURL(), destination.getName(), dataAuthenticationDomain);
118        if(result == null ||result.trim().equals(""))
119        {
120          JOptionPane.showMessageDialog(parent, "Failed scheduling workflow. Returned workflow job name is blank.", ERROR, JOptionPane.ERROR_MESSAGE);
121        }
122        else
123        {
124          returnedWorkflowJobName =result;
125          if(schedule != null)
126          {
127            schedule.setWorkflowJobName(returnedWorkflowJobName);
128            if(scheduleChangeController != null)
129            {
130              try
131              {
132                scheduleChangeController.addSchedule(schedule);
133              }
134              catch(Exception e)
135              {
136                log.error(""+e.getMessage());
137              }
138              
139            }
140          }
141          log.debug("ScheduleWorkflowAction.doAction - The returned workflow job name is "+returnedWorkflowJobName);
142          JOptionPane.showMessageDialog(parent, "Successfully scheduled workflow.", "Success", JOptionPane.CLOSED_OPTION);
143        }
144      }
145      catch(WorkflowSchedulerException e)
146      {
147        JOptionPane.showMessageDialog(parent, "Failed scheduling workflow:\n"+e.getMessage(), ERROR, JOptionPane.ERROR_MESSAGE);
148        if(e != null && e.getMessage() != null && e.getMessage().indexOf("session" )!=-1 && e.getMessage().indexOf("is not valid")!=-1)
149        {
150          //sessionID = null;
151          domainSessionHash.remove(authenDomainForSourceKar);
152          JOptionPane.showMessageDialog(parent, "The login session id is invalid. Please click \"Schedule\" button again to schedule the workflow", "Info", JOptionPane.CLOSED_OPTION);
153        }
154      }
155    }
156    else
157    {
158      JOptionPane.showMessageDialog(parent, "Couldn't schedule a workflow since the schedule object is null", ERROR, JOptionPane.ERROR_MESSAGE);
159    }
160    
161  }
162  
163 
164}