if (!comments)
comments = boost::make_shared<Dictionary>();
- int legacy_id = m_NextCommentID;
- m_NextCommentID++;
- comment->Set("legacy_id", legacy_id);
-
String id = Utility::NewUUID();
comments->Set(id, comment);
owner->Set("comments", comments);
String CommentProcessor::GetIDFromLegacyID(int id)
{
- return Convert::ToString(id);
+ map<int, String>::iterator it = m_LegacyCommentCache.find(id);
+
+ if (it == m_LegacyCommentCache.end())
+ throw_exception(invalid_argument("Invalid legacy comment ID specified."));
+
+ return it->second;
}
DynamicObject::Ptr CommentProcessor::GetOwnerByCommentID(const String& id)
String id;
Dictionary::Ptr comment;
BOOST_FOREACH(tie(id, comment), comments) {
- if (comment->Contains("legacy_id")) {
- int legacy_id = comment->Get("legacy_id");
+ int legacy_id;
+
+ if (!comment->Contains("legacy_id")) {
+ legacy_id = m_NextCommentID;
+ m_NextCommentID++;
+ comment->Set("legacy_id", legacy_id);
+ } else {
+ legacy_id = comment->Get("legacy_id");
+ }
+
+ if (legacy_id >= m_NextCommentID)
+ m_NextCommentID = legacy_id + 1;
- m_LegacyCommentCache[legacy_id] = id;
+ if (m_LegacyCommentCache.find(legacy_id) != m_LegacyCommentCache.end()) {
+ /* The legacy_id is already in use by another comment;
+ * this shouldn't usually happen - assign it a new ID */
- if (legacy_id > m_NextCommentID)
- m_NextCommentID = legacy_id;
+ legacy_id = m_NextCommentID;
+ m_NextCommentID++;
+ comment->Set("legacy_id", legacy_id);
}
+ m_LegacyCommentCache[legacy_id] = id;
m_CommentCache[id] = owner;
}
}
static Dictionary::Ptr GetCommentByID(const String& id);
static void InvalidateCommentCache(void);
+ static void ValidateCommentCache(void);
private:
static int m_NextCommentID;
CommentProcessor(void);
static void AddCommentsToCache(const DynamicObject::Ptr& owner);
- static void ValidateCommentCache(void);
};
}
if (!downtimes)
downtimes = boost::make_shared<Dictionary>();
- int legacy_id = m_NextDowntimeID;
- m_NextDowntimeID++;
- downtime->Set("legacy_id", legacy_id);
-
String id = Utility::NewUUID();
downtimes->Set(id, downtime);
owner->Set("downtimes", downtimes);
String DowntimeProcessor::GetIDFromLegacyID(int id)
{
- return Convert::ToString(id);
+ map<int, String>::iterator it = m_LegacyDowntimeCache.find(id);
+
+ if (it == m_LegacyDowntimeCache.end())
+ throw_exception(invalid_argument("Invalid legacy downtime ID specified."));
+
+ return it->second;
}
DynamicObject::Ptr DowntimeProcessor::GetOwnerByDowntimeID(const String& id)
String id;
Dictionary::Ptr downtime;
BOOST_FOREACH(tie(id, downtime), downtimes) {
- if (downtime->Contains("legacy_id")) {
- int legacy_id = downtime->Get("legacy_id");
+ int legacy_id;
+
+ if (!downtime->Contains("legacy_id")) {
+ legacy_id = m_NextDowntimeID;
+ m_NextDowntimeID++;
+ downtime->Set("legacy_id", legacy_id);
+ } else {
+ legacy_id = downtime->Get("legacy_id");
+ }
- m_LegacyDowntimeCache[legacy_id] = id;
+ if (legacy_id >= m_NextDowntimeID)
+ m_NextDowntimeID = legacy_id + 1;
- if (legacy_id > m_NextDowntimeID)
- m_NextDowntimeID = legacy_id;
+ if (m_LegacyDowntimeCache.find(legacy_id) != m_LegacyDowntimeCache.end()) {
+ /* The legacy_id is already in use by another downtime;
+ * this shouldn't usually happen - assign it a new ID. */
+ legacy_id = m_NextDowntimeID;
+ m_NextDowntimeID++;
+ downtime->Set("legacy_id", legacy_id);
}
+ m_LegacyDowntimeCache[legacy_id] = id;
m_DowntimeCache[id] = owner;
}
}
static bool IsDowntimeActive(const Dictionary::Ptr& downtime);
static void InvalidateDowntimeCache(void);
+ static void ValidateDowntimeCache(void);
private:
static int m_NextDowntimeID;
DowntimeProcessor(void);
static void AddDowntimesToCache(const DynamicObject::Ptr& owner);
- static void ValidateDowntimeCache(void);
};
}
Dictionary::Ptr Host::GetDowntimes(void) const
{
+ DowntimeProcessor::ValidateDowntimeCache();
+
return Get("downtimes");
}
Dictionary::Ptr Host::GetComments(void) const
{
+ CommentProcessor::ValidateCommentCache();
+
return Get("comments");
}
Dictionary::Ptr Service::GetDowntimes(void) const
{
+ DowntimeProcessor::ValidateDowntimeCache();
+
return Get("downtimes");
}
Dictionary::Ptr Service::GetComments(void) const
{
+ CommentProcessor::ValidateCommentCache();
+
return Get("comments");
}