ItemSupplier::~ItemSupplier() { // Delete the association from the related objects if (getSupplier()) getSupplier()->items.erase(this); if (getItem()) getItem()->suppliers.erase(this); // Delete all owned purchase operations while (firstOperation) delete firstOperation; // Trigger level and cluster recomputation HasLevel::triggerLazyRecomputation(); }
DECLARE_EXPORT void ItemSupplier::validate(Action action) { // Catch null supplier and item pointers Supplier *sup = getSupplier(); Item *it = getItem(); Location *loc = getLocation(); if (!sup || !it) { // Invalid ItemSupplier model if (!sup && !it) throw DataException("Missing supplier and item on a itemsupplier"); else if (!sup) throw DataException("Missing supplier on a itemsupplier on item '" + it->getName() + "'"); else if (!it) throw DataException("Missing item on a itemsupplier on supplier '" + sup->getName() + "'"); } // Check if a ItemSupplier with 1) identical supplier, 2) identical item // 3) identical location, and 4) overlapping effectivity dates already exists Supplier::itemlist::const_iterator i = sup->getItems().begin(); for (; i != sup->getItems().end(); ++i) if (i->getItem() == it && i->getEffective().overlap(getEffective()) && i->getLocation() == loc && &*i != this) break; // Apply the appropriate action switch (action) { case ADD: if (i != sup->getItems().end()) { throw DataException("ItemSupplier of '" + sup->getName() + "' and '" + it->getName() + "' already exists"); } break; case CHANGE: throw DataException("Can't update a itemsupplier"); case ADD_CHANGE: // ADD is handled in the code after the switch statement if (i == sup->getItems().end()) break; throw DataException("Can't update a itemsupplier"); case REMOVE: // This ItemSupplier was only used temporarily during the reading process delete this; if (i == sup->getItems().end()) // Nothing to delete throw DataException("Can't remove nonexistent itemsupplier of '" + sup->getName() + "' and '" + it->getName() + "'"); delete &*i; return; } }