/Docs/07-Implementacion/Source/trunk/EDUAR_Regular/EDUAR/EDUAR_UI/Private/Account/RegisterUser.aspx.cs

http://blpm.googlecode.com/ · C# · 453 lines · 330 code · 30 blank · 93 comment · 19 complexity · 599864e22fca27550c8913373096d162 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web.UI;
  4. using System.Web.UI.WebControls;
  5. using EDUAR_BusinessLogic.Common;
  6. using EDUAR_BusinessLogic.Security;
  7. using EDUAR_BusinessLogic.Shared;
  8. using EDUAR_Entities;
  9. using EDUAR_Entities.Security;
  10. using EDUAR_UI.Shared;
  11. using EDUAR_UI.Utilidades;
  12. using EDUAR_Utility.Constantes;
  13. using EDUAR_Utility.Enumeraciones;
  14. using EDUAR_Utility.Utilidades;
  15. namespace EDUAR_UI
  16. {
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. public partial class RegisterUser : EDUARBasePage
  21. {
  22. #region --[Atributos]--
  23. private BLSeguridad atrBLSeguridad;
  24. private BLPersona atrBLPersona;
  25. #endregion
  26. #region --[Propiedades]--
  27. /// <summary>
  28. /// Propiedad que contiene el objeto seguridad que devuelve la consulta a la Capa de Negocio.
  29. /// </summary>
  30. public DTSeguridad propSeguridad
  31. {
  32. get
  33. {
  34. if (ViewState["objSeguridad"] == null)
  35. return null;
  36. return (DTSeguridad)ViewState["objSeguridad"];
  37. }
  38. set { ViewState["objSeguridad"] = value; }
  39. }
  40. /// <summary>
  41. /// Gets or sets the prop persona.
  42. /// </summary>
  43. /// <value>
  44. /// The prop persona.
  45. /// </value>
  46. public Persona propPersona
  47. {
  48. get
  49. {
  50. if (ViewState["propPersona"] == null)
  51. return new Persona();
  52. return (Persona)ViewState["propPersona"];
  53. }
  54. set { ViewState["propPersona"] = value; }
  55. }
  56. /// <summary>
  57. /// Gets or sets the prop lista personas.
  58. /// </summary>
  59. /// <value>
  60. /// The prop lista personas.
  61. /// </value>
  62. public List<Persona> propListaPersonas
  63. {
  64. get
  65. {
  66. if (ViewState["propListaPersonas"] == null)
  67. return new List<Persona>();
  68. return (List<Persona>)ViewState["propListaPersonas"];
  69. }
  70. set { ViewState["propListaPersonas"] = value; }
  71. }
  72. #endregion
  73. #region --[Eventos]--
  74. /// <summary>
  75. /// Método que se ejecuta al dibujar los controles de la página.
  76. /// Se utiliza para gestionar las excepciones del método Page_Load().
  77. /// </summary>
  78. /// <param name="e"></param>
  79. protected override void OnPreRender(EventArgs e)
  80. {
  81. base.OnPreRender(e);
  82. if (AvisoMostrar)
  83. {
  84. AvisoMostrar = false;
  85. try
  86. {
  87. Master.ManageExceptions(AvisoExcepcion);
  88. }
  89. catch (Exception ex) { Master.ManageExceptions(ex); }
  90. }
  91. }
  92. /// <summary>
  93. /// Handles the Load event of the Page control.
  94. /// </summary>
  95. /// <param name="sender">The source of the event.</param>
  96. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  97. protected void Page_Load(object sender, EventArgs e)
  98. {
  99. try
  100. {
  101. Master.BotonAvisoAceptar += (VentanaAceptar);
  102. Master.BotonAvisoCancelar += (VentanaCancelar);
  103. if (!Page.IsPostBack)
  104. {
  105. propSeguridad = new DTSeguridad();
  106. atrBLSeguridad = new BLSeguridad(propSeguridad);
  107. propPersona = new Persona();
  108. propPersona.idTipoPersona = 1;
  109. CargarPresentacion();
  110. CargarCamposFiltros();
  111. CargarGrilla();
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. AvisoMostrar = true;
  117. AvisoExcepcion = ex;
  118. }
  119. }
  120. /// <summary>
  121. /// Ejecuta las acciones requeridas por el boton aceptar de la ventana de informacion.
  122. /// </summary>
  123. /// <param name="sender"></param>
  124. /// <param name="e"></param>
  125. void VentanaAceptar(object sender, EventArgs e)
  126. {
  127. try
  128. {
  129. switch (AccionPagina)
  130. {
  131. case enumAcciones.Guardar:
  132. GuardarUsuario();
  133. AccionPagina = enumAcciones.Limpiar;
  134. Master.MostrarMensaje(enumTipoVentanaInformacion.Satisfactorio.ToString(), UIConstantesGenerales.MensajeGuardadoOk, enumTipoVentanaInformacion.Satisfactorio);
  135. break;
  136. case enumAcciones.Salir:
  137. Response.Redirect("~/Default.aspx", false);
  138. break;
  139. case enumAcciones.Limpiar:
  140. propSeguridad = new DTSeguridad();
  141. atrBLSeguridad = new BLSeguridad(propSeguridad);
  142. propPersona = new Persona();
  143. propListaPersonas = null;
  144. propPersona.idTipoPersona = 1;
  145. LimpiarCampos();
  146. CargarPresentacion();
  147. //CargarCamposFiltros();
  148. CargarGrilla();
  149. break;
  150. default:
  151. break;
  152. }
  153. udpRoles.Update();
  154. udpGrilla.Update();
  155. }
  156. catch (Exception ex)
  157. {
  158. Master.ManageExceptions(ex);
  159. }
  160. }
  161. /// <summary>
  162. /// Ejecuta las acciones requeridas por el boton cancelar de la ventana de informacion.
  163. /// </summary>
  164. /// <param name="sender"></param>
  165. /// <param name="e"></param>
  166. void VentanaCancelar(object sender, EventArgs e)
  167. {
  168. try
  169. {
  170. //Response.Redirect("~/Default.aspx", false);
  171. }
  172. catch (Exception ex)
  173. {
  174. Master.ManageExceptions(ex);
  175. }
  176. }
  177. /// <summary>
  178. /// Handles the Click event of the btnGuardar control.
  179. /// </summary>
  180. /// <param name="sender">The source of the event.</param>
  181. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  182. protected void btnBuscar_Click(object sender, EventArgs e)
  183. {
  184. try
  185. {
  186. BuscarFiltrando();
  187. }
  188. catch (Exception ex)
  189. {
  190. Master.ManageExceptions(ex);
  191. }
  192. }
  193. /// <summary>
  194. /// Handles the Click event of the btnGuardar control.
  195. /// </summary>
  196. /// <param name="sender">The source of the event.</param>
  197. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  198. protected void btnGuardar_Click(object sender, EventArgs e)
  199. {
  200. try
  201. {
  202. if (ValidarPagina())
  203. {
  204. if (Page.IsValid)
  205. {
  206. AccionPagina = enumAcciones.Guardar;
  207. Master.MostrarMensaje(enumTipoVentanaInformacion.Confirmación.ToString(), UIConstantesGenerales.MensajeConfirmarCambios, enumTipoVentanaInformacion.Confirmación);
  208. }
  209. else
  210. {
  211. AccionPagina = enumAcciones.Limpiar;
  212. Master.MostrarMensaje(enumTipoVentanaInformacion.Advertencia.ToString(), UIConstantesGenerales.MensajeDatosRequeridos, enumTipoVentanaInformacion.Advertencia);
  213. }
  214. }
  215. }
  216. catch (Exception ex)
  217. {
  218. Master.ManageExceptions(ex);
  219. }
  220. }
  221. /// <summary>
  222. /// Handles the Click event of the btnVolver control.
  223. /// </summary>
  224. /// <param name="sender">The source of the event.</param>
  225. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  226. protected void btnVolver_Click(object sender, EventArgs e)
  227. {
  228. try
  229. {
  230. CargarPresentacion();
  231. }
  232. catch (Exception ex)
  233. {
  234. Master.ManageExceptions(ex);
  235. }
  236. }
  237. /// <summary>
  238. /// Método que se llama al hacer click sobre las acciones de la grilla
  239. /// </summary>
  240. /// <param name="sender">The source of the event.</param>
  241. /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCommandEventArgs"/> instance containing the event data.</param>
  242. protected void gvwUsuarios_RowCommand(object sender, GridViewCommandEventArgs e)
  243. {
  244. try
  245. {
  246. switch (e.CommandName)
  247. {
  248. case "Nuevo":
  249. propPersona = new Persona();
  250. propPersona.idPersona = Convert.ToInt32(e.CommandArgument.ToString());
  251. AccionPagina = enumAcciones.Modificar;
  252. CargarPersona();
  253. btnGuardar.Visible = true;
  254. btnVolver.Visible = true;
  255. btnBuscar.Visible = false;
  256. udpRoles.Visible = true;
  257. udpFiltrosBusqueda.Visible = false;
  258. gvwUsuarios.Visible = false;
  259. udpRoles.Update();
  260. udpFiltros.Update();
  261. break;
  262. }
  263. }
  264. catch (Exception ex)
  265. {
  266. Master.ManageExceptions(ex);
  267. }
  268. }
  269. /// <summary>
  270. /// Handles the PageIndexChanging event of the gvwUsuarios control.
  271. /// </summary>
  272. /// <param name="sender">The source of the event.</param>
  273. /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
  274. protected void gvwUsuarios_PageIndexChanging(object sender, GridViewPageEventArgs e)
  275. {
  276. try
  277. {
  278. gvwUsuarios.PageIndex = e.NewPageIndex;
  279. CargarGrilla();
  280. }
  281. catch (Exception ex) { Master.ManageExceptions(ex); }
  282. }
  283. #endregion
  284. #region --[Métodos Privados]--
  285. /// <summary>
  286. /// Cargars the presentacion.
  287. /// </summary>
  288. private void CargarPresentacion()
  289. {
  290. btnBuscar.Visible = true;
  291. btnGuardar.Visible = false;
  292. btnVolver.Visible = false;
  293. udpFiltrosBusqueda.Visible = true;
  294. gvwUsuarios.Visible = true;
  295. udpRoles.Visible = false;
  296. udpGrilla.Update();
  297. udpFiltros.Update();
  298. }
  299. /// <summary>
  300. /// Cargars the campos filtros.
  301. /// </summary>
  302. private void CargarCamposFiltros()
  303. {
  304. atrBLSeguridad.GetRoles();
  305. UIUtilidades.BindCombo<DTRol>(ddlListRoles, atrBLSeguridad.Data.ListaRoles, "NombreCorto", "Nombre", true);
  306. UIUtilidades.BindComboTipoPersona(ddlTipoUsuario);
  307. }
  308. /// <summary>
  309. /// Buscars the filtrando.
  310. /// </summary>
  311. private void BuscarFiltrando()
  312. {
  313. Persona objPersona = new Persona();
  314. objPersona.nombre = txtNombreBusqueda.Text;
  315. objPersona.apellido = txtApellidoBusqueda.Text;
  316. objPersona.activo = chkActivoBusqueda.Checked;
  317. objPersona.username = null;
  318. objPersona.idTipoPersona = Convert.ToInt32(ddlTipoUsuario.SelectedValue);
  319. BuscarPersona(objPersona);
  320. }
  321. /// <summary>
  322. /// Obteners the datos.
  323. /// </summary>
  324. private void BuscarPersona(Persona objPersona)
  325. {
  326. atrBLPersona = new BLPersona(objPersona);
  327. propListaPersonas = atrBLPersona.GetPersonas(objPersona);
  328. CargarGrilla();
  329. }
  330. /// <summary>
  331. /// Cargars the grilla.
  332. /// </summary>
  333. private void CargarGrilla()
  334. {
  335. gvwUsuarios.DataSource = propListaPersonas;
  336. gvwUsuarios.DataBind();
  337. gvwUsuarios.SelectedIndex = -1;
  338. udpRoles.Visible = false;
  339. udpGrilla.Update();
  340. }
  341. /// <summary>
  342. /// Cargars the usuario.
  343. /// </summary>
  344. private void CargarPersona()
  345. {
  346. LimpiarCampos();
  347. atrBLPersona = new BLPersona();
  348. //propPersona.username = string.Empty;
  349. atrBLPersona.Data = propPersona;
  350. atrBLPersona.GetById();
  351. Persona objPersona = atrBLPersona.Data;
  352. lblNombreApellido.Text = objPersona.nombre + " " + objPersona.apellido;
  353. txtUserName.Text = string.Empty;
  354. txtEmailUsuario.Text = objPersona.email;
  355. lblPreguntaUsuario.Text = BLConfiguracionGlobal.ObtenerConfiguracion(enumConfiguraciones.PreguntaDefault);
  356. lblRespuestaUsuario.Text = objPersona.numeroDocumento.ToString();
  357. udpRoles.Visible = true;
  358. udpRoles.Update();
  359. }
  360. /// <summary>
  361. /// Limpiars the campos.
  362. /// </summary>
  363. private void LimpiarCampos()
  364. {
  365. txtUserName.Text = string.Empty;
  366. txtNombreBusqueda.Text = string.Empty;
  367. txtApellidoBusqueda.Text = string.Empty;
  368. txtEmailUsuario.Text = string.Empty;
  369. ddlListRoles.SelectedValue = "-1"; //SELECCIONE
  370. ddlTipoUsuario.SelectedValue = "0"; //TODOS
  371. }
  372. /// <summary>
  373. /// Guardars the usuario.
  374. /// </summary>
  375. private void GuardarUsuario()
  376. {
  377. DTUsuario objUsuario = new DTUsuario();
  378. objUsuario.Nombre = txtUserName.Text;
  379. objUsuario.Email = txtEmailUsuario.Text;
  380. objUsuario.Aprobado = chkHabilitado.Checked;
  381. objUsuario.PaswordPregunta = lblPreguntaUsuario.Text;
  382. objUsuario.PaswordRespuesta = lblRespuestaUsuario.Text;
  383. objUsuario.EsUsuarioInicial = true;
  384. objUsuario.ListaRoles = new List<DTRol>();
  385. objUsuario.ListaRoles.Add(new DTRol() { NombreCorto = ddlListRoles.SelectedValue, Nombre = ddlListRoles.SelectedItem.Text });
  386. DTSeguridad objSeguridad = new DTSeguridad();
  387. objSeguridad.Usuario = objUsuario;
  388. atrBLSeguridad = new BLSeguridad(objSeguridad);
  389. atrBLSeguridad.CrearUsuario();
  390. Persona objPersona = new Persona();
  391. atrBLPersona = new BLPersona(propPersona);
  392. atrBLPersona.GetById();
  393. atrBLPersona.Data.username = objUsuario.Nombre;
  394. atrBLPersona.Save();
  395. }
  396. /// <summary>
  397. /// Validars the pagina.
  398. /// </summary>
  399. private bool ValidarPagina()
  400. {
  401. if (txtUserName.Text.Trim().Length == 0)
  402. {
  403. AccionPagina = enumAcciones.Limpiar;
  404. Master.MostrarMensaje(enumTipoVentanaInformacion.Advertencia.ToString(), UIConstantesGenerales.MensajeDatosRequeridos, enumTipoVentanaInformacion.Advertencia);
  405. return false;
  406. }
  407. if (!EDUARUtilidades.EsEmailValido(txtEmailUsuario.Text.Trim()))
  408. {
  409. AccionPagina = enumAcciones.Limpiar;
  410. Master.MostrarMensaje(enumTipoVentanaInformacion.Advertencia.ToString(), UIConstantesGenerales.MensajeDatosRequeridos, enumTipoVentanaInformacion.Advertencia);
  411. return false;
  412. }
  413. if (ddlListRoles.SelectedValue == "0" || ddlListRoles.SelectedValue == "-1")
  414. {
  415. AccionPagina = enumAcciones.Limpiar;
  416. Master.MostrarMensaje(enumTipoVentanaInformacion.Advertencia.ToString(), UIConstantesGenerales.MensajeDatosRequeridos, enumTipoVentanaInformacion.Advertencia);
  417. return false;
  418. }
  419. return true;
  420. }
  421. #endregion
  422. }
  423. }