PageRenderTime 552ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/AnalisisDeRiesgos/AnalisisDeRiesgos/Models/Utils.cs

#
C# | 287 lines | 236 code | 51 blank | 0 comment | 22 complexity | 20c5fd0681ac17489b1465e147b36d32 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using AnalisisDeRiesgos.Dal;
  8. using System.Web.Mvc;
  9. using System.Globalization;
  10. namespace AnalisisDeRiesgos.Models
  11. {
  12. public enum Privileges
  13. {
  14. Administrator = 0,
  15. GeneralSecurity = 1,
  16. AreaSecurity = 2,
  17. Employe = 3
  18. }
  19. public class Utils
  20. {
  21. public static CultureInfo culture = new CultureInfo("es-ES");
  22. public static string CalculateSHA1(object message)
  23. {
  24. SHA1 sha1 = new SHA1CryptoServiceProvider();
  25. byte []msg = Encoding.Default.GetBytes(message.ToString());
  26. return BitConverter.ToString(sha1.ComputeHash(msg)).ToString().Replace("-", "");
  27. }
  28. public static bool HasPrivilege(int CI, Privileges requiredPrivilege)
  29. {
  30. riskanalysisEntities entities = new riskanalysisEntities();
  31. var resp = from u in entities.users
  32. where u.CI == CI && u.enabled
  33. select u;
  34. if (resp.Count() <= 0)
  35. return false;
  36. user theUser = resp.First();
  37. return theUser.privilege <= Convert.ToInt32(requiredPrivilege);
  38. }
  39. public static bool MustHavePrivilege(int CI, Privileges requiredPrivilege)
  40. {
  41. riskanalysisEntities entities = new riskanalysisEntities();
  42. var resp = from u in entities.users
  43. where u.CI == CI && u.enabled
  44. select u;
  45. if (resp.Count() <= 0)
  46. return false;
  47. user theUser = resp.First();
  48. return theUser.privilege == Convert.ToInt32(requiredPrivilege);
  49. }
  50. public static List<string> Frequences = new List<string>();
  51. public static List<SelectListItem> GetFrequencyList()
  52. {
  53. List<SelectListItem> frequencyList = new List<SelectListItem>();
  54. Frequences = new List<string>();
  55. riskanalysisEntities entities = new riskanalysisEntities();
  56. var query = from f in entities.frequences
  57. select f;
  58. foreach (frequence f in query)
  59. Frequences.Add(f.name);
  60. for (int i = 0; i < Frequences.Count; i++)
  61. frequencyList.Add(new SelectListItem
  62. {
  63. Text = Frequences[i],
  64. Value = (i + 1).ToString()
  65. });
  66. return frequencyList;
  67. }
  68. public static List<string> Affecteds = new List<string>();
  69. public static List<SelectListItem> GetAffectedList()
  70. {
  71. List<SelectListItem> affectedList = new List<SelectListItem>();
  72. Affecteds = new List<string>();
  73. riskanalysisEntities entities = new riskanalysisEntities();
  74. var query = from a in entities.affecteds
  75. select a;
  76. foreach (affected a in query)
  77. Affecteds.Add(a.name);
  78. for (int i = 0; i < Affecteds.Count; i++)
  79. affectedList.Add(
  80. new SelectListItem
  81. {
  82. Text = Affecteds[i],
  83. Value = (i + 1).ToString()
  84. });
  85. return affectedList;
  86. }
  87. public static List<string> ControlExistance = new List<string>();
  88. public static List<SelectListItem> GetControlExistenceList()
  89. {
  90. List<SelectListItem> controlsList = new List<SelectListItem>();
  91. ControlExistance = new List<string>();
  92. riskanalysisEntities entities = new riskanalysisEntities();
  93. var query = from c in entities.controlexistances
  94. select c;
  95. foreach (controlexistance c in query)
  96. ControlExistance.Add(c.name);
  97. for (int i = 0; i < ControlExistance.Count; i++)
  98. {
  99. controlsList.Add(
  100. new SelectListItem
  101. {
  102. Text = ControlExistance[i],
  103. Value = (i + 1).ToString()
  104. });
  105. }
  106. return controlsList;
  107. }
  108. public static List<string> Severitys = new List<string>();
  109. public static List<SelectListItem> GetSeverityList()
  110. {
  111. List<SelectListItem> severityList = new List<SelectListItem>();
  112. Severitys = new List<string>();
  113. riskanalysisEntities entities = new riskanalysisEntities();
  114. var query = from s in entities.severities
  115. select s;
  116. foreach (severity s in query)
  117. Severitys.Add(s.name);
  118. for ( int i = 0; i < Severitys.Count; i++ )
  119. severityList.Add(
  120. new SelectListItem
  121. {
  122. Text = Severitys[i],
  123. Value = (i + 1).ToString()
  124. });
  125. return severityList;
  126. }
  127. public static Dictionary<int, string> KValuesDictionary = new Dictionary<int,string>();
  128. public static string[] KValuesText = { "0 No se hizo nada",
  129. "0.4 Se efectuaron medidas y se valido su efectividad",
  130. "0.7 Se ejecutaron medidas y controles requeridos",
  131. "1 Se tiene avances dentro del plazo establecido",
  132. "1.2 No se ejecuto ni programo las actividades en el plazo establecido" };
  133. public static int[] KValues = { 0, 4, 7, 10, 12 };
  134. public static List<SelectListItem> GetKValuesList()
  135. {
  136. List<SelectListItem> kValuesList = new List<SelectListItem>();
  137. KValuesDictionary = new Dictionary<int, string>();
  138. for (int i = 0; i < KValues.Length; i++)
  139. {
  140. KValuesDictionary.Add(KValues[i], KValuesText[i]);
  141. kValuesList.Add(
  142. new SelectListItem
  143. {
  144. Text = KValuesText[i],
  145. Value = KValues[i].ToString()
  146. });
  147. }
  148. return kValuesList;
  149. }
  150. public static List<SelectListItem> FillProcessList(int managementId)
  151. {
  152. List<SelectListItem> list = new List<SelectListItem>();
  153. riskanalysisEntities entities = new riskanalysisEntities();
  154. var queryProcesses = from p in entities.processes
  155. where p.enabled && p.managementId == managementId
  156. select p;
  157. if (queryProcesses.Count() < 1)
  158. return list;
  159. foreach (process item in queryProcesses)
  160. {
  161. list.Add(
  162. new SelectListItem
  163. {
  164. Text = item.name,
  165. Value = item.processId.ToString()
  166. });
  167. }
  168. return list;
  169. }
  170. public static List<SelectListItem> FillAreaList(int processId)
  171. {
  172. List<SelectListItem> list = new List<SelectListItem>();
  173. riskanalysisEntities entities = new riskanalysisEntities();
  174. var queryProcesses = from a in entities.areas
  175. where a.enabled && a.processId == processId
  176. select a;
  177. if (queryProcesses.Count() < 1)
  178. return list;
  179. foreach (area item in queryProcesses)
  180. {
  181. list.Add(
  182. new SelectListItem
  183. {
  184. Text = item.name,
  185. Value = item.areaId.ToString()
  186. });
  187. }
  188. return list;
  189. }
  190. public static List<SelectListItem> FillActivityList(int areaId)
  191. {
  192. List<SelectListItem> list = new List<SelectListItem>();
  193. riskanalysisEntities entities = new riskanalysisEntities();
  194. var queryProcesses = from a in entities.activities
  195. where a.enabled && a.areaId == areaId
  196. select a;
  197. if (queryProcesses.Count() < 1)
  198. return list;
  199. foreach (activity item in queryProcesses)
  200. {
  201. list.Add(
  202. new SelectListItem
  203. {
  204. Text = item.name,
  205. Value = item.activityId.ToString()
  206. });
  207. }
  208. return list;
  209. }
  210. public static List<SelectListItem> FillAvailableActionTypesList(int dangerId)
  211. {
  212. List<SelectListItem> list = new List<SelectListItem>();
  213. riskanalysisEntities entities = new riskanalysisEntities();
  214. var actualActivitys = from a in entities.actions
  215. where a.dangerId == dangerId
  216. select a.actiontype;
  217. var query = from a in entities.actiontypes
  218. select a;
  219. List<actiontype> listActions = new List<actiontype>(query.ToList());
  220. foreach (var item in actualActivitys)
  221. {
  222. listActions.Remove(item);
  223. }
  224. foreach (var item in listActions)
  225. {
  226. list.Add(
  227. new SelectListItem
  228. {
  229. Text = item.name,
  230. Value = item.actionTypeId.ToString()
  231. });
  232. }
  233. return list;
  234. }
  235. }
  236. }