using System;
using Yavsc.Model.WorkFlow;
using System.Configuration;
using System.Collections.Specialized;
using Yavsc.Model.FrontOffice;
using System.Configuration.Provider;
using Yavsc.Model.FrontOffice.Catalog;
using System.Collections.Generic;
using Yavsc.Model.Skill;
using System.Linq;
using Yavsc.Model.Calendar;
using Yavsc.Model.Google.Api;
using System.Net.Mail;
using System.Web.Security;
using System.Web.Configuration;
using System.Net;
using System.IO;
namespace Yavsc.Model.WorkFlow
{
///
/// Work flow manager.
/// It takes orders store them and raise some events for modules
/// It publishes estimates and invoices
///
public static class WorkFlowManager
{
///
/// Finds the activity.
///
/// The activity.
/// Pattern.
/// If set to true exerted.
public static Activity[] FindActivity (string pattern = "%", bool exerted = true)
{
List activities = new List ();
foreach (var provider in Providers) {
foreach (var act in provider.FindActivity (pattern, exerted))
if (!activities.Contains (act))
activities.Add (act);
}
return activities.ToArray ();
}
///
/// Finds the performer.
///
/// The performer.
/// MEA code.
/// Skills.
public static PerformerProfile [] FindPerformer (string MEACode, SkillRating[] skills)
{
string[] usernames = SkillManager.FindPerformer (MEACode, skills);
List result = new List ();
foreach (string user in usernames)
result.Add (SkillManager.GetUserSkills (user));
return result.ToArray ();
}
///
/// Gets the activity.
///
/// The activity.
/// MAE code.
public static Activity GetActivity (string meacode)
{
return DefaultProvider.GetActivity (meacode);
}
///
/// Gets or sets the catalog.
///
/// The catalog.
public static Catalog Catalog { get; set; }
///
/// Registers the command.
///
/// The command.
/// COM.
public static CommandRegistration RegisterCommand (Command com)
{
long cmdid = DefaultProvider.RegisterCommand (com);
string errorMsgGCM=null;
if (com.GetType ().GetInterface ("INominative") != null) {
var result = new NominativeCommandRegistration ();
result.CommandId = cmdid;
INominative cmdn = com as INominative;
NominativeEventPub ev = new NominativeEventPub ();
ev.PerformerName = cmdn.PerformerName;
string desc = com.GetDescription ();
ev.Description = desc;
// TODO send a location
try {
var gnresponse = GoogleHelpers.NotifyEvent (ev);
if (gnresponse.failure > 0 || gnresponse.success <=0)
result.NotifiedOnMobile = false;
else result.NotifiedOnMobile = true;
} catch (WebException ex) {
using (var respstream = ex.Response.GetResponseStream ()) {
using (StreamReader rdr = new StreamReader (respstream)) {
errorMsgGCM = rdr.ReadToEnd ();
rdr.Close ();
}
respstream.Close ();
}
if (errorMsgGCM == null)
throw;
throw new Exception (errorMsgGCM,ex);
}
string errorEMail = null;
try {
var pref = Membership.GetUser (cmdn.PerformerName);
using (System.Net.Mail.MailMessage msg =
new MailMessage (
WebConfigurationManager.AppSettings.Get ("OwnerEMail"),
pref.Email,
"[Demande de devis] " + com.ClientName,
desc)) {
using (System.Net.Mail.SmtpClient sc = new SmtpClient ()) {
sc.Send (msg);
result.EmailSent = true;
}
}
} catch (Exception ex) {
errorEMail = ex.Message;
result.EmailSent = false;
}
return result;
} else
throw new NotImplementedException ();
}
///
/// Updates the estimate.
///
/// Estim.
public static void UpdateEstimate (Estimate estim)
{
DefaultProvider.Update (estim);
}
///
/// Gets the estimate.
///
/// The estimate.
/// Estid.
public static Estimate GetEstimate (long estid)
{
return DefaultProvider.Get (estid);
}
///
/// Gets the estimates, refering the
/// given client or username .
///
/// The estimates.
/// Responsible.
public static Estimate [] GetResponsibleEstimates (string responsible)
{
return DefaultProvider.GetEstimates (null, responsible);
}
///
/// Gets the client estimates.
///
/// The client estimates.
/// Client.
public static Estimate [] GetClientEstimates (string client)
{
return DefaultProvider.GetEstimates (client, null);
}
///
/// Gets the user estimates.
///
/// The user estimates.
/// Username.
public static Estimate [] GetUserEstimates (string username)
{
return DefaultProvider.GetEstimates (username);
}
///
/// Gets the stock for a given product reference.
///
/// The stock status.
/// Product reference.
public static StockStatus GetStock (string productReference)
{
return DefaultProvider.GetStockStatus (productReference);
}
///
/// Updates the writting.
///
/// Wr.
public static void UpdateWritting (Writting wr)
{
DefaultProvider.UpdateWritting (wr);
}
///
/// Drops the writting.
///
/// Wrid.
public static void DropWritting (long wrid)
{
DefaultProvider.DropWritting (wrid);
}
///
/// Drops the estimate.
///
/// Estid.
public static void DropEstimate (long estid)
{
DefaultProvider.DropEstimate (estid);
}
static IContentProvider defaultProvider;
///
/// Gets the content provider.
///
/// The content provider.
public static IContentProvider DefaultProvider {
get {
if (defaultProvider == null)
defaultProvider = ManagerHelper.CreateDefaultProvider
("system.web/workflow") as IContentProvider;
return defaultProvider;
}
}
///
/// Drops the writting tag.
///
/// Wrid.
/// Tag.
public static void DropWrittingTag (long wrid, string tag)
{
throw new NotImplementedException ();
}
///
/// Gets the providers.
///
/// The providers.
public static IContentProvider [] Providers {
get {
if (providers == null) {
var pbs = ManagerHelper.CreateProviders
("system.web/workflow");
providers = new IContentProvider [pbs.Length];
for (var i = 0; i < pbs.Length; i++)
providers [i] = pbs [i] as IContentProvider;
}
return providers;
}
}
private static IContentProvider[] providers = null;
///
/// Creates the estimate.
///
/// The estimate.
/// Responsible.
/// Client.
/// Title.
/// Description.
public static Estimate CreateEstimate (string responsible, string client, string title, string description)
{
Estimate created = DefaultProvider.CreateEstimate (responsible, client, title, description);
return created;
}
///
/// Write the specified estid, desc, ucost, count and productid.
///
/// Estid.
/// Desc.
/// Ucost.
/// Count.
/// Productid.
public static long Write (long estid, string desc, decimal ucost, int count, string productid)
{
if (!string.IsNullOrWhiteSpace (productid)) {
if (Catalog == null)
Catalog = CatalogManager.GetCatalog ();
if (Catalog == null)
throw new Exception ("No catalog");
Product p = Catalog.FindProduct (productid);
if (p == null)
throw new Exception ("Product not found");
// TODO new EstimateChange Event
}
return DefaultProvider.Write (estid, desc, ucost, count, productid);
}
///
/// Sets the estimate status.
///
/// Estid.
/// Status.
/// Username.
public static void SetEstimateStatus (long estid, int status, string username)
{
DefaultProvider.SetEstimateStatus (estid, status, username);
}
///
/// Gets the commands.
///
/// The commands.
/// Username.
public static CommandSet GetCommands (string username)
{
return DefaultProvider.GetCommands (username);
}
///
/// Registers the activity.
///
/// Activity name.
/// Meacode.
/// Comment.
public static void RegisterActivity (string activityName, string meacode, string comment)
{
DefaultProvider.RegisterActivity (activityName, meacode, comment);
}
}
}