yavsc/yavscModel/FrontOffice/Commande.cs

110 lines
2.9 KiB
C#

using System;
using Yavsc;
using System.Collections.Specialized;
using Yavsc.Model.WorkFlow;
using Yavsc.Model.FileSystem;
using System.Web;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace Yavsc.Model.FrontOffice
{
/// <summary>
/// Commande.
/// </summary>
public abstract class Command
{
/// <summary>
/// Gets or sets the creation date.
/// </summary>
/// <value>The creation date.</value>
public DateTime CreationDate { get; set; }
/// <summary>
/// Gets or sets the identifier.
/// </summary>
/// <value>The identifier.</value>
public long Id { get; set; }
/// <summary>
/// Gets or sets the product reference.
/// </summary>
/// <value>The prod reference.</value>
public CommandStatus Status { get; set; }
/// <summary>
/// Gets or sets the product reference.
/// </summary>
/// <value>The product reference.</value>
public string ProductRef { get; set; }
/// <summary>
/// The parameters.
/// </summary>
public Dictionary<string,string> Parameters = new Dictionary<string,string> ();
IEnumerable<FileInfo> Files {
get {
return UserFileSystemManager.GetFiles (Id.ToString());
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Yavsc.Model.FrontOffice.Command"/> class.
/// </summary>
public Command()
{
}
/// <summary>
/// Froms the post.
/// </summary>
/// <param name="collection">Collection.</param>
/// <param name="files">Files.</param>
private void FromPost(NameValueCollection collection, NameObjectCollectionBase files)
{
// string catref=collection["catref"]; // Catalog Url from which formdata has been built
ProductRef=collection["ref"]; // Required product reference
CreationDate = DateTime.Now;
Status = CommandStatus.Inserted;
// stores the parameters:
Parameters.Clear ();
foreach (string key in collection.AllKeys) {
if (key!="ref")
Parameters.Add (key, collection [key]);
}
WorkFlowManager.RegisterCommand (this); // gives a value to this.Id
string strcmdid = Id.ToString ();
UserFileSystemManager.Put(Path.Combine("commandes",strcmdid),files);
}
/// <summary>
/// Creates a command using the specified collection
/// as command parameters, handles the files upload.
/// </summary>
/// <param name="collection">Collection.</param>
/// <param name="files">Files.</param>
public static Command CreateCommand (NameValueCollection collection, NameObjectCollectionBase files)
{
var cmd = CreateCommand (collection ["type"]);
cmd.FromPost (collection, files);
return cmd;
}
/// <summary>
/// Creates the command.
/// </summary>
/// <returns>The command.</returns>
/// <param name="className">Class name.</param>
public static Command CreateCommand (string className)
{
var type = Type.GetType (className);
ConstructorInfo ci = type.GetConstructor(new Type[]{});
var cmd = ci.Invoke (new object[]{}) as Command;
return cmd;
}
}
}