public static void ValidaTieneLlavePrimariaCorrecta <T>(T objeto) { //se obtienes los valores de la clave primaria object[] strLenAttr2 = objeto.GetType().GetCustomAttributes(true); //se obtiene el idFormado, para omitirlo de la validacion ANC_IDFormado atributoID = typeof(T).GetCustomAttribute <ANC_IDFormado>(true); foreach (object atributo in strLenAttr2) { if (atributo is ANC_Identificador) { if (atributoID != null && (atributo as ANC_Identificador).Columna.Equals(atributoID.Columna)) { //se omite, porque esta propiedad se formara con las idFormadoParte. continue; } String columna = (atributo as ANC_Identificador).Columna; String propiedad = (atributo as ANC_Identificador).Propiedad; PropertyInfo pi = objeto.GetType().GetProperty(propiedad, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (pi.GetValue(objeto) == null) { throw new EX_IdentificadorIncompletoException(objeto, "La propiedad '" + propiedad + "' debe tener un valor al ser parte del identificador.", propiedad); } } } }
public static String ObtenConsecutivo <T>() where T : CLASE_BASE { ANC_Tabla nombreTabla = typeof(T).GetCustomAttribute <ANC_Tabla>(true); ANC_IDFormado campoId = typeof(T).GetCustomAttribute <ANC_IDFormado>(true); String str = "SELECT MAX(" + campoId.Columna + ")+1 FROM " + nombreTabla.Nombre; return(str); }
public static String ObtenConsecutivo <T>(String idFormadoParcial) where T : CLASE_BASE { ANC_Tabla nombreTabla = typeof(T).GetCustomAttribute <ANC_Tabla>(true); ANC_IDFormado campoId = typeof(T).GetCustomAttribute <ANC_IDFormado>(true); String str = "SELECT MAX(REPLACE('" + campoId.Columna + "','" + idFormadoParcial + "'))+1 FROM " + nombreTabla.Nombre + " WHERE " + campoId.Columna + " LIKE '" + idFormadoParcial + "%'"; return(str); }
public static String CrearIDFormadoParcial <T>(T obj) { //obtenemos el nombre de la columna que tiene el ID Formado ANC_IDFormado nombreTabla = obj.GetType().GetCustomAttribute <ANC_IDFormado>(true); String id = ""; if (nombreTabla == null) { return(""); //throw new Exception("La entidad de tipo '" + obj.GetType().Name + "' no tiene definida una columna ID con la anotación 'ANC_IDFormado'."); } List <ANC_IDFormadoParte> atributosIdFormado = obj.GetType().GetCustomAttributes <ANC_IDFormadoParte>(true).ToList().OrderBy(x => x.Orden).ToList(); if (atributosIdFormado == null || atributosIdFormado.Count == 0) { //solo es un consecutivo, y se acumula } else { //se forma de acuerdo a las partes consultadas foreach (ANC_IDFormadoParte atributo in atributosIdFormado) { if (atributo.Propiedad != null) { object valor = obj.GetType().GetProperty(atributo.Propiedad, BindingFlags.Instance | BindingFlags.Public).GetValue(obj); if (valor == null) { throw new Exception("La propiedad '" + atributo.Propiedad + "' es parte del ID, debe ser diferente de null."); } Int64 valorInt64 = Convert.ToInt64(valor); String formato = CrearFormatoToString(atributo.Digitos); id += valorInt64.ToString(formato); } } } return(id); }
private void CrearID <T>(T obj) where T : CLASE_BASE { try { String idParcial = GeneracionID.CrearIDFormadoParcial <T>(obj); if (idParcial.Equals("")) { //si el idParcial es vacio, entonces la tabla no lo requiere ya que podría ser un detalle //solo se concluye el algoritmo return; } ANC_IDFormado atributoID = typeof(T).GetCustomAttribute <ANC_IDFormado>(true); //se valida que solo sea un ID, sin q se componga de otras columnas if (typeof(T).GetCustomAttributes <ANC_IDFormadoParte>(true).ToList().Count == 0) { //si no trae partes, se genera un consecutivo String queryObtenSiguienteFolio = Estructura.ObtenConsecutivo <T>(); DataTable dtValor = cliente.EjecutarSelect(queryObtenSiguienteFolio); if (dtValor.Rows.Count > 0 && dtValor.Rows[0][0] != DBNull.Value) { Int64 valor = Convert.ToInt64(dtValor.Rows[0][0]); idParcial += valor; } else { Int64 valor = 1; idParcial += valor; } } else { ANC_IDFormadoParte atributoConsecutivo = typeof(T).GetCustomAttributes <ANC_IDFormadoParte>(true).ToList().Where(x => x.EsConsecutivo).FirstOrDefault(); //si el atributo consecutivo es null, el id queda hasta este punto y se asigna if (atributoConsecutivo == null) { obj.GetType().GetProperty(atributoID.Propiedad, BindingFlags.Instance | BindingFlags.Public).SetValue(obj, Convert.ToInt64(idParcial)); return; } String queryObtenSiguienteFolio = Estructura.ObtenConsecutivo <T>(idParcial); DataTable dtValor = cliente.EjecutarSelect(queryObtenSiguienteFolio); if (dtValor.Rows.Count > 0 && dtValor.Rows[0][0] != DBNull.Value) { Int64 valor = Convert.ToInt64(dtValor.Rows[0][0]); idParcial += valor.ToString(GeneracionID.CrearFormatoToString(atributoConsecutivo.Digitos)); } else { Int64 valor = 1; idParcial += valor.ToString(GeneracionID.CrearFormatoToString(atributoConsecutivo.Digitos)); } } obj.GetType().GetProperty(atributoID.Propiedad, BindingFlags.Instance | BindingFlags.Public).SetValue(obj, Convert.ToInt64(idParcial)); } catch (Exception ex) { throw ex; } }