unit ApplicationContext; // Dit is een klasse bedoeld om data te delen over de applicatie heen. // Dit kan bijvoorbeeld usernaam, login token, computernaam, ... // Informatie die elk component/klasse kan gebruiken. interface uses ObserverPattern, System.Generics.Collections, SysUtils, ReadOnlyList; // SUB CONTEXTS: klassen die overeenkomen met bepaalde sub-context (bv. alles gerelateerd met authenticatie). type // Configs (uit INI bestand) TConfigsContext = class private FIsProductieOmgeving: boolean; // Productie FSelectServerUrl: string; FOptiServerUrl: string; FUserServerUrl: string; public property IsProductieOmgeving: boolean read FIsProductieOmgeving write FIsProductieOmgeving; property SelectServerUrl: string read FSelectServerUrl write FSelectServerUrl; property OptiServerUrl: string read FOptiServerUrl write FOptiServerUrl; property UserServerUrl: string read FUserServerUrl write FUserServerUrl; end; // - User TUserContext = class(TSubject) private FSessionKey: string; FGebruikersNaam: string; FIsOK: boolean; FErrorMessage: string; FInternalErrorMessage: string; public procedure NotifyChanged(); property SessionKey: string read FSessionKey write FSessionKey; property GebruikersNaam: string read FGebruikersNaam write FGebruikersNaam; property IsOK: boolean read FIsOK write FIsOK; property ErrorMessage: string read FErrorMessage write FErrorMessage; property InternalErrorMessage: string read FInternalErrorMessage write FInternalErrorMessage; end; // - UI instellingen TUiInstellingenContext = class(TSubject) private FPositieByKey: TDictionary; FPositieByValue: TDictionary; FPlaatsingByKey: TDictionary; FPlaatsingByValue: TDictionary; FRichtingByKey: TDictionary; FRichtingByValue: TDictionary; FOptiTypeByKey: TDictionary; FOptiTypeByValue: TDictionary; FIsOK: boolean; FErrorMessage: string; FInternalErrorMessage: string; public constructor Create(); procedure NotifyChanged(); property Posities: TDictionary read FPositieByKey; property PositiesByValue: TDictionary read FPositieByValue; property Plaatsingen: TDictionary read FPlaatsingByKey; property PlaatsingByValue: TDictionary read FPlaatsingByValue; property Richtingen: TDictionary read FRichtingByKey; property RichtingenByValue: TDictionary read FRichtingByValue; property OptiTypes: TDictionary read FOptiTypeByKey; property OptiTypesByValue: TDictionary read FOptiTypeByValue; property IsOK: boolean read FIsOK write FIsOK; property ErrorMessage: string read FErrorMessage write FErrorMessage; property InternalErrorMessage: string read FInternalErrorMessage write FInternalErrorMessage; end; // - Product Selection TProductInformatie = class private FProductNr: Integer; FTekst: string; FKol1: string; FKol2: string; FKol3: string; FKol4: string; function GetOmschrijving1(): string; function GetOmschrijving2(): string; public property ProductNr: Integer read FProductNr write FProductNr; property Tekst: string read FTekst write FTekst; property Kol1: string read FKol1 write FKol1; property Kol2: string read FKol2 write FKol2; property Kol3: string read FKol3 write FKol3; property Kol4: string read FKol4 write FKol4; property Omschrijving1: string read GetOmschrijving1; property Omschrijving2: string read GetOmschrijving2; end; TProductsContext = class(TSubject) private FProducten: TList; FGeselecteerdProduct: TProductInformatie; FIsOK: boolean; FErrorMessage: string; FInternalErrorMessage: string; procedure SetGeselecteerdProduct(ProductInformatie: TProductInformatie); function GetProductenLijst: TReadOnlyList; public constructor Create(); procedure NotifyChanged(); procedure Reset(Notify: boolean = True); property Producten: TReadOnlyList read GetProductenLijst; procedure SetProducten(Collection: TEnumerable); property GeselecteerdProduct: TProductInformatie read FGeselecteerdProduct write SetGeselecteerdProduct; property IsOK: boolean read FIsOK write FIsOK; property ErrorMessage: string read FErrorMessage write FErrorMessage; property InternalErrorMessage: string read FInternalErrorMessage write FInternalErrorMessage; end; // - Product Detail TProductVerpakkingParameter = class private FID: string; FProgLabel: string; FToolTip: string; FWaarde: string; FDefaultWaarde: string; public property ID: string read FID write FID; property ProgLabel: string read FProgLabel write FProgLabel; property ToolTip: string read FToolTip write FToolTip; property Waarde: string read FWaarde write FWaarde; property DefaultWaarde: string read FDefaultWaarde write FDefaultWaarde; end; TProductVerpakking = class private FDefaultRawData: TObject; FID: string; FObjType: string; FObjRef: string; FCutOrder: Integer; FDeelVanCaption: string; FDeelVanID: string; FDeelVan: TProductVerpakking; FVolumeVanCaption: string; FVolumeVanID: string; FMetaCaption: string; FMetaID: string; FPosities: TList; FDefaultPosities: TList; FOptiType: string; FDefaultOptiType: string; FRichtingen: TList; FDefaultRichtingen: TList; FPlaatsing: string; FDefaultPlaatsing: string; FHoogte: string; FDefaultHoogte: string; FBreedte: string; FDefaultBreedte: string; FDiepte: string; FDefaultDiepte: string; FAantal: string; FMaxCombinAantal: string; FParameters: TList; function GetDeelVanDisplay(): string; function GetMetaCaptionDisplay(): string; function GetBreedteDisplay: string; function GetDiepteDisplay: string; function GetHoogteDisplay: string; function GetParametersLijst: TReadOnlyList; public property RawDefaultData: TObject read FDefaultRawData write FDefaultRawData; property ID: string read FID write FID; property ObjType: string read FObjType write FObjType; property ObjRef: string read FObjRef write FObjRef; property CutOrder: Integer read FCutOrder write FCutOrder; property DeelVanID: string read FDeelVanID write FDeelVanID; property DeelVanCaption: string read FDeelVanCaption write FDeelVanCaption; property DeelVan: TProductVerpakking read FDeelVan write FDeelVan; property DeelVanDisplay: string read GetDeelVanDisplay; property VolumeVanCaption: string read FVolumeVanCaption write FVolumeVanCaption; property VolumeVanID: string read FVolumeVanID write FVolumeVanID; property MetaCaptionDisplay: string read GetMetaCaptionDisplay; property MetaCaption: string read FMetaCaption write FMetaCaption; property MetaID: string read FMetaID write FMetaID; property Posities: TList read FPosities; property DefaultPosities: TList read FDefaultPosities; property OptiType: string read FOptiType write FOptiType; property DefaultOptiType: string read FDefaultOptiType write FDefaultOptiType; property Richtingen: TList read FRichtingen; property DefaultRichtingen: TList read FDefaultRichtingen; property Plaatsing: string read FPlaatsing write FPlaatsing; property DefaultPlaatsing: string read FDefaultPlaatsing write FDefaultPlaatsing; property HoogteDisplay: string read GetHoogteDisplay; property Hoogte: string read FHoogte write FHoogte; property DefaultHoogte: string read FDefaultHoogte write FDefaultHoogte; property BreedteDisplay: string read GetBreedteDisplay; property Breedte: string read FBreedte write FBreedte; property DefaultBreedte: string read FDefaultBreedte write FDefaultBreedte; property DiepteDisplay: string read GetDiepteDisplay; property Diepte: string read FDiepte write FDiepte; property DefaultDiepte: string read FDefaultDiepte write FDefaultDiepte; property Aantal: string read FAantal write FAantal; property MaxCombinAantal: string read FMaxCombinAantal write FMaxCombinAantal; property Parameters: TReadOnlyList read GetParametersLijst; procedure SetParameters(Collection: TEnumerable); function Equals(Obj: TObject): boolean; override; constructor Create(); end; TProductVerpakkingenContext = class(TSubject) private FProductVerpakkingen: TList; FGeselecteerdeVerpakking: TProductVerpakking; FIsOK: boolean; FErrorMessage: string; FInternalErrorMessage: string; function GetProductVerpakkingenLijst: TReadOnlyList; procedure SetGeselecteerdeVerpakking(ProductVerpakking: TProductVerpakking); public constructor Create(); procedure NotifyChanged(); procedure Reset(Notify: boolean = True); property ProductVerpakkingen: TReadOnlyList read GetProductVerpakkingenLijst; procedure SetProductVerpakkingen(Collection: TEnumerable); procedure AddProductVerpakking(ProductVerpakking: TProductVerpakking); procedure VerwijderProductVerpakking(ProductVerpakking: TProductVerpakking); function GetDeelVanOpties(ProductVerpakking: TProductVerpakking): TReadOnlyList; function GetDeelVan(DeelVanIdentificatie: string): TProductVerpakking; property GeselecteerdeVerpakking: TProductVerpakking read FGeselecteerdeVerpakking write SetGeselecteerdeVerpakking; property IsOK: boolean read FIsOK write FIsOK; property ErrorMessage: string read FErrorMessage write FErrorMessage; property InternalErrorMessage: string read FInternalErrorMessage write FInternalErrorMessage; end; // VERPAKKING META TProductVerpakkingMeta = class private FID: Integer; FName: string; FOptiType: string; public property ID: Integer read FID write FID; property Name: string read FName write FName; property OptiType: string read FOptiType write FOptiType; end; TProductVerpakkingMetaContext = class(TSubject) private FProductGroepen: TList; FMetas: TList; FIsOK: boolean; FErrorMessage: string; FInternalErrorMessage: string; function GetProductGroepen: TReadOnlyList; function GetProductMetas: TReadOnlyList; public constructor Create(); property ProductGroepen: TReadOnlyList read GetProductGroepen; procedure SetProductGroepen(Collection: TEnumerable); property Metas: TReadOnlyList read GetProductMetas; procedure SetMetas(Collection: TEnumerable); property IsOK: boolean read FIsOK write FIsOK; property ErrorMessage: string read FErrorMessage write FErrorMessage; property InternalErrorMessage: string read FInternalErrorMessage write FInternalErrorMessage; end; // APPLICATION CONTEXT // Application context bevat alle sub-contexts TApplicationContext = class private FConfigsContext: TConfigsContext; FUserContext: TUserContext; FUiInstellingenContext: TUiInstellingenContext; FProductsContext: TProductsContext; FProductDetailContext: TProductVerpakkingenContext; FProductVerpakkingMetaContext: TProductVerpakkingMetaContext; public constructor Create(); property ConfigsContext: TConfigsContext read FConfigsContext write FConfigsContext; property UserContext: TUserContext read FUserContext write FUserContext; property UiInstellingenContext: TUiInstellingenContext read FUiInstellingenContext write FUiInstellingenContext; property ProductsContext: TProductsContext read FProductsContext write FProductsContext; property ProductVerpakkingContext: TProductVerpakkingenContext read FProductDetailContext write FProductDetailContext; property ProductVerpakkingMetaContext: TProductVerpakkingMetaContext read FProductVerpakkingMetaContext write FProductVerpakkingMetaContext; end; implementation // USER procedure TUserContext.NotifyChanged(); begin self.Change(); end; // UiInstellingen constructor TUiInstellingenContext.Create(); begin inherited Create(); FPositieByKey := TDictionary.Create(); FPositieByValue := TDictionary.Create(); FPlaatsingByKey := TDictionary.Create(); FPlaatsingByValue := TDictionary.Create(); FRichtingByKey := TDictionary.Create(); FRichtingByValue := TDictionary.Create(); FOptiTypeByKey := TDictionary.Create(); FOptiTypeByValue := TDictionary.Create(); end; procedure TUiInstellingenContext.NotifyChanged(); begin self.Change(); end; // PRODUCT constructor TProductsContext.Create(); begin inherited Create(); FProducten := TList.Create(); end; procedure TProductsContext.NotifyChanged(); begin self.Change(); end; function TProductsContext.GetProductenLijst: TReadOnlyList; begin Result := TReadOnlyList.Create(FProducten); end; procedure TProductsContext.SetProducten(Collection: TEnumerable); begin FProducten.Clear(); FProducten.AddRange(Collection); self.NotifyChanged(); end; procedure TProductsContext.SetGeselecteerdProduct(ProductInformatie: TProductInformatie); begin FGeselecteerdProduct := ProductInformatie; self.NotifyChanged(); end; // Enkel updaten wanneer nog niet op nil stond. procedure TProductsContext.Reset(Notify: boolean = True); begin if (GeselecteerdProduct <> nil) or (FProducten.Count > 0) then begin FProducten.Clear(); GeselecteerdProduct := nil; if Notify then NotifyChanged(); end; end; function TProductInformatie.GetOmschrijving1(): string; begin if FKol4 = '' then begin Result := ''; end else begin try Result := copy(FKol4, 0, Pos(',', FKol4) - 1); except Result := ''; end; end; end; function TProductInformatie.GetOmschrijving2(): string; begin if FKol4 = '' then begin Result := ''; end else begin try Result := copy(FKol4, Pos(',', FKol4) + 2); Result := copy(Result, 0, Pos(',', Result) - 1); // Tweede deel na de komma except Result := ''; end; end; end; // PRODUCT DETAIL constructor TProductVerpakkingenContext.Create; begin inherited Create(); FProductVerpakkingen := TList.Create(); end; procedure TProductVerpakkingenContext.NotifyChanged(); begin self.Change(); end; procedure TProductVerpakkingenContext.Reset(Notify: boolean = True); begin if (FGeselecteerdeVerpakking <> nil) or (FProductVerpakkingen.Count > 0) then begin FProductVerpakkingen.Clear(); FGeselecteerdeVerpakking := nil; if Notify then NotifyChanged(); end; end; function TProductVerpakkingenContext.GetProductVerpakkingenLijst: TReadOnlyList; begin Result := TReadOnlyList.Create(FProductVerpakkingen); end; procedure TProductVerpakkingenContext.SetProductVerpakkingen(Collection: TEnumerable); begin FProductVerpakkingen.Clear(); FProductVerpakkingen.AddRange(Collection); self.NotifyChanged(); end; procedure TProductVerpakkingenContext.AddProductVerpakking(ProductVerpakking: TProductVerpakking); begin if ProductVerpakking = nil then exit; FProductVerpakkingen.Add(ProductVerpakking); FGeselecteerdeVerpakking := ProductVerpakking; self.NotifyChanged(); end; procedure TProductVerpakkingenContext.VerwijderProductVerpakking(ProductVerpakking: TProductVerpakking); var TempVerpakking: TProductVerpakking; I: Integer; begin if ProductVerpakking = nil then exit; // De andere objecten afgaan en we verwijderen elke 'Deel van' referentie for TempVerpakking in FProductVerpakkingen do begin if (TempVerpakking.DeelVan <> nil) and TempVerpakking.DeelVan.Equals(ProductVerpakking) then begin TempVerpakking.DeelVan := nil; TempVerpakking.DeelVanCaption := ''; end; end; // We gaan matchen met equals, niet op de objectinstantie. // Zoeken I := 0; while (I < FProductVerpakkingen.Count) and (not FProductVerpakkingen[I].Equals(ProductVerpakking)) do I := I + 1; // Wissen if I < FProductVerpakkingen.Count then begin FProductVerpakkingen.Delete(I); // Als dit product gelijk was aan het geselecteerde product, geselecteerde product leegmaken. if ProductVerpakking.Equals(FGeselecteerdeVerpakking) then FreeAndNil(FGeselecteerdeVerpakking); end; self.NotifyChanged(); end; procedure TProductVerpakkingenContext.SetGeselecteerdeVerpakking(ProductVerpakking: TProductVerpakking); begin FGeselecteerdeVerpakking := ProductVerpakking; self.NotifyChanged(); end; function TProductVerpakkingenContext.GetDeelVanOpties(ProductVerpakking: TProductVerpakking): TReadOnlyList; var ProductVerpakkingTeZoeken: TProductVerpakking; ProductVerpakkingen: TList; begin ProductVerpakkingen := TList.Create(); for ProductVerpakkingTeZoeken in FProductVerpakkingen do if not ProductVerpakkingTeZoeken.Equals(ProductVerpakking) then ProductVerpakkingen.Add(ProductVerpakkingTeZoeken); Result := TReadOnlyList.Create(ProductVerpakkingen); end; function TProductVerpakkingenContext.GetDeelVan(DeelVanIdentificatie: string): TProductVerpakking; var TempVerpakking: TProductVerpakking; begin for TempVerpakking in FProductVerpakkingen do begin if DeelVanIdentificatie = TempVerpakking.GetDeelVanDisplay then begin Result := TempVerpakking; exit; end; end; Result := nil; end; // Product verpakking constructor TProductVerpakking.Create(); begin inherited Create(); FPosities := TList.Create(); FDefaultPosities := TList.Create(); FRichtingen := TList.Create(); FDefaultRichtingen := TList.Create(); FParameters := TList.Create(); end; function TProductVerpakking.Equals(Obj: TObject): boolean; begin if Obj = nil then Result := False else if not(Obj is TProductVerpakking) then Result := False else if (Obj as TProductVerpakking).ID = self.ID then Result := True else Result := False; end; function TProductVerpakking.GetDeelVanDisplay(): string; begin Result := intToStr(CutOrder) + ': ' + GetMetaCaptionDisplay(); end; function TProductVerpakking.GetMetaCaptionDisplay(): string; begin if FMetaCaption <> '' then Result := copy(FMetaCaption, Pos(':', FMetaCaption) + 2) else Result := ''; end; function TProductVerpakking.GetBreedteDisplay: string; begin if FBreedte <> '' then Result := FBreedte else if FDefaultBreedte <> '' then Result := FDefaultBreedte else Result := '0'; end; function TProductVerpakking.GetDiepteDisplay: string; begin if FDiepte <> '' then Result := FDiepte else if FDefaultDiepte <> '' then Result := FDefaultDiepte else Result := '0'; end; function TProductVerpakking.GetHoogteDisplay: string; begin if FHoogte <> '' then Result := FHoogte else if FDefaultHoogte <> '' then Result := FDefaultHoogte else Result := '0'; end; function TProductVerpakking.GetParametersLijst: TReadOnlyList; begin Result := TReadOnlyList.Create(FParameters); end; procedure TProductVerpakking.SetParameters(Collection: TEnumerable); begin FParameters.Clear(); FParameters.AddRange(Collection); end; // VERPAKKING META constructor TProductVerpakkingMetaContext.Create(); begin inherited Create(); FProductGroepen := TList.Create(); FMetas := TList.Create(); end; function TProductVerpakkingMetaContext.GetProductGroepen(): TReadOnlyList; begin Result := TReadOnlyList.Create(FProductGroepen); end; function TProductVerpakkingMetaContext.GetProductMetas(): TReadOnlyList; begin Result := TReadOnlyList.Create(FMetas); end; procedure TProductVerpakkingMetaContext.SetProductGroepen(Collection: TEnumerable); begin FProductGroepen.Clear(); FProductGroepen.AddRange(Collection); self.Change(); end; procedure TProductVerpakkingMetaContext.SetMetas(Collection: TEnumerable); begin FMetas.Clear(); FMetas.AddRange(Collection); self.Change(); end; // APPLICATION CONTEXT constructor TApplicationContext.Create(); begin FConfigsContext := TConfigsContext.Create(); FUserContext := TUserContext.Create(); FUiInstellingenContext := TUiInstellingenContext.Create(); FProductsContext := TProductsContext.Create(); FProductDetailContext := TProductVerpakkingenContext.Create(); FProductVerpakkingMetaContext := TProductVerpakkingMetaContext.Create(); end; end.