Created
May 30, 2012 12:58
-
-
Save rarous/2836140 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
using Atlas.Opus.Objects; | |
using Atlas.Opus.Web; | |
using log4net; | |
namespace Atlas.Catalog.Presenter { | |
/// <summary> | |
/// Trida presentera pro registraci. | |
/// </summary> | |
public class RegistrationPresenter { | |
#region Fields | |
private readonly IRegistrationView _view; | |
private readonly IRegistrationDataSource _dataSource; | |
private readonly ILog _log; | |
#endregion | |
#region Constructors | |
/// <summary> | |
/// Vytvari nou instanci tridy RegistrationPresenter. | |
/// </summary> | |
/// <param name="view">Injected view.</param> | |
/// <param name="dataSource">Injected data source.</param> | |
public RegistrationPresenter(IRegistrationView view, IRegistrationDataSource dataSource) { | |
_view = view; | |
_dataSource = dataSource; | |
_log = LogManager.GetLogger(GetType()); | |
} | |
/// <summary> | |
/// Vytvari nou instanci tridy RegistrationPresenter. Jako zdroj dat je predvybran OPUS. | |
/// </summary> | |
/// <param name="view">Injected view.</param> | |
public RegistrationPresenter(IRegistrationView view) | |
: this(view, new OpusRepository()) { | |
} | |
#endregion | |
#region Public Methods | |
/// <summary> | |
/// Odhlasi uzivatele z registrace | |
/// </summary> | |
public void Logout() { | |
SetAction(RegistrationAction.Logout, String.Empty); | |
} | |
/// <summary> | |
/// Dokonci registraci odkazu pridanim kontaktni osoby a ulozenim do databaze. | |
/// </summary> | |
public void SaveLink() { | |
CatalogItem link = _view.CatalogItem; | |
if (link != null) { | |
ContactPerson contact = GetContactObject(_view); | |
if (VerifyContactData(contact) == false) { | |
SetAction(RegistrationAction.Contact, Constants.ERROR_MESSAGE_NOT_COMPLETE_CONTACT); | |
return; | |
} | |
link.ResponsiblePerson = contact; | |
try { | |
LoginUser(); | |
_dataSource.SaveLink(link); | |
} | |
catch (TreeObjectException ex) { | |
_log.Fatal("Pri ukladani odkazu doslo k chybe.", ex); | |
SetAction(RegistrationAction.Contact, Constants.ERROR_MESSAGE_DATABASE); | |
return; | |
} | |
SetAction(RegistrationAction.Thanks, String.Empty); | |
return; | |
} | |
SetAction(RegistrationAction.Contact, Constants.ERROR_MESSAGE_NOT_COMPLETE_CONTACT); | |
} | |
/// <summary> | |
/// Vrati se na formular. | |
/// </summary> | |
public void BackToForm() { | |
_view.CatalogItem = _view.CatalogItem; | |
SetAction(RegistrationAction.Form, String.Empty); | |
} | |
/// <summary> | |
/// Nacte data z formulare. | |
/// </summary> | |
public void SendForm() { | |
if (IsInvalidTitle()) { | |
SetAction(RegistrationAction.Form, Constants.ERROR_MESSAGE_TITLE_REQUIRED); | |
return; | |
} | |
if (IsInvalidDescription()) { | |
SetAction(RegistrationAction.Form, Constants.ERROR_MESSAGE_SHORT_DESCRIPTION); | |
return; | |
} | |
if (_view.CatalogItem != null && IsValidCategory()) { | |
CatalogItem link = _view.CatalogItem; | |
link.Title = FormsHelper.GetTextValue(_view.LinkTitle); | |
link.Description = FormsHelper.GetTextValue(_view.Description); | |
link.DirectParentNode = CatalogNode.GetTreeObject<CatalogNode>(_view.Category1); | |
link.ExpirationDate = GetDate(_view.Year, _view.Month, _view.Day); | |
link.Address.Country = FormsHelper.GetLookupValue(Lookups.Country, Address.CZECH_REPUBLIC); | |
link.Address.County = FormsHelper.GetLookupValue(Lookups.County, _view.County); | |
link.Address.District = FormsHelper.GetLookupValue(Lookups.District, _view.District); | |
_view.CatalogItem = link; | |
SetAction(RegistrationAction.Contact, String.Empty); | |
return; | |
} | |
SetAction(RegistrationAction.Form, Constants.ERROR_MESSAGE_BAD_FORM_DATA); | |
} | |
/// <summary> | |
/// Prihlasi uzivatele. | |
/// </summary> | |
/// <returns>Vraci <c>true</c> pokud se prihlaseni povedlo. Jinak <c>false</c>.</returns> | |
public bool Login() { | |
if (String.IsNullOrEmpty(_view.Url)) { | |
SetAction(RegistrationAction.Login, Constants.ERROR_MESSAGE_NO_DATA); | |
return false; | |
} | |
CatalogItem link; | |
try { | |
link = _dataSource.LoadLink(_view.Url); | |
} | |
catch (UriFormatException) { | |
SetAction(RegistrationAction.Login, Constants.ERROR_MESSAGE_BAD_URL_FORMAT); | |
return false; | |
} | |
if (String.IsNullOrEmpty(link.Password)) { | |
link.Password = _view.Password; | |
} | |
if (User.HashPassword(_view.Password) == link.Password) { | |
if (_view.Category1 > 0) { | |
link.DirectParentNode = CatalogNode.GetById<CatalogNode>(_view.Category1); | |
} | |
_view.CatalogItem = link; | |
SetAction(RegistrationAction.Form, String.Empty); | |
return true; | |
} | |
SetAction(RegistrationAction.Login, Constants.ERROR_MESSAGE_BAD_LOGIN); | |
return false; | |
} | |
#endregion | |
#region Private Methods | |
private void SetAction(RegistrationAction action, string errorMessage) { | |
_view.ErrorMessage = errorMessage; | |
_view.Action = action; | |
_log.Info("Action set to: " + action); | |
} | |
private bool IsInvalidTitle() { | |
return (String.IsNullOrEmpty(_view.LinkTitle)) | |
|| (_view.LinkTitle.Length > 40); | |
} | |
private bool IsInvalidDescription() { | |
return (String.IsNullOrEmpty(_view.Description)) | |
|| (_view.Description.Length < 80) | |
|| (_view.Description.Length > 250); | |
} | |
private bool IsValidCategory() { | |
return (_view.Category1 > 0); | |
} | |
#endregion | |
#region Static Methods | |
/// <summary> | |
/// Pokud neni v aktualnim Contextu prihlaseny uzivatel, tak ho prihlasi. | |
/// </summary> | |
public static void LoginUser() { | |
User webuser = new User("webuser_firmy"); | |
if (HttpContext.Current != null) { | |
HttpContext.Current.User = webuser; | |
} | |
else { | |
System.Threading.Thread.CurrentPrincipal = webuser; | |
} | |
} | |
private static ContactPerson GetContactObject(IRegistrationView view) { | |
ContactPerson contact = new ContactPerson(); | |
contact.Name = FormsHelper.GetTextValue(view.Name); | |
contact.Surname = FormsHelper.GetTextValue(view.Surname); | |
contact.Email = FormsHelper.GetTextValue(view.Email); | |
contact.Phone = Atlas.Opus.Data.Utils.ParsePhoneNumber(FormsHelper.GetTextValue(view.Phone)); | |
return contact; | |
} | |
/// <summary> | |
/// Overi zda ma kontaktni osoba vyplnene vsechny pozadovane vlastnosti. | |
/// </summary> | |
/// <param name="contact">Objekt kontaktni osoby.</param> | |
/// <returns>Vraci <c>true</c> pokud ma kontaktni osoba vyplnene vsechny povinne vlastnosti. | |
/// Jinak vraci<c>false</c>.</returns> | |
public static bool VerifyContactData(ContactPerson contact) { | |
if (String.IsNullOrEmpty(contact.Name) || String.IsNullOrEmpty(contact.Surname) | |
|| String.IsNullOrEmpty(contact.Email)) { | |
return false; | |
} | |
return true; | |
} | |
/// <summary> | |
/// Vraci datum podle zadanych parametru. | |
/// </summary> | |
/// <param name="year">Rok.</param> | |
/// <param name="month">Mesic.</param> | |
/// <param name="day">Den.</param> | |
/// <returns>V pripade, ze jsou parametry nulove vraci <c>null</c>, jinak vraci nejepozdejsi rozpoznane datum. | |
/// V pripade ze je zadany pouze rok, bude datum 31.12. toho roku. Pokud je zadany i mesic bude zvolen | |
/// posledni den mesice. V pripade ze jsou vsechny parametry nenulove, jse zvoleno specifikovane datum.</returns> | |
public static DateTime? GetDate(int year, int month, int day) { | |
if ((year > 0) && (month > 0) && (day > 0)) { | |
return new DateTime(year, month, day); | |
} | |
else if ((year > 0) && (month > 0)) { | |
System.Globalization.GregorianCalendar cal = new System.Globalization.GregorianCalendar(); | |
return new DateTime(year, month, cal.GetDaysInMonth(year, month)); | |
} | |
else if (year > 0) { | |
return new DateTime(year, 12, 31); | |
} | |
return null; | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment