Exemplo n.º 1
0
    protected void SaveButton_Click(object sender, EventArgs e)
    {
        try
        {
            // Get the common web service instance.
            ServiceAccess           serviceLoader = ServiceAccess.GetInstance();
            FarmService.FarmService farmService   = serviceLoader.GetFarm();
            string contactIds = ContactIdsHiddenField.Value.ToString();

            string[] arrContactId = contactIds.Split(new char[] { ';' });

            RegistrationService.LoginInfo loginInfo = (RegistrationService.LoginInfo)Session["loginInfo"];
            int userId = loginInfo.UserId;

            if (PlotListDropDownList.SelectedValue.ToString() == "-2")
            {
                //Check the New Plot Name for Duplicate
                if (farmService.IsPlotNameDuplicateWhileAddingNewPlot(Convert.ToInt32(FarmIdHiddenField.Value.ToString()), NewPlotHiddenField.Value.ToString()))
                {
                    ErrorLiteral.Text = "Duplicate Plot Name provided. Please provide a different Plot Name";
                    PlotListDropDownList.Items.FindByValue("-2").Text = "New Plot ...";
                    PlotListDropDownList.SelectedIndex = 0;
                    NewPlotHiddenField.Value           = "";
                    return;
                }

                //Default Plot Check
                if (farmService.IsDefaultPlot(Convert.ToInt32(PlotIdHiddenField.Value.ToString())))
                {
                    if (arrContactId.Length >= farmService.GetContactCountForPlot(Convert.ToInt32(PlotIdHiddenField.Value.ToString())))
                    {
                        ErrorLiteral.Text = "Error: Default Plot Cannot be empty.";
                        PlotListDropDownList.Items.FindByValue("-2").Text = "New Plot ...";
                        NewPlotHiddenField.Value = "";
                        return;
                    }
                }

                //Building Model Object
                FarmService.PlotInfo plot = new FarmService.PlotInfo();
                plot.PlotId       = 0;
                plot.PlotName     = NewPlotHiddenField.Value.ToString();
                plot.FarmId       = Convert.ToInt32(FarmIdHiddenField.Value.ToString());
                plot.LastModifyBy = userId;
                int plotId = farmService.CreatePlotForMoveContacts(plot);
                for (int i = 0; i < arrContactId.Length; i++)
                {
                    farmService.MoveContactToPlot(Convert.ToInt64(arrContactId[i]), plotId, "", userId);
                }
            }
            else
            {
                int plotId = int.Parse(PlotListDropDownList.SelectedValue.ToString());
                for (int i = 0; i < arrContactId.Length; i++)
                {
                    farmService.MoveContactToPlot(Convert.ToInt64(arrContactId[i]), plotId, "", userId);
                }
            }

            ErrorLiteral.Text          = "Contacts have been moved successfully.";
            SaveButton.Visible         = false;
            CancelButton.OnClientClick = "javascript: opener.location.href('ViewFarm.aspx?farmId=" + FarmIdHiddenField.Value.ToString() + "'); window.close();";
            CancelButton.Text          = "Close";
        }
        catch (Exception exception)
        {
            log.Error("UNKNOWN ERROR WHILE MOVING A CONTACT:", exception);
            if (exception.Message.Contains("Cannot Empty Default Plot"))
            {
                ErrorLiteral.Text = "Cannot Empty Default Plot";
            }
            else if (exception.Message.Contains("Plot Name already Exist. Please Provide a different Plot Name"))
            {
                ErrorLiteral.Text = "Plot Name already Exist. Please Provide a different Plot Name";
            }
            else
            {
                ErrorLiteral.Text = "Unable to Move a Contact";
            }
        }
    }