예제 #1
0
        private void btnRegister_Click(object sender, EventArgs e)
        {
            CrmPluginImage image = new CrmPluginImage(m_org);

            #region Extract Information

            if (trvPlugins.SelectedNode == null ||
                (m_currentImage == null && trvPlugins.SelectedNode.NodeType != CrmTreeNodeType.Step))
            {
                MessageBox.Show("A Step must be selected when registering an image",
                                "Registration", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (!chkImageTypePost.Checked && !chkImageTypePre.Checked)
            {
                MessageBox.Show("At least one Image Type must be specified. This field is required.",
                                "Registration", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (txtEntityAlias.TextLength == 0 || txtEntityAlias.Text.Trim().Length == 0)
            {
                MessageBox.Show("You must enter an alias for this Image. This is a required field.", "Registration",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (!crmParameters.HasAttributes)
            {
                MessageBox.Show("You must specify at least one attribute. This is a required field", "Registration",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            //Retrieve the step for this image
            CrmPluginStep step;
            if (m_currentImage == null)
            {
                step = (CrmPluginStep)trvPlugins.SelectedNode;

                //Verify that the step is not a system item
                if (step.IsSystemCrmEntity)
                {
                    m_orgControl.ShowSystemItemError("Cannot register image on this step");
                    return;
                }
            }
            else
            {
                step = m_org[m_currentImage.AssemblyId][m_currentImage.PluginId][m_currentImage.StepId];
            }

            //Retrieve the message
            CrmMessage message = m_org.Messages[step.MessageId];

            //Verify that this step can have images
            if (0 == message.ImageMessagePropertyNames.Count)
            {
                //Create a list of the messages that can have images
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Only steps registered on the following messages can have images:");
                foreach (CrmMessage item in m_org.Messages.Values)
                {
                    if (0 != item.ImageMessagePropertyNames.Count)
                    {
                        sb.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "- {0}{1}",
                                        item.Name, Environment.NewLine);
                    }
                }

                sb.AppendLine();
                sb.Append("Please select a different step and try again.");

                MessageBox.Show(sb.ToString(), "Invalid Step Selected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            //Start populating the image that will be used
            image.AssemblyId          = step.AssemblyId;
            image.PluginId            = step.PluginId;
            image.StepId              = step.StepId;
            image.Attributes          = crmParameters.Attributes;
            image.EntityAlias         = txtEntityAlias.Text.Trim();
            image.Name                = txtName.Text.Trim();
            image.MessagePropertyName = MessagePropertyNameForm.SelectMessagePropertyName(message);
            if (null == image.MessagePropertyName)
            {
                return;
            }

            if (chkImageTypePre.Checked && chkImageTypePost.Checked)
            {
                image.ImageType = CrmPluginImageType.Both;
            }
            else if (chkImageTypePre.Checked)
            {
                image.ImageType = CrmPluginImageType.PreImage;
            }
            else
            {
                image.ImageType = CrmPluginImageType.PostImage;
            }

            #endregion Extract Information

            #region Register the Image

            try
            {
                if (m_currentImage == null)
                {
                    image.ImageId = RegistrationHelper.RegisterImage(m_org, image);

                    List <ICrmEntity> entityList = new List <ICrmEntity>(new ICrmEntity[] { image });
                    OrganizationHelper.UpdateDates(m_org, entityList);

                    step.AddImage(image);
                    m_orgControl.AddImage(image);
                }
                else
                {
                    image.ImageId = m_currentImage.ImageId;
                    RegistrationHelper.UpdateImage(m_org, image);

                    m_currentImage.AssemblyId          = step.AssemblyId;
                    m_currentImage.PluginId            = step.PluginId;
                    m_currentImage.StepId              = step.StepId;
                    m_currentImage.ImageId             = image.ImageId;
                    m_currentImage.Attributes          = image.Attributes;
                    m_currentImage.EntityAlias         = image.EntityAlias;
                    m_currentImage.MessagePropertyName = image.MessagePropertyName;
                    m_currentImage.ImageType           = image.ImageType;

                    image = m_currentImage;

                    List <ICrmEntity> entityList = new List <ICrmEntity>(new ICrmEntity[] { image });
                    OrganizationHelper.UpdateDates(m_org, entityList);

                    m_orgControl.RefreshImage(m_currentImage);
                }
            }
            catch (Exception ex)
            {
                ErrorMessageForm.ShowErrorMessageBox(this, "Unable to register the Image due to an error.", "Registration", ex);
                return;
            }

            #endregion Register the Image

            DialogResult = DialogResult.OK;
            Close();
        }