using System; using System.Collections.Generic; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.ModelBinding; using Kendo.Mvc.UI; using Kreta.BusinessLogic.Exceptions; using Kreta.BusinessLogic.HelperClasses; using Kreta.BusinessLogic.Helpers; using Kreta.BusinessLogic.Logic; using Kreta.BusinessLogic.Security; using Kreta.Core; using Kreta.Enums.ManualEnums; using Kreta.Framework.Entities; using Kreta.Framework.Util; using Kreta.Resources; using Kreta.Web.Areas.DualisKepzes.Models; using Kreta.Web.Helpers; using Kreta.Web.Helpers.Error; using Kreta.Web.Helpers.Grid; using Kreta.Web.Security; using Newtonsoft.Json; namespace Kreta.Web.Areas.DualisKepzes.ApiControllers { [ApiRoleClaimsAuthorize(true)] [ApiRolePackageAuthorize(KretaClaimPackages.Adminisztrator.ClaimValue, KretaClaimPackages.Dualis_Admin.ClaimValue)] public class DualisKepzohelyApiController : ApiController { [ApiRolePackageAuthorize(KretaClaimPackages.Adminisztrator.ClaimValue, KretaClaimPackages.Dualis_Admin.ClaimValue)] public DataSourceResult GetDualisKepzohelyGrid(string data, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] KretaGridDataSourceRequest request) { return GetDualisKepzohelyGrid(data, string.Empty, request); } [ApiRolePackageAuthorize(KretaClaimPackages.Adminisztrator.ClaimValue, KretaClaimPackages.Dualis_Admin.ClaimValue)] public DataSourceResult GetDualisKepzohelyGrid(string data, string parentId, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] KretaGridDataSourceRequest request) { var (gridParameter, modelList) = GetGridData(data, parentId, string.IsNullOrWhiteSpace(parentId) ? SzervezetAdatokHalmazaEnum.Szervezet : SzervezetAdatokHalmazaEnum.Alszervezetek, request); return modelList.ToDataSourceResult(gridParameter); } public HttpResponseMessage GetExport(string data, DataSourceRequest request) { try { var (gridParameter, modelList) = GetGridData(data, null, SzervezetAdatokHalmazaEnum.SzervezetEsAlSzervezetek, request); modelList = modelList.SortingAndPaging(gridParameter.OrderDictionary); var simpleExportColumnCos = SimpleExportLogic.GetSimpleExportColumnCos(DualisKepzohelyGridModel.DualisKepzohelyListajaExportAttributeId); var memoryStream = SimpleExportLogic.GetExport(DualisResource.DualisKepzohelyekListajaExportSheetName, simpleExportColumnCos, modelList, ClaimData.SelectedTanevID.Value); return HttpResponseExtensions.GetFileHttpResponse(memoryStream.ToArray(), $"{DualisResource.DualisKepzoHelyekExportFilename}_{DateTime.Now:yyyy_MM_dd}.xlsx"); } catch (Exception ex) { throw new StatusError(HttpStatusCode.BadRequest, ErrorResource.HibaTortentAFajlExportalasaKozben) { UnHandledException = ex }; } } [HttpPost] [ApiValidateAjaxAntiForgeryToken] public HttpResponseMessage SaveDualisKepzoHely(DualisKepzohelyModel model) { DualisKepzohelyModel.ValidateModel(model, ModelState); if (ModelState.IsValid) { try { var helper = new SzervezetHelper(ConnectionTypeExtensions.GetSessionConnectionType()); var co = DualisKepzohelyModel.ConvertModelToCO(model); helper.InsertOrUpdateDualisKepzohely(co); return Request.CreateResponse(HttpStatusCode.OK, new { parentId = model.ParentSzervezetId }); } catch (Exception ex) { throw new StatusError(HttpStatusCode.InternalServerError, ErrorResource.NemSikerultAMuvelet) { UnHandledException = ex }; } } return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } [HttpPost] [ApiValidateAjaxAntiForgeryToken] public HttpResponseMessage DeleteDualisKepzoHely(int id) { try { var h = new SzervezetHelper(ConnectionTypeExtensions.GetSessionConnectionType()); h.DeleteDualisKepzohely(id); return new HttpResponseMessage(HttpStatusCode.OK); } catch (CannotBeDeletedException ex) { throw new StatusError(HttpStatusCode.BadRequest, ex.Message); } catch (EntityDeleteFailedException ex) { var uzenet = string.Format(ErrorResource.Az0NemTorolhetoMertEgyVagyTobbKapcsolodasaVanKapcsolatok1, DualisResource.DualisKepzohely, ex.ConnectionErrorMessage); throw new StatusError(HttpStatusCode.BadRequest, uzenet); } } [HttpPost] [ApiValidateAjaxAntiForgeryToken] public IHttpActionResult DeleteSelectedDualisKepzoHely(List idList) { var h = new SzervezetHelper(ConnectionTypeExtensions.GetSessionConnectionType()); string errorMsg = string.Empty, entityName = string.Empty; var counter = 0; foreach (var id in idList) { try { h.DeleteDualisKepzohely(id); counter++; } catch (CannotBeDeletedException ex) { errorMsg += $"{ex.Message}{Environment.NewLine}"; continue; } catch (EntityDeleteFailedException ex) { var dualisKepzohely = h.GetDualisKepzohelyById(id); var errorMessage = string.Format(ErrorResource.NemTorolhetoKapcsolatMiatt, string.Format("{0} ({1})", dualisKepzohely.KepzohelyNeve, dualisKepzohely.KepzohelyAdoszama), ex.ConnectionErrorMessage); errorMsg += $"{errorMessage}{Environment.NewLine}{Environment.NewLine}"; continue; } } if (string.IsNullOrWhiteSpace(errorMsg)) { return Json(new { Message = string.Format(ErrorResource.NSorTorlesSikeres, counter) }); } if (counter > 0) { errorMsg += Environment.NewLine + string.Format(ErrorResource.NSorTorlesSikeres, counter); } throw new StatusError(HttpStatusCode.BadRequest, errorMsg); } private (GridParameters gridParameter, List modelList) GetGridData(string data, string parentId, SzervezetAdatokHalmazaEnum adatokHalmaza, DataSourceRequest request) { var model = JsonConvert.DeserializeObject(data); var gridParameter = Converter.GridParameter(request); var pId = string.IsNullOrWhiteSpace(parentId) ? (int?)null : int.Parse(parentId); if (!model.SzervezetId.HasValue) { model.SzervezetId = pId; } var szervezetHelper = new SzervezetHelper(ConnectionTypeExtensions.GetSessionConnectionType()); var felhasznaloSzervezetId = szervezetHelper.GetAlkalmazottSzervezetId(ClaimData.FelhasznaloId); var coList = szervezetHelper.GetDualisKepzohelyekListajaCoList(model.ConvertToCO(), (int)adatokHalmaza, felhasznaloSzervezetId); var modelList = new List(); foreach (var co in coList) { var gridModel = new DualisKepzohelyGridModel(co); modelList.Add(gridModel); } return (gridParameter, modelList); } public DataSourceResult GetDualisCsoportGrid(string id, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request) { var helper = new SzervezetHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()); helper.GridParameters = Converter.GridParameter(request); var ds = helper.GetDualisKepzohelyCsoportjai(int.Parse(id)); return ds.ToDataSourceResult(); } public DataSourceResult GetDualisHelyszinGrid(string id, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request) { var helper = new SzervezetHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()); helper.GridParameters = Converter.GridParameter(request); var ds = helper.GetDualisKepzohelyHelyszinjei(int.Parse(id)); return ds.ToDataSourceResult(); } public DataSourceResult GetDualisOktatoGrid(string id, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request) { var helper = new SzervezetHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()); helper.GridParameters = Converter.GridParameter(request); var ds = helper.GetDualisKepzohelyOktatoi(int.Parse(id)); return ds.ToDataSourceResult(); } public DataSourceResult GetDualisTanuloGrid(string id, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request) { var helper = new SzervezetHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()); helper.GridParameters = Converter.GridParameter(request); var ds = helper.GetDualisKepzohelyTanuloi(int.Parse(id)); return ds.ToDataSourceResult(); } public DataSourceResult GetDualisTantargyGrid(string id, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request) { var helper = new SzervezetHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()); helper.GridParameters = Converter.GridParameter(request); var ds = helper.GetDualisKepzohelyTantargyai(int.Parse(id)); return ds.ToDataSourceResult(); } } }