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. // Voor schaalbaarheid en isolatie worden interfaces gebruikt die deze klasse implementeert. interface uses ObserverPattern, System.Generics.Collections, SysUtils; // SUB CONTEXTS: klassen die overeenkomen met bepaalde sub-context (bv. alles gerelateerd met authenticatie). // - User type 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 FPositie: TDictionary; FPlaatsing: TDictionary; FRichting: TDictionary; FIsOK: Boolean; FErrorMessage: string; FInternalErrorMessage: string; public constructor Create(); procedure NotifyChanged(); property Positie: TDictionary read FPositie write FPositie; property Plaatsing: TDictionary read FPlaatsing write FPlaatsing; property Richting: TDictionary read FRichting write FRichting; 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; FIsOK: Boolean; FErrorMessage: string; FInternalErrorMessage: string; public constructor Create(); procedure Reset(); procedure NotifyChanged(); property Producten: TList read FProducten; property IsOK: Boolean read FIsOK write FIsOK; property ErrorMessage: string read FErrorMessage write FErrorMessage; property InternalErrorMessage: string read FInternalErrorMessage write FInternalErrorMessage; end; // - Product Detail TProductVerpakking = class private FRawData: TObject; FID: string; FMetaCaption: string; FCutOrder: Integer; FBreedte: string; FDefaultBreedte: string; FDiepte: string; FDefaultDiepte: string; FHoogte: string; FDefaultHoogte: string; FAantal: string; FMaxCombinAantal: string; FPlaatsing: string; FPositie: string; FRichting: string; FDeelVanID: string; FDeelVan: TProductVerpakking; public property RawData: TObject read FRawData write FRawData; property ID: string read FID write FID; property MetaCaption: string read FMetaCaption write FMetaCaption; property CutOrder: Integer read FCutOrder write FCutOrder; property Breedte: string read FBreedte write FBreedte; property DefaultBreedte: string read FDefaultBreedte write FDefaultBreedte; property Diepte: string read FDiepte write FDiepte; property DefaultDiepte: string read FDefaultDiepte write FDefaultDiepte; property Hoogte: string read FHoogte write FHoogte; property DefaultHoogte: string read FDefaultHoogte write FDefaultHoogte; property Aantal: string read FAantal write FAantal; property MaxCombinAantal: string read FMaxCombinAantal write FMaxCombinAantal; property Plaatsing: string read FPlaatsing write FPlaatsing; property Positie: string read FPositie write FPositie; property Richting: string read FRichting write FRichting; property DeelVanID: string read FDeelVanID write FDeelVanID; property DeelVan: TProductVerpakking read FDeelVan write FDeelVan; function Equals(Obj: TObject): Boolean; override; end; TProductDetailContext = class(TSubject) private FGeselecteerdProduct: TProductInformatie; FProductVerpakkingen: TList; FIsOK: Boolean; FErrorMessage: string; FInternalErrorMessage: string; public constructor Create(); procedure NotifyChanged(); procedure Reset(); property GeselecteerdProduct: TProductInformatie read FGeselecteerdProduct write FGeselecteerdProduct; property ProductVerpakkingen: TList read FProductVerpakkingen; function GetDeelVanOpties(ProductVerpakking: TProductVerpakking): TList; 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 FUserContext: TUserContext; FUiInstellingenContext: TUiInstellingenContext; FProductsContext: TProductsContext; FProductDetailContext: TProductDetailContext; public constructor Create(); property UserContext: TUserContext read FUserContext write FUserContext; property UiInstellingenContext: TUiInstellingenContext read FUiInstellingenContext write FUiInstellingenContext; property ProductsContext: TProductsContext read FProductsContext write FProductsContext; property ProductDetailContext: TProductDetailContext read FProductDetailContext write FProductDetailContext; end; implementation // USER procedure TUserContext.NotifyChanged(); begin self.Change(); end; // UiInstellingen constructor TUiInstellingenContext.Create(); begin inherited Create(); FPositie := TDictionary.Create(); FPlaatsing := TDictionary.Create(); FRichting := 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; // Enkel updaten wanneer nog niet op nil stond. procedure TProductsContext.Reset(); begin if FProducten.Count > 0 then begin FProducten.Clear(); 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 TProductDetailContext.Create; begin inherited Create(); FProductVerpakkingen := TList.Create(); end; procedure TProductDetailContext.NotifyChanged(); begin self.Change(); end; // Enkel updaten wanneer nog niet op nil stond. procedure TProductDetailContext.Reset(); begin if (GeselecteerdProduct <> nil) or (ProductVerpakkingen.Count > 0) then begin GeselecteerdProduct := nil; ProductVerpakkingen.Clear(); NotifyChanged(); end; end; function TProductDetailContext.GetDeelVanOpties(ProductVerpakking: TProductVerpakking): TList; var ProductVerpakkingTeZoeken: TProductVerpakking; Namen: TList; begin Namen := TList.Create(); for ProductVerpakkingTeZoeken in FProductVerpakkingen do begin if ProductVerpakkingTeZoeken.Equals(ProductVerpakking) then Namen.Add(intToStr(ProductVerpakkingTeZoeken.CutOrder) + ': ' + ProductVerpakkingTeZoeken.MetaCaption) end; Result := Namen; end; // Product verpakking 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; // APPLICATION CONTEXT constructor TApplicationContext.Create(); begin FUserContext := TUserContext.Create(); FUiInstellingenContext := TUiInstellingenContext.Create(); FProductsContext := TProductsContext.Create(); FProductDetailContext := TProductDetailContext.Create(); end; end.