using library_spo_utils.Enums; using library_spo_utils.Interfaces.Repositories; using library_spo_utils.Interfaces.Services; using Microsoft.Extensions.Logging; using Microsoft.SharePoint.Client; namespace library_spo_utils.Services; internal class SiteService : ISiteService { private readonly ILogger _logger; private readonly ISharePointAuthenticationManager _authMgr; private readonly ISharePointCustomOperation _spc; private readonly ISiteOptions _siteOptions; private readonly ISubSiteService _subSiteService; private readonly IOneNoteService _oneNoteService; private readonly IProjectRepository _projectRepository; private readonly IProjectDocLibraryService _projectDocLibraryService; private readonly IProjectQuickMenuService _projectQuickMenuService; private readonly ITenantService _tenantService; private readonly IProjectYearService _projectYearService; private readonly IFieldEntryDataUpdate _fieldEntryDataUpdate; public SiteService( ILogger logger, ISharePointAuthenticationManager authMgr, ISharePointCustomOperation spc, ISiteOptions siteOptions, ISubSiteService subSiteService, IOneNoteService oneNoteService, IProjectRepository projectRepository, IProjectDocLibraryService projectDocLibraryService, IProjectQuickMenuService projectQuickMenuService, ITenantService tenantService, IProjectYearService projectYearService, IFieldEntryDataUpdate fieldEntryDataUpdate) { _logger = logger; _authMgr = authMgr; _spc = spc; _siteOptions = siteOptions; _subSiteService = subSiteService; _oneNoteService = oneNoteService; _projectRepository = projectRepository; _projectDocLibraryService = projectDocLibraryService; _projectQuickMenuService = projectQuickMenuService; _tenantService = tenantService; _projectYearService = projectYearService; _fieldEntryDataUpdate = fieldEntryDataUpdate; } public bool CreateProjectSiteIfNotExists(string projName) { try { var site = _siteOptions.GetProjectYearSite(projName); var listTitle = _siteOptions.GetProjListTitle(projName); using var ctx = _authMgr.GetContext(site); if (_spc.SiteExist(ctx)) { return true; } _tenantService.CreateForProject(projName); _projectYearService.CreateList(projName, listTitle, ctx); } catch (Exception e) { _logger.LogError(e, "Site Service"); return false; } return true; } public bool CreateSubSiteIfNotExists(string projName) { var projectSite = _siteOptions.GetProjectYearSite(projName); var subProjSite = _siteOptions.GetSubProjSite(projName); var siteContext = _authMgr.GetContext(projectSite); var subSiteContext = _authMgr.GetContext(subProjSite); if (!_spc.SiteExist(siteContext)) { throw new Exception($"Site {siteContext.Url} not exists"); } if (_spc.SiteExist(subSiteContext)) { _logger.LogInformation($"The subsite {subSiteContext.Url} already exists"); _fieldEntryDataUpdate.FieldUpdate(siteContext, _siteOptions.GetProjListTitle(projName), projName, FieldUpdateType.Project); return true; } var existSubSite = _subSiteService.Create(projName, siteContext); if (!existSubSite) { return true; } _oneNoteService.CreateFolderInsideProject(projName, siteContext); CreateProjectListEntry(projName, siteContext); _projectDocLibraryService.Create(projName, subSiteContext); _subSiteService.AddColumnsToListView(projName, subSiteContext); _projectQuickMenuService.CreateForProject(projName, subSiteContext); return true; } public bool CreateQuotationSiteIfNotExists(string quotationName) { try { var site = _siteOptions.GetQuotationSite(); using var ctx = _authMgr.GetContext(site); if (_spc.SiteExist(ctx)) { return true; } _tenantService.CreateForQuotation(); } catch (Exception e) { _logger.LogError(e, "Site Service"); return false; } return true; } public bool CreateNonComplianceSiteIfNotExists(string nonComplianceName) { try { var site = _siteOptions.GetNonComplianceSite(); using var ctx = _authMgr.GetContext(site); if (_spc.SiteExist(ctx)) { return true; } _tenantService.CreateForNonCompliance(); } catch (Exception e) { _logger.LogError(e, "Site Service"); return false; } return true; } private void CreateProjectListEntry(string projName, ClientContext ctx) { try { var listTitle = _siteOptions.GetProjListTitle(projName); var tenant = _siteOptions.GetProjYearTenant(projName).Replace(" ",string.Empty); var list = ctx.Web.Lists.GetByTitle(listTitle); var itemCreateInfo = new ListItemCreationInformation(); var oItem = list.AddItem(itemCreateInfo); var link = new FieldUrlValue { Url = $"/sites/{tenant}/{projName}", Description = projName }; oItem["PAL_ID_Project"] = link; oItem.Update(); ctx.ExecuteQuery(); _fieldEntryDataUpdate.FieldUpdate(ctx, listTitle, projName, FieldUpdateType.Project); _logger.LogInformation($"> La Commessa {projName} è stata aggiunta alla lista {listTitle} con successo!"); } catch (Exception ex) { _logger.LogError(ex, "Create List Entry"); } } }