I am upgrading mirth connect 2.2.1 to 3.7.0. In the latest version messageid is generated as long datatype where as in old version it is generated as GUID. Our SQL tables have unique-identifier columns to accept the messageid. So now I need to convert Long datatype to UUID/GUID in javascript. Every message has the unique messageid so for a single message the GUID generated should be same. How to convert it?
-
I am not sure if I understood correctly. You want a function that: When you pass a long it returns a guid. When you pass the same long it always returns the same guid There is no straight way as far as i know. but you could just create a random guid and the last characters could be your long – Carlos Garcia Apr 17 '19 at 09:20
-
UUID by definition depends on many elements (e.g. time: https://www.ietf.org/rfc/rfc4122.txt), but not on some passed in value. You can simply generate one time UUID and save it, why you need relation with some variable? – Justinas Apr 17 '19 at 09:22
-
Possible duplicate of [Create GUID / UUID in JavaScript?](https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript) – Justinas Apr 17 '19 at 09:22
2 Answers
Since you are using Mirth Connect which uses the Rhino engine to run JavaScript and optionally load Java classes, the easiest way is going to be to use Java to help you other than pure Javascript.
var guid = new java.util.UUID(messageId, messageId);
Since your messageId isn't going to be unique across channels, you can replace one of the parameters with something else. Maybe take part of the channelId?
const UUID = java.util.UUID;
var guid = new UUID(UUID.fromString(channelId).getMostSignificantBits(), messageId);
-
Not sure why I got two downvotes 6 months after I posted this answer without any comments as to why, but this answer does solve the question asked of how to convert a java long to a GUID using javascript in mirth (which uses mozilla rhino for the javascript engine and does have access to java classes.) – agermano Oct 29 '19 at 15:44
Others should correct me if I am wrong but no, JavaScript does not provide a function for converting GUID to Long.
In fact, I am not even sure it is possible to go from Long to GUID consistently without data loss or compression in that both data structures don't have the same lengths.
In some cases, where the GUID upper bits are zeros, it could be possible to convert from Long to GUID but in most cases, there would be a serious security risk of overflowing, no?

- 330
- 3
- 15
-
A GUID is just a string representation of a 128-bit number. A Java long, which is what is relevant in a mirth context, is 64-bits, or exactly half of a GUID. – agermano Apr 17 '19 at 14:53