override public void update(Cliente cliente) { RepositorioIdentidad repoIdentidad = new RepositorioIdentidad(); //AGREGO VALIDACIONES AL UPDATE if (repoIdentidad.yaExisteOtraIdentidadMismoMailOTipoYNumDoc(cliente.getIdentidad())) { throw new ElementoYaExisteException("Ya existe un cliente o un usuario con el mismo mail o tipo y numero de documento."); } if (this.exists(cliente)) { String connectionString = ConfigurationManager.AppSettings["BaseLocal"]; SqlConnection sqlConnection = new SqlConnection(connectionString); SqlCommand sqlCommand = new SqlCommand(); SqlDataReader reader; sqlCommand.CommandType = CommandType.Text; sqlCommand.Connection = sqlConnection; //PARAMETERS DE LA DIRECCION sqlCommand.Parameters.AddWithValue("@Pais", cliente.getIdentidad().getDireccion().getPais()); sqlCommand.Parameters.AddWithValue("@Ciudad", cliente.getIdentidad().getDireccion().getCiudad()); sqlCommand.Parameters.AddWithValue("@Calle", cliente.getIdentidad().getDireccion().getCalle()); sqlCommand.Parameters.AddWithValue("@NumeroCalle", cliente.getIdentidad().getDireccion().getNumeroCalle()); sqlCommand.Parameters.AddWithValue("@Piso", cliente.getIdentidad().getDireccion().getPiso()); sqlCommand.Parameters.AddWithValue("@Departamento", cliente.getIdentidad().getDireccion().getDepartamento()); sqlCommand.Parameters.AddWithValue("@idDireccion", cliente.getIdentidad().getDireccion().getIdDireccion()); //PARAMETERS DE LA IDENTIDAD sqlCommand.Parameters.AddWithValue("@TipoIdent", cliente.getIdentidad().getTipoIdentidad()); sqlCommand.Parameters.AddWithValue("@Nombre", cliente.getIdentidad().getNombre()); sqlCommand.Parameters.AddWithValue("@Apellido", cliente.getIdentidad().getApellido()); sqlCommand.Parameters.AddWithValue("@TipoDoc", cliente.getIdentidad().getTipoDocumento()); sqlCommand.Parameters.AddWithValue("@NroDoc", cliente.getIdentidad().getNumeroDocumento()); sqlCommand.Parameters.AddWithValue("@Mail", cliente.getIdentidad().getMail()); sqlCommand.Parameters.AddWithValue("@FecNac", cliente.getIdentidad().getFechaNacimiento()); sqlCommand.Parameters.AddWithValue("@Nacion", cliente.getIdentidad().getNacionalidad()); sqlCommand.Parameters.AddWithValue("@Tel", cliente.getIdentidad().getTelefono()); sqlCommand.Parameters.AddWithValue("@idIdentidad", cliente.getIdentidad().getIdIdentidad()); //PARAMETERS DEL CLIENTE ///////////////////////// //POR EL MOMENTO NO CONSIDERAMOS LAS RESERVAS EN EL CLIENTE EN EL UPDATE ///////////////////////// sqlCommand.Parameters.AddWithValue("@Activo", cliente.getActivo()); sqlCommand.Parameters.AddWithValue("@idCliente", cliente.getIdCliente()); StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append(@" BEGIN TRY BEGIN TRANSACTION UPDATE LOS_BORBOTONES.Direccion SET Pais = @Pais, Ciudad = @Ciudad, Calle = @Calle, NumeroCalle = @NumeroCalle, Piso = @Piso, Depto = @Departamento WHERE idDireccion = @idDireccion; UPDATE LOS_BORBOTONES.Identidad SET TipoIdentidad = @TipoIdent, Nombre = @Nombre, Apellido = @Apellido, TipoDocumento = @TipoDoc, NumeroDocumento = @NroDoc, Mail = @Mail, FechaNacimiento = @FecNac, Nacionalidad = @Nacion, Telefono = @Tel WHERE idIdentidad = @idIdentidad; UPDATE LOS_BORBOTONES.Cliente SET Activo = @Activo, idIdentidad = @idIdentidad, Inconsistente = 0 WHERE idCliente = @idCliente; COMMIT END TRY BEGIN CATCH ROLLBACK END CATCH "); //LA LOGICA EN PONER 'INCONSISTENTE = 0' EN TODOS LOS UPDATE //ES QUE POR GUI YO AVISO QUE EL CLIENTE ESTA INCONSISTENTE //SI EL USUARIO DEL SISTEMA DECIDE CONTINUAR EDITANDOLO DEBE GARANTIZAR QUE //VERIFICA LA IDENTIDAD DEL CLIENTE MEDIANTE DOCUMENTO Y VALIDA SU MAIL sqlCommand.CommandText = sqlBuilder.ToString(); sqlConnection.Open(); reader = sqlCommand.ExecuteReader(); sqlConnection.Close(); } else { throw new NoExisteIDException("No existe el cliente que intenta actualizar"); } }
override public void update(Usuario usuario) { RepositorioIdentidad repoIdentidad = new RepositorioIdentidad(); //AGREGO VALIDACIONES AL UPDATE if (repoIdentidad.yaExisteOtraIdentidadMismoMailOTipoYNumDoc(usuario.getIdentidad())) { throw new ElementoYaExisteException("Ya existe un cliente o un usuario con el mismo mail o tipo y numero de documento."); } /* * if (this.yaExisteMismoMailDistintoUsuario(usuario)) * { * throw new ElementoYaExisteException("Ya existe un usuario o cliente con el mismo mail."); * } * * if (this.yaExisteMismoTipoYDocDistintoUsuario(usuario)) * { * throw new ElementoYaExisteException("Ya existe un usuario o cliente con el mismo documento."); * } */ if (this.yaExisteMismoUsername(usuario)) { throw new ElementoYaExisteException("Ya existe un usuario con el mismo nombre de usuario."); } if (this.exists(usuario)) { String connectionString = ConfigurationManager.AppSettings["BaseLocal"]; SqlConnection sqlConnection = new SqlConnection(connectionString); SqlCommand sqlCommand = new SqlCommand(); SqlDataReader reader; sqlCommand.CommandType = CommandType.Text; sqlCommand.Connection = sqlConnection; //PARAMETERS DE LA DIRECCION sqlCommand.Parameters.AddWithValue("@Pais", usuario.getIdentidad().getDireccion().getPais()); sqlCommand.Parameters.AddWithValue("@Ciudad", usuario.getIdentidad().getDireccion().getCiudad()); sqlCommand.Parameters.AddWithValue("@Calle", usuario.getIdentidad().getDireccion().getCalle()); sqlCommand.Parameters.AddWithValue("@NumeroCalle", usuario.getIdentidad().getDireccion().getNumeroCalle()); sqlCommand.Parameters.AddWithValue("@Piso", usuario.getIdentidad().getDireccion().getPiso()); sqlCommand.Parameters.AddWithValue("@Departamento", usuario.getIdentidad().getDireccion().getDepartamento()); sqlCommand.Parameters.AddWithValue("@idDireccion", usuario.getIdentidad().getDireccion().getIdDireccion()); //PARAMETERS DE LA IDENTIDAD sqlCommand.Parameters.AddWithValue("@TipoIdent", usuario.getIdentidad().getTipoIdentidad()); sqlCommand.Parameters.AddWithValue("@Nombre", usuario.getIdentidad().getNombre()); sqlCommand.Parameters.AddWithValue("@Apellido", usuario.getIdentidad().getApellido()); sqlCommand.Parameters.AddWithValue("@TipoDoc", usuario.getIdentidad().getTipoDocumento()); sqlCommand.Parameters.AddWithValue("@NroDoc", usuario.getIdentidad().getNumeroDocumento()); sqlCommand.Parameters.AddWithValue("@Mail", usuario.getIdentidad().getMail()); sqlCommand.Parameters.AddWithValue("@FecNac", usuario.getIdentidad().getFechaNacimiento()); sqlCommand.Parameters.AddWithValue("@Nacion", usuario.getIdentidad().getNacionalidad()); sqlCommand.Parameters.AddWithValue("@Tel", usuario.getIdentidad().getTelefono()); sqlCommand.Parameters.AddWithValue("@idIdentidad", usuario.getIdentidad().getIdIdentidad()); //PARAMETERS DEL USUARIO sqlCommand.Parameters.AddWithValue("@Username", usuario.getUsername()); sqlCommand.Parameters.AddWithValue("@Activo", usuario.getActivo()); sqlCommand.Parameters.AddWithValue("@Password", usuario.getPassword()); //SI CAMBIO EL ESTADO DEL USUARIO //RESETEO LA CANTIDAD DE INTENTOS FALLIDOS Boolean usuarioActivoEnBase = this.getById(usuario.getIdUsuario()).getActivo(); Boolean usuarioActivoEnModelo = usuario.getActivo(); if (!usuarioActivoEnBase && usuarioActivoEnModelo) { sqlCommand.Parameters.AddWithValue("@IntentosFallidosLogin", 0); } else { sqlCommand.Parameters.AddWithValue("@IntentosFallidosLogin", usuario.getIntentosFallidosLogin()); } sqlCommand.Parameters.AddWithValue("@idUsuario", usuario.getIdUsuario()); //HABRÍA QUE ANALIZAR PROS Y CONTRAS DE ACTUALIZAR/CREAR UN USUARIO TODO EN LA MISMA CONSULTA COMO EN ESTE METODO //O ACTUALIZARLO/CREARLO POR SEPARADO CON LOS METODOS DEL REPOSITORIO (USANDO LOS METODOS DEL REPO HAY QUE DEFINIR COMO SE MANEJA EL ROLLBACK SI UN UPDATE O CREATE FALLA) StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.Append(@" BEGIN TRY BEGIN TRANSACTION UPDATE LOS_BORBOTONES.Direccion SET Pais = @Pais, Ciudad = @Ciudad, Calle = @Calle, NumeroCalle = @NumeroCalle, Piso = @Piso, Depto = @Departamento WHERE idDireccion = @idDireccion; UPDATE LOS_BORBOTONES.Identidad SET TipoIdentidad = @TipoIdent, Nombre = @Nombre, Apellido = @Apellido, TipoDocumento = @TipoDoc, NumeroDocumento = @NroDoc, Mail = @Mail, FechaNacimiento = @FecNac, Nacionalidad = @Nacion, Telefono = @Tel WHERE idIdentidad = @idIdentidad; UPDATE LOS_BORBOTONES.Usuario SET Username = @Username, Password = @Password, IntentosFallidosLogin = @IntentosFallidosLogin, Activo = @Activo, idIdentidad = @idIdentidad WHERE idUsuario = @idUsuario; "); //TENGO QUE BORRAR TODAS LAS RELACIONES QUE TENGO CON LOS ROLES sqlBuilder.Append("DELETE FROM LOS_BORBOTONES.Rol_X_Usuario WHERE idUsuario = @idUsuario;"); //TENGO QUE BORRAR TODAS LAS RELACIONES QUE TENGO CON LOS HOTELES sqlBuilder.Append("DELETE FROM LOS_BORBOTONES.Hotel_X_Usuario WHERE idUsuario = @idUsuario;"); //AGREGO DINAMICAMENTE LOS ROLES A LA CONSULTA int i = 1; foreach (Rol r in usuario.getRoles()) { String paramName = "@idRol" + i.ToString(); sqlBuilder.AppendFormat("INSERT INTO LOS_BORBOTONES.Rol_X_Usuario(idRol,idUsuario) VALUES ({0}, @idUsuario)", paramName); sqlCommand.Parameters.AddWithValue(paramName, r.getIdRol()); i++; } //AGREGO DINAMICAMENTE LOS HOTELES A LA CONSULTA int k = 1; foreach (Hotel h in usuario.getHoteles()) { String paramName = "@idHotel" + k.ToString(); sqlBuilder.AppendFormat("INSERT INTO LOS_BORBOTONES.Hotel_X_Usuario(idHotel,idUsuario) VALUES ({0}, @idUsuario)", paramName); sqlCommand.Parameters.AddWithValue(paramName, h.getIdHotel()); k++; } sqlBuilder.Append(@" COMMIT END TRY BEGIN CATCH ROLLBACK END CATCH "); sqlCommand.CommandText = sqlBuilder.ToString(); sqlConnection.Open(); reader = sqlCommand.ExecuteReader(); sqlConnection.Close(); } else { throw new NoExisteIDException("No existe el usuario que intenta actualizar"); } }