← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5104: Minor modification to uid code generator to ensure codes don't start with a number. This is to m...

 

------------------------------------------------------------
revno: 5104
committer: Bob Jolliffe bobjolliffe@xxxxxxxxx
branch nick: dhis2
timestamp: Thu 2011-11-03 14:36:56 +0000
message:
  Minor modification to uid code generator to ensure codes don't start with a number.  This is to make them compatible with xml:id spec.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/CodeGenerator.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/CodeGenerator.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/CodeGenerator.java	2011-10-23 20:18:55 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/CodeGenerator.java	2011-11-03 14:36:56 +0000
@@ -34,8 +34,10 @@
  */
 public class CodeGenerator
 {
-    public static final String allowedChars = "0123456789" + "abcdefghijklmnopqrstuvwxyz"
+    public static final String letters = "abcdefghijklmnopqrstuvwxyz"
         + "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+    
+    public static final String allowedChars = "0123456789" + letters;
 
     public static final int NUMBER_OF_CODEPOINTS = allowedChars.length();
     public static final int CODESIZE = 11;
@@ -51,7 +53,11 @@
         SecureRandom sr = new SecureRandom();
 
         char[] randomChars = new char[CODESIZE];
-        for ( int i = 0; i < CODESIZE; ++i )
+        
+        // first char should be a letter
+        randomChars[0] = letters.charAt( sr.nextInt( letters.length() ) );
+        
+        for ( int i = 1; i < CODESIZE; ++i )
         {
             randomChars[i] = allowedChars.charAt( sr.nextInt( NUMBER_OF_CODEPOINTS ) );
         }