unit ProductsAgent;
interface
uses SysUtils, System.Classes,
ObserverPattern, ApplicationContext,
SelectService, OptiServerService, UI_Data;
// Resource strings are stored as resources and linked into the executable or
// library so that they can be modified without recompiling the program.
resourcestring
SELECT_SERVER_URL = 'http://cacheaccept2010:57772/csp/dev1/WS.Prod.Select.CLS';
OPTI_SERVER_URL = 'http://cacheaccept2010:57772/csp/dev1/WS.Prod.OptiBox.OptiServer.CLS';
type
TZoekMethode = (KortTekst, IdentificatieNr, LeveranciersRef, Tekst);
TProductsAgent = class
private
FSelectServerSoap: SelectService.ProdSelectServerSoap;
FOptiBoxServerSoap: OptiServerService.OptiBoxServerSoap;
function GeefUiConfigLink(UserContext: TUserContext; UiInstellingenContext: TUiInstellingenContext): string;
procedure RegistreerMislukteOpzoeking(UiInstellingenContext: TUiInstellingenContext;
pxStatusObj: OptiServerService.pxStatus); overload;
procedure RegistreerLoadResultaten(UiInstellingenContext: TUiInstellingenContext; Instellingen: UI_Data.IXMLOptiBoxType);
function GeefBruikbareLijnenUitXMLDataSet(DataSetXml: string): TStringList;
procedure RegistreerZoekResultaten(ProductsContext: TProductsContext; DataSet: string); overload;
procedure RegistreerMislukteOpzoeking(ProductsContext: TProductsContext; pxStatusObj: SelectService.pxStatus); overload;
procedure RegistreerZoekResultaten(ProductDetailContext: TProductDetailContext;
pxBoxDataObj: OptiServerService.pxBoxData); overload;
procedure RegistreerMislukteOpzoeking(ProductDetailContext: TProductDetailContext;
pxStatusObj: OptiServerService.pxStatus); overload;
function GetBoxDataIDsViaProduct(ProductNr: Integer; UserContext: TUserContext;
ProductDetailContext: TProductDetailContext): string;
public
Constructor Create();
procedure LaadUIInstellingen(UserContext: TUserContext; UiInstellingenContext: TUiInstellingenContext);
procedure ZoekOp(UserContext: TUserContext; ProductsContext: TProductsContext; Zoekmethode: TZoekMethode; Zoek: string;
KLNr: string; LevNr: string; StockType: string; Taal: string);
procedure ZoekProductDataMetProductNr(ProductNr: Integer; UserContext: TUserContext;
ProductDetailContext: TProductDetailContext);
end;
implementation
uses
StrUtils, Xml.XMLIntf, Xml.XMLDoc;
constructor TProductsAgent.Create();
begin
FSelectServerSoap := SelectService.GetProdSelectServerSoap(false, SELECT_SERVER_URL, nil);
FOptiBoxServerSoap := OptiServerService.GetOptiBoxServerSoap(false, OPTI_SERVER_URL, nil);
end;
function TProductsAgent.GeefUiConfigLink(UserContext: TUserContext; UiInstellingenContext: TUiInstellingenContext): string;
var
ZoekData: OptiServerService.GetUIResource;
GetUIResourceResponseObj: OptiServerService.GetUIResourceResponse;
begin
ZoekData := nil;
try
// Request
ZoekData := OptiServerService.GetUIResource.Create();
ZoekData.SessionKey := UserContext.SessionKey;
GetUIResourceResponseObj := FOptiBoxServerSoap.GetUIResource(ZoekData);
// Response
if GetUIResourceResponseObj.GetUIResourceResult <> nil then
// Mislukt
RegistreerMislukteOpzoeking(UiInstellingenContext, GetUIResourceResponseObj.GetUIResourceResult);
// Gelukt, bruikbare informatie uit antwoord halen
if GetUIResourceResponseObj.varUIResource <> nil then
Result := GetUIResourceResponseObj.varUIResource.ValuesURL
else
Result := ''
finally
ZoekData.Free();
end;
end;
procedure TProductsAgent.RegistreerMislukteOpzoeking(UiInstellingenContext: TUiInstellingenContext;
pxStatusObj: OptiServerService.pxStatus);
begin
// Ander velden leegmaken
UiInstellingenContext.Instellingen := nil;
UiInstellingenContext.IsOK := pxStatusObj.IsOK;
UiInstellingenContext.ErrorMessage := pxStatusObj.Message_;
UiInstellingenContext.InternalErrorMessage := pxStatusObj.InternalMessage;
UiInstellingenContext.NotifyChanged();
end;
procedure TProductsAgent.LaadUIInstellingen(UserContext: TUserContext; UiInstellingenContext: TUiInstellingenContext);
var
UiConfigLink: string;
OptiBox: UI_Data.IXMLOptiBoxType;
begin
UiConfigLink := GeefUiConfigLink(UserContext, UiInstellingenContext);
if UiConfigLink = '' then
exit; // Return error response from previous call
OptiBox := LoadOptiBox(UiConfigLink);
RegistreerLoadResultaten(UiInstellingenContext, OptiBox);
end;
procedure TProductsAgent.RegistreerLoadResultaten(UiInstellingenContext: TUiInstellingenContext;
Instellingen: UI_Data.IXMLOptiBoxType);
begin
// Ander velden leegmaken
UiInstellingenContext.IsOK := True;
UiInstellingenContext.ErrorMessage := '';
UiInstellingenContext.InternalErrorMessage := '';
UiInstellingenContext.Instellingen := Instellingen;
UiInstellingenContext.NotifyChanged();
end;
procedure TProductsAgent.ZoekOp(UserContext: TUserContext; ProductsContext: TProductsContext; Zoekmethode: TZoekMethode;
Zoek: string; KLNr: string; LevNr: string; StockType: string; Taal: string);
var
ZoekData: SelectService.Select;
ZoekCriteria: SelectService.pxSelectCriteria; // Property van ZoekData
SelectResponseObj: SelectService.SelectResponse;
At: string;
begin
// At bepalen
case Zoekmethode of
KortTekst:
At := 'K';
IdentificatieNr:
At := 'I';
LeveranciersRef:
At := 'R';
Tekst:
At := 'T';
else
raise Exception.Create('Ongeldige zoekmethode');
end;
ZoekData := nil;
try
// Request
ZoekData := SelectService.Select.Create();
ZoekCriteria := SelectService.pxSelectCriteria.Create();
ZoekData.SessionKey := UserContext.SessionKey;
ZoekCriteria.Zoek := Zoek;
ZoekCriteria.At := At;
ZoekCriteria.KLNr := KLNr;
ZoekCriteria.LevNr := LevNr;
ZoekCriteria.StockType := StockType;
ZoekCriteria.SubAt := Taal;
ZoekCriteria.FormatCompact := 'KortTekst;IdentNr;StockType';
ZoekCriteria.FormatExpand := 'KortTekst;IdentNr;StockType;LangTekstN';
ZoekData.Criteria := ZoekCriteria;
SelectResponseObj := FSelectServerSoap.Select(ZoekData);
// Response
if SelectResponseObj.SelectResult = nil then
begin
// Gelukt
RegistreerZoekResultaten(ProductsContext, SelectResponseObj.varDataSet);
end
else
begin
// Mislukt
RegistreerMislukteOpzoeking(ProductsContext, SelectResponseObj.SelectResult);
end;
finally
ZoekData.Free();
end;
end;
function TProductsAgent.GeefBruikbareLijnenUitXMLDataSet(DataSetXml: string): TStringList;
var
BeginOpslaan: Boolean;
Lijn: string;
Lijnen: TStringList;
begin
Lijnen := TStringList.Create;
// Elke lijn overlopen (skip lege lijnen), maar pas beginnen opslaan vanaf dat we voorbij het schema zitten
BeginOpslaan := false;
for Lijn in SplitString(DataSetXml, sLineBreak) do
begin
if Lijn = '' then
continue;
if BeginOpslaan then
Lijnen.Add(Lijn);
if (BeginOpslaan = false) and (ContainsStr(Lijn, '')) then
begin
// De tekst is nu '<... xmlns="">', eerste deel er af knippen
Lijnen.Add(Copy(Lijn, 12));
// Vanaf nu opslaan
BeginOpslaan := True;
end;
end;
Result := Lijnen;
end;
procedure TProductsAgent.RegistreerZoekResultaten(ProductsContext: TProductsContext; DataSet: string);
var
TempStr: string;
Lijn: string;
Lijnen: TStringList;
LijnenProduct: TStringList;
XMLDoc: IXMLDocument;
NieuwProduct: TProductInformatie;
begin
// Andere velden juist zetten
ProductsContext.IsOK := True;
ProductsContext.ErrorMessage := '';
ProductsContext.InternalErrorMessage := '';
ProductsContext.Producten.Clear();
// Producten inladen
LijnenProduct := TStringList.Create;
Lijnen := GeefBruikbareLijnenUitXMLDataSet(DataSet);
try
// De 'Select' elementen overlopen en elk element mappen naar een property van een instantie.
// Deze instantie toevoegen aan de lijst.
for Lijn in Lijnen do
begin
if ContainsStr(Lijn, '') then
begin
// Product blok afgerond
// De eindtag zit geplakt aan de tag voor het nieuwe product. Bv ');
// Er zit een volledig item in de lijst nu
TempStr := LijnenProduct.Text;
XMLDoc := LoadXMLData(TempStr); // XML document van maken
// Elementen uit XML laden, toewijzen en instantie toevoegen aan lijst.
NieuwProduct := TProductInformatie.Create();
NieuwProduct.ProductNr := XMLDoc.DocumentElement.ChildValues['ProductNr'];
NieuwProduct.Tekst := XMLDoc.DocumentElement.ChildValues['Tekst'];
NieuwProduct.Kol1 := XMLDoc.DocumentElement.ChildValues['Kol1'];
NieuwProduct.Kol2 := XMLDoc.DocumentElement.ChildValues['Kol2'];
NieuwProduct.Kol3 := XMLDoc.DocumentElement.ChildValues['Kol3'];
NieuwProduct.Kol4 := XMLDoc.DocumentElement.ChildValues['Kol4'];
ProductsContext.Producten.Add(NieuwProduct);
// klaarzetten voor (mogelijks) volgend product
LijnenProduct.Clear();
LijnenProduct.Add('