using System; using System.Collections.Generic; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using GeneralApi.Core.Repositories; using Microsoft.Practices.ServiceLocation; using System.Drawing; using System.Linq; using System.Web.UI.WebControls; using Microsoft.SharePoint.Utilities; using NHibernate.Validator.Engine; using Taloyhtio.GeneralApi.Common.Extensions; using Taloyhtio.GeneralApi.Core.Entities; namespace Taloyhtio.GeneralApi.MOSS { public partial class RelateCondo : LayoutsPageBase { private const int NO_CONDO_INDEX = -1; public const string NO_SELECTION = "[No selection]"; protected void Page_Load(object sender, EventArgs e) { var currentWeb = SPContext.Current.Web; if (!currentWeb.DoesUserHavePermissions(SPBasePermissions.ManageWeb)) { SPUtility.TransferToErrorPage("You don't have necessary permissions to perform this operation", currentWeb.Title, currentWeb.Url); } if (!this.IsPostBack) { var condoRepository = ServiceLocator.Current.GetInstance(); var allCondos = condoRepository.GetAll(); var viewModelCondos = new List(); if (!allCondos.IsNullOrEmpty()) { viewModelCondos = allCondos.Select( c => (object)new { Id = c.Id, OfficialName = c.Deactivated ? string.Format("{0} (deactivated)", c.OfficialName) : c.OfficialName }).ToList(); } viewModelCondos.Insert(0, new { Id = NO_CONDO_INDEX, OfficialName = NO_SELECTION }); this.ddlCondos.DataSource = viewModelCondos; this.ddlCondos.DataBind(); var currentCondo = allCondos.FirstOrDefault(c => c.WebId == SPContext.Current.Web.ID); if (currentCondo != null) { this.ddlCondos.SelectedValue = currentCondo.Id.ToString(); this.lblExistingCondo.Text = currentCondo.OfficialName; this.pnlAlreadyRelated.Visible = true; } } } protected void SaveButtonClick(object sender, EventArgs e) { this.pnlAlreadyRelated.Visible = false; var condoRepository = ServiceLocator.Current.GetInstance(); // unrelate old condos first var oldCondos = condoRepository.GetByWebId(SPContext.Current.Web.ID); if (!oldCondos.IsNullOrEmpty()) { foreach (Condo oldCondo in oldCondos) { oldCondo.WebAppId = null; oldCondo.SiteId = null; oldCondo.WebId = null; } } if (this.ddlCondos.SelectedValue == NO_CONDO_INDEX.ToString()) { this.showText("Current site is not assoiated with any Condo from master database now", Color.Green); return; } // relate site with seleced condo int id = int.Parse(this.ddlCondos.SelectedValue); var condo = condoRepository.GetById(id); if (condo == null) { this.showText(string.Format("Condo with id = '{0}' not found", id), Color.Red); return; } condo.WebAppId = SPContext.Current.Site.WebApplication.Id; condo.SiteId = SPContext.Current.Site.ID; condo.WebId = SPContext.Current.Web.ID; var validator = ServiceLocator.Current.GetInstance(); var errors = validator.Validate(condo); if (!errors.IsNullOrEmpty()) { this.showText(string.Join("
", errors.Select(c => c.ToString()).ToArray()), Color.Red); return; } condo.LastUpdated = DateTime.Now; condoRepository.Save(condo); this.showText(string.Format("Condo '{0}' was sucessfully associated with site '{1}'", this.ddlCondos.SelectedItem.Text, SPContext.Current.Web.Url), Color.Green); } private void showText(string text, Color c) { this.lblStatus.Text = text; this.lblStatus.ForeColor = c; } protected void CancelButtonClick(object sender, EventArgs e) { SPUtility.Redirect(SPContext.Current.Web.Url, SPRedirectFlags.Default, Context); } } }