#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>
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 {
return false;
}
-
+
String typeDir = type->GetPluralName();
boost::algorithm::to_lower(typeDir);
return true;
}
-
\ No newline at end of file
+
+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);
+}
+
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);
};
}
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) {
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.");