543 lines
21 KiB
C#
543 lines
21 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Web.Http;
|
|
using System.Web.Http.Results;
|
|
using Kendo.Mvc.UI;
|
|
using Kreta.BusinessLogic.Classes;
|
|
using Kreta.BusinessLogic.Classes.ComboBox;
|
|
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;
|
|
using Kreta.Framework.Entities;
|
|
using Kreta.Framework.Util;
|
|
using Kreta.Resources;
|
|
using Kreta.Web.Areas.Adminisztracio.Models;
|
|
using Kreta.Web.Helpers;
|
|
using Kreta.Web.Helpers.Error;
|
|
using Kreta.Web.Helpers.Grid;
|
|
using Kreta.Web.Security;
|
|
using Newtonsoft.Json;
|
|
using SDA.DataProvider;
|
|
|
|
namespace Kreta.Web.Areas.Adminisztracio.ApiControllers
|
|
{
|
|
[ApiRoleClaimsAuthorize(true)]
|
|
[ApiRolePackageAuthorize(KretaClaimPackages.Adminisztrator.ClaimValue)]
|
|
public class AdatszotarApiController : ApiController
|
|
{
|
|
/// <summary>
|
|
/// Adatszótár grid visszaadás
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
public DataSourceResult GetAdatszotarGrid(string data, DataSourceRequest request)
|
|
{
|
|
var model = JsonConvert.DeserializeObject<AdatszotarSearchModel>(data);
|
|
|
|
if (!model.AdatszotarTipusTypeId.IsEntityId())
|
|
{
|
|
return new DataSourceResult();
|
|
}
|
|
|
|
var (gridParameter, modelList) = GetGridData(request, model.AdatszotarTipusTypeId);
|
|
|
|
return modelList.ToDataSourceResult(gridParameter);
|
|
}
|
|
|
|
public HttpResponseMessage GetExport(string data, DataSourceRequest request)
|
|
{
|
|
var model = JsonConvert.DeserializeObject<AdatszotarSearchModel>(data);
|
|
|
|
if (!model.AdatszotarTipusTypeId.IsEntityId())
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, ErrorResource.AdatszotarTipusaNincsMegadva);
|
|
}
|
|
|
|
try
|
|
{
|
|
var (gridParameter, modelList) = GetGridData(request, model.AdatszotarTipusTypeId);
|
|
|
|
modelList = modelList.SortingAndPaging(gridParameter.OrderDictionary);
|
|
|
|
var exportAttribute = model.AdatszotarTipusTypeId == (int)GeneratedAdatszotarTipusEnum.NapTipus ? AdatszotarGridModel.AdatszotarNapTipusExportAttributeId : AdatszotarGridModel.AdatszotarExportAttributeId;
|
|
|
|
var simpleExportColumnCos = SimpleExportLogic.GetSimpleExportColumnCos<AdatszotarGridModel>(exportAttribute);
|
|
|
|
var memoryStream = SimpleExportLogic.GetExport(AdminisztracioResource.AdatszotarExportSheetName, simpleExportColumnCos, modelList, ClaimData.SelectedTanevID.Value);
|
|
|
|
return HttpResponseExtensions.GetFileHttpResponse(memoryStream.ToArray(), AdminisztracioResource.AdatszotarExportFileName);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, ErrorResource.HibaTortentAFajlExportalasaKozben) { UnHandledException = ex };
|
|
}
|
|
}
|
|
|
|
private (GridParameters gridParameter, List<AdatszotarGridModel> modelList) GetGridData(DataSourceRequest request, int? adatszotarTipusTypeId)
|
|
{
|
|
var gridParameter = Converter.GridParameter(request);
|
|
|
|
var coList = new AdatszotarHelper(ConnectionTypeExtensions.GetSessionConnectionType()).GetAdatszotarCoList((GeneratedAdatszotarTipusEnum)adatszotarTipusTypeId);
|
|
|
|
var modelList = new List<AdatszotarGridModel>();
|
|
|
|
foreach (var co in coList)
|
|
{
|
|
var gridModel = new AdatszotarGridModel(co);
|
|
modelList.Add(gridModel);
|
|
}
|
|
|
|
return (gridParameter, modelList);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ApiValidateAjaxAntiForgeryToken]
|
|
public HttpResponseMessage SaveAdatszotarData(AdatszotarGridModel model)
|
|
{
|
|
try
|
|
{
|
|
var helper = new AdatszotarHelper(ConnectionTypeExtensions.GetSessionConnectionType());
|
|
if (model.AdatszotarTipusId == (int)GeneratedAdatszotarTipusEnum.SorolasOkaTipus)
|
|
{
|
|
if (model.ZaradekSzovegList.Any(x => !string.IsNullOrWhiteSpace(x.Szoveg) && x.Szoveg.Length > 255))
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, string.Format(CommonResource.ZaradekSzoveg0Max255Karakter, ((AnyanyelvEnum)model.ZaradekSzovegList.First(x => x.Szoveg.Length > 255).NyelvId).GetDisplayName(ClaimData.SelectedTanevID.Value)));
|
|
}
|
|
}
|
|
|
|
if (Core.Constants.NemBovithetoAdatszotarTipusLista.Contains(model.AdatszotarTipusId) || model.IsProtected)
|
|
{
|
|
if (!model.AdatszotarID.HasValue || model.AdatszotarID.Value == 0)
|
|
{
|
|
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ErrorResource.KivalasztottAdatszotarNemBovitheto);
|
|
}
|
|
else
|
|
{
|
|
var co = helper.GetAdatszotarElem(model.AdatszotarID.Value);
|
|
co.Lathato = model.IsLathato;
|
|
if (model.AdatszotarTipusId == (int)GeneratedAdatszotarTipusEnum.NapTipus)
|
|
{
|
|
co.IsSorszamozando = model.IsSorszamozando;
|
|
co.IsTanorai = model.IsTanorai;
|
|
co.IsTanorankivuli = model.IsTanorankivuli;
|
|
co.IsLeNemKotottMunkaido = model.IsLeNemKotottMunkaido;
|
|
}
|
|
|
|
if (model.AdatszotarTipusId == (int)GeneratedAdatszotarTipusEnum.ErtekelesMod)
|
|
{
|
|
co.Color = model.Color != null ? model.Color.Replace("#", "") : "";
|
|
co.IsBold = model.IsBold;
|
|
co.Suly = model.Suly;
|
|
}
|
|
|
|
if (model.AdatszotarTipusId == (int)GeneratedAdatszotarTipusEnum.SorolasOkaTipus)
|
|
{
|
|
co.ZaradekSzovegCoList =
|
|
model.ZaradekSzovegList.Where(x => x.NyelvId != (int)AnyanyelvEnum.magyar).ToList().ConvertAll(x => new AdatszotarZaradekNyelvCo
|
|
{
|
|
NyelvId = x.NyelvId,
|
|
Name = x.Szoveg
|
|
});
|
|
}
|
|
|
|
helper.Update(co, ClaimData.KovTanevID);
|
|
|
|
return new HttpResponseMessage(HttpStatusCode.OK);
|
|
}
|
|
}
|
|
|
|
if (ModelState.IsValid &&
|
|
(model.AdatszotarTipusId == (int)GeneratedAdatszotarTipusEnum.ErtekelesMod ||
|
|
model.AdatszotarTipusId == (int)GeneratedAdatszotarTipusEnum.NapTipus ||
|
|
model.AdatszotarTipusId == (int)GeneratedAdatszotarTipusEnum.SorolasOkaTipus ||
|
|
!model.IsProtected))
|
|
{
|
|
if (model.AdatszotarID.HasValue && model.AdatszotarID > 0)
|
|
{
|
|
AdatszotarCO co = model.ToCo();
|
|
helper.Update(co, ClaimData.KovTanevID);
|
|
}
|
|
else
|
|
{
|
|
AdatszotarCO co = model.ToCo();
|
|
|
|
helper.Insert(co);
|
|
}
|
|
|
|
return new HttpResponseMessage(HttpStatusCode.OK);
|
|
}
|
|
else if (model.AdatszotarTipusId == (int)GeneratedAdatszotarTipusEnum.ErtekelesMod || model.AdatszotarTipusId == (int)GeneratedAdatszotarTipusEnum.NapTipus || !model.IsProtected)
|
|
{
|
|
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ErrorResource.VedettAdatszotarnakAMegnevezesetNemLehetModositani);
|
|
}
|
|
else
|
|
{
|
|
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
|
|
}
|
|
}
|
|
catch (UniqueKeyViolationException)
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, CommonResource.AMegadottNevMarLetezik);
|
|
}
|
|
catch (CannotBeInsertedException ex)
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adatszótár láthatóságának beállítása és mentése
|
|
/// </summary>
|
|
/// <param name="elemId"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiValidateAjaxAntiForgeryToken]
|
|
public HttpResponseMessage SetLathatosag([FromBody] string elemId)
|
|
{
|
|
AdatszotarHelper helper = new AdatszotarHelper(ConnectionTypeExtensions.GetSessionConnectionType());
|
|
|
|
var co = helper.GetAdatszotarElem(Convert.ToInt32(elemId));
|
|
co.Lathato = !co.Lathato;
|
|
helper.Update(co, ClaimData.KovTanevID);
|
|
|
|
return new HttpResponseMessage(HttpStatusCode.OK);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adatszótár aktiválás mentés
|
|
/// </summary>
|
|
/// <param name="parameters"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiValidateAjaxAntiForgeryToken]
|
|
public HttpResponseMessage SaveAdatszotarActivate([FromBody] ItemListModel parameters)
|
|
{
|
|
if (parameters.Items.Count == 0)
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, CommonResource.ValasszonKiLegalabbEgyElemet);
|
|
}
|
|
|
|
bool success = GroupModifyItemActivity(parameters, true);
|
|
if (!success)
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, AdminisztracioResource.AktivalasNemLehetsegesAzElemVedett);
|
|
}
|
|
|
|
return new HttpResponseMessage(HttpStatusCode.OK);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adatszótár deaktiválás mentés
|
|
/// </summary>
|
|
/// <param name="parameters"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiValidateAjaxAntiForgeryToken]
|
|
public HttpResponseMessage SaveAdatszotarDeactivate([FromBody] ItemListModel parameters)
|
|
{
|
|
if (parameters.Items.Count == 0)
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, CommonResource.ValasszonKiLegalabbEgyElemet);
|
|
}
|
|
|
|
bool success = GroupModifyItemActivity(parameters, false);
|
|
if (!success)
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, AdminisztracioResource.DeaktivalasNemLehetsegesAzElemVedett);
|
|
}
|
|
|
|
return new HttpResponseMessage(HttpStatusCode.OK);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ApiValidateAjaxAntiForgeryToken]
|
|
public HttpResponseMessage SaveElementsOrder(OrderItemListModel parameters)
|
|
{
|
|
if (parameters.Items.Count == 0)
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, AdminisztracioResource.NincsAdatszotarLekerveMentesCsakKeresesUtanLehetseges);
|
|
}
|
|
|
|
foreach (var item in parameters.Items)
|
|
{
|
|
if (item.Sorszam <= 0)
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, AdminisztracioResource.SorszamErtekeCsakPozitivSzamLehet);
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
var helper = new AdatszotarHelper(ConnectionTypeExtensions.GetSessionConnectionType());
|
|
|
|
foreach (var item in parameters.Items)
|
|
{
|
|
helper.UpdateSorszam(item.Id, item.Sorszam, ClaimData.KovTanevID);
|
|
}
|
|
|
|
return new HttpResponseMessage(HttpStatusCode.OK);
|
|
}
|
|
catch (CannotBeModifiedException ex)
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, ex.Message);
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adatszótár törlés
|
|
/// </summary>
|
|
/// <param name="parameters"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiValidateAjaxAntiForgeryToken]
|
|
public HttpResponseMessage DeleteAdatszotar([FromBody] int id)
|
|
{
|
|
try
|
|
{
|
|
var helper = new AdatszotarHelper(ConnectionTypeExtensions.GetSessionConnectionType());
|
|
|
|
if (helper.IsProtected(id))
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, AdminisztracioResource.TorlesNemLehetsegesAzElemVedett);
|
|
}
|
|
|
|
helper.Delete(id);
|
|
|
|
return new HttpResponseMessage(HttpStatusCode.OK);
|
|
}
|
|
catch (EntityDeleteFailedException ex)
|
|
{
|
|
var uzenet = string.Format(ErrorResource.AdatszotarNemTorolhetoKapcsolatMiatt, ex.ConnectionErrorMessage);
|
|
throw new StatusError(HttpStatusCode.BadRequest, uzenet);
|
|
}
|
|
catch (StatusError)
|
|
{
|
|
throw;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, ErrorResource.AzElemNemTorolhetoMertEgyVagyTobbKapcsolodasaVan) { UnHandledException = e };
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[ApiValidateAjaxAntiForgeryToken]
|
|
public IHttpActionResult DeleteSelectedAdatszotar(List<int> idList)
|
|
{
|
|
var helper = new AdatszotarHelper(ConnectionTypeExtensions.GetSessionConnectionType());
|
|
|
|
string errorMsg = string.Empty;
|
|
var counter = 0;
|
|
|
|
foreach (var id in idList)
|
|
{
|
|
try
|
|
{
|
|
if (helper.IsProtected(id))
|
|
{
|
|
var adatszotarElem = helper.GetAdatszotarElem(id);
|
|
errorMsg += $"{adatszotarElem.Megnevezes}: {AdminisztracioResource.TorlesNemLehetsegesAzElemVedett}" + Environment.NewLine + Environment.NewLine;
|
|
continue;
|
|
}
|
|
|
|
helper.Delete(id);
|
|
|
|
counter++;
|
|
}
|
|
catch (EntityDeleteFailedException ex)
|
|
{
|
|
var adatszotarElem = helper.GetAdatszotarElem(id);
|
|
var uzenet = string.Format(ErrorResource.AdatszotarNemTorolhetoKapcsolatMiatt, ex.ConnectionErrorMessage);
|
|
|
|
errorMsg += $"{adatszotarElem.Megnevezes}: {uzenet}" + Environment.NewLine + Environment.NewLine;
|
|
continue;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
var adatszotarElem = helper.GetAdatszotarElem(id);
|
|
errorMsg += $"{adatszotarElem.Megnevezes}: {ErrorResource.AzElemNemTorolhetoMertEgyVagyTobbKapcsolodasaVan}" + 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);
|
|
}
|
|
|
|
#region Helpers
|
|
|
|
private AdatszotarCO Convert_Model_to_CO(AdatszotarGridModel model)
|
|
{
|
|
AdatszotarCO co = new AdatszotarCO
|
|
{
|
|
ID = model.AdatszotarID,
|
|
AdatszotarTipus = model.AdatszotarTipusNev,
|
|
AdatszotarTipusId = model.AdatszotarTipusId,
|
|
Lathato = model.IsLathato,
|
|
Megnevezes = model.Megnevezes,
|
|
Sorszam = model.Sorszam,
|
|
Megnevezes1 = model.Megnevezes1,
|
|
Megnevezes2 = model.Megnevezes2,
|
|
Megnevezes3 = model.Megnevezes3,
|
|
Megnevezes4 = model.Megnevezes4,
|
|
Color = model.Color != null ? model.Color.Replace("#", "") : "",
|
|
IsBold = model.IsBold,
|
|
Suly = model.Suly,
|
|
Protected = model.IsProtected,
|
|
IsSorszamozando = model.IsSorszamozando,
|
|
IsTanorai = model.IsTanorai,
|
|
IsTanorankivuli = model.IsTanorankivuli,
|
|
IsLeNemKotottMunkaido = model.IsLeNemKotottMunkaido,
|
|
};
|
|
|
|
return co;
|
|
}
|
|
|
|
private AdatszotarGridModel Convert_CO_to_Model(AdatszotarCO co)
|
|
{
|
|
AdatszotarGridModel model = new AdatszotarGridModel()
|
|
{
|
|
ID = string.Empty,
|
|
AdatszotarTipusNev = co.AdatszotarTipus,
|
|
AdatszotarTipusId = co.AdatszotarTipusId,
|
|
AdatszotarID = co.ID,
|
|
IsLathato = co.Lathato ?? false,
|
|
Megnevezes = co.Megnevezes,
|
|
Sorszam = co.Sorszam,
|
|
Megnevezes1 = co.Megnevezes1,
|
|
Megnevezes2 = co.Megnevezes2,
|
|
Megnevezes3 = co.Megnevezes3,
|
|
Megnevezes4 = co.Megnevezes4,
|
|
Color = "#" + co.Color != null ? co.Color : "000000",
|
|
IsBold = co.IsBold,
|
|
Suly = co.Suly,
|
|
IsProtected = co.Protected,
|
|
IsSorszamozando = co.IsSorszamozando,
|
|
IsTanorai = co.IsTanorai,
|
|
IsTanorankivuli = co.IsTanorankivuli,
|
|
IsLeNemKotottMunkaido = co.IsLeNemKotottMunkaido,
|
|
};
|
|
|
|
return model;
|
|
}
|
|
|
|
public AdatszotarGridModel GetAdatszotarElem(int id)
|
|
{
|
|
return new AdatszotarGridModel(new AdatszotarHelper(ConnectionTypeExtensions.GetSessionConnectionType()).GetAdatszotarElem(id));
|
|
}
|
|
|
|
private bool GroupModifyItemActivity(ItemListModel parameters, bool active)
|
|
{
|
|
var helper = new AdatszotarHelper(ConnectionTypeExtensions.GetSessionConnectionType());
|
|
|
|
var adatszotarIds = new List<int>();
|
|
foreach (var item in parameters.Items)
|
|
{
|
|
adatszotarIds.Add(item.Id);
|
|
}
|
|
|
|
bool success = helper.ModifyItemActivity(adatszotarIds, active);
|
|
return success;
|
|
}
|
|
|
|
private DataSet FormatDataset(DataSet ds)
|
|
{
|
|
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
|
|
{
|
|
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
|
|
{
|
|
if (ds.Tables[0].Columns[j].ColumnName == "Lathato")
|
|
ds.Tables[0].Rows[i]["Lathato"] = SDAConvert.ToBooleanFromTF(ds.Tables[0].Rows[i]["Lathato"], false);
|
|
|
|
if (ds.Tables[0].Columns[j].ColumnName == "Protected")
|
|
ds.Tables[0].Rows[i]["Protected"] = SDAConvert.ToBooleanFromTF(ds.Tables[0].Rows[i]["Protected"], false);
|
|
}
|
|
}
|
|
|
|
return ds;
|
|
}
|
|
|
|
public class ItemListModel
|
|
{
|
|
public List<ItemModel> Items { set; get; }
|
|
}
|
|
|
|
public class ItemModel
|
|
{
|
|
public int Id { set; get; }
|
|
}
|
|
|
|
public class OrderItemListModel
|
|
{
|
|
public List<OrderItemModel> Items { set; get; }
|
|
}
|
|
|
|
public class OrderItemModel
|
|
{
|
|
public int Id { set; get; }
|
|
|
|
public int Sorszam { set; get; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
public JsonResult<List<ComboBoxListItem>> GetAdatszotarTipusList()
|
|
{
|
|
var tipusok = new AdatszotarHelper(ConnectionTypeExtensions.GetSessionConnectionType()).GetEditableAdatszotarTipusok().OrderBy(x => x.megnevezes).ToList();
|
|
|
|
var dropdownListItems = new List<ComboBoxListItem>();
|
|
|
|
foreach ((int id, string megnevezes) in tipusok)
|
|
{
|
|
var sli = new ComboBoxListItem() { Text = megnevezes, Value = id.ToString() };
|
|
dropdownListItems.Add(sli);
|
|
}
|
|
|
|
return Json(dropdownListItems);
|
|
}
|
|
|
|
public HttpResponseMessage ChangeSelectedAdatszotarToLathato(List<int> idList)
|
|
{
|
|
AdatszotarHelper helper = new AdatszotarHelper(ConnectionTypeExtensions.GetSessionConnectionType());
|
|
|
|
foreach (var id in idList)
|
|
{
|
|
var co = helper.GetAdatszotarElem(Convert.ToInt32(id));
|
|
co.Lathato = true;
|
|
helper.Update(co, ClaimData.KovTanevID);
|
|
}
|
|
|
|
return new HttpResponseMessage(HttpStatusCode.OK);
|
|
}
|
|
|
|
public HttpResponseMessage ChangeSelectedAdatszotarToRejtett(List<int> idList)
|
|
{
|
|
AdatszotarHelper helper = new AdatszotarHelper(ConnectionTypeExtensions.GetSessionConnectionType());
|
|
|
|
foreach (var id in idList)
|
|
{
|
|
var co = helper.GetAdatszotarElem(Convert.ToInt32(id));
|
|
co.Lathato = false;
|
|
helper.Update(co, ClaimData.KovTanevID);
|
|
}
|
|
|
|
return new HttpResponseMessage(HttpStatusCode.OK);
|
|
}
|
|
}
|
|
}
|