unit ProductsAgent; interface uses SysUtils, System.Classes, System.Generics.Collections, 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(ProductVerpakkingenContext: TProductVerpakkingenContext; pxBoxDataObj: OptiServerService.pxBoxData); overload; procedure RegistreerMislukteOpzoeking(ProductDetailContext: TProductVerpakkingenContext; pxStatusObj: OptiServerService.pxStatus); overload; function GetBoxDataIDsViaProduct(ProductNr: Integer; UserContext: TUserContext; ProductDetailContext: TProductVerpakkingenContext): TList; 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 ZoekProductVerpakkingenMetProductNr(ProductNr: Integer; UserContext: TUserContext; ProductDetailContext: TProductVerpakkingenContext); 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 // Andere velden leegmaken UiInstellingenContext.Posities.Clear(); UiInstellingenContext.Plaatsing.Clear(); UiInstellingenContext.Richtingen.Clear(); 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); var Node: IXMLNode; I: Integer; KeyStr: string; begin // Ander velden leegmaken UiInstellingenContext.IsOK := True; UiInstellingenContext.ErrorMessage := ''; UiInstellingenContext.InternalErrorMessage := ''; UiInstellingenContext.Posities.Clear(); UiInstellingenContext.PositiesByValue.Clear(); if Instellingen.Positie <> nil then begin for I := 0 to Instellingen.Positie.Count - 1 do begin Node := Instellingen.Positie[I]; KeyStr := Node.GetAttributeNS('Key', ''); UiInstellingenContext.Posities.Add(KeyStr, Node.Text); UiInstellingenContext.PositiesByValue.Add(Node.Text, KeyStr); end; end; UiInstellingenContext.Plaatsing.Clear(); UiInstellingenContext.PlaatsingByValue.Clear(); if Instellingen.Plaatsing <> nil then begin for I := 0 to Instellingen.Plaatsing.Count - 1 do begin Node := Instellingen.Plaatsing[I]; KeyStr := Node.GetAttributeNS('Key', ''); UiInstellingenContext.Plaatsing.Add(KeyStr, Node.Text); UiInstellingenContext.PlaatsingByValue.Add(Node.Text, KeyStr); end; end; UiInstellingenContext.Richtingen.Clear(); UiInstellingenContext.RichtingenByValue.Clear(); if Instellingen.Richting <> nil then begin for I := 0 to Instellingen.Richting.Count - 1 do begin Node := Instellingen.Richting[I]; KeyStr := Node.GetAttributeNS('Key', ''); UiInstellingenContext.Richtingen.Add(KeyStr, Node.Text); UiInstellingenContext.RichtingenByValue.Add(Node.Text, KeyStr); end; end; UiInstellingenContext.OptiTypes.Clear(); UiInstellingenContext.OptiTypesByValue.Clear(); if Instellingen.Richting <> nil then begin for I := 0 to Instellingen.OptiType.Count - 1 do begin Node := Instellingen.OptiType[I]; KeyStr := Node.GetAttributeNS('Key', ''); UiInstellingenContext.OptiTypes.Add(KeyStr, Node.Text); UiInstellingenContext.OptiTypesByValue.Add(Node.Text, KeyStr); end; end; 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; NieuweProducten: TList; NieuwProduct: TProductInformatie; begin // Andere velden juist zetten ProductsContext.IsOK := True; ProductsContext.ErrorMessage := ''; ProductsContext.InternalErrorMessage := ''; try // Producten inladen LijnenProduct := TStringList.Create; Lijnen := GeefBruikbareLijnenUitXMLDataSet(DataSet); NieuweProducten := TList.Create(); // 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(); with NieuwProduct do begin ProductNr := XMLDoc.DocumentElement.ChildValues['ProductNr']; Tekst := XMLDoc.DocumentElement.ChildValues['Tekst']; Kol1 := XMLDoc.DocumentElement.ChildValues['Kol1']; Kol2 := XMLDoc.DocumentElement.ChildValues['Kol2']; Kol3 := XMLDoc.DocumentElement.ChildValues['Kol3']; Kol4 := XMLDoc.DocumentElement.ChildValues['Kol4']; end; NieuweProducten.Add(NieuwProduct); // klaarzetten voor (mogelijks) volgend product LijnenProduct.Clear(); LijnenProduct.Add('