Beispiel #1
0
        public bool AddOrChangeUser(User user)
        {
            if (user == null)
            {
                return(false);
            }

            try
            {
                Session.BeginTransaction();
                try
                {
                    //first handle the image
                    if (user.Photo != null && user.Photo.ID <= 0)
                    {
                        //save photo
                        var   gnrlManager = new GeneralManager();
                        Photo photo       = user.Photo;
                        gnrlManager.SavePhotoToServer(ref photo);
                        user.Photo = photo;
                    }

                    //user
                    Session.SaveOrUpdate(user);

                    //if the password has a value it means it was set somewhere, so lets push the change to the database through the stored procedure
                    if (!String.IsNullOrWhiteSpace(user.Password))
                    {
                        Session.CreateSQLQuery("exec Users.AssignPassword :UserID, :Password")
                        .SetParameter("UserID", user.ID)
                        .SetParameter("Password", user.Password)
                        .ExecuteUpdate();
                    }

                    Session.Transaction.Commit();
                }
                catch
                {
                    Session.Transaction.Rollback();
                    throw;
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"User_Manager_Failed_Save_User : {ex.Message} {ex?.InnerException?.Message??""}", ex);
            }

            return(true);
        }
Beispiel #2
0
        public void ChangeTasks(Task task)
        {
            if (task == null)
            {
                return;
            }

            var connection = Session.Connection;

            using (var trans = Session.BeginTransaction())
            {
                try
                {
                    using (var command = Session.Connection.CreateCommand())
                    {
                        trans.Enlist(command);
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandText = "Tasks.ChangeTask";

                        var taskID = command.CreateParameter();
                        taskID.ParameterName = "TaskID";
                        taskID.Value         = task.ID;
                        command.Parameters.Add(taskID);

                        var changed_parameter = command.CreateParameter();
                        changed_parameter.ParameterName = "Description";
                        changed_parameter.Value         = task.Description;
                        command.Parameters.Add(changed_parameter);

                        changed_parameter = command.CreateParameter();
                        changed_parameter.ParameterName = "Urguent";
                        changed_parameter.Value         = task.IsUrguent;
                        command.Parameters.Add(changed_parameter);

                        //we need SqlParameter sadly...
                        //but the casting seems to work fine since our connection is probably SQL, so meh :P
                        changed_parameter = command.CreateParameter();
                        var parReporterInfo = (SqlParameter)command.CreateParameter();
                        parReporterInfo.ParameterName = "reporterInfo";
                        parReporterInfo.Value         = User.GenerateUserTable(
                            task.ReporterUser?.ID ?? 0,
                            task.Reporter);
                        parReporterInfo.SqlDbType = SqlDbType.Structured;
                        parReporterInfo.TypeName  = "Users.UserInfo";
                        changed_parameter         = parReporterInfo;
                        command.Parameters.Add(changed_parameter);

                        changed_parameter = command.CreateParameter();
                        changed_parameter.ParameterName = "TechnicianID";
                        changed_parameter.Value         = (task.TechnicianID == 0) ? (object)DBNull.Value : (object)task.TechnicianID;
                        command.Parameters.Add(changed_parameter);

                        changed_parameter = command.CreateParameter();
                        changed_parameter.ParameterName = "MachineID";
                        changed_parameter.Value         = ((task.Device?.ID ?? 0) == 0) ? (object)DBNull.Value : (object)task.Device?.ID;
                        command.Parameters.Add(changed_parameter);

                        changed_parameter = command.CreateParameter();
                        changed_parameter.ParameterName = "StatusID";
                        changed_parameter.Value         = task.StatusID;
                        command.Parameters.Add(changed_parameter);

                        changed_parameter = command.CreateParameter();
                        changed_parameter.ParameterName = "locationID";
                        changed_parameter.Value         = task.Location.ID;
                        command.Parameters.Add(changed_parameter);

                        changed_parameter = command.CreateParameter();
                        changed_parameter.ParameterName = "ActivationDate";
                        changed_parameter.Value         = (object)task.RepeatingInfo?.ActivationDate ?? (object)DBNull.Value;
                        command.Parameters.Add(changed_parameter);

                        changed_parameter = command.CreateParameter();
                        changed_parameter.ParameterName = "DaysToRepeat";
                        changed_parameter.Value         = (object)task.RepeatingInfo?.Interval ?? (object)DBNull.Value;
                        command.Parameters.Add(changed_parameter);

                        if (command.Parameters.Count < 1 || command.ExecuteNonQuery() == 0)
                        {
                            trans.Rollback();
                            return;
                        }

                        foreach (var note in task.Notes)
                        {
                            if (note.ID > 0)
                            {
                                continue;
                            }

                            using (var noteCommand = connection.CreateCommand())
                            {
                                trans.Enlist(noteCommand);
                                noteCommand.CommandType = CommandType.StoredProcedure;
                                noteCommand.CommandText = "Tasks.AddNote";

                                var paraNote = noteCommand.CreateParameter();
                                paraNote.ParameterName = "Note";
                                paraNote.Value         = note.Text;
                                noteCommand.Parameters.Add(paraNote);

                                var parTaskID = noteCommand.CreateParameter();
                                parTaskID.ParameterName = "TaskID";
                                parTaskID.Value         = task.ID;
                                noteCommand.Parameters.Add(parTaskID);

                                if (noteCommand.ExecuteNonQuery() == 0)
                                {
                                    continue;
                                }
                            }
                        }

                        //assign new photos
                        foreach (var photo in task.Photos)
                        {
                            if (photo.ID > 0)
                            {
                                continue;
                            }

                            var gnrlManager = new GeneralManager();
                            var item        = photo;

                            if (!gnrlManager.SavePhotoToServer(ref item) && item.ID <= 0)
                            {
                                continue;
                            }

                            using (var photoCommand = connection.CreateCommand())
                            {
                                trans.Enlist(photoCommand);
                                photoCommand.CommandType = CommandType.StoredProcedure;
                                photoCommand.CommandText = "Tasks.AssignPhoto";

                                var parTaskID = photoCommand.CreateParameter();
                                parTaskID.ParameterName = "taskID";
                                parTaskID.Value         = task.ID;
                                photoCommand.Parameters.Add(parTaskID);

                                var parPhoto = photoCommand.CreateParameter();
                                parPhoto.ParameterName = "photoName";
                                parPhoto.Value         = item.FileName;
                                photoCommand.Parameters.Add(parPhoto);

                                if (photoCommand.ExecuteNonQuery() == 0)
                                {
                                    continue;
                                }
                            }
                        }
                        trans.Commit();
                    }
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
            }
        }
Beispiel #3
0
        public void AddTasks(Task task)
        {
            if (task == null)
            {
                return;
            }

            var con = Session.Connection;
            int errors;
            int newTaskID;

            if (!(con is SqlConnection))
            {
                throw new Exception("TaskManager.AddTasks : function requires SQLconnection, which was not retrieved");
            }
            var connection = con as SqlConnection;

            var transaction = Session.BeginTransaction();

            errors    = 0;
            newTaskID = 0;
            try
            {
                //add task
                using (var commandAddTask = connection.CreateCommand())
                {
                    transaction.Enlist(commandAddTask);
                    commandAddTask.CommandType = CommandType.StoredProcedure;
                    var paraDescription = commandAddTask.CreateParameter();
                    paraDescription.ParameterName = "Description";
                    paraDescription.Value         = task.Description;
                    paraDescription.DbType        = DbType.String;
                    paraDescription.Size          = task.Description.Length;
                    commandAddTask.Parameters.Add(paraDescription);

                    var parUrguent = commandAddTask.CreateParameter();
                    parUrguent.ParameterName = "Urguent";
                    parUrguent.Value         = task.IsUrguent;
                    parUrguent.DbType        = DbType.Boolean;
                    commandAddTask.Parameters.Add(parUrguent);

                    SqlParameter parReporterInfo;
                    parReporterInfo = commandAddTask.Parameters.AddWithValue("reporterInfo",
                                                                             User.GenerateUserTable(
                                                                                 task.ReporterUser != null ? task.ReporterUser.ID : 0,
                                                                                 task.Reporter)
                                                                             );
                    parReporterInfo.SqlDbType = SqlDbType.Structured;
                    parReporterInfo.TypeName  = "Users.UserInfo";

                    var parLocation = commandAddTask.CreateParameter();
                    parLocation.Value         = task.Location.ID;
                    parLocation.ParameterName = "locationID";
                    commandAddTask.Parameters.Add(parLocation);

                    var parMachine = commandAddTask.CreateParameter();
                    parMachine.ParameterName = "MachineID";
                    parMachine.Value         = ((task.Device?.ID ?? 0) == 0) ? (object)DBNull.Value : task.Device.ID;
                    commandAddTask.Parameters.Add(parMachine);

                    var parStatus = commandAddTask.CreateParameter();
                    parStatus.ParameterName = "StatusID";
                    parStatus.Value         = task.StatusID;
                    commandAddTask.Parameters.Add(parStatus);

                    var parTechnician = commandAddTask.CreateParameter();
                    parTechnician.ParameterName = "Technician";
                    parMachine.Value            = ((task.TechnicianID ?? 0) == 0) ? (object)DBNull.Value : task.Device.ID;
                    commandAddTask.Parameters.Add(parTechnician);

                    var TaskID = commandAddTask.CreateParameter();
                    TaskID.Direction = ParameterDirection.ReturnValue;
                    commandAddTask.Parameters.Add(TaskID);

                    var TypeID = task.TypeID;
                    if ((TypeID == Convert.ToInt32(TaskTypes.RepeatingTask)) & (task.RepeatingInfo != null))
                    {
                        commandAddTask.CommandText = "Tasks.CreateRepeatingTask";

                        var parActivationDate = commandAddTask.CreateParameter();
                        parActivationDate.ParameterName = "ActivationDate";
                        parActivationDate.Value         = task.RepeatingInfo.ActivationDate;
                        parActivationDate.DbType        = DbType.DateTime;
                        commandAddTask.Parameters.Add(parActivationDate);

                        var parInterval = commandAddTask.CreateParameter();
                        parInterval.ParameterName = "DaysToRepeat";
                        parInterval.Value         = task.RepeatingInfo.Interval;
                        commandAddTask.Parameters.Add(parInterval);
                    }
                    else
                    {
                        commandAddTask.CommandText = "Tasks.CreateNormalTask";
                    }

                    if (commandAddTask.ExecuteNonQuery() == 0)
                    {
                        //Drop this shit and go to the next task
                        Session.Transaction.Rollback();
                        return;
                    }

                    newTaskID = (int)TaskID.Value;

                    if (newTaskID == 0)
                    {
                        transaction.Rollback();
                        return;
                    }
                }

                //task created, add notes && photo
                foreach (var note in task.Notes)
                {
                    using (var command = connection.CreateCommand())
                    {
                        transaction.Enlist(command);
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandText = "Tasks.AddNote";

                        var paraNote = command.CreateParameter();
                        paraNote.ParameterName = "Note";
                        paraNote.Value         = note.Text;
                        command.Parameters.Add(paraNote);

                        var parTaskID = command.CreateParameter();
                        parTaskID.ParameterName = "TaskID";
                        parTaskID.Value         = newTaskID;
                        command.Parameters.Add(parTaskID);

                        if (command.ExecuteNonQuery() == 0)
                        {
                            errors = 1;
                            break;
                        }
                    }
                }

                if (errors != 0)
                {
                    transaction.Rollback();
                    return;
                }

                var gnrlManager = new GeneralManager();
                //no errors there, so lets add the photos to the DB...
                foreach (var photo in task.Photos)
                {
                    photo.FileName = string.Format("Task_{0}_Photo_{1}", newTaskID,
                                                   DateTime.Now.ToString("yyyyMMdd_HH_mm_ss"));

                    var item = photo;
                    if (!gnrlManager.SavePhotoToServer(ref item))
                    {
                        continue;
                    }

                    var photoID = item.ID;
                    if (photoID <= 0)
                    {
                        continue;
                    }

                    using (var command = connection.CreateCommand())
                    {
                        transaction.Enlist(command);
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandText = "Tasks.AssignPhoto";

                        var parTaskID = command.CreateParameter();
                        parTaskID.ParameterName = "taskID";
                        parTaskID.Value         = newTaskID;
                        command.Parameters.Add(parTaskID);

                        var parPhoto = command.CreateParameter();
                        parPhoto.ParameterName = "photoName";
                        parPhoto.Value         = item.FileName;
                        command.Parameters.Add(parPhoto);

                        if (command.ExecuteNonQuery() == 0)
                        {
                            errors = 1;
                            break;
                        }
                    }
                }

                if (errors != 0)
                {
                    transaction.Rollback();
                    return;
                }
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw ex;
            }

            transaction.Commit();
        }