/*
* P4.Net *
Copyright (c) 2007-2010 Shawn Hladky
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
namespace P4API
{
///
/// A P4Callback class that populates a P4Recordset.
///
///
/// Subclassing P4RecordsetCallback can be usefull when changing code that used the P4Connection.Run method.
/// By subclassing P4RecordsetCallback, you can intercept calls to provide status updates, but still have access
/// to the populated recordset when the command completes. Be sure to call the base methods in overriden methods to
/// ensure the recordset is correctly poulated.
///
public class P4RecordsetCallback : P4Callback
{
private P4BaseRecordSet _P4Result;
// instance variable for the super class when this is created externally
private P4RecordSet _P4ResultRecordset;
internal P4RecordsetCallback(P4BaseRecordSet p4Result)
{
_P4Result = p4Result;
_P4ResultRecordset = null;
}
///
/// Initializes a new instance of the class.
///
public P4RecordsetCallback()
{
_P4ResultRecordset = new P4RecordSet();
_P4Result = _P4ResultRecordset;
}
///
/// Gets the recordset.
///
/// The recordset.
///
/// The recordset that has been populated when the command completes.
///
public P4RecordSet Recordset
{
get
{
return _P4ResultRecordset;
}
}
#region P4Callback Members
///
/// Edits the specified f1.
///
/// The f1.
public override void Edit(System.IO.FileInfo f1)
{
throw new P4API.Exceptions.FormCommandException();
}
///
/// Prompts the specified MSG.
///
/// The MSG.
/// The RSP.
public override void Prompt(string msg, ref string rsp)
{
rsp = _P4Result.RaiseOnPromptEvent(msg);
}
///
/// Resolves the file.
///
/// The merge data.
///
public override MergeAction ResolveFile(MergeData mergeData)
{
// don't handle a merge action here
throw new Exceptions.MergeNotImplemented();
}
///
/// Finisheds this instance.
///
public override void Finished()
{
_P4Result.SpecDef = base.SpecDef;
_P4Result.Finished();
}
///
/// Outputs the info.
///
/// The data.
public override void OutputInfo(string data)
{
_P4Result.AddInfo(data);
}
///
/// Cancels this instance.
///
///
public override bool Cancel()
{
return false;
}
///
/// Outputs the content.
///
/// The buffer.
/// if set to true [is text].
public override void OutputContent(byte[] buffer, bool IsText)
{
if (IsText)
{
string data = base.ContentEncoding.GetString(buffer);
_P4Result.AddInfo(data);
}
else
{
_P4Result.BinaryOutput = buffer;
}
}
///
/// Inputs the data.
///
/// The buffer.
public override void InputData(System.Text.StringBuilder buffer)
{
buffer.Append(_P4Result.InputData);
}
///
/// Outputs the record.
///
/// The record.
public override void OutputRecord(P4Record record)
{
_P4Result.AddRecord(record);
}
///
/// Outputs the message.
///
/// The message.
public override void OutputMessage(P4Message message)
{
_P4Result.AddP4Message(message);
//Console.WriteLine("{0}: {1}", (int)message.Severity, message.Identity);
switch (message.Severity)
{
case P4MessageSeverity.Empty: // E_EMPTY (0) | no error
_P4Result.AddString(message.Format());
break;
case P4MessageSeverity.Info: // E_INFO (1) | information, not necessarily an error
_P4Result.AddInfo(message.Format());
break;
case P4MessageSeverity.Warning: // E_WARN (2) | a minor error occurred
_P4Result.AddWarning(message.Format());
break;
case P4MessageSeverity.Failed: // E_FAILED(3) | the command was used incorrectly
_P4Result.AddError(message.Format());
break;
case P4MessageSeverity.Fatal: // E_FATAL (4) | fatal error, the command can't be processed
_P4Result.AddError(message.Format());
break;
default:
//TODO throw an error... unknown severity
break;
}
}
#endregion
}
}