97 lines
2.9 KiB
Java
97 lines
2.9 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() {
|
|
// Studierende
|
|
this.add(new Student("students/jotto5", "Jocelyn", "Otto", "0001"));
|
|
this.add(new Student("students/vnachn", "Vorname", "Nachname", "0002"));
|
|
this.add(new Student("students/alovel", "Ada", "Lovelace", "0003"));
|
|
this.add(new Student("students/aturin", "Alan", "Turing", "0004"));
|
|
this.add(new Student("students/dknuth", "Donald", "Knuth", "0005"));
|
|
this.add(new Student("students/ltorva", "Linus", "Torvalds", "0006"));
|
|
this.add(new Student("students/dritch", "Dennis", "Ritchie", "0007"));
|
|
|
|
// Gruppen
|
|
this.add(new Group("groups/if24ws2-b", "Softwareentwicklung WiSe 24/25 Seminargruppe 2"));
|
|
this.add(new Group("groups/admins", "Administrator:innen"));
|
|
this.add(new Group("groups/alumni", "Alumni"));
|
|
this.add(new Group("groups/leer", "Leere Gruppe"));
|
|
|
|
this.addRelation("groups/if24ws2-b", "students/jotto5");
|
|
this.addRelation("groups/admins", "students/vnachn");
|
|
this.addRelation("groups/admins", "students/ltorva");
|
|
this.addRelation("groups/alumni", "students/alovel");
|
|
this.addRelation("groups/alumni", "students/aturin");
|
|
this.addRelation("groups/alumni", "students/ltorva");
|
|
}
|
|
|
|
/**
|
|
* Fügt ein neues DataObject hinzu.
|
|
*/
|
|
public void add(DataObject a) {
|
|
// TODO: Auf doppelte UIDs überprüfen!
|
|
this.objs.put(a.uid, a);
|
|
}
|
|
|
|
/**
|
|
* Fügt eine neue Objekt-Beziehung hinzu.
|
|
*/
|
|
public void addRelation(String a, String b) {
|
|
// TODO: Prüfen ob die Objekte existieren!
|
|
this.relations.put(a, b);
|
|
}
|
|
|
|
/**
|
|
* Gibt das DataObject mit der angegebenen UID zurück
|
|
* oder null wenn es keines gibt.
|
|
*/
|
|
public DataObject get(String path) {
|
|
if ( !objs.containsKey(path) ) return null;
|
|
return objs.get(path);
|
|
}
|
|
|
|
}
|