try {
if (dObj instanceof Project) { // Projekt löschen
final Project p = (Project) dObj;
for (Employee e : p.getEmployees()) { // Employees des Projekts auf rauslinken vorbereiten
EPRData eprd = new EPRData(); // (müssen nach dem Löschen des Projekte rausgelinkt werden)
eprd.employee = e.getId();
eprd.project = p.getId();
EmployeeProjectRelation epr = new EmployeeProjectRelation(this, eprd);
epr.setState(DataObject.Status.REMOVED);
dbConnector.execute(epr.toSQL(owner));
SDBCPool.getInstance().informEx(epr);
}
} else if (dObj instanceof WorkPackage) { // WorkPackage löschen
final WorkPackage wp = (WorkPackage) dObj;
for (Employee e : wp.getEmployees()) { // Employees aus dem Workpackage rauslinken
EWPRData ewprd = new EWPRData();
ewprd.employee = e.getId();
ewprd.workpackage = wp.getId();
DataObject tmp = new EmployeeWorkPackageRelation(this, ewprd);
tmp.setState(DataObject.Status.REMOVED);
dbConnector.execute(tmp.toSQL(owner));
syncDown.addObject(tmp);
SDBCPool.getInstance().informEx(tmp);
}
} else if (dObj instanceof EmployeeWorkPackageRelation) { // EWPR Löschen
final EmployeeWorkPackageRelation relation = (EmployeeWorkPackageRelation) dObj;
final WorkPackage wp = relation.getRight();
final Employee e = relation.getLeft();
logger.info("Remove " + e.getName() + " from wp " + wp.getName());
if (e.isWorkPackageManager(wp)) { // Neuen Manager suchen
Employee newMgr = getNewMgr(wp, wp.getManager());
logger.info("was the manager of the wp, new manager will be " + newMgr.getName());
if (!wp.hasEmployee(newMgr)) { // ggf neuen Manager verlinken
EWPRData ewprd = new EWPRData();
ewprd.employee = newMgr.getId();
ewprd.workpackage = wp.getId();
EmployeeWorkPackageRelation ewpr = new EmployeeWorkPackageRelation(this, ewprd);
if (isAlreadyInDB(ewpr)) {
ewpr.setState(DataObject.Status.MODIFIED);
dbConnector.execute(ewpr.toSQL(owner));
ewpr.setState(DataObject.Status.NEW);
} else {
dbConnector.execute(ewpr.toSQL(owner));
}
syncDown.addObject(ewpr);
SDBCPool.getInstance().informEx(ewpr);
}
wp.setManager(newMgr);
dbConnector.execute(wp.toSQL(owner));
SDBCPool.getInstance().informEx(wp);
}
for (Task t : wp.getTasks(null)) { // Alle Tasks & Durations des Employees löschen
if (t.getEmployeeId() == e.getId()) {
for (Duration d : t.getDurations()) {
if (d.getEmployeeId() != e.getId()) {
throw new RuntimeException("Should not happen");
}
d.setState(DataObject.Status.REMOVED);
adopt(d);
}
if (!t.getDurations().isEmpty()) {
throw new RuntimeException("Should not happen");
}
t.setState(DataObject.Status.REMOVED);
adopt(t);
}
}
} else if (dObj instanceof EmployeeProjectRelation) { // Employee aus Projekt linken
final EmployeeProjectRelation relation = (EmployeeProjectRelation) dObj;
final Employee e = relation.getLeft();
if (existsProject(relation.getRightId())) { // Falls das Projekt nich gelöscht wurde, den Employee auch aus den WP linken
final Project p = relation.getRight();
logger.info("Remove " + e.getName() + " from project " + p.getName());
for (WorkPackage wp : p.getWorkPackages()) {
if (wp.hasEmployee(e)) {
logger.info(" is involved in " + wp.getName());
EWPRData ewprd = new EWPRData();
ewprd.employee = e.getId();
ewprd.workpackage = wp.getId();
EmployeeWorkPackageRelation ewpr = new EmployeeWorkPackageRelation(this, ewprd);
ewpr.setState(DataObject.Status.REMOVED);
adopt(ewpr);
}
}
for (DataObject dataObject : p.getFTOverrides()) {
FTOverride override = (FTOverride) dataObject;
if (override.getEmployeeId() == e.getId()) {
logger.info(" there is also an override " + override);
override.setState(DataObject.Status.REMOVED);
adopt(override);
}
}
if (e.isProjectManager(p)) { // Neuen Manager suchen
Employee newMgr = getNewMgr(p, p.getManager());
logger.info("was the company manager, the new one will be " + newMgr.getName());
if (!p.hasEmployee(newMgr)) { // ggf ins Projekt linken
EPRData eprd = new EPRData();
eprd.employee = newMgr.getId();
eprd.project = p.getId();
EmployeeProjectRelation epr = new EmployeeProjectRelation(this, eprd);
if (isAlreadyInDB(epr)) {