97 lines
2.8 KiB
Java
97 lines
2.8 KiB
Java
package hsmw.jotto5.beleg.data;
|
|
|
|
import java.util.HashMap;
|
|
|
|
/**
|
|
* Das Model der Anwendung.
|
|
*
|
|
* Stellt einfache Abfragen bereit und verwaltet die Anbindung an ein Speicher-Backend.
|
|
* Zugriff auf Daten ist Thread-sicher. Um ständiges Zusammenbauen von Objekten
|
|
* zu vermeiden und um besser vom eigentlichen Backend zu abstrahieren sind viele
|
|
* einfache Abfragen als Methode umgesetzt.
|
|
* <p>
|
|
* Denkbar ist zum Beispiel eine Implementation auf LDAP-Basis, oder in einer relationalen
|
|
* Datenbank.
|
|
* <p>
|
|
* Die aktuelle Implementierung speichert alle DataObjects in einer HashMap, mit ihrer
|
|
* UID als Key. Das erlaubt schnelle forward-lookups (also Suche nach Obj mit gegebener UID),
|
|
* was die deutlich häufiger ausgeführte Operation sein sollte.
|
|
*/
|
|
public class Model {
|
|
|
|
// die Hilfsvariable für das Singleton-Pattern
|
|
private static Model model = null;
|
|
|
|
private HashMap<String, DataObject> objs;
|
|
private HashMap<String, String> relations;
|
|
|
|
/**
|
|
* Singleton.
|
|
*/
|
|
public static Model getModel() {
|
|
if (model == null)
|
|
model = new Model();
|
|
return model;
|
|
}
|
|
|
|
/**
|
|
* Initialisiert ein leeres Model.
|
|
*/
|
|
public Model() {
|
|
objs = new HashMap<String, DataObject>();
|
|
relations = new HashMap<String, String>();
|
|
}
|
|
|
|
/**
|
|
* Füllt das Model mit Beispieldaten.
|
|
*/
|
|
public void fillMockData() {
|
|
Group g1, g2, g3;
|
|
|
|
// Studierende
|
|
new Student("students/jotto5", "Jocelyn", "Otto").bind(this);
|
|
new Student("students/vnachn", "Vorname", "Nachname").bind(this);
|
|
new Student("students/alovel", "Ada", "Lovelace").bind(this);
|
|
new Student("students/aturin", "Alan", "Turing").bind(this);
|
|
new Student("students/dknuth", "Donald", "Knuth").bind(this);
|
|
new Student("students/ltorva", "Linus", "Torvalds").bind(this);
|
|
new Student("students/dritch", "Dennis", "Ritchie").bind(this);
|
|
|
|
// Gruppen
|
|
g1 = new Group("groups/IF24wS2-B", "Softwareentwicklung WiSe 24/25 Seminargruppe 2");
|
|
g2 = new Group("groups/admins", "Administrator:innen");
|
|
g3 = new Group("groups/alumni", "Alumni");
|
|
new Group("groups/leer", "Leere Gruppe").bind(this);
|
|
g1.bind(this);
|
|
g2.bind(this);
|
|
g3.bind(this);
|
|
g1.addMember("students/jotto5");
|
|
g2.addMember("students/vnachn");
|
|
g2.addMember("students/ltorva");
|
|
g3.addMember("students/alovel");
|
|
g3.addMember("students/aturin");
|
|
g3.addMember("students/ltorva");
|
|
}
|
|
|
|
/*
|
|
* Folgende Funktionen sind nur durch die DataObject-Objekte
|
|
* zu verwenden, und sind daher protected.
|
|
*/
|
|
|
|
/**
|
|
* Fügt ein neues DataObject hinzu, also bindet es an das Model.
|
|
*/
|
|
protected void add(DataObject a) {
|
|
// TODO: Auf doppelte UIDs überprüfen!
|
|
this.objs.put(a.uid, a);
|
|
}
|
|
|
|
/**
|
|
* Fügt eine neue Objekt-Beziehung hinzu.
|
|
*/
|
|
protected void addRelation(String a, String b) {
|
|
// TODO: Prüfen ob die Objekte existieren!
|
|
this.relations.put(a, b);
|
|
}
|
|
|
|
}
|