public BaseResponse BatchUpdateFilePrivilege(Requests.FileManagerRolePrivilege.BatchUpdateFilePrivilegeRequest request) { var response = new BaseResponse(); try { int addCounter = 0; int updatedCounter = 0; foreach (var item in request.BatchUpdateFilePrivilege) { if (item.ProcessBlueprint_Id > 0 && item.RoleGroup_Id > 0) { var toUpdate = DataContext.FileManagerRolePrivileges.Find(item.ProcessBlueprint_Id, item.RoleGroup_Id); if (toUpdate != null) { // put update code here toUpdate.AllowBrowse = item.AllowBrowse; toUpdate.AllowCopy = item.AllowCopy; toUpdate.AllowCreate = item.AllowCreate; toUpdate.AllowDelete = item.AllowDelete; toUpdate.AllowDownload = item.AllowDownload; toUpdate.AllowMove = item.AllowMove; toUpdate.AllowRename = item.AllowRename; toUpdate.AllowUpload = item.AllowUpload; DataContext.Entry(toUpdate).State = EntityState.Modified; updatedCounter++; } else { //put insert code here var privilege = item.MapTo<FileManagerRolePrivilege>(); DataContext.FileManagerRolePrivileges.Add(privilege); addCounter++; } } } DataContext.SaveChanges(); response.IsSuccess = true; response.Message = string.Format("{0} data has been added, {1} data has been updated", addCounter.ToString() , updatedCounter.ToString()); } catch (InvalidOperationException inval) { response.Message = inval.Message; } catch (ArgumentNullException arg) { response.Message = arg.Message; } return response; }
private BaseResponse SaveMultiAxis(SaveLayoutItemRequest request) { var response = new BaseResponse(); try { var derLayoutItem = request.MapTo<DerLayoutItem>();// new DerLayoutItem(); var derLayout = new DerLayout { Id = request.DerLayoutId }; DataContext.DerLayouts.Attach(derLayout); derLayoutItem.DerLayout = derLayout; //derLayoutItem.Column = request.Column; //derLayoutItem.Row = request.Row; //derLayoutItem.Type = request.Type; var derArtifact = request.MapTo<DerArtifact>(); //derArtifact.GraphicType = request.Type; //derArtifact.HeaderTitle = request.Artifact.HeaderTitle; /*var measurement = new Measurement { Id = request.Artifact.MeasurementId }; DataContext.Measurements.Attach(measurement); derArtifact.Measurement = measurement;*/ derArtifact.Charts = new List<DerArtifactChart>(); foreach (var item in request.Artifact.MultiAxis.Charts) { var chart = item.MapTo<DerArtifactChart>(); var measurement = new Measurement { Id = item.MeasurementId }; if (DataContext.Measurements.Local.FirstOrDefault(x => x.Id == measurement.Id) == null) { DataContext.Measurements.Attach(measurement); } else { measurement = DataContext.Measurements.Local.FirstOrDefault(x => x.Id == measurement.Id); } DataContext.Measurements.Attach(measurement); chart.Measurement = measurement; foreach (var s in item.Series) { var serie = s.MapTo<DerArtifactSerie>(); var kpi = new Kpi { Id = s.KpiId }; if (DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpi.Id) == null) { DataContext.Kpis.Attach(kpi); } else { kpi = DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpi.Id); } serie.Kpi = kpi; serie.Artifact = derArtifact; chart.Series.Add(serie); } derArtifact.Charts.Add(chart); } derLayoutItem.Artifact = derArtifact; //DataContext.DerArtifacts.Add(derArtifact); DataContext.DerLayoutItems.Add(derLayoutItem); DataContext.SaveChanges(); response.IsSuccess = true; response.Message = "Multiaxis has been configured"; /*var charts = request.Artifact.MultiAxis.Charts.Select(x => new DerArtifactChart { FractionScale = x.FractionScale, GraphicType = x.GraphicType, IsOpposite = x.IsOpposite, MaxFractionScale = x.MaxFractionScale, Measurement = DataContext.Measurements.Single(x => x.) })*/ /* var series = request.Artifact.LineChart.Series.Select(x => new DerArtifactSerie { Color = x.Color, Kpi = DataContext.Kpis.FirstOrDefault(y => y.Id == x.KpiId), Label = x.Label }).ToList(); derArtifact.Series = series; DataContext.DerArtifacts.Add(derArtifact); derLayoutItem.Artifact = derArtifact; DataContext.DerLayoutItems.Add(derLayoutItem);*/ } catch (Exception exception) { response.Message = exception.Message; } return response; }
private BaseResponse SaveLineChart(SaveLayoutItemRequest request) { var response = new BaseResponse(); try { var derLayoutItem = new DerLayoutItem(); var derLayout = new DerLayout { Id = request.DerLayoutId }; DataContext.DerLayouts.Attach(derLayout); derLayoutItem.DerLayout = derLayout; derLayoutItem.Column = request.Column; derLayoutItem.Row = request.Row; derLayoutItem.Type = request.Type; var derArtifact = new DerArtifact(); derArtifact.GraphicType = request.Type; derArtifact.HeaderTitle = request.Artifact.HeaderTitle; var measurement = new Measurement { Id = request.Artifact.MeasurementId }; DataContext.Measurements.Attach(measurement); derArtifact.Measurement = measurement; var series = request.Artifact.LineChart.Series.Select(x => new DerArtifactSerie { Color = x.Color, Kpi = DataContext.Kpis.FirstOrDefault(y => y.Id == x.KpiId), Label = x.Label, Artifact = derArtifact }).ToList(); derArtifact.Series = series; DataContext.DerArtifacts.Add(derArtifact); derLayoutItem.Artifact = derArtifact; DataContext.DerLayoutItems.Add(derLayoutItem); DataContext.SaveChanges(); response.IsSuccess = true; } catch (Exception exception) { response.Message = exception.Message; } return response; }
private BaseResponse SaveSpeedometer(SaveLayoutItemRequest request) { var response = new BaseResponse(); try { var derLayoutItem = new DerLayoutItem(); var derLayout = new DerLayout { Id = request.DerLayoutId }; DataContext.DerLayouts.Attach(derLayout); derLayoutItem.DerLayout = derLayout; derLayoutItem.Column = request.Column; derLayoutItem.Row = request.Row; derLayoutItem.Type = request.Type; var derArtifact = new DerArtifact(); derArtifact.GraphicType = request.Type; var plots = request.Artifact.Speedometer.PlotBands.Select(x => new DerArtifactPlot { Color = x.Color, From = x.From, To = x.To }).ToList(); derArtifact.Plots = plots; derArtifact.CustomSerie = DataContext.Kpis.FirstOrDefault(y => y.Id == request.Artifact.Speedometer.Series.KpiId); if (request.Artifact.Speedometer.LabelSeries != null) { var labelSeries = new DerArtifactSerie { Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == request.Artifact.Speedometer.LabelSeries.KpiId), Label = "ton/d", Color = "#000" }; derArtifact.Series = new List<DerArtifactSerie>(); derArtifact.Series.Add(labelSeries); } DataContext.DerArtifacts.Add(derArtifact); derLayoutItem.Artifact = derArtifact; DataContext.DerLayoutItems.Add(derLayoutItem); DataContext.SaveChanges(); response.IsSuccess = true; } catch (Exception exception) { response.Message = exception.Message; } return response; }
private BaseResponse _ReadExcelFile(string filename, int scenarioId, string configType) { var response = new BaseResponse(); string periodType = string.Empty; PeriodeType pType = PeriodeType.Yearly; int tahun = DateTime.Now.Year, bulan = DateTime.Now.Month; List<OperationDataConfigurationViewModel.Item> list_data = new List<OperationDataConfigurationViewModel.Item>(); if (filename != Path.GetFullPath(filename)) { filename = Server.MapPath(filename); } /* * cek file exist and return immediatelly if not exist */ if (!System.IO.File.Exists(filename)) { response.IsSuccess = false; response.Message = "File Not Found"; return response; } Workbook workbook = new Workbook(); using (FileStream stream = new FileStream(filename, FileMode.Open)) { workbook.LoadDocument(stream, DevExpress.Spreadsheet.DocumentFormat.OpenXml); #region foreach foreach (var worksheet in workbook.Worksheets) { string[] name = worksheet.Name.Split('_'); if (name[0] == "Daily" || name[0] == "Monthly" || name[0] == "Yearly") { periodType = name[0]; pType = string.IsNullOrEmpty(periodType) ? PeriodeType.Yearly : (PeriodeType)Enum.Parse(typeof(PeriodeType), periodType); string period = name[name.Count() - 1]; string[] periodes = null; //validate and switch value by periodType if (periodType != period && !string.IsNullOrEmpty(period)) { switch (periodType) { case "Daily": periodes = period.Split('-'); tahun = int.Parse(periodes[0]); bulan = int.Parse(periodes[periodes.Count() - 1]); break; case "Monthly": tahun = int.Parse(period); break; case "Yearly": default: break; } } workbook.Worksheets.ActiveWorksheet = worksheet; //get row Range range = worksheet.GetUsedRange(); int rows = range.RowCount; int column = range.ColumnCount - 2; int Kpi_Id = 0; DateTime periodData = new DateTime(); double? nilai = null; List<int> list_Kpi = new List<int>(); for (int i = 1; i < rows; i++) { for (int j = 0; j < column; j++) { if (j == 0) { if (worksheet.Cells[i, j].Value.Type == CellValueType.Numeric) { int Kpis_Id = int.Parse(worksheet.Cells[i, j].Value.ToString()); list_Kpi.Add(Kpis_Id); } } } } var operationsId = _operationConfigService.GetOperationIn(new GetOperationsInRequest { KpiIds = list_Kpi }); //get rows for (int i = 1; i < rows; i++) { for (int j = 0; j < column; j++) { bool fromExistedToNull = false; if (j == 0) { if (worksheet.Cells[i, j].Value.Type == CellValueType.Numeric) { Kpi_Id = int.Parse(worksheet.Cells[i, j].Value.ToString()); } } else if (j > 1) { var operationId = 0; var operation = operationsId.KeyOperations.FirstOrDefault(x => x.KpiId == Kpi_Id); if (operation != null) { operationId = operation.Id; } if (worksheet.Cells[0, j].Value.Type == CellValueType.DateTime) { periodData = DateTime.Parse(worksheet.Cells[0, j].Value.ToString()); } if (worksheet.Cells[i, j].Value.Type == CellValueType.Numeric) { nilai = double.Parse(worksheet.Cells[i, j].Value.ToString()); } else if (worksheet.Cells[i, j].Value.Type == CellValueType.Text) { fromExistedToNull = true; nilai = null; } else { nilai = null; } if (nilai != null || fromExistedToNull) { // try to cacth and update var data = new OperationDataConfigurationViewModel.Item() { Value = nilai, KpiId = Kpi_Id, Periode = periodData, PeriodeType = pType, ScenarioId = scenarioId, OperationId = operationId }; list_data.Add(data); //switch (configType) //{ // case "KpiTarget": // response = this._UpdateKpiTarget(data); // break; // case "KpiAchievement": // response = this._UpdateKpiAchievement(data); // break; // case "Economic": // response = this._UpdateEconomic(data); // break; // default: // response.IsSuccess = false; // response.Message = "No Table Selected"; // break; //} } } } } } else { response.IsSuccess = false; response.Message = "File Not Valid"; break; } switch (configType) { //case "KpiTarget": // response = this._UpdateKpiTarget(list_data); // break; //case "KpiAchievement": // response = this._UpdateKpiAchievement(list_data, pType.ToString(), tahun, bulan); // break; case "Economic": response = this._UpdateEconomic(list_data); break; default: response.IsSuccess = false; response.Message = "No Table Selected"; break; } } #endregion } //here to read excel fileController return response; }
private BaseResponse UpdateOperationData(IEnumerable<ConfigurationViewModel.Item> datas) { var response = new BaseResponse { IsSuccess = false, Message = "Data Not Valid" }; if (datas != null) { var batch = new BatchUpdateOperationDataRequest(); foreach (var data in datas) { var prepare = new UpdateOperationDataRequest() { Id = data.Id, KpiId = data.KpiId, Periode = data.Periode, Value = data.Value, PeriodeType = data.PeriodeType, Remark = data.Remark, KeyOperationConfigId = data.OperationId, ScenarioId = data.ScenarioId };// data.MapTo<UpdateKpiAchievementItemRequest>(); batch.BatchUpdateOperationDataItemRequest.Add(prepare); } response = _operationDataService.BatchUpdateOperationDatas(batch); } return response; }
private BaseResponse ReadExcelFile(ProcessFileViewModel viewModel) { var response = new BaseResponse(); try { int year = DateTime.Now.Year; int month = DateTime.Now.Month; var listData = new List<ConfigurationViewModel.Item>(); if (viewModel.Filename != Path.GetFullPath(viewModel.Filename)) { viewModel.Filename = Server.MapPath(viewModel.Filename); } if (!System.IO.File.Exists(viewModel.Filename)) { response.IsSuccess = false; response.Message = "File Not Found"; return response; } Workbook workbook = new Workbook(); using (FileStream stream = new FileStream(viewModel.Filename, FileMode.Open)) { workbook.LoadDocument(stream, DevExpress.Spreadsheet.DocumentFormat.OpenXml); #region foreach foreach (var worksheet in workbook.Worksheets) { string[] name = worksheet.Name.Split('_'); PeriodeType pType; if (name[0] == "Daily" || name[0] == "Monthly" || name[0] == "Yearly") { string periodeType = name[0]; pType = string.IsNullOrEmpty(periodeType) ? PeriodeType.Yearly : (PeriodeType)Enum.Parse(typeof(PeriodeType), periodeType); string period = name[name.Count() - 1]; string[] periodes = null; if (periodeType != period && !string.IsNullOrEmpty(period)) { switch (periodeType) { case "Yearly": break; case "Monthly": year = int.Parse(period); break; case "Daily": periodes = period.Split('-'); year = int.Parse(periodes[0]); month = int.Parse(periodes[periodes.Count() - 1]); break; } } workbook.Worksheets.ActiveWorksheet = worksheet; Range range = worksheet.GetUsedRange(); int rows = range.RowCount; int column = range.ColumnCount - 2; int kpiId = 0; var kpiListWithOperationConfigId = new List<int>(); var operationIds = new GetOperationsInResponse(); #region get kpi list with key operation config if (viewModel.ConfigType.ToLowerInvariant().Equals("operationdata")) { for (int i = 1; i < rows; i++) { for (int j = 0; j < column; j++) { if (j == 0) { if (worksheet.Cells[i, j].Value.Type == CellValueType.Numeric) { kpiId = int.Parse(worksheet.Cells[i, j].Value.ToString()); kpiListWithOperationConfigId.Add(kpiId); } } } } operationIds = _operationConfigService.GetOperationIn(new GetOperationsInRequest { KpiIds = kpiListWithOperationConfigId }); } #endregion for (int i = 1; i < rows; i++) { bool isAuthorizedKPI = false; for (int j = 0; j < column; j++) { if (j == 0) { if (worksheet.Cells[i, j].Value.Type == CellValueType.Numeric) { kpiId = int.Parse(worksheet.Cells[i, j].Value.ToString()); //this will validate authorized KPI based on Role isAuthorizedKPI = ValidateAuthorizeKPI(kpiId); if (!isAuthorizedKPI) { break; } } } else if (j > 1) { if (worksheet.Cells[0, j].Value.Type == CellValueType.DateTime) { DateTime periodData = DateTime.Parse(worksheet.Cells[0, j].Value.ToString()); if (worksheet.Cells[i, j].Value.Type == CellValueType.Numeric || worksheet.Cells[i, j].Value.Type == CellValueType.Text) { string value = worksheet.Cells[i, j].Value.ToString(); int operationId = 0; if (viewModel.ConfigType.ToLowerInvariant().Equals(ConfigType.OperationData.ToString().ToLowerInvariant())) { var operation = operationIds.KeyOperations.FirstOrDefault(x => x.KpiId == kpiId); if (operation != null) { operationId = operation.Id; } } if (!string.IsNullOrEmpty(value)) { var data = new ConfigurationViewModel.Item { Value = value, KpiId = kpiId, Periode = periodData, PeriodeType = pType, OperationId = operationId, ScenarioId = viewModel.ScenarioId }; listData.Add(data); } } } } } if (!isAuthorizedKPI) continue; } } else { response.IsSuccess = false; response.Message = "File Not Valid"; break; } switch (viewModel.ConfigType) { case "KpiTarget": response = UpdateKpiTarget(listData); break; case "KpiAchievement": response = UpdateKpiAchievement(listData); break; case "OperationData": response = UpdateOperationData(listData); break; default: response.IsSuccess = false; response.Message = string.Format(@"config type for {0} is not existed", viewModel.ConfigType); break; } } #endregion } } catch (Exception exception) { response.IsSuccess = false; response.Message = exception.Message; } return response; }
private BaseResponse SavePie(SaveLayoutItemRequest request) { var response = new BaseResponse(); try { var derLayoutItem = new DerLayoutItem(); var derLayout = new DerLayout { Id = request.DerLayoutId }; DataContext.DerLayouts.Attach(derLayout); derLayoutItem.DerLayout = derLayout; derLayoutItem.Column = request.Column; derLayoutItem.Row = request.Row; derLayoutItem.Type = request.Type; var derArtifact = request.MapTo<DerArtifact>(); derArtifact.ShowLegend = request.Artifact.ShowLegend; derArtifact.Is3D = request.Artifact.Is3D; derArtifact.Charts = new List<DerArtifactChart>(); var measurement = new Measurement { Id = request.Artifact.MeasurementId }; DataContext.Measurements.Attach(measurement); derArtifact.Measurement = measurement; var series = request.Artifact.Pie.Series.Select(x => new DerArtifactSerie { Color = x.Color, Kpi = DataContext.Kpis.FirstOrDefault(y => y.Id == x.KpiId), Artifact = derArtifact }).ToList(); derArtifact.Series = series; DataContext.DerArtifacts.Add(derArtifact); derLayoutItem.Artifact = derArtifact; DataContext.DerLayoutItems.Add(derLayoutItem); DataContext.SaveChanges(); response.IsSuccess = true; } catch (Exception exception) { response.Message = exception.Message; } return response; }
private BaseResponse _UpdateKpiTarget(List<ConfigurationViewModel.Item> datas) { var response = new BaseResponse(); if (datas != null) { var batch = new BatchUpdateTargetRequest(); foreach (var data in datas) { var prepare = new SaveKpiTargetRequest() { Value = data.Value, KpiId = data.KpiId, Periode = data.Periode, PeriodeType = data.PeriodeType, Remark = data.Remark }; batch.BatchUpdateKpiTargetItemRequest.Add(prepare); } response = _kpiTargetService.BatchUpdateKpiTargetss(batch); } return response; }
public BaseResponse BatchUpdateOperationDatas(BatchUpdateOperationDataRequest request) { //var response = new BaseResponse(); //try //{ // int i = 0; // foreach (var item in request.BatchUpdateOperationDataItemRequest) // { // var operationData = item.MapTo<KeyOperationData>(); // operationData.Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == item.KpiId); // operationData.Scenario = DataContext.Scenarios.FirstOrDefault(x => x.Id == item.ScenarioId); // operationData.KeyOperationConfig = DataContext.KeyOperationConfigs.FirstOrDefault(x => x.Id == item.KeyOperationConfigId); // var exist = DataContext.KeyOperationDatas.FirstOrDefault(x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode && x.Value == item.Value && x.Remark == item.Remark && x.Scenario.Id == item.ScenarioId && x.KeyOperationConfig.Id == item.KeyOperationConfigId); // //skip no change value // if (exist != null) // { // continue; // } // var attachedEntity = DataContext.KeyOperationDatas.FirstOrDefault(x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode && x.Scenario.Id == item.ScenarioId && x.KeyOperationConfig.Id == item.KeyOperationConfigId); // if (attachedEntity != null) // { // operationData.Id = attachedEntity.Id; // } // //jika tidak ada perubahan di skip aja // //if (existing.Value.Equals(item.Value) && existing.Periode.Equals(item.Periode) && existing.Kpi.Id.Equals(item.KpiId) && existing.PeriodeType.Equals(item.PeriodeType)) { // // break; // //} // if (operationData.Id != 0) // { // //var attachedEntity = DataContext.KpiAchievements.Find(item.Id); // if (attachedEntity != null && DataContext.Entry(attachedEntity).State != EntityState.Detached) // { // DataContext.Entry(attachedEntity).State = EntityState.Detached; // } // DataContext.KeyOperationDatas.Attach(operationData); // DataContext.Entry(operationData).State = EntityState.Modified; // } // else // { // operationData.Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == item.KpiId); // DataContext.KeyOperationDatas.Add(operationData); // } // i++; // } // DataContext.SaveChanges(); // response.IsSuccess = true; // if (i > 0) // { // response.Message = string.Format("{0} Operation Data items has been updated successfully", i.ToString()); // } // else // { // response.Message = "File Successfully Parsed, but no data changed!"; // } //} //catch (InvalidOperationException invalidOperationException) //{ // response.Message = invalidOperationException.Message; //} //catch (ArgumentNullException argumentNullException) //{ // response.Message = argumentNullException.Message; //} //return response; var response = new BaseResponse(); try { int deletedCounter = 0; int updatedCounter = 0; int addedCounter = 0; int skippedCounter = 0; foreach (var item in request.BatchUpdateOperationDataItemRequest) { if (!string.IsNullOrEmpty(item.Value)) { var existedOperationDatum = DataContext.KeyOperationDatas .Include(x => x.KeyOperationConfig) .Include(x => x.Scenario) .FirstOrDefault(x =>x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode && x.Scenario.Id == item.ScenarioId); if (existedOperationDatum != null) { if (item.Value.Equals("-") || item.Value.ToLowerInvariant().Equals("null")) { DataContext.KeyOperationDatas.Remove(existedOperationDatum); deletedCounter++; } else { string oldValue = !existedOperationDatum.Value.HasValue ? string.Empty : existedOperationDatum.Value.Value.ToString(CultureInfo.InvariantCulture); string newValue = !item.RealValue.HasValue ? string.Empty : item.RealValue.Value.ToString(CultureInfo.InvariantCulture); if (oldValue.Equals(newValue)) { skippedCounter++; } else { existedOperationDatum.Value = item.RealValue; DataContext.Entry(existedOperationDatum).State = EntityState.Modified; updatedCounter++; } } } else { var operationDatum = item.MapTo<KeyOperationData>(); if (operationDatum.Value.HasValue) { operationDatum.Kpi = DataContext.Kpis.Single(x => x.Id == item.KpiId); operationDatum.KeyOperationConfig = DataContext.KeyOperationConfigs.Single(x => x.Id == item.KeyOperationConfigId); operationDatum.Scenario = DataContext.Scenarios.Single(x => x.Id == item.ScenarioId); DataContext.KeyOperationDatas.Add(operationDatum); addedCounter++; } } } } DataContext.SaveChanges(); response.IsSuccess = true; response.Message = string.Format("{0} data has been added, {1} data has been updated, {2} data has been removed, {3} data didn't change", addedCounter.ToString() , updatedCounter.ToString(), deletedCounter.ToString(), skippedCounter.ToString()); } catch (InvalidOperationException invalidOperationException) { response.Message = invalidOperationException.Message; } catch (ArgumentNullException argumentNullException) { response.Message = argumentNullException.Message; } return response; }
private BaseResponse _UpdateKpiAchievement(List<ConfigurationViewModel.Item> datas, string periodeType, int year, int month) { var response = new BaseResponse(); if (datas != null) { var batch = new BatchUpdateKpiAchievementRequest(); foreach (var data in datas) { var prepare = new UpdateKpiAchievementItemRequest() { Id = data.Id, KpiId = data.KpiId, Periode = data.Periode, Value = data.Value, PeriodeType = data.PeriodeType, Remark = data.Remark };// data.MapTo<UpdateKpiAchievementItemRequest>(); batch.BatchUpdateKpiAchievementItemRequest.Add(prepare); } response = _kpiAchievementService.BatchUpdateKpiAchievements(batch); } return response; }
private BaseResponse _UpdateEconomic(List<ConfigurationViewModel.Item> datas) { var response = new BaseResponse { IsSuccess = false, Message = "Data Not Valid" }; return response; }
private BaseResponse UpdateSpeedometer(SaveLayoutItemRequest request) { var response = new BaseResponse(); try { var derLayoutItem = DataContext.DerLayoutItems .Include(x => x.Artifact) .Include(x => x.Artifact.Plots) .Include(x => x.Artifact.CustomSerie) .Include(x => x.Artifact.Series) .Single(x => x.Id == request.Id); //DataContext.DerArtifacts.Remove(derLayoutItem.Artifact); var derLayout = new DerLayout { Id = request.DerLayoutId }; DataContext.DerLayouts.Attach(derLayout); derLayoutItem.DerLayout = derLayout; derLayoutItem.Column = request.Column; derLayoutItem.Row = request.Row; derLayoutItem.Type = request.Type; var derArtifact = new DerArtifact(); derArtifact.GraphicType = request.Type; var plots = request.Artifact.Speedometer.PlotBands.Select(x => new DerArtifactPlot { Color = x.Color, From = x.From, To = x.To }).ToList(); derArtifact.Plots = plots; derArtifact.CustomSerie = DataContext.Kpis.FirstOrDefault(y => y.Id == request.Artifact.Speedometer.Series.KpiId); if (request.Artifact.Speedometer.LabelSeries != null) { if (derArtifact.Series != null) { foreach (var serie in derArtifact.Series.ToList()) { derArtifact.Series.Remove(serie); } } else { derArtifact.Series = new List<DerArtifactSerie>(); } var labelSeries = new DerArtifactSerie { Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == request.Artifact.Speedometer.LabelSeries.KpiId), Label = "tonnes/day", Color = "#000" }; derArtifact.Series.Add(labelSeries); } DataContext.DerArtifacts.Add(derArtifact); derLayoutItem.Artifact = derArtifact; //DataContext.DerLayoutItems.Add(derLayoutItem); var oldArtifact = new DerArtifact { Id = request.Artifact.Id }; if (DataContext.DerArtifacts.Local.FirstOrDefault(x => x.Id == oldArtifact.Id) == null) { DataContext.DerArtifacts.Attach(oldArtifact); } else { oldArtifact = DataContext.DerArtifacts.Local.FirstOrDefault(x => x.Id == oldArtifact.Id); } foreach(var plot in oldArtifact.Plots.ToList()) { DataContext.DerArtifactPlots.Remove(plot); } DataContext.DerArtifacts.Remove(oldArtifact); DataContext.SaveChanges(); response.IsSuccess = true; } catch (Exception exception) { response.Message = exception.Message; } return response; }
/*public BaseResponse BatchUpdateKpiTargetss(BatchUpdateTargetRequest request) { var response = new BaseResponse(); try { int i = 0; foreach (var item in request.BatchUpdateKpiTargetItemRequest) { var kpiTarget = item.MapTo<KpiTarget>(); var exist = DataContext.KpiTargets.FirstOrDefault(x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode && x.Value == item.Value && x.Remark == item.Remark); //skip no change value if (exist != null) { continue; } var attachedEntity = DataContext.KpiTargets.FirstOrDefault(x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode); if (attachedEntity != null) { kpiTarget.Id = attachedEntity.Id; } //jika tidak ada perubahan di skip aja //if (existing.Value.Equals(item.Value) && existing.Periode.Equals(item.Periode) && existing.Kpi.Id.Equals(item.KpiId) && existing.PeriodeType.Equals(item.PeriodeType)) { // break; //} if (kpiTarget.Id != 0) { //var attachedEntity = DataContext.KpiAchievements.Find(item.Id); if (attachedEntity != null && DataContext.Entry(attachedEntity).State != EntityState.Detached) { DataContext.Entry(attachedEntity).State = EntityState.Detached; } DataContext.KpiTargets.Attach(kpiTarget); DataContext.Entry(kpiTarget).State = EntityState.Modified; } else { kpiTarget.Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == item.KpiId); DataContext.KpiTargets.Add(kpiTarget); } i++; } DataContext.SaveChanges(); response.IsSuccess = true; if (i > 0) { response.Message = string.Format("{0} KPI Target items has been updated successfully", i.ToString()); } else { response.Message = "File Successfully Parsed, but no data changed!"; } } catch (InvalidOperationException invalidOperationException) { response.Message = invalidOperationException.Message; } catch (ArgumentNullException argumentNullException) { response.Message = argumentNullException.Message; } return response; }*/ public BaseResponse BatchUpdateKpiTargetss(BatchUpdateTargetRequest request) { var response = new BaseResponse(); try { int deletedCounter = 0; int updatedCounter = 0; int addedCounter = 0; int skippedCounter = 0; foreach (var item in request.BatchUpdateKpiTargetItemRequest) { if (!string.IsNullOrEmpty(item.Value)) { var existedKpiTarget = DataContext.KpiTargets.FirstOrDefault( x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode); if (existedKpiTarget != null) { if (item.Value.Equals("-") || item.Value.ToLowerInvariant().Equals("null")) { DataContext.KpiTargets.Remove(existedKpiTarget); deletedCounter++; } else { if (existedKpiTarget.Value.Equals(item.RealValue)) { skippedCounter++; } else { existedKpiTarget.Value = item.RealValue; DataContext.Entry(existedKpiTarget).State = EntityState.Modified; updatedCounter++; } } } else { var kpiTarget = item.MapTo<KpiTarget>(); if (kpiTarget.Value.HasValue) { kpiTarget.Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == item.KpiId); DataContext.KpiTargets.Add(kpiTarget); addedCounter++; } else { skippedCounter++; } } } } DataContext.SaveChanges(); response.IsSuccess = true; response.Message = string.Format("{0} data has been added, {1} data has been updated, {2} data has been removed, {3} data didn't change", addedCounter.ToString() , updatedCounter.ToString(), deletedCounter.ToString(), skippedCounter.ToString()); } catch (InvalidOperationException invalidOperationException) { response.Message = invalidOperationException.Message; } catch (ArgumentNullException argumentNullException) { response.Message = argumentNullException.Message; } return response; }
public BaseResponse DeleteLayout(int id) { var response = new BaseResponse(); //var derLayoutItems = new List<DerLayoutItem>(); //var res = new DeleteDerLayoutItemResponse(); try { var derLayout = DataContext.DerLayouts .Include(x => x.Items) .Single(x => x.Id == id); derLayout.IsDeleted = true; DataContext.Entry(derLayout).State = EntityState.Modified; DataContext.SaveChanges(); response.IsSuccess = true; } catch (Exception exception) { response.Message = exception.Message; } //derLayoutItems = derLayout.Items.ToList(); //foreach(var item in derLayoutItems) //{ // res = DeleteLayoutItem(id, item.Type); // if(res.Message != null) // { // break; // } //}; //if (res.Message == null) //{ // DataContext.DerLayouts.Remove(derLayout); // DataContext.SaveChanges(); //} else //{ // response.Message = res.Message; //} return response; }
public BaseResponse InsertOwnerPrivilege(Requests.FileManagerRolePrivilege.FilePrivilegeRequest request) { BaseResponse response = new BaseResponse(); try { var privilege = request.MapTo<FileManagerRolePrivilege>(); DataContext.FileManagerRolePrivileges.Add(privilege); DataContext.SaveChanges(); response.IsSuccess = true; } catch (InvalidOperationException inval) { response.Message = inval.Message; } catch (ArgumentNullException arg) { response.Message = arg.Message; } return response; }
private BaseResponse UpdateMultiAxis(SaveLayoutItemRequest request) { var response = new BaseResponse(); try { var derLayoutItem = DataContext.DerLayoutItems .Include(x => x.Artifact) .Include(x => x.Artifact.Measurement) .Include(x => x.Artifact.Series) .Single(x => x.Id == request.Id); var derLayout = new DerLayout { Id = request.DerLayoutId }; DataContext.DerLayouts.Attach(derLayout); derLayoutItem.DerLayout = derLayout; derLayoutItem.Column = request.Column; derLayoutItem.Row = request.Row; derLayoutItem.Type = request.Type; var derArtifact = new DerArtifact(); derArtifact.GraphicType = request.Type; derArtifact.HeaderTitle = request.Artifact.HeaderTitle; /*var measurement = new Measurement { Id = request.Artifact.MeasurementId }; DataContext.Measurements.Attach(measurement); derArtifact.Measurement = measurement;*/ derArtifact.Charts = new List<DerArtifactChart>(); foreach (var item in request.Artifact.MultiAxis.Charts) { var chart = item.MapTo<DerArtifactChart>(); var measurement = new Measurement { Id = item.MeasurementId }; if (DataContext.Measurements.Local.FirstOrDefault(x => x.Id == measurement.Id) == null) { DataContext.Measurements.Attach(measurement); } else { measurement = DataContext.Measurements.Local.FirstOrDefault(x => x.Id == measurement.Id); } DataContext.Measurements.Attach(measurement); chart.Measurement = measurement; foreach (var s in item.Series) { var serie = s.MapTo<DerArtifactSerie>(); var kpi = new Kpi { Id = s.KpiId }; if (DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpi.Id) == null) { DataContext.Kpis.Attach(kpi); } else { kpi = DataContext.Kpis.Local.FirstOrDefault(x => x.Id == kpi.Id); } serie.Kpi = kpi; serie.Artifact = derArtifact; chart.Series.Add(serie); } derArtifact.Charts.Add(chart); } derLayoutItem.Artifact = derArtifact; //DataContext.DerArtifacts.Add(derArtifact); //DataContext.DerLayoutItems.Add(derLayoutItem); var oldArtifact = DataContext.DerArtifacts .Include(x => x.Charts) .Include(x => x.Charts.Select(y => y.Series)) .Single(x => x.Id == request.Artifact.Id); foreach (var chart in oldArtifact.Charts.ToList()) { foreach (var series in chart.Series.ToList()) { DataContext.DerArtifactSeries.Remove(series); } DataContext.DerArtifactCharts.Remove(chart); } DataContext.DerArtifacts.Remove(oldArtifact); DataContext.SaveChanges(); } catch (Exception exception) { response.Message = exception.Message; } return response; }
public BaseResponse Delete(int Id) { var response = new BaseResponse(); try { var prosess = DataContext.ProcessBlueprints.Single(x => x.Id == Id); DataContext.ProcessBlueprints.Remove(prosess); DataContext.SaveChanges(); response.IsSuccess = true; } catch (DbUpdateException ex) { response.IsSuccess = false; response.Message = ex.Message; } return response; }
private BaseResponse UpdatePie(SaveLayoutItemRequest request) { var response = new BaseResponse(); try { var derLayoutItem = DataContext.DerLayoutItems .Include(x => x.Artifact) .Include(x => x.Artifact.Measurement) .Include(x => x.Artifact.Series) .Single(x => x.Id == request.Id); var derLayout = new DerLayout { Id = request.DerLayoutId }; DataContext.DerLayouts.Attach(derLayout); derLayoutItem.DerLayout = derLayout; derLayoutItem.Column = request.Column; derLayoutItem.Row = request.Row; derLayoutItem.Type = request.Type; var derArtifact = new DerArtifact(); derArtifact.ShowLegend = request.Artifact.ShowLegend; derArtifact.Is3D = request.Artifact.Is3D; derArtifact.HeaderTitle = request.Artifact.HeaderTitle; derArtifact.GraphicType = request.Type; derArtifact.Charts = new List<DerArtifactChart>(); var measurement = new Measurement { Id = request.Artifact.MeasurementId }; if (DataContext.Measurements.Local.FirstOrDefault(x => x.Id == measurement.Id) == null) { DataContext.Measurements.Attach(measurement); } else { measurement = DataContext.Measurements.Local.FirstOrDefault(x => x.Id == measurement.Id); } derArtifact.Measurement = measurement; var series = request.Artifact.Pie.Series.Select(x => new DerArtifactSerie { Color = x.Color, Kpi = DataContext.Kpis.FirstOrDefault(y => y.Id == x.KpiId), Artifact = derArtifact }).ToList(); derArtifact.Series = series; DataContext.DerArtifacts.Add(derArtifact); derLayoutItem.Artifact = derArtifact; //DataContext.DerLayoutItems.Add(derLayoutItem); var oldArtifact = new DerArtifact { Id = request.Artifact.Id }; if (DataContext.DerArtifacts.Local.FirstOrDefault(x => x.Id == oldArtifact.Id) == null) { DataContext.DerArtifacts.Attach(oldArtifact); } else { oldArtifact = DataContext.DerArtifacts.Local.FirstOrDefault(x => x.Id == oldArtifact.Id); } DataContext.DerArtifacts.Remove(oldArtifact); DataContext.SaveChanges(); response.IsSuccess = true; } catch (Exception exception) { response.Message = exception.Message; } return response; }
private BaseResponse UpdateKpiTarget(IEnumerable<ConfigurationViewModel.Item> data) { var response = new BaseResponse { IsSuccess = false, Message = "Data Not Valid" }; if (data != null) { var batch = new BatchUpdateTargetRequest(); foreach (var datum in data) { var prepare = new SaveKpiTargetRequest() { Value = datum.Value, KpiId = datum.KpiId, Periode = datum.Periode, PeriodeType = datum.PeriodeType, Remark = datum.Remark }; batch.BatchUpdateKpiTargetItemRequest.Add(prepare); } response = _kpiTargetService.BatchUpdateKpiTargetss(batch); } return response; }
private BaseResponse UpdateTank(SaveLayoutItemRequest request) { var response = new BaseResponse(); try { var derLayoutItem = DataContext.DerLayoutItems .Include(x => x.Artifact) .Include(x => x.Artifact.Tank) .Single(x => x.Id == request.Id); var derLayout = new DerLayout { Id = request.DerLayoutId }; DataContext.DerLayouts.Attach(derLayout); derLayoutItem.DerLayout = derLayout; derLayoutItem.Column = request.Column; derLayoutItem.Row = request.Row; derLayoutItem.Type = request.Type; var derArtifact = new DerArtifact();//request.MapTo<DerArtifact>(); derArtifact.HeaderTitle = request.Artifact.HeaderTitle; derArtifact.GraphicType = request.Type; derLayoutItem.Artifact = derArtifact; derLayoutItem.Artifact.Tank = request.Artifact.Tank.MapTo<DerArtifactTank>(); var volumeInventory = new Kpi { Id = request.Artifact.Tank.VolumeInventoryId }; if (DataContext.Kpis.Local.FirstOrDefault(x => x.Id == volumeInventory.Id) == null) { DataContext.Kpis.Attach(volumeInventory); } else { volumeInventory = DataContext.Kpis.Local.FirstOrDefault(x => x.Id == request.Artifact.Tank.VolumeInventoryId); } var daysToTankTop = new Kpi { Id = request.Artifact.Tank.DaysToTankTopId }; if (DataContext.Kpis.Local.FirstOrDefault(x => x.Id == daysToTankTop.Id) == null) { DataContext.Kpis.Attach(daysToTankTop); } else { daysToTankTop = DataContext.Kpis.Local.FirstOrDefault(x => x.Id == request.Artifact.Tank.DaysToTankTopId); } derLayoutItem.Artifact.Tank.VolumeInventory = volumeInventory; derLayoutItem.Artifact.Tank.DaysToTankTop = daysToTankTop; DataContext.DerArtifacts.Add(derArtifact); //DataContext.DerLayoutItems.Add(derLayoutItem); var oldArtifact = new DerArtifact { Id = request.Artifact.Id }; if (DataContext.DerArtifacts.Local.FirstOrDefault(x => x.Id == oldArtifact.Id) == null) { DataContext.DerArtifacts.Attach(oldArtifact); } else { oldArtifact = DataContext.DerArtifacts.Local.FirstOrDefault(x => x.Id == oldArtifact.Id); } DataContext.DerArtifacts.Remove(oldArtifact); DataContext.SaveChanges(); response.IsSuccess = true; } catch (Exception exception) { response.Message = exception.Message; } return response; }
public BaseResponse BatchUpdateKpiTargetss(BatchUpdateTargetRequest request) { var response = new BaseResponse(); try { int i = 0; foreach (var item in request.BatchUpdateKpiTargetItemRequest) { var kpiTarget = item.MapTo<KpiTarget>(); var exist = DataContext.KpiTargets.FirstOrDefault(x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode && x.Value == item.Value && x.Remark == item.Remark); //skip no change value if (exist != null) { continue; } var attachedEntity = DataContext.KpiTargets.FirstOrDefault(x => x.Kpi.Id == item.KpiId && x.PeriodeType == item.PeriodeType && x.Periode == item.Periode); if (attachedEntity != null) { kpiTarget.Id = attachedEntity.Id; } //jika tidak ada perubahan di skip aja //if (existing.Value.Equals(item.Value) && existing.Periode.Equals(item.Periode) && existing.Kpi.Id.Equals(item.KpiId) && existing.PeriodeType.Equals(item.PeriodeType)) { // break; //} if (kpiTarget.Id != 0) { //var attachedEntity = DataContext.KpiAchievements.Find(item.Id); if (attachedEntity != null && DataContext.Entry(attachedEntity).State != EntityState.Detached) { DataContext.Entry(attachedEntity).State = EntityState.Detached; } DataContext.KpiTargets.Attach(kpiTarget); DataContext.Entry(kpiTarget).State = EntityState.Modified; } else { kpiTarget.Kpi = DataContext.Kpis.FirstOrDefault(x => x.Id == item.KpiId); DataContext.KpiTargets.Add(kpiTarget); } i++; } DataContext.SaveChanges(); response.IsSuccess = true; if (i > 0) { response.Message = string.Format("{0} KPI Target items has been updated successfully", i.ToString()); } else { response.Message = "File Successfully Parsed, but no data changed!"; } } catch (InvalidOperationException invalidOperationException) { response.Message = invalidOperationException.Message; } catch (ArgumentNullException argumentNullException) { response.Message = argumentNullException.Message; } return response; }
public SaveLayoutItemResponse SaveLayoutItem(SaveLayoutItemRequest request) { var baseResponse = new BaseResponse(); switch (request.Type.ToLowerInvariant()) { case "line": { baseResponse = request.Id > 0 ? UpdateLineChart(request) : SaveLineChart(request); break; } case "multiaxis": { baseResponse = request.Id > 0 ? UpdateMultiAxis(request) : SaveMultiAxis(request); break; } case "pie": { baseResponse = request.Id > 0 ? UpdatePie(request) : SavePie(request); break; } case "tank": { baseResponse = request.Id > 0 ? UpdateTank(request) : SaveTank(request); break; } case "highlight": { baseResponse = request.Id > 0 ? UpdateHighlight(request) : SaveHighlight(request); break; } case "weather": case "alert": case "wave": case "nls": { baseResponse = SaveDynamicHighlight(request); break; } case "avg-ytd-key-statistic": case "safety": case "lng-and-cds": case "security": case "job-pmts": case "total-feed-gas": case "table-tank": case "mgdp": case "hhv": case "lng-and-cds-production": case "weekly-maintenance": case "critical-pm": case "procurement": case "indicative-commercial-price": case "plant-availability": case "economic-indicator": { baseResponse = request.Id > 0 ? UpdateKpiInformations(request) : SaveKpiInformations(request); break; } case "dafwc": { baseResponse = SaveDafwc(request); break; } } var response = new SaveLayoutItemResponse { IsSuccess = baseResponse.IsSuccess, Message = baseResponse.Message }; return response; }
private BaseResponse _UpdateEconomic(List<OperationDataConfigurationViewModel.Item> datas) { var response = new BaseResponse(); if (datas != null) { var batch = new BatchUpdateOperationDataRequest(); foreach (var data in datas) { //var prepare = new UpdateOperationDataRequest() { Id = data.Id, KpiId = data.KpiId, Periode = data.Periode, Value = data.Value, PeriodeType = data.PeriodeType, Remark = data.Remark, KeyOperationConfigId = data.OperationId, ScenarioId = data.ScenarioId };// data.MapTo<UpdateKpiAchievementItemRequest>(); //batch.BatchUpdateOperationDataItemRequest.Add(prepare); } response = _operationDataService.BatchUpdateOperationDatas(batch); } return response; }
public BaseResponse DeleteKpiAchievement(int kpiId, DateTime periode, PeriodeType periodeType) { var response = new BaseResponse(); try { var achievements = DataContext.KpiAchievements.Where( x => x.Kpi.Id == kpiId && x.Periode == periode && x.PeriodeType == periodeType).ToList(); foreach (var achievement in achievements) { DataContext.KpiAchievements.Remove(achievement); } DataContext.SaveChanges(); response.IsSuccess = true; } catch (InvalidOperationException invalidOperationException) { response.Message = invalidOperationException.Message; response.ExceptionType = typeof (InvalidOperationException); } catch (ArgumentNullException argumentNullException) { response.Message = argumentNullException.Message; response.ExceptionType = typeof(ArgumentNullException); } return response; }