using FrameworkDAL.CustomCode.PortLet; using FrameworkDAL.DTO.Page; using FrameworkDAL.DTO.Portlet; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.ResponseStandard; using GB5Shared.Validation; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; namespace FrameworkBLL.PortLet { public class PortletBLL : IPortletBLL { private readonly IPortletDAL _PortletDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public PortletBLL(IPortletDAL PortletDAL, AutoNumber AutoNumber, IValidation Validation) { _PortletDAL = PortletDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetPortlet(int PortletId, LoginDTO LoginDTO) { try { return await _PortletDAL.GetPortlet(PortletId, LoginDTO); } catch (Exception) { throw; } } public async Task SavePortlet(PortletDTO PortletDTO, LoginDTO LoginDTO) { if (PortletDTO == null) throw new ArgumentNullException(nameof(PortletDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(PortletDTO.PortletCode, nameof(PortletDTO.PortletCode)); await _Validation.NotEmpty(PortletDTO.PortletName, nameof(PortletDTO.PortletName)); if (PortletDTO.DefaultCriteriaConfigId > 0 && !await _PortletDAL.CriteriaConfigExists(PortletDTO.DefaultCriteriaConfigId, LoginDTO)) throw new ArgumentException($"DefaultCriteriaConfigId {PortletDTO.DefaultCriteriaConfigId} does not refer to an active criteria config."); // Phase 4: PublishChannelName/PublishFieldName (and the Subscribe pair) are a // paired contract — one without the other is a half-configured pub/sub binding // that would silently do nothing client-side. if (!string.IsNullOrWhiteSpace(PortletDTO.PortletPublishChannelName) != !string.IsNullOrWhiteSpace(PortletDTO.PortletPublishFieldName)) throw new ArgumentException("PortletPublishChannelName and PortletPublishFieldName must be set together, or not at all."); if (!string.IsNullOrWhiteSpace(PortletDTO.PortletSubscribeChannelName) != !string.IsNullOrWhiteSpace(PortletDTO.PortletSubscribeFieldName)) throw new ArgumentException("PortletSubscribeChannelName and PortletSubscribeFieldName must be set together, or not at all."); PortletDTO.PortletModifiedById = LoginDTO.UserId; PortletDTO.PortletModifiedOn = DateTime.UtcNow; int count = 1; if (PortletDTO.PortletId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "PORTLET", LoginDTO); PortletDTO.PortletId = autoNumberDTO.StartNumber; PortletDTO.PortletCreatedById = LoginDTO.UserId; PortletDTO.PortletCreatedOn = DateTime.UtcNow; await _PortletDAL.SavePortlet(PortletDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {PortletDTO.PortletId}"; } else { await _PortletDAL.UpdatePortlet(PortletDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {PortletDTO.PortletId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && PortletDTO.PortletId > 0) await _AutoNumber.RollbackAutoNumber("PORTLET", PortletDTO.PortletId, LoginDTO); throw; } } public async Task DeletePortlet(int PortletId, LoginDTO LoginDTO) { try { return await _PortletDAL.DeletePortlet(PortletId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListPortlet(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _PortletDAL.GetSelectListPortlet(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task GetPortletListWithCriteria(int UserId, int PageId, LoginDTO LoginDTO, int DashboardId = -1) { try { return await _PortletDAL.GetPortletListWithCriteria(UserId, PageId, LoginDTO, DashboardId); } catch (Exception) { throw; } } public async Task GetPageWithMenuPageList(int RoleId, LoginDTO LoginDTO) { try { return await _PortletDAL.GetPageWithMenuPageList(RoleId, LoginDTO); } catch (Exception) { throw; } } public async Task GetPageList(int UserId, LoginDTO LoginDTO) { try { return await _PortletDAL.GetPageList(UserId, LoginDTO); } catch (Exception) { throw; } } public async Task GetPortletList(int UserId, int PageId, LoginDTO LoginDTO) { try { return await _PortletDAL.GetPortletList(UserId, PageId, LoginDTO); } catch (Exception) { throw; } } public async Task GetPortletListForUser(int UserId, LoginDTO LoginDTO) { try { return await _PortletDAL.GetPortletListForUser(UserId, LoginDTO); } catch (Exception) { throw; } } } }