Esempio n. 1
0
        /// <summary>
        /// Required by IGPFunction2 interface; this function is called when the GP tool is ready to be executed.
        /// </summary>
        /// <param name="paramValues"></param>
        /// <param name="trackCancel"></param>
        /// <param name="envMgr"></param>
        /// <param name="msgs"></param>
        public override void Execute(IArray paramValues, ITrackCancel trackCancel, IGPEnvironmentManager envMgr, IGPMessages msgs)
        {
            IJTXJob4 job = null;

            // Do some common error-checking
            base.Execute(paramValues, trackCancel, envMgr, msgs);

            //////////////////////////////////////////////////////////////////////
            // TODO: Update the job-creation logic.
            //
            // In subsequent builds of Workflow Manager (post-10.0), there may be
            // a new API that creates jobs and handles much of the logic included
            // in this function (and this class at large).  If so, this GP tool
            // should be revised to make use of this simplified interface.
            //
            // Anyone using this tool as a reference, particularly with regards
            // to creating Workflow Manager jobs, should keep this in mind.
            //////////////////////////////////////////////////////////////////////

            // Try to create the job, as requested
            try
            {
                IJTXJobManager2   jobManager = this.WmxDatabase.JobManager as IJTXJobManager2;
                IJTXConfiguration configMgr  = this.WmxDatabase.ConfigurationManager as IJTXConfiguration;
                IJTXJobType4      jobTypeObj = configMgr.GetJobType(m_jobTypeAsString) as IJTXJobType4;

                // Set up the description object to be used to create this job
                IJTXJobDescription jobDescription = new JTXJobDescriptionClass();
                jobDescription.JobTypeName = m_jobTypeAsString;
                jobDescription.AOI         = GetPolygonFromSpecifiedLayer(m_aoiLayer);

                // Set up the ownership & assignment of the job
                jobDescription.OwnedBy = m_jobOwner;
                if (m_assigneeType.Equals(C_OPT_ASSIGN_TO_GROUP))
                {
                    jobDescription.AssignedType = jtxAssignmentType.jtxAssignmentTypeGroup;
                    jobDescription.AssignedTo   = m_assignee;
                }
                else if (m_assigneeType.Equals(C_OPT_ASSIGN_TO_USER))
                {
                    jobDescription.AssignedType = jtxAssignmentType.jtxAssignmentTypeUser;
                    jobDescription.AssignedTo   = m_assignee;
                }
                else if (m_assigneeType.Equals(C_OPT_UNASSIGNED))
                {
                    jobDescription.AssignedType = jtxAssignmentType.jtxAssignmentTypeUnassigned;
                    jobDescription.AssignedTo   = string.Empty;
                }
                else
                {
                    // Do nothing; let the job type defaults take over
                    msgs.AddMessage("Using job type defaults for job assignment");
                    jobDescription.AssignedType = jobTypeObj.DefaultAssignedType;
                    jobDescription.AssignedTo   = jobTypeObj.DefaultAssignedTo;
                }

                // Start date
                if (m_startDate != null && m_startDate.Value != null)
                {
                    string tempStr = m_startDate.Value.ToString();

                    // Workflow Manager stores times as UTC times; input times must
                    // therefore be pre-converted
                    DateTime tempDate = DateTime.Parse(tempStr);
                    jobDescription.StartDate = TimeZone.CurrentTimeZone.ToUniversalTime(tempDate);
                }
                else
                {
                    msgs.AddMessage("Using job type defaults for start date");
                    jobDescription.StartDate = jobTypeObj.DefaultStartDate;
                }

                // Due date
                if (m_dueDate != null && m_dueDate.Value != null)
                {
                    string tempStr = m_dueDate.Value.ToString();

                    // Workflow Manager stores times as UTC times; input times must
                    // therefore be pre-converted
                    DateTime tempDate = DateTime.Parse(tempStr);
                    jobDescription.DueDate = TimeZone.CurrentTimeZone.ToUniversalTime(tempDate);
                }
                else
                {
                    msgs.AddMessage("Using job type defaults for due date");
                    jobDescription.DueDate = jobTypeObj.DefaultDueDate;
                }

                // Priority
                if (!m_priority.Equals(string.Empty))
                {
                    IJTXPriority priority = configMgr.GetPriority(m_priority);
                    jobDescription.Priority = priority;
                }
                else
                {
                    msgs.AddMessage("Using job type defaults for priority");
                    jobDescription.Priority = jobTypeObj.DefaultPriority;
                }

                // Parent job
                if (m_parentJobId > 0)
                {
                    jobDescription.ParentJobId = m_parentJobId;
                }

                // Data workspace
                if (m_dataWorkspaceId.Equals(C_OPT_VAL_NOT_SET))
                {
                    jobDescription.DataWorkspaceID = string.Empty;
                }
                else if (!m_dataWorkspaceId.Equals(string.Empty))
                {
                    jobDescription.DataWorkspaceID = m_dataWorkspaceId;
                }
                else
                {
                    msgs.AddMessage("Using job type defaults for data workspace");
                    if (jobTypeObj.DefaultDataWorkspace != null)
                    {
                        jobDescription.DataWorkspaceID = jobTypeObj.DefaultDataWorkspace.DatabaseID;
                    }
                }

                // Parent version
                if (m_parentVersion.Equals(C_OPT_VAL_NOT_SET))
                {
                    jobDescription.ParentVersionName = string.Empty;
                }
                else if (!m_parentVersion.Equals(string.Empty))
                {
                    jobDescription.ParentVersionName = m_parentVersion;
                }
                else
                {
                    msgs.AddMessage("Using job type defaults for parent version");
                    jobDescription.ParentVersionName = jobTypeObj.DefaultParentVersionName;
                }

                // Auto-execution
                jobDescription.AutoExecuteOnCreate = m_executeNewJob;

                // Create the new job
                int             expectedNumJobs = 1;
                bool            checkAoi        = true;
                IJTXJobSet      jobSet          = null;
                IJTXExecuteInfo execInfo;
                try
                {
                    jobSet = jobManager.CreateJobsFromDescription(jobDescription, expectedNumJobs, checkAoi, out execInfo);
                }
                catch (System.Runtime.InteropServices.COMException comEx)
                {
                    throw new WmauException(WmauErrorCodes.C_CREATE_JOB_ERROR, comEx);
                }

                if ((execInfo != null && execInfo.ThrewError) ||
                    jobSet == null ||
                    jobSet.Count != expectedNumJobs)
                {
                    if (execInfo != null && !string.IsNullOrEmpty(execInfo.ErrorDescription))
                    {
                        throw new WmauException(
                                  WmauErrorCodes.C_CREATE_JOB_ERROR,
                                  new Exception(execInfo.ErrorCode.ToString() + ": " + execInfo.ErrorDescription));
                    }
                    else
                    {
                        throw new WmauException(WmauErrorCodes.C_CREATE_JOB_ERROR);
                    }
                }

                // If it gets all the way down here without errors, set the output ID with the
                // ID of the job that was created.
                job = jobSet.Next() as IJTXJob4;
                WmauParameterMap paramMap   = new WmauParameterMap(paramValues);
                IGPValue         jobIdGpVal = new GPLongClass();
                jobIdGpVal.SetAsText(job.ID.ToString());
                IGPParameterEdit3 jobIdParam = paramMap.GetParamEdit(C_PARAM_NEWJOBID);
                jobIdParam.Value = jobIdGpVal;
                msgs.AddMessage("Created job: " + job.ID.ToString() + " (" + job.Name + ")");

                msgs.AddMessage(Properties.Resources.MSG_DONE);
            }
            catch (WmauException wmEx)
            {
                try
                {
                    msgs.AddError(wmEx.ErrorCodeAsInt, wmEx.Message);
                    if (job != null)
                    {
                        this.WmxDatabase.JobManager.DeleteJob(job.ID, true);
                    }
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
            catch (Exception ex)
            {
                try
                {
                    WmauError error = new WmauError(WmauErrorCodes.C_CREATE_JOB_ERROR);
                    msgs.AddError(error.ErrorCodeAsInt, error.Message + "; " + ex.Message);
                    if (job != null)
                    {
                        this.WmxDatabase.JobManager.DeleteJob(job.ID, true);
                    }
                }
                catch
                {
                    // Catch anything else that possibly happens
                }
            }
        }
        ////////////////////////////////////////////////////////////////////////
        // METHOD: CreateJobs
        private int CreateJobs(IJTXJob2 pParentJob)
        {
            try
            {
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

                bool bAutoCommit = ConfigurationCache.AutoCommitWorkflow;

                m_ipDatabase.LogMessage(5, 2000, "CreateJobs");

                // Set the job template values
                IJTXJobManager2 pJobMan = m_ipDatabase.JobManager as IJTXJobManager2;
                IJTXJobDescription pJobDescription = new JTXJobDescriptionClass();

                pJobDescription.Description = pParentJob.Description;
                pJobDescription.Priority = pParentJob.Priority;
                pJobDescription.ParentJobId = pParentJob.ID;

                pJobDescription.StartDate = pParentJob.StartDate;

                if (m_dueDate != Constants.NullDate)
                    pJobDescription.DueDate = m_dueDate;
                else if (m_duration > 0)
                    pJobDescription.DueDate = System.DateTime.Now.AddDays(m_duration);
                else
                    pJobDescription.DueDate = pParentJob.DueDate;

                if (!String.IsNullOrEmpty(m_paramAssignToGroup))
                {
                    pJobDescription.AssignedType = jtxAssignmentType.jtxAssignmentTypeGroup;
                    pJobDescription.AssignedTo = m_paramAssignToGroup;
                }
                else if (!String.IsNullOrEmpty(m_paramAssignToUser))
                {
                    pJobDescription.AssignedType = jtxAssignmentType.jtxAssignmentTypeUser;
                    pJobDescription.AssignedTo = m_paramAssignToUser;
                }
                else
                {
                    pJobDescription.AssignedType = jtxAssignmentType.jtxAssignmentTypeUnassigned;
                }

                pJobDescription.OwnedBy = ConfigurationCache.GetCurrentJTXUser().UserName;

                if (pParentJob.ActiveDatabase != null)
                    pJobDescription.DataWorkspaceID = pParentJob.ActiveDatabase.DatabaseID;

                // Set the parent version.  This only makes sense if the active workspace has been set
                if (pJobDescription.DataWorkspaceID != null)
                {
                    if (m_paramCreateVersionType == CreateVersionType.None
                        || m_paramCreateVersionType == CreateVersionType.UseParentJobsVersion)
                        pJobDescription.ParentVersionName = pParentJob.VersionName;	// This has to be set here because setting the job workspace resets the value
                    else if (m_paramCreateVersionType == CreateVersionType.UseJobTypeDefaultSettings)
                    {
                        IJTXJobType pJobType = m_ipDatabase.ConfigurationManager.GetJobType(m_paramJobTypeName);
                        if (pJobType != null)
                            pJobDescription.ParentVersionName = pJobType.DefaultParentVersionName;
                    }
                    else if (m_paramCreateVersionType == CreateVersionType.UseParentJobsDefaultVersion)
                        pJobDescription.ParentVersionName = pParentJob.JobType.DefaultParentVersionName;
                    else if (m_paramCreateVersionType == CreateVersionType.UseParentJobsParentVersion)
                        pJobDescription.ParentVersionName = pParentJob.ParentVersion;
                }

                // Determine the number of jobs to make
                m_ipDatabase.LogMessage(5, 2000, "Before Determining Number of Jobs");

                IArray aoiList = null;
                int numJobs;
                if (!GetNumberOfJobs(pParentJob, ref aoiList, out numJobs))
                    return 1;

                if (numJobs <= 0)
                {
                    MessageBox.Show(Properties.Resources.ZeroJobCount);
                    return 0;
                }
                pJobDescription.AOIList = aoiList;
                m_ipDatabase.LogMessage(5, 2000, "After Determining Number of Jobs");


                // Create the job objects
                m_ipDatabase.LogMessage(5, 2000, "Before CreateJobs");
                pJobDescription.JobTypeName = m_paramJobTypeName;
                IJTXExecuteInfo pExecInfo;
                m_ipJobs = pJobMan.CreateJobsFromDescription(pJobDescription, numJobs, true, out pExecInfo);
                m_ipDatabase.LogMessage(5, 2000, "After CreateJobs");


                // Populate the job data
                for (int i = 0; i < m_ipJobs.Count; ++i)
                {
                    IJTXJob pJob = m_ipJobs.get_Item(i);

                    SetJobProperties(pJobMan, pJob, pParentJob);
                }
                return 1;
            }
            catch (COMException ex)
            {
                if (ex.ErrorCode == (int)fdoError.FDO_E_SE_INVALID_COLUMN_VALUE)
                {
                    MessageBox.Show(Properties.Resources.InvalidColumn, Properties.Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    MessageBox.Show(ex.Message);
                }
                return 0;
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, Properties.Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return 0;
            }
            finally
            {
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
            }
        }
        ////////////////////////////////////////////////////////////////////////
        // METHOD: CreateJobs
        private int CreateJobs(IJTXJob2 pParentJob)
        {
            try
            {
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

                bool bAutoCommit = ConfigurationCache.AutoCommitWorkflow;

                m_ipDatabase.LogMessage(5, 2000, "CreateJobs");

                // Set the job template values
                IJTXJobManager2    pJobMan         = m_ipDatabase.JobManager as IJTXJobManager2;
                IJTXJobDescription pJobDescription = new JTXJobDescriptionClass();

                pJobDescription.Description = pParentJob.Description;
                pJobDescription.Priority    = pParentJob.Priority;
                pJobDescription.ParentJobId = pParentJob.ID;

                pJobDescription.StartDate = pParentJob.StartDate;

                if (m_dueDate != Constants.NullDate)
                {
                    pJobDescription.DueDate = m_dueDate;
                }
                else if (m_duration > 0)
                {
                    pJobDescription.DueDate = System.DateTime.Now.AddDays(m_duration);
                }
                else
                {
                    pJobDescription.DueDate = pParentJob.DueDate;
                }

                if (!String.IsNullOrEmpty(m_paramAssignToGroup))
                {
                    pJobDescription.AssignedType = jtxAssignmentType.jtxAssignmentTypeGroup;
                    pJobDescription.AssignedTo   = m_paramAssignToGroup;
                }
                else if (!String.IsNullOrEmpty(m_paramAssignToUser))
                {
                    pJobDescription.AssignedType = jtxAssignmentType.jtxAssignmentTypeUser;
                    pJobDescription.AssignedTo   = m_paramAssignToUser;
                }
                else
                {
                    pJobDescription.AssignedType = jtxAssignmentType.jtxAssignmentTypeUnassigned;
                }

                pJobDescription.OwnedBy = ConfigurationCache.GetCurrentJTXUser().UserName;

                if (pParentJob.ActiveDatabase != null)
                {
                    pJobDescription.DataWorkspaceID = pParentJob.ActiveDatabase.DatabaseID;
                }

                // Set the parent version.  This only makes sense if the active workspace has been set
                if (pJobDescription.DataWorkspaceID != null)
                {
                    if (m_paramCreateVersionType == CreateVersionType.None ||
                        m_paramCreateVersionType == CreateVersionType.UseParentJobsVersion)
                    {
                        pJobDescription.ParentVersionName = pParentJob.VersionName;     // This has to be set here because setting the job workspace resets the value
                    }
                    else if (m_paramCreateVersionType == CreateVersionType.UseJobTypeDefaultSettings)
                    {
                        IJTXJobType pJobType = m_ipDatabase.ConfigurationManager.GetJobType(m_paramJobTypeName);
                        if (pJobType != null)
                        {
                            pJobDescription.ParentVersionName = pJobType.DefaultParentVersionName;
                        }
                    }
                    else if (m_paramCreateVersionType == CreateVersionType.UseParentJobsDefaultVersion)
                    {
                        pJobDescription.ParentVersionName = pParentJob.JobType.DefaultParentVersionName;
                    }
                    else if (m_paramCreateVersionType == CreateVersionType.UseParentJobsParentVersion)
                    {
                        pJobDescription.ParentVersionName = pParentJob.ParentVersion;
                    }
                }

                // Determine the number of jobs to make
                m_ipDatabase.LogMessage(5, 2000, "Before Determining Number of Jobs");

                IArray aoiList = null;
                int    numJobs;
                if (!GetNumberOfJobs(pParentJob, ref aoiList, out numJobs))
                {
                    return(1);
                }

                if (numJobs <= 0)
                {
                    MessageBox.Show(Properties.Resources.ZeroJobCount);
                    return(0);
                }
                pJobDescription.AOIList = aoiList;
                m_ipDatabase.LogMessage(5, 2000, "After Determining Number of Jobs");


                // Create the job objects
                m_ipDatabase.LogMessage(5, 2000, "Before CreateJobs");
                pJobDescription.JobTypeName = m_paramJobTypeName;
                IJTXExecuteInfo pExecInfo;
                m_ipJobs = pJobMan.CreateJobsFromDescription(pJobDescription, numJobs, true, out pExecInfo);
                m_ipDatabase.LogMessage(5, 2000, "After CreateJobs");


                // Populate the job data
                for (int i = 0; i < m_ipJobs.Count; ++i)
                {
                    IJTXJob pJob = m_ipJobs.get_Item(i);

                    SetJobProperties(pJobMan, pJob, pParentJob);
                }
                return(1);
            }
            catch (COMException ex)
            {
                if (ex.ErrorCode == (int)fdoError.FDO_E_SE_INVALID_COLUMN_VALUE)
                {
                    MessageBox.Show(Properties.Resources.InvalidColumn, Properties.Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    MessageBox.Show(ex.Message);
                }
                return(0);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, Properties.Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(0);
            }
            finally
            {
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
            }
        }