public async Task <IHttpActionResult> PutChannel(Guid id, Channel channel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != channel.Id) { return(BadRequest()); } db.Entry(channel).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ChannelExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <bool> AddRefreshToken(RefreshToken token) { var existingToken = _ctx.RefreshTokens.Where(r => r.Subject == token.Subject && r.ClientId == token.ClientId).SingleOrDefault(); if (existingToken != null) { var result = await RemoveRefreshToken(existingToken); } _ctx.RefreshTokens.Add(token); return(await _ctx.SaveChangesAsync() > 0); }
public async Task <IHttpActionResult> PutAlertSetting(Guid id, AlertSetting alertSetting) { int writeLevel = Util.GetResourcePermission("Master Data", Util.ReourceOperations.Write); if (writeLevel != 2) { ModelState.AddModelError("Access Level", "Unauthorized update access."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != alertSetting.Id) { return(BadRequest()); } DbEntityEntry entry = db.Entry(alertSetting); entry.State = EntityState.Modified; // Marking properties to update by compareing default object AlertSetting defaultSetting = new AlertSetting(); entry = Util.GetUpdatedProperties(defaultSetting, alertSetting, entry); try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AlertSettingExists(alertSetting.EngineModelID, alertSetting.ChannelID)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> PutVessel(Guid id, Vessel vessel) { int writeLevel = Util.GetResourcePermission("Vessel", Util.ReourceOperations.Write); if (writeLevel != 2) { ModelState.AddModelError("Access Level", "Unauthorized update access."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != vessel.Id) { return(BadRequest()); } DbEntityEntry entry = db.Entry(vessel); entry.State = EntityState.Modified; // Marking properties to update by compareing default object Vessel defaultVessel = new Vessel(); entry = Util.GetUpdatedProperties(defaultVessel, vessel, entry); try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!VesselExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> PutAlternatorMaker(Guid id, AlternatorMaker alternatorMaker) { int writeLevel = Util.GetResourcePermission("Master Data", Util.ReourceOperations.Write); if (writeLevel != 2) { ModelState.AddModelError("Access Level", "Unauthorized write access."); } var am = db.AlternatorMakers.Where(a => a.Name == alternatorMaker.Name).FirstOrDefault(); if (am != null) { ModelState.AddModelError("Duplicate", "Alternator Maker already existed."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != alternatorMaker.Id) { return(BadRequest()); } db.Entry(alternatorMaker).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AlternatorMakerExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> PostKendoInlineChannelGroup(ChannelGroup ChannelGroup) { int deleteLevel = Util.GetResourcePermission("ChannelGroup", Util.ReourceOperations.Write); if (deleteLevel != 2) { ModelState.AddModelError("Access Level", "Unauthorized write access."); } var foundDup = db.ChannelGroups.Where(m => m.Name == ChannelGroup.Name).FirstOrDefault(); if (foundDup != null) { ModelState.AddModelError("Found Duplicate", "Duplicate channel group name found."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.ChannelGroups.Add(ChannelGroup); try { await db.SaveChangesAsync(); } catch (DbUpdateException) { if (ChannelGroupExists(ChannelGroup.Id)) { return(Conflict()); } else { throw; } } return(CreatedAtRoute("DefaultApi", new { id = ChannelGroup.Id }, ChannelGroup)); }
public async Task <IHttpActionResult> PostKendoInlineModel(Model model) { int deleteLevel = Util.GetResourcePermission("Master Data", Util.ReourceOperations.Write); if (deleteLevel != 2) { ModelState.AddModelError("Access Level", "Unauthorized write access."); } var foundDup = db.Models.Where(m => m.Name == model.Name && m.EngineTypeID == model.EngineTypeID).FirstOrDefault(); if (foundDup != null) { ModelState.AddModelError("Found Duplicate", "Duplicate engine model found."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.Models.Add(model); try { await db.SaveChangesAsync(); } catch (DbUpdateException) { if (ModelExists(model.Id)) { return(Conflict()); } else { throw; } } return(CreatedAtRoute("DefaultApi", new { id = model.Id }, model)); }
public async Task <IHttpActionResult> PostKendoInlineShipClasses(ShipClass shipClass) { int deleteLevel = Util.GetResourcePermission("Master Data", Util.ReourceOperations.Write); if (deleteLevel != 2) { ModelState.AddModelError("Access Level", "Unauthorized write access."); } var foundDup = db.ShipClasses.Where(a => a.Name == shipClass.Name).FirstOrDefault(); if (foundDup != null) { ModelState.AddModelError("Found Duplicate", "Duplicate ship class found."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.ShipClasses.Add(shipClass); try { await db.SaveChangesAsync(); } catch (DbUpdateException) { if (ShipClassesExists(shipClass.Id)) { return(Conflict()); } else { throw; } } return(CreatedAtRoute("DefaultApi", new { id = shipClass.Id }, shipClass)); }
public async Task <IHttpActionResult> PutShipClass(Guid id, ShipClass shipClass) { int deleteLevel = Util.GetResourcePermission("Master Data", Util.ReourceOperations.Write); if (deleteLevel != 2) { ModelState.AddModelError("Access Level", "Unauthorized write access."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != shipClass.Id) { return(BadRequest()); } db.Entry(shipClass).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ShipClassExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> DeleteClient(string id) { Client client = await db.Clients.FindAsync(id); if (client == null) { return(NotFound()); } db.Clients.Remove(client); await db.SaveChangesAsync(); return(Ok(client)); }
public async Task <IHttpActionResult> UserPasswordReset(Guid id) { User user = db.Users.Where(u => u.Id == id).FirstOrDefault(); if (user != null) { string emailHost = ConfigurationManager.AppSettings["EmailHost"]; string emailPort = ConfigurationManager.AppSettings["EmailPort"]; string emailSSL = ConfigurationManager.AppSettings["EmailSSL"]; string emailAddr = ConfigurationManager.AppSettings["EmailAddress"]; string emailName = ConfigurationManager.AppSettings["EmailName"]; string emailPwd = ConfigurationManager.AppSettings["EmailPwd"]; string pwd = System.Web.Security.Membership.GeneratePassword(8, 3); var fromAddress = new MailAddress(emailAddr, emailName); var toAddress = new MailAddress(user.Email, user.FullName); string fromPassword = emailPwd; string subject = "Security Update - Password Reset"; string body = string.Format("This is your new password >>>> \t {0}", pwd); var smtp = new SmtpClient { Host = emailHost, Port = int.Parse(emailPort), EnableSsl = bool.Parse(emailSSL), DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) }; using (var message = new MailMessage(fromAddress, toAddress) { Subject = subject, Body = body }) { byte[] hashBytes = new PasswordHash(pwd).ToArray(); string strBase64 = Util.ByteArrayToString(hashBytes); user.PasswordHash = strBase64; DbEntityEntry entry = db.Entry(user); entry.State = EntityState.Modified; // Marking properties to update by compareing default object User defaultUser = new User(); entry = Util.GetUpdatedProperties(defaultUser, user, entry); try { await db.SaveChangesAsync(); await smtp.SendMailAsync(message); } catch (DbUpdateConcurrencyException) { if (!UserExists(id)) { return(NotFound()); } else { throw; } } catch (Exception) { throw; } } return(Ok(user)); } else { return(InternalServerError(new Exception("User ID not found."))); } }
public async Task <IHttpActionResult> ProcessStagingData() { Remax_Entities db = new Remax_Entities(); List <Channel> newChannels = new List <Channel>(); List <Monitoring> monitoringList = new List <Monitoring>(); List <Alert> alertList = new List <Alert>(); #region --------------------------- Checking service user & root account --------------------------- User serviceUser = (from u in db.Users where (string.IsNullOrEmpty(u.FullName) ? "" : u.FullName).ToLower() == "service" select u).FirstOrDefault(); if (serviceUser != null) { db.ServiceUser = serviceUser; } else { logger.DebugFormat("Service user not found : Create \"Service\" user."); return(StatusCode(HttpStatusCode.InternalServerError)); } Account rootAccount = (from a in db.Accounts where (string.IsNullOrEmpty(a.AccountID) ? "" : a.AccountID).ToLower() == "root" select a).FirstOrDefault(); if (rootAccount != null) { db.RootAccount = rootAccount; } else { logger.DebugFormat("Root account not found : Create new account with account ID \"Root\"."); return(StatusCode(HttpStatusCode.InternalServerError)); } #endregion logger.InfoFormat("Process Staging Data start at {0}", DateTime.Now); try { #region --------------------------- Vessel & Engine Creation --------------------------- // Vessel check and create var vesselsCheck = from m in db.Monitorings where !(m.Processed.HasValue ? m.Processed.Value : false) group m.IMO_No by m.IMO_No into g select new { g.Key }; foreach (var imo in vesselsCheck.ToList()) { var found = (from v in db.Vessels where v.IMO_No == imo.Key select v).FirstOrDefault(); if (found == null) { logger.DebugFormat("Creating new Vessel : {0}", imo.Key); Vessel v = new Vessel { IMO_No = imo.Key, VesselName = "No Name", OwnerID = db.RootAccount.Id, OperatorID = db.RootAccount.Id }; db.Vessels.Add(v); db.Entry(v).State = System.Data.Entity.EntityState.Added; int rowAffected = await db.SaveChangesAsync(); if (rowAffected == 1) { logger.DebugFormat("New Vessel created : {0}", imo.Key); } } } // Engine check and create var enginesCheck = from m in db.Monitorings where !(m.Processed.HasValue ? m.Processed.Value : false) group m by new { IMO_No = m.IMO_No, SerialNo = m.SerialNo } into g select new { g.Key }; foreach (var eng in enginesCheck.ToList()) { var vesselFound = (from v in db.Vessels where v.IMO_No == eng.Key.IMO_No select v).FirstOrDefault(); if (vesselFound != null) { var engineFound = (from e in db.Engines join v in db.Vessels on e.VesselID equals v.Id into ev from e_v in ev.DefaultIfEmpty() where new { e_v.Id, e.SerialNo } == new { vesselFound.Id, eng.Key.SerialNo } select e).FirstOrDefault(); if (engineFound == null) { logger.DebugFormat("Creating new Engine : {0}", new { vesselFound.Id, eng.Key.SerialNo }); Engine e = new Engine { VesselID = vesselFound.Id, SerialNo = eng.Key.SerialNo }; db.Engines.Add(e); db.Entry(e).State = System.Data.Entity.EntityState.Added; int rowAffected = await db.SaveChangesAsync(); if (rowAffected == 1) { logger.DebugFormat("New Engine created : Vessel {0}, Serial No {1}", eng.Key.IMO_No, eng.Key.SerialNo); } } } } #endregion #region --------------------------- Data Extraction (Monitoring and Alert Settings) --------------------------- var monitoring = from m in db.Monitorings join v in db.Vessels on m.IMO_No equals v.IMO_No into mv from m_v in mv.DefaultIfEmpty() join e in db.Engines on m.SerialNo equals e.SerialNo into me from m_e in me.DefaultIfEmpty() join ml in db.Models on(m_e.EngineModelID.HasValue?m_e.EngineModelID : Guid.Empty) equals ml.Id into mml from m_ml in mml.DefaultIfEmpty() join c in db.Channels on new { m.ChannelNo, ID = (m_ml != null && m_ml.Id != null) ? m_ml.Id : Guid.Empty } equals new { c.ChannelNo, ID = c.ModelID.HasValue ? c.ModelID.Value : Guid.Empty } into mch from m_ch in mch.DefaultIfEmpty() join ct in db.ChartTypes on m_ch.ChartTypeID equals ct.Id into mct from m_ct in mct.DefaultIfEmpty() where !(m.Processed.HasValue ? m.Processed.Value : false) select new { Id = m.Id, IMONo = m.IMO_No, VesselId = m_v.Id, VesselName = m_v.VesselName, SerialNo = m.SerialNo, EngineID = m_e.Id != null ? m_e.Id : Guid.Empty, EngineModelID = m_e.EngineModelID != null ? m_e.EngineModelID : Guid.Empty, ModelName = m_ml.Name, ChannelNo = m.ChannelNo, ChannelID = m_ch.Id != null ? m_ch.Id : Guid.Empty, ChannelName = m_ch.Name, ChannelDocURL = m_ch.DocumentURL, DisplayUnit = m.Unit, IncomingChannelName = m.ChannelDescription, ChartType = m_ct.Name, Processed = m.Processed, Value = m.Value }; var conditions = from o in db.OptionSets join og in db.OptionSetGroups on o.GroupId equals og.Id where og.Name == "condition" select o; var alertLevels = from o in db.OptionSets join og in db.OptionSetGroups on o.GroupId equals og.Id where og.Name == "alertlevel" select o; var alertSettings = from s in db.AlertSettings select s; #endregion #region --------------------------- Processing data and Creating Alerts --------------------------- // Do not remove ToList() in below line, it will trigger "There is already an open DataReader associated with this Command which must be closed first." logger.DebugFormat("Total line to process : {0}", monitoring.ToList().Count()); foreach (var m in monitoring.ToList()) { var monitor = db.Monitorings.Where(mo => mo.Id == m.Id).FirstOrDefault(); bool error = false; if (monitor != null) { #region --------------------------- Processing monitoring data --------------------------- monitor.ProcessedError = string.Empty; if (m.VesselName == null) { monitor.ProcessedError += "IMO number not found."; } if (m.EngineID == null || m.EngineID == Guid.Empty) { monitor.ProcessedError += "Engine not found. "; } if (m.EngineModelID == null || m.EngineModelID == Guid.Empty) { monitor.ProcessedError += "Engine model not found. "; } monitor.ProcessedError = monitor.ProcessedError.Trim(); error = (m.VesselName == null || m.EngineID == null || m.EngineID == Guid.Empty || m.EngineModelID == null || m.EngineModelID == Guid.Empty ); if (!error) { monitor.Processed = true; } monitoringList.Add(monitor); #endregion #region --------------------------- Checking and Adding Alerts --------------------------- var foundAlert = alertSettings.Where(s => s.EngineModelID == m.EngineModelID && s.ChannelID == m.ChannelID).FirstOrDefault(); if (foundAlert != null) { string strAVal = foundAlert.Value; string strMVal = m.Value; decimal aVal = decimal.MinValue; decimal mVal = decimal.MinValue; decimal.TryParse(strAVal, out aVal); decimal.TryParse(strMVal, out mVal); bool isAlertValIsNumber = aVal != decimal.MinValue; bool isNeed2Alert = false; switch (foundAlert.Condition) { case 1: //Equal to if (isAlertValIsNumber) { isNeed2Alert = (mVal == aVal); //number equal } else { isNeed2Alert = (strMVal.Trim().ToLower() == strAVal.Trim().ToLower()); //string equal } break; case 2: //Not equal to if (isAlertValIsNumber) { isNeed2Alert = (mVal != aVal); //number not equal } else { isNeed2Alert = (strMVal.Trim().ToLower() != strAVal.Trim().ToLower()); //string not equal } break; case 3: //Greater than if (isAlertValIsNumber) { isNeed2Alert = (mVal > aVal); //number not equal } break; case 4: //Less than if (isAlertValIsNumber) { isNeed2Alert = (mVal < aVal); //number not equal } break; case 5: //Greater than or equal to if (isAlertValIsNumber) { isNeed2Alert = (mVal >= aVal); //number not equal } break; case 6: //Less than or equal to if (isAlertValIsNumber) { isNeed2Alert = (mVal <= aVal); //number not equal } break; default: isNeed2Alert = false; break; } if (isNeed2Alert) { logger.InfoFormat("Alert found: [AlertID:{0}] [MonintoringID:{1}]", foundAlert.Id, m.Id); EmailTemplate emailTemplate = (from t in db.EmailTemplates where t.Name.ToLower() == EMAIL_ALERT_TEMPLATE_NAME select t).FirstOrDefault(); IQueryable <Vessel> vessels = from v in db.Vessels where v.IMO_No == m.IMONo select v; Vessel vesselFound = vessels.Include("OperatorAccount").Include("OwnerAccount").FirstOrDefault(); if (emailTemplate != null && vesselFound != null) { logger.InfoFormat("Alert creating."); string emails = string.Empty; if (vesselFound.OwnerAccount != null && vesselFound.OwnerAccount.Email != null) { if (IsValidEmail(vesselFound.OwnerAccount.Email)) { emails += vesselFound.OwnerAccount.Email + ";"; } } if (vesselFound.OperatorAccount != null && vesselFound.OperatorAccount.Email != null) { if (IsValidEmail(vesselFound.OperatorAccount.Email)) { emails += vesselFound.OperatorAccount.Email + ";"; } } string msg = emailTemplate.Template; string osCondition = conditions.Where(c => c.Value == foundAlert.Condition).Select(c => c.Name).FirstOrDefault(); string osAlertLevel = alertLevels.Where(al => al.Value == foundAlert.AlertLevel).Select(al => al.Name).FirstOrDefault(); string strDocURL = string.Empty; if (!string.IsNullOrWhiteSpace(m.ChannelDocURL)) { strDocURL = string.Format("<a href='{0}' target='_new'>Click here to open troubleshooting document</a>", m.ChannelDocURL); } msg = msg.Replace("[[Vessel.IMO_No]]", m.IMONo) .Replace("[[Engine.SerialNo]]", m.SerialNo) .Replace("[[Channel.Name]]", m.ChannelName) .Replace("[[AlertSetting.Condition]]", osCondition != null? osCondition.ToLower() : "") .Replace("[[AlertSetting.Value]]", foundAlert.Value) .Replace("[[AlertSetting.AlertLevel]]", osAlertLevel) .Replace("[[AlertSetting.Message]]", foundAlert.Message) .Replace("[[Channel.DocumentURL]]", strDocURL); Alert a = new Alert() { MonitoringId = m.Id, VesselId = m.VesselId, IMO_No = m.IMONo, VesselName = m.VesselName, EngineId = m.EngineID, SerialNo = m.SerialNo, ModelId = m.EngineModelID, ModelName = m.ModelName, ChannelId = m.ChannelID, ChannelName = m.ChannelName, Value = m.Value, DisplayUnit = m.DisplayUnit, AlertSettingId = foundAlert.Id, Condition = foundAlert.Condition, ConditionValue = osCondition, AlertValue = foundAlert.Value, AlertLevel = foundAlert.AlertLevel, AlertLevelValue = osAlertLevel, Recipients = emails, Subject = string.Format("Alert for {0} - {1}", m.IMONo, m.SerialNo), AlertMessage = foundAlert.Message, AlertEmailMessage = msg, DocumentURL = m.ChannelDocURL, AlertTime = DateTime.UtcNow }; // Add to alert collection and update to database later alertList.Add(a); } else if (emailTemplate == null) { logger.DebugFormat("Email template {0} not found", EMAIL_ALERT_TEMPLATE_NAME); } else if (vesselFound == null) { logger.DebugFormat("Vessel not found"); } } } #endregion } #region ---------------------- Adding new channels ----------------------- // No channel name found but have incoming channel name and no error e.g engine serial no. not found if (m.ChannelName == null && !string.IsNullOrEmpty(m.IncomingChannelName) && !error) // Consider new channel for related engine model { Channel c = new Channel() { ChannelNo = m.ChannelNo, Name = m.IncomingChannelName, ModelID = m.EngineModelID, DisplayUnit = m.DisplayUnit, //Id = Guid.NewGuid(), //CreatedBy = Guid.Empty, //CreatedOn = DateTime.Now, //ModifiedBy = Guid.Empty, //ModifiedOn = DateTime.Now }; if (c.Name.Contains("Port")) { c.Side = 1; } else if (c.Name.Contains("Starboard") || c.Name.Contains("STBD")) { c.Side = 2; } var checkChannelDB = db.Channels.Where( ch => ch.ChannelNo.ToLower() == (string.IsNullOrEmpty(c.ChannelNo) ? "" : c.ChannelNo.ToLower()) && ch.ModelID == m.EngineModelID ).FirstOrDefault(); var checkChannelMemory = newChannels.Where( ch => ch.ChannelNo.ToLower() == (string.IsNullOrEmpty(c.ChannelNo) ? "" : c.ChannelNo.ToLower()) && ch.ModelID == m.EngineModelID ).FirstOrDefault(); if (checkChannelDB == null && checkChannelMemory == null) { newChannels.Add(c); } } #endregion } #endregion int recordAffected = 0; #region ------------------------ Creating new channels ------------------------ logger.DebugFormat("Total new Channels to be created : {0}", newChannels.ToList().Count()); foreach (var c in newChannels) { db.Channels.Add(c); db.Entry(c).State = System.Data.Entity.EntityState.Added; } recordAffected = await db.SaveChangesAsync(); logger.DebugFormat("Total new Channels created : {0}", recordAffected); #endregion #region ------------------------ Updating monitoring values ------------------------ logger.DebugFormat("Total values to be processed : {0}", monitoringList.ToList().Count()); foreach (var m in monitoringList) { if (m.Id == null) { db.Monitorings.Add(m); db.Entry(m).State = System.Data.Entity.EntityState.Added; } else { db.Entry(m).State = System.Data.Entity.EntityState.Modified; } } recordAffected = await db.SaveChangesAsync(); logger.DebugFormat("Total values processed : {0}", recordAffected); #endregion #region ------------------------ Creating alerts and sending emails------------------------ logger.DebugFormat("Total alerts to be created : {0}", monitoringList.ToList().Count()); foreach (var alert in alertList) { #region ----------------------- Send alert email ----------------------- string emailHost = ConfigurationManager.AppSettings["EmailHost"]; string emailPort = ConfigurationManager.AppSettings["EmailPort"]; string emailSSL = ConfigurationManager.AppSettings["EmailSSL"]; string emailAddr = ConfigurationManager.AppSettings["EmailAddress"]; string emailName = ConfigurationManager.AppSettings["EmailName"]; string emailPwd = ConfigurationManager.AppSettings["EmailPwd"]; var fromAddress = new MailAddress(emailAddr, emailName); string fromPassword = emailPwd; var smtp = new SmtpClient { Host = emailHost, Port = int.Parse(emailPort), EnableSsl = bool.Parse(emailSSL), DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) }; using (var message = new MailMessage() { From = fromAddress, Subject = alert.Subject, Body = alert.AlertEmailMessage, IsBodyHtml = true }) { string[] toRecipient = alert.Recipients.Split(new char[] { ';', ',' }); foreach (var r in toRecipient) { if (IsValidEmail(r)) { message.To.Add(new MailAddress(r)); } } await smtp.SendMailAsync(message); alert.Notified = true; } #endregion db.Alerts.Add(alert); db.Entry(alert).State = System.Data.Entity.EntityState.Added; } recordAffected = await db.SaveChangesAsync(); logger.DebugFormat("Total alert created : {0}", recordAffected); #endregion } catch (Exception ex) { if (ex.InnerException != null) { logger.Error(ex.InnerException.Message, ex.InnerException); } else { logger.Error(ex.Message, ex); } return(StatusCode(HttpStatusCode.InternalServerError)); } logger.InfoFormat("Process Staging Data end at {0}", DateTime.Now); logger.InfoFormat(Environment.NewLine); return(StatusCode(HttpStatusCode.OK)); }
protected async Task <FileProcessSummary> ProcessSingleFile(string fileNamePath) { FileProcessSummary summary = new FileProcessSummary(); string[] lines = File.ReadAllLines(fileNamePath); bool hasError = false; long totalLine = lines.Length; int recordAffected = 0; if (totalLine > 0) { string headerLine = lines[0]; string[] header = headerLine.Split(new char[] { ',' }); if (header.Length != 7) { logger.Error(Consts.INTEGRATION_FILE_HEADER_LENGTH); throw new FileProcessingException(Consts.INTEGRATION_FILE_HEADER_LENGTH, FileProcessingException.ErrorType.File); } summary.ErrorList = new List <string>(); summary.ErrorList.Add(headerLine); summary.Total = lines.Length - 1; summary.StartTime = DateTime.Now; List <Monitoring> monitoringList = new List <Monitoring>(); for (long i = 0; i < lines.Length; i++) { long currentLine = i; string line = lines[i]; Regex CSVParser = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))"); string[] raw = CSVParser.Split(line); //string[] raw = line.Split(new char[] { ',' }); try { Monitoring monitor = new Monitoring(); // IMO Number if (!string.IsNullOrWhiteSpace(raw[0])) { monitor.IMO_No = RemoveDoubleQuotes(raw[0]); } // Engine Serial No. if (!string.IsNullOrWhiteSpace(raw[1])) { monitor.SerialNo = RemoveDoubleQuotes(raw[1]); } // Channel No if (!string.IsNullOrWhiteSpace(raw[2])) { monitor.ChannelNo = RemoveDoubleQuotes(raw[2]); } // Channel Description if (!string.IsNullOrWhiteSpace(raw[3])) { monitor.ChannelDescription = RemoveDoubleQuotes(raw[3]); } // TimeStamp if (!string.IsNullOrWhiteSpace(raw[4])) { monitor.TimeStamp = DateTime.Parse(raw[4]).ToUniversalTime(); } // Data Values if (!string.IsNullOrWhiteSpace(raw[5])) { monitor.Value = RemoveDoubleQuotes(raw[5]); } // Unit of Measurement if (!string.IsNullOrWhiteSpace(raw[6])) { monitor.Unit = RemoveDoubleQuotes(raw[6]); } // System Info //monitor.Id = Guid.NewGuid(); //monitor.CreatedBy = Guid.Empty; //monitor.CreatedOn = DateTime.Now; //monitor.ModifiedBy = Guid.Empty; //monitor.ModifiedOn = DateTime.Now; monitor.DataRecord = line; monitor.FileName = Path.GetFileName(fileNamePath); monitor.TimeStampOriginal = raw[4]; monitoringList.Add(monitor); summary.Success = summary.Success++; } catch (Exception ex) { summary.ErrorList.Add(string.Format("{0},{1},{2}", line, i.ToString(), ex.Message)); hasError = true; summary.Failure++; continue; } } summary.EndTime = DateTime.Now; Remax_Entities remax_Entities = new Remax_Entities(); remax_Entities.Monitorings.AddRange(monitoringList); recordAffected = 0; // Do not remove this User serviceUser = (from u in remax_Entities.Users where (string.IsNullOrEmpty(u.FullName) ? "" : u.FullName).ToLower() == "service" select u).FirstOrDefault(); remax_Entities.ServiceUser = serviceUser; try { recordAffected = await remax_Entities.SaveChangesAsync(); } catch (Exception ex) { hasError = true; logger.Error(ex.Message, ex); summary.Failure += (summary.Total - summary.Failure) - recordAffected; } } summary.DatabaseInsert = recordAffected; summary.Success = recordAffected; summary.SuccessfullyProcessed = !hasError; return(summary); }
public async Task <IHttpActionResult> PutUser(Guid id, User user) { int writeLevel = Util.GetResourcePermission("User", Util.ReourceOperations.Write); if (writeLevel != 2) { ModelState.AddModelError("Access Level", "Unauthorized update access."); } var duplicate = db.Users.Where(u => u.Email == user.Email && u.Id != user.Id).FirstOrDefault(); if (duplicate != null) { ModelState.AddModelError("Duplicate email found", "Email already exists in system. Please use another email address."); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != user.Id) { return(BadRequest()); } //var newRoles = user.UserRoles.ToList(); //var currentUser = db.Users.Where(u => u.Id == user.Id).Include("UserRoles").FirstOrDefault(); //var currentRoles = new List<UserRole>(); //if (currentUser != null) { // currentRoles = currentUser.UserRoles.ToList(); // db.Entry(currentUser).State = EntityState.Detached; //} //user.UserRoles.Clear(); //foreach (var r in db.UserRoles) //{ // if (newRoles.Any(rr => rr.Id == r.Id)) // { // if (!currentRoles.Any(rr => rr.Id == r.Id)) // { // db.UserRoles.Attach(r); // db.Entry(r).State = EntityState.Modified; // user.UserRoles.Add(r); // } // } // else { // user.UserRoles.Remove(r); // } //} List <string> roles = user.UserRoles.Select(r => r.Id.ToString()).ToList(); string strRoles = string.Join(",", roles.ToArray()); //foreach (var r in currentRoles) //{ // if (!newRoles.Any(rr => r.Id == r.Id)) // { // db.UserRoles.Attach(r); // user.UserRoles.Remove(r); // } //} DbEntityEntry entry = db.Entry(user); entry.State = EntityState.Modified; // Marking properties to update by compareing default object User defaultUser = new User(); entry = Util.GetUpdatedProperties(defaultUser, user, entry); try { await db.SaveChangesAsync(); if (!string.IsNullOrEmpty(strRoles)) { db.sp_AssignRoles(user.Id, strRoles); } } catch (DbUpdateConcurrencyException) { if (!UserExists(id)) { return(NotFound()); } else { throw; } } catch (Exception ex) { throw ex; } return(StatusCode(HttpStatusCode.NoContent)); }