Exemplo n.º 1
0
        protected void Save_Click(object sender, EventArgs e)
        {
            var customerAnonymizeSuccesses = new StringBuilder();
            var cannotBeAnonymized         = new StringBuilder();
            var customerAnonymizeErrors    = new StringBuilder();

            var customerDeleteSuccesses = new StringBuilder();
            var cannotBeDeleted         = new StringBuilder();
            var customerDeleteErrors    = new StringBuilder();

            var orderAnonymizeSuccesses = new StringBuilder();
            var orderAnonymizeErrors    = new StringBuilder();

            foreach (GridViewRow row in Grid.Rows)
            {
                if (row.RowType != DataControlRowType.DataRow)
                {
                    continue;
                }

                // We're getting the customer id through a hidden field because the grid data keys are cleared in the GridLoad() sorting above.
                var customerId   = Convert.ToInt32(((HiddenField)row.FindControl("hidCustomerId")).Value);
                var chkAnonymize = (CheckBox)row.FindControl("chkAnonymizeCustomer");
                var chkRemove    = (CheckBox)row.FindControl("chkRemoveCustomer");
                var customerInfo = ((HiddenField)row.FindControl("hidCustomerInfo")).Value;

                if (chkAnonymize.Checked)
                {
                    if (!DataRetentionService.CustomerCanBeAnonymized(customerId))
                    {
                        cannotBeAnonymized
                        .Append(customerInfo)
                        .Append("<br />");

                        continue;
                    }

                    var result = DataRetentionService.AnonymizeCustomer(customerId);

                    if (!result.Success)
                    {
                        customerAnonymizeErrors
                        .Append(customerInfo)
                        .Append("<br />");

                        SysLog.LogException(result.Error, MessageTypeEnum.GeneralException, MessageSeverityEnum.Error);

                        continue;
                    }

                    if (!result.Value)
                    {
                        customerAnonymizeErrors
                        .Append(customerInfo)
                        .Append("<br />");

                        continue;
                    }

                    customerAnonymizeSuccesses
                    .Append(customerInfo)
                    .Append("<br />");

                    foreach (var orderResult in DataRetentionService.AnonymizeOrders(customerId))
                    {
                        if (!orderResult.Success)
                        {
                            orderAnonymizeErrors
                            .Append(customerInfo)
                            .Append("<br />");

                            SysLog.LogException(result.Error, MessageTypeEnum.GeneralException, MessageSeverityEnum.Error);

                            continue;
                        }

                        if (!orderResult.Value)
                        {
                            orderAnonymizeErrors
                            .Append(customerInfo)
                            .Append("<br />");

                            continue;
                        }

                        orderAnonymizeSuccesses
                        .Append(customerInfo)
                        .Append("<br />");
                    }
                }
            }

            if (customerAnonymizeSuccesses.Length > 0)
            {
                AlertMessageDisplay.PushAlertMessage(
                    string.Concat("The following customer(s) have been anonymized:<br />", customerAnonymizeSuccesses.ToString()),
                    AlertMessage.AlertType.Success);
            }

            if (cannotBeAnonymized.Length > 0)
            {
                AlertMessageDisplay.PushAlertMessage(
                    string.Concat("The following customer(s) and their orders could not be anonymized:<br />", cannotBeAnonymized.ToString()),
                    AlertMessage.AlertType.Error);
            }

            if (customerAnonymizeErrors.Length > 0)
            {
                AlertMessageDisplay.PushAlertMessage(
                    string.Concat("The following customer(s) could not be anonymized:<br />", customerAnonymizeErrors.ToString()),
                    AlertMessage.AlertType.Error);
            }

            if (customerDeleteSuccesses.Length > 0)
            {
                AlertMessageDisplay.PushAlertMessage(
                    string.Concat("The following customer(s) have been removed:<br />", customerDeleteSuccesses.ToString()),
                    AlertMessage.AlertType.Success);
            }

            if (cannotBeDeleted.Length > 0)
            {
                AlertMessageDisplay.PushAlertMessage(
                    string.Concat("The following customer(s) and their orders could not be removed:<br />", cannotBeDeleted.ToString()),
                    AlertMessage.AlertType.Error);
            }

            if (customerDeleteErrors.Length > 0)
            {
                AlertMessageDisplay.PushAlertMessage(
                    string.Concat("The following customer(s) could not be removed:<br />", customerDeleteErrors.ToString()),
                    AlertMessage.AlertType.Error);
            }

            if (orderAnonymizeSuccesses.Length > 0)
            {
                AlertMessageDisplay.PushAlertMessage(
                    string.Concat("The following customer(s) orders have been anonymized:<br />", orderAnonymizeSuccesses.ToString()),
                    AlertMessage.AlertType.Success);
            }

            if (orderAnonymizeErrors.Length > 0)
            {
                AlertMessageDisplay.PushAlertMessage(
                    string.Concat("The following customer's orders could not be anonymized:<br />", orderAnonymizeErrors.ToString()),
                    AlertMessage.AlertType.Error);
            }

            FilteredListing.Rebind();
        }