Skip to content

Commit

Permalink
Use dependency graph when deleting objects
Browse files Browse the repository at this point in the history
refs #9096
  • Loading branch information
gunnarbeutner committed Aug 26, 2015
1 parent 2a9ac26 commit de09a56
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
40 changes: 32 additions & 8 deletions lib/remote/configobjectutility.cpp
Expand Up @@ -24,6 +24,7 @@
#include "config/configwriter.hpp"
#include "base/exception.hpp"
#include "base/serializer.hpp"
#include "base/dependencygraph.hpp"
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/case_conv.hpp>
Expand Down Expand Up @@ -144,18 +145,29 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full

return true;
}
bool ConfigObjectUtility::DeleteObject(const ConfigObject::Ptr& object, const Array::Ptr& errors)

bool ConfigObjectUtility::DeleteObjectHelper(const ConfigObject::Ptr& object, bool cascade, const Array::Ptr& errors)
{
if (object->GetModule() != "_api") {
std::vector<Object::Ptr> parents = DependencyGraph::GetParents(object);

if (!parents.empty() && !cascade) {
if (errors)
errors->Add("Object cannot be deleted because it was not created using the API.");
errors->Add("Object cannot be deleted because other objects depend on it. Use cascading delete to delete it anyway.");

return false;
}

BOOST_FOREACH(const Object::Ptr& pobj, parents) {
ConfigObject::Ptr parentObj = dynamic_pointer_cast<ConfigObject>(pobj);

if (!parentObj)
continue;

DeleteObjectHelper(parentObj, cascade, errors);
}

Type::Ptr type = object->GetReflectionType();

ConfigItem::Ptr item = ConfigItem::GetObject(type->GetName(), object->GetName());

try {
Expand All @@ -172,7 +184,7 @@ bool ConfigObjectUtility::DeleteObject(const ConfigObject::Ptr& object, const Ar

return false;
}

String typeDir = type->GetPluralName();
boost::algorithm::to_lower(typeDir);

Expand All @@ -190,4 +202,16 @@ bool ConfigObjectUtility::DeleteObject(const ConfigObject::Ptr& object, const Ar

return true;
}


bool ConfigObjectUtility::DeleteObject(const ConfigObject::Ptr& object, bool cascade, const Array::Ptr& errors)
{
if (object->GetModule() != "_api") {
if (errors)
errors->Add("Object cannot be deleted because it was not created using the API.");

return false;
}

return DeleteObjectHelper(object, cascade, errors);
}

3 changes: 2 additions & 1 deletion lib/remote/configobjectutility.hpp
Expand Up @@ -44,10 +44,11 @@ class I2_REMOTE_API ConfigObjectUtility
const Array::Ptr& templates, const Dictionary::Ptr& attrs,
const Array::Ptr& errors);

static bool DeleteObject(const ConfigObject::Ptr& object, const Array::Ptr& errors);
static bool DeleteObject(const ConfigObject::Ptr& object, bool cascade, const Array::Ptr& errors);

private:
static String EscapeName(const String& name);
static bool DeleteObjectHelper(const ConfigObject::Ptr& object, bool cascade, const Array::Ptr& errors);
};

}
Expand Down
5 changes: 4 additions & 1 deletion lib/remote/deleteobjecthandler.cpp
Expand Up @@ -60,6 +60,8 @@ bool DeleteObjectHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& r

std::vector<ConfigObject::Ptr> objs = FilterUtility::GetFilterTargets(qd, params);

bool cascade = HttpUtility::GetLastParameter(params, "cascade");

Array::Ptr results = new Array();

BOOST_FOREACH(const ConfigObject::Ptr& obj, objs) {
Expand All @@ -70,9 +72,10 @@ bool DeleteObjectHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& r

Array::Ptr errors = new Array();

if (!ConfigObjectUtility::DeleteObject(obj, errors)) {
if (!ConfigObjectUtility::DeleteObject(obj, cascade, errors)) {
result1->Set("code", 500);
result1->Set("status", "Object could not be deleted.");
result1->Set("errors", errors);
} else {
result1->Set("code", 200);
result1->Set("status", "Object was deleted.");
Expand Down

0 comments on commit de09a56

Please sign in to comment.