unit ProductsAgent; interface uses SysUtils, System.Classes, ObserverPattern, ApplicationContext, SelectService, OptiServerService; // 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 TProductsAgent = class private FSelectServerSoap: SelectService.ProdSelectServerSoap; FOptiBoxServerSoap: OptiServerService.OptiBoxServerSoap; 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 ZoekOpMetKortTekst(Zoek: string; KLNr: string; At: string; UserContext: TUserContext; ProductsContext: TProductsContext); 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; procedure TProductsAgent.ZoekOpMetKortTekst(Zoek: string; KLNr: string; At: string; UserContext: TUserContext; ProductsContext: TProductsContext); var ZoekData: SelectService.Select; ZoekCriteria: SelectService.pxSelectCriteria; // Property van ZoekData SelectResponseObj: SelectService.SelectResponse; begin ZoekData := nil; try // Request ZoekData := SelectService.Select.Create(); ZoekCriteria := SelectService.pxSelectCriteria.Create(); ZoekData.SessionKey := UserContext.SessionKey; ZoekCriteria.KLNr := KLNr; ZoekCriteria.Zoek := Zoek; ZoekCriteria.At := At; 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 LijnenProduct.Add(Lijn); if ContainsStr(Lijn, '') then begin // Er zit een volledig item in de lijst 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); end; end; finally Lijnen.Free(); LijnenProduct.Free(); end; ProductsContext.NotifyChanged(); end; procedure TProductsAgent.RegistreerMislukteOpzoeking(ProductsContext: TProductsContext; pxStatusObj: SelectService.pxStatus); begin // Ander velden leegmaken ProductsContext.Producten.Clear(); ProductsContext.IsOK := pxStatusObj.IsOK; ProductsContext.ErrorMessage := pxStatusObj.Message_; ProductsContext.InternalErrorMessage := pxStatusObj.InternalMessage; ProductsContext.NotifyChanged(); end; function TProductsAgent.GetBoxDataIDsViaProduct(ProductNr: Integer; UserContext: TUserContext; ProductDetailContext: TProductDetailContext): string; var ZoekData: OptiServerService.GetBoxDataIDsViaProduct; GetBoxDataIDsViaProductResponseObj: OptiServerService.GetBoxDataIDsViaProductResponse; TempStr: string; Lijn: string; Lijnen: TStringList; LijnenBoxDataID: TStringList; XMLDoc: IXMLDocument; begin ZoekData := nil; try // Request ZoekData := OptiServerService.GetBoxDataIDsViaProduct.Create(); ZoekData.SessionKey := UserContext.SessionKey; ZoekData.ProductNr := ProductNr; GetBoxDataIDsViaProductResponseObj := FOptiBoxServerSoap.GetBoxDataIDsViaProduct(ZoekData); // Response if GetBoxDataIDsViaProductResponseObj.GetBoxDataIDsViaProductResult <> nil then // Mislukt RegistreerMislukteOpzoeking(ProductDetailContext, GetBoxDataIDsViaProductResponseObj.GetBoxDataIDsViaProductResult); // Bruikbare informatie uit antwoord halen // Producten inladen LijnenBoxDataID := TStringList.Create; Lijnen := GeefBruikbareLijnenUitXMLDataSet(GetBoxDataIDsViaProductResponseObj.varDataSet); try // 'GetBoxDataIDsViaObjTypeAndRef' element overlopen en mappen naar een instantie. for Lijn in Lijnen do begin LijnenBoxDataID.Add(Lijn); if ContainsStr(Lijn, '') then begin // Er zit een volledig item in de lijst TempStr := LijnenBoxDataID.Text; XMLDoc := LoadXMLData(TempStr); // XML document van maken // Elementen uit XML laden. Result := XMLDoc.DocumentElement.ChildValues['ID']; end; end; finally Lijnen.Free(); LijnenBoxDataID.Free(); end; finally ZoekData.Free(); end; end; procedure TProductsAgent.ZoekProductDataMetProductNr(ProductNr: Integer; UserContext: TUserContext; ProductDetailContext: TProductDetailContext); var ZoekData: OptiServerService.GetBoxDataViaId; BoxDataID: string; GetBoxDataViaIDResponseObj: GetBoxDataViaIDResponse; begin BoxDataID := GetBoxDataIDsViaProduct(ProductNr, UserContext, ProductDetailContext); // Controleren dat er resultaat was if BoxDataID = '' then raise Exception.Create('Er is geen Box ID gevonden.'); // Request // Met ID, box data opvragen ZoekData := nil; try ZoekData := OptiServerService.GetBoxDataViaId.Create(); ZoekData.SessionKey := UserContext.SessionKey; ZoekData.BoxDataID := BoxDataID; GetBoxDataViaIDResponseObj := FOptiBoxServerSoap.GetBoxDataViaId(ZoekData); // Response if GetBoxDataViaIDResponseObj.GetBoxDataViaIDResult = nil then begin // Gelukt RegistreerZoekResultaten(ProductDetailContext, GetBoxDataViaIDResponseObj.varBoxData); end else begin // Mislukt RegistreerMislukteOpzoeking(ProductDetailContext, GetBoxDataViaIDResponseObj.GetBoxDataViaIDResult); end; finally ZoekData.Free(); end; end; procedure TProductsAgent.RegistreerZoekResultaten(ProductDetailContext: TProductDetailContext; pxBoxDataObj: OptiServerService.pxBoxData); begin // Andere velden juist zetten ProductDetailContext.IsOK := True; ProductDetailContext.ErrorMessage := ''; ProductDetailContext.InternalErrorMessage := ''; ProductDetailContext.ProductDetailInformatie := pxBoxDataObj; ProductDetailContext.NotifyChanged(); end; procedure TProductsAgent.RegistreerMislukteOpzoeking(ProductDetailContext: TProductDetailContext; pxStatusObj: OptiServerService.pxStatus); begin ProductDetailContext.ProductDetailInformatie := nil; ProductDetailContext.IsOK := pxStatusObj.IsOK; ProductDetailContext.ErrorMessage := pxStatusObj.Message_; ProductDetailContext.InternalErrorMessage := pxStatusObj.InternalMessage; ProductDetailContext.NotifyChanged(); end; end.