Hey all,
After getting A LOT of help from everyone in these forums with my Sugar setup, I'm happy to say that I finally have something to contribute back: Java code for generating GUIDs.
If you're using Java to interact with Sugar (as I am), you may run in to the problem of how to generate new IDs for objects you want to create. Since Sugar is super-flexible when it comes to IDs, you can pretty much just use whatever id-generation scheme you want, and it should work just fine. However, if you want to keep your IDs "sugary", then you can use the code below to generate "real" SugarCRM-style GUIDs in Java.
I can't really take much credit for this code, as basically I did was take the existing PHP code for generating a GUID and convert it in to Java. Hopefully someone will find it useful though ...
I'm also working on some classes which correspond to the Sugar modules (eg. Account), so that you can do all the standard CRUD stuff with SugarCRM records, but from within Java. These classes all extend a base class which uses reflection HEAVILY, so that the various module sub-classes really have almost nothing in them except an (annotated) field list.Code:/** * This class contains general-purpose static methods for use with SugarCRM. * It contains no original code, only converted SugarCRM PHP code. * * @author Jeremy Walker * @author SugarCRM Development Team (original PHP code) * */ public class SugarUtil { public static String generateGuid() { Calendar cal = Calendar.getInstance(); int milliSeconds = cal.get(Calendar.MILLISECOND); int seconds = cal.get(Calendar.SECOND); String milliSecondsHex = Integer.toHexString(milliSeconds* 1000000); String secondsHex = Integer.toHexString(seconds); ensureLength(milliSecondsHex, 5); ensureLength(secondsHex, 6); String ret = milliSecondsHex + ""; ret += createGuidSection(3); ret += '-'; ret += createGuidSection(4); ret += '-'; ret += createGuidSection(4); ret += '-'; ret += createGuidSection(4); ret += '-'; ret += secondsHex; ret += createGuidSection(6); return ret; } public static void ensureLength(String original, int length) { int diff = original.length() - length; if (diff > 0) { // String is too long; trim it down to the proper side original = original.substring(0, length); } else if (diff < 0 ) { // String is too short; pad it with trailing zeroes for (int i = 0; i < diff; i ++) { original += "0"; } } return; } public static String createGuidSection (int characters) { String ret = ""; Random random = new Random(); for (int i=0; i < characters; i++) { ret += Integer.toHexString(random.nextInt(15)); } return ret; } }
I'll try and post said classes to this forum someday when they are ready, but I'm also kind of lazy, so if those classes sounds like something you'd really like to have feel free to post back here or private message me and let me know.


LinkBack URL
About LinkBacks



Reply With Quote
Bookmarks