using System;
using Npgsql;
using NpgsqlTypes;
using System.Configuration;
using System.Collections.Specialized;
using Yavsc.Model.WorkFlow;
using System.Configuration.Provider;
using System.Collections.Generic;
using Yavsc.Model.FrontOffice;
using System.Web.Security;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
using Newtonsoft.Json;
namespace Yavsc
{
///
/// Npgsql content provider.
///
public class NpgsqlContentProvider: ProviderBase, IContentProvider
{
///
/// Registers the command.
///
/// The command id in db.
/// COM.
public long RegisterCommand (Command com)
{
long id;
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"insert into commandes (prdref,creation,params,clientname,applicationname) values (@pref,@creat,@prms,@cli,@app) returning id";
cmd.Parameters.AddWithValue ("@pref", com.ProductRef);
cmd.Parameters.AddWithValue ("@creat", com.CreationDate);
cmd.Parameters.AddWithValue ("@prms", JsonConvert.SerializeObject(com.Parameters));
cmd.Parameters.AddWithValue ("@cli", Membership.GetUser().UserName);
cmd.Parameters.AddWithValue ("@app", ApplicationName);
cnx.Open ();
com.Id = id = (long)cmd.ExecuteScalar ();
}
}
return id;
}
///
/// Gets the commands.
///
/// The commands.
/// Username.
public CommandSet GetCommands (string username )
{
// Check the user's authorisations
MembershipUser user = Membership.GetUser ();
if (user.UserName != username)
if (!Roles.IsUserInRole ("Admin"))
if (!Roles.IsUserInRole ("FrontOffice"))
throw new Exception ("Not allowed");
CommandSet cmds = new CommandSet ();
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"select id,creation,prdref,params from commandes where @user = clientname and applicationname = @app";
cmd.Parameters.AddWithValue ("@user", username);
cmd.Parameters.AddWithValue ("@app", this.ApplicationName);
cnx.Open ();
using (NpgsqlDataReader rdr = cmd.ExecuteReader ()) {
while (rdr.Read ()) {
Command ycmd = new Command();
ycmd.Id = rdr.GetInt64(0);
ycmd.CreationDate = rdr.GetDateTime(1);
ycmd.ProductRef = rdr.GetString(2);
object prms = JsonConvert.DeserializeObject>(rdr.GetString (3));
ycmd.Parameters = prms as Dictionary ;
cmds.Add (ycmd);
}
}
}
cnx.Close ();
}
return cmds;
}
///
/// Gets the stock status.
///
/// The stock status.
/// Product reference.
public virtual StockStatus GetStockStatus (string productReference)
{
return StockStatus.NonExistent;
}
///
/// Gets the writting status changes.
///
/// The writting statuses.
/// Wrid.
public StatusChange[] GetWrittingStatuses (long wrid)
{
throw new NotImplementedException ();
}
///
/// Gets the estimate status changes.
///
/// The estimate statuses.
/// Estid.
public StatusChange[] GetEstimateStatuses (long estid)
{
throw new NotImplementedException ();
}
///
/// Tags the writting.
///
/// Wrid.
/// Tag.
public void TagWritting (long wrid, string tag)
{
throw new NotImplementedException ();
}
///
/// Drops the writting tag .
///
/// Wrid.
/// Tag.
public void DropWrittingTag (long wrid, string tag)
{
throw new NotImplementedException ();
}
///
/// Sets the writting status.
///
/// Wrtid.
/// Status.
/// Username.
public void SetWrittingStatus (long wrtid, int status, string username)
{
throw new NotImplementedException ();
}
///
/// Sets the estimate status.
///
/// Estid.
/// Status.
/// Username.
public void SetEstimateStatus (long estid, int status, string username)
{
throw new NotImplementedException ();
}
///
/// Releases all resource used by the object.
///
/// Call when you are finished using the . The
/// method leaves the in an unusable state. After
/// calling , you must release all references to the so
/// the garbage collector can reclaim the memory that the was occupying.
public void Dispose ()
{
throw new NotImplementedException ();
}
///
/// Install the model in database using the specified cnx.
///
/// Cnx.
public void Install (System.Data.IDbConnection cnx)
{
throw new NotImplementedException ();
}
///
/// Uninstall the module data and data model from
/// database, using the specified connection.
///
/// Cnx.
/// If set to true remove config.
public void Uninstall (System.Data.IDbConnection cnx, bool removeConfig)
{
throw new NotImplementedException ();
}
///
/// Defaults the config.
///
/// The config.
/// App name.
/// Cnx string.
public ConfigurationSection DefaultConfig (string appName, string cnxStr)
{
throw new NotImplementedException ();
}
///
/// Gets or sets a value indicating whether this is active.
///
/// true if active; otherwise, false.
public bool Active {
get {
throw new NotImplementedException ();
}
set {
throw new NotImplementedException ();
}
}
///
/// Gets the different status labels.
/// 0 is the starting status. Each status is an integer and the 0-based index
/// of a string in this array.
///
/// The status labels.
public string[] Statuses {
get {
return new string[] { "Created", "Validated", "Success", "Error" };
}
}
///
/// Gets the final statuses.
///
/// The final statuses.
public bool[] FinalStatuses {
get {
return new bool[] { false, false, true, true };
}
}
///
/// Gets the estimates created by
/// or for the given user by user name.
///
/// The estimates.
/// user name.
public Estimate[] GetEstimates (string username)
{
if (username == null)
throw new InvalidOperationException (
"username cannot be" +
" null at searching for estimates");
List ids = new List ();
List ests = new List ();
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"select _id from estimate where client = @uname or username = @uname";
cmd.Parameters.AddWithValue ("@uname", username);
cnx.Open ();
using (NpgsqlDataReader rdr = cmd.ExecuteReader ()) {
while (rdr.Read ()) {
ids.Add (rdr.GetInt64 (0));
}
rdr.Close ();
}
cnx.Close ();
}
foreach (long id in ids)
ests.Add(GetEstimate(id));
return ests.ToArray();
}
}
///
/// Gets the estimates.
///
/// The estimates.
/// Client.
/// Responsible.
public Estimate[] GetEstimates (string client, string responsible)
{
if (client == null && responsible == null)
throw new InvalidOperationException (
"client and responsible cannot be" +
" both null at searching for estimates");
List ids = new List ();
List ests = new List ();
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"select _id from estimate where ";
if (client != null) {
cmd.CommandText += "client = @clid";
if (responsible != null)
cmd.CommandText += " and ";
cmd.Parameters.AddWithValue ("@clid", client);
}
if (responsible != null) {
cmd.CommandText += "username = @resp";
cmd.Parameters.AddWithValue ("@resp", responsible);
}
cnx.Open ();
using (NpgsqlDataReader rdr = cmd.ExecuteReader ()) {
while (rdr.Read ()) {
ids.Add (rdr.GetInt64 (0));
}
rdr.Close ();
}
foreach (long id in ids)
ests.Add(GetEstimate(id));
return ests.ToArray();
}
}
}
///
/// Drops the writting.
///
/// Wrid.
public void DropWritting (long wrid)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"delete from writtings where _id = @wrid";
cmd.Parameters.AddWithValue ("@wrid", wrid);
cnx.Open ();
cmd.ExecuteNonQuery ();
}
}
}
///
/// Drops the estimate.
///
/// Estid.
public void DropEstimate (long estid)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"delete from estimate where _id = @estid";
cmd.Parameters.AddWithValue ("@estid", estid);
cnx.Open ();
cmd.ExecuteNonQuery ();
}
}
}
///
/// Gets the estimate by identifier.
///
/// The estimate.
/// Estimid.
public Estimate GetEstimate (long estimid)
{
Estimate est = null;
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"select title,username,client,description from estimate where _id = @estid";
cmd.Parameters.AddWithValue ("@estid", estimid);
cnx.Open ();
using (NpgsqlDataReader rdr = cmd.ExecuteReader ()) {
if (!rdr.Read ()) {
return null;
}
est = new Estimate ();
est.Title = rdr.GetString(
rdr.GetOrdinal("title"));
est.Responsible = rdr.GetString(
rdr.GetOrdinal("username"));
int clientidx = rdr.GetOrdinal ("client");
if (!rdr.IsDBNull (clientidx))
est.Client = rdr.GetString (clientidx);
int index = rdr.GetOrdinal ("description");
if (!rdr.IsDBNull (index))
est.Description = rdr.GetString (index);
est.Id = estimid;
rdr.Close ();
}
}
// assert est != null
using (NpgsqlCommand cmdw = new NpgsqlCommand ("select _id, productid, ucost, count, description from writtings where estimid = @estid", cnx)) {
cmdw.Parameters.AddWithValue("@estid", estimid);
using (NpgsqlDataReader rdrw = cmdw.ExecuteReader ()) {
List lw = null;
if (rdrw.HasRows) {
lw = new List ();
while (rdrw.Read ()) {
Writting w = new Writting ();
int dei = rdrw.GetOrdinal ("description");
if (!rdrw.IsDBNull (dei))
w.Description = rdrw.GetString (dei);
int opi = rdrw.GetOrdinal ("productid");
if (!rdrw.IsDBNull (opi))
w.ProductReference = rdrw.GetString(opi);
int oco = rdrw.GetOrdinal ("count");
if (!rdrw.IsDBNull (oco))
w.Count = rdrw.GetInt32 (oco);
int ouc = rdrw.GetOrdinal ("ucost");
if (!rdrw.IsDBNull (ouc))
w.UnitaryCost = rdrw.GetDecimal (ouc);
w.Id = rdrw.GetInt64 (rdrw.GetOrdinal ("_id"));
lw.Add (w);
}
est.Lines = lw.ToArray ();
}
}
}
return est;
}
}
///
/// Updates the writting.
///
/// Wr.
public void UpdateWritting (Writting wr)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"update writtings set " +
"description = @desc, " +
"ucost = @ucost, " +
"count = @count, " +
"productid = @prdid " +
"where _id = @wrid";
cmd.Parameters.AddWithValue ("@wrid", wr.Id);
cmd.Parameters.AddWithValue ("@desc", wr.Description);
cmd.Parameters.AddWithValue ("@ucost", wr.UnitaryCost);
cmd.Parameters.AddWithValue ("@prdid", wr.ProductReference);
cmd.Parameters.AddWithValue ("@count", wr.Count);
cnx.Open ();
cmd.ExecuteNonQuery ();
cnx.Close ();
}
}
}
///
/// Saves the given Estimate object in database.
///
/// the Estimate object.
public void UpdateEstimate (Estimate estim)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"update estimate set title = @tit, username = @un, " +
"description = @descr, client = @cli where _id = @estid";
cmd.Parameters.AddWithValue ("@tit", estim.Title);
cmd.Parameters.AddWithValue ("@un", estim.Responsible);
cmd.Parameters.AddWithValue ("@descr", estim.Description);
cmd.Parameters.AddWithValue ("@cli", estim.Client);
cmd.Parameters.AddWithValue ("@estid", estim.Id);
cnx.Open ();
cmd.ExecuteNonQuery ();
cnx.Close ();
}
}
}
///
/// Add a line to the specified estimate by id,
/// using the specified desc, ucost, count and productid.
///
/// Estimate identifier.
/// Textual description for this line.
/// Unitary cost.
/// Cost multiplier.
/// Product identifier.
public long Write (long estid, string desc, decimal ucost, int count, string productid)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"insert into writtings (description, estimid, ucost, count, productid) VALUES (@dscr,@estid,@ucost,@count,@prdid) returning _id";
cmd.Parameters.AddWithValue ("@dscr", desc);
cmd.Parameters.AddWithValue("@estid", estid);
cmd.Parameters.AddWithValue("@ucost", ucost);
cmd.Parameters.AddWithValue("@count", count);
cmd.Parameters.AddWithValue("@prdid", productid);
cnx.Open ();
long res = (long) cmd.ExecuteScalar ();
cnx.Close ();
return res;
}
}
}
///
/// Sets the desc.
///
/// Writid.
/// New desc.
public void SetDesc (long writid, string newDesc)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"update writtings set description = @dscr where _id = @writid";
cmd.Parameters.AddWithValue ("@tit", newDesc);
cmd.Parameters.AddWithValue ("@writid", writid);
cnx.Open ();
cmd.ExecuteNonQuery ();
cnx.Close ();
}
}
}
///
/// Creates the estimate.
///
/// The estimate.
/// Client.
/// Title.
/// Responsible.
/// Description.
public Estimate CreateEstimate (string responsible, string client, string title, string description)
{
using (NpgsqlConnection cnx = CreateConnection ()) {
using (NpgsqlCommand cmd = cnx.CreateCommand ()) {
cmd.CommandText =
"insert into estimate (title,description,username,client,applicationname) " +
"values (@tit,@descr,@resp,@un,@app) returning _id";
cmd.Parameters.AddWithValue ("@tit", title);
cmd.Parameters.AddWithValue ("@un", client);
cmd.Parameters.AddWithValue ("@resp", responsible);
cmd.Parameters.AddWithValue ("@descr", description);
cmd.Parameters.AddWithValue("@app", ApplicationName);
cnx.Open ();
Estimate created = new Estimate ();
created.Id = (long) cmd.ExecuteScalar ();
cnx.Close ();
created.Title = title;
created.Description = description;
created.Client = client;
created.Responsible = responsible;
return created;
}
}
}
string applicationName=null;
///
/// Gets or sets the name of the application.
///
/// The name of the application.
public string ApplicationName {
get {
return applicationName;
}
set {
applicationName = value;
}
}
string cnxstr = null;
///
/// Initialize this object using the specified name and config.
///
/// Name.
/// Config.
public override void Initialize (string name, NameValueCollection config)
{
if ( string.IsNullOrWhiteSpace(config ["connectionStringName"]))
throw new ConfigurationErrorsException ("No name for Npgsql connection string found");
cnxstr = ConfigurationManager.ConnectionStrings [config ["connectionStringName"]].ConnectionString;
applicationName = config["applicationName"] ?? "/";
}
///
/// Creates the connection.
///
/// The connection.
protected NpgsqlConnection CreateConnection ()
{
return new NpgsqlConnection (cnxstr);
}
}
}