using System;
using Yavsc.Model.WorkFlow;
using System.Configuration;
using System.Collections.Specialized;
using Yavsc.Model.FrontOffice;
using System.Configuration.Provider;
namespace Yavsc.Model.WorkFlow
{
///
/// Work flow manager.
/// It takes orders store them and raise some events for modules
/// It publishes estimates and invoices
///
public class WorkFlowManager
{
///
/// Gets or sets the catalog.
///
/// The catalog.
public static Catalog Catalog { get; set; }
///
/// Registers the command.
///
/// The command.
/// COM.
public long RegisterCommand(Command com)
{
return ContentProvider.RegisterCommand (com);
}
///
/// Updates the estimate.
///
/// Estim.
public void UpdateEstimate (Estimate estim)
{
ContentProvider.Update (estim);
}
///
/// Gets the estimate.
///
/// The estimate.
/// Estid.
public Estimate GetEstimate (long estid)
{
return ContentProvider.Get (estid);
}
///
/// Gets the estimates, refering the
/// given client or username .
///
/// The estimates.
/// Responsible.
public Estimate [] GetResponsibleEstimates (string responsible)
{
return ContentProvider.GetEstimates (null, responsible);
}
///
/// Gets the client estimates.
///
/// The client estimates.
/// Client.
public Estimate [] GetClientEstimates (string client)
{
return ContentProvider.GetEstimates (client, null);
}
///
/// Gets the user estimates.
///
/// The user estimates.
/// Username.
public Estimate [] GetUserEstimates (string username)
{
return ContentProvider.GetEstimates (username);
}
///
/// Gets the stock for a given product reference.
///
/// The stock status.
/// Product reference.
public StockStatus GetStock(string productReference)
{
return ContentProvider.GetStockStatus (productReference);
}
///
/// Updates the writting.
///
/// Wr.
public void UpdateWritting (Writting wr)
{
ContentProvider.UpdateWritting (wr);
}
///
/// Drops the writting.
///
/// Wrid.
public void DropWritting (long wrid)
{
ContentProvider.DropWritting (wrid);
}
///
/// Drops the estimate.
///
/// Estid.
public void DropEstimate (long estid)
{
ContentProvider.DropEstimate(estid);
}
IContentProvider contentProvider;
///
/// Gets the content provider.
///
/// The content provider.
public IContentProvider ContentProvider {
get {
DataProviderConfigurationSection c = (DataProviderConfigurationSection) ConfigurationManager.GetSection ("system.web/workflow");
if (c == null)
throw new Exception ("No system.web/workflow configuration section found");
ProviderSettings confprov = c.Providers[c.DefaultProvider] as ProviderSettings;
if (confprov == null)
throw new Exception ("Default workflow provider not found (system.web/workflow@defaultProvider)");
string clsName = confprov.Type;
if (clsName == null)
throw new Exception ("Provider type not specified (system.web/workflow@type)");
if (contentProvider != null)
{
if (contentProvider.GetType ().Name != clsName)
contentProvider = null;
}
if (contentProvider == null)
{
Type cpt = Type.GetType (clsName);
if (cpt == null)
throw new Exception (string.Format("Type not found : {0} (wrong name, or missing assembly reference?)",clsName));
System.Reflection.ConstructorInfo ci =cpt.GetConstructor (System.Type.EmptyTypes);
contentProvider = (IContentProvider)ci.Invoke (System.Type.EmptyTypes);
}
contentProvider.Initialize (confprov.Name, confprov.Parameters);
return contentProvider;
}
}
///
/// Creates the estimate.
///
/// The estimate.
/// Responsible.
/// Client.
/// Title.
/// Description.
public Estimate CreateEstimate(string responsible, string client, string title, string description)
{
Estimate created = ContentProvider.CreateEstimate (responsible, client, title, description);
return created;
}
///
/// Write the specified estid, desc, ucost, count and productid.
///
/// Estid.
/// Desc.
/// Ucost.
/// Count.
/// Productid.
public 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 ContentProvider.Write(estid, desc, ucost, count, productid);
}
///
/// Sets the estimate status.
///
/// Estid.
/// Status.
/// Username.
public void SetEstimateStatus(long estid, int status, string username)
{
ContentProvider.SetEstimateStatus (estid, status, username);
}
///
/// Gets the commands.
///
/// The commands.
/// Username.
public CommandSet GetCommands(string username)
{
return ContentProvider.GetCommands (username);
}
}
}