1 package org.splat.dal.bo.som;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
8 import java.text.DecimalFormat;
9 import java.text.SimpleDateFormat;
10 import java.util.Date;
12 import org.splat.dal.dao.som.Database;
15 public class IDBuilder {
17 @SuppressWarnings("unused")
19 private int base; // Number of studies created in this cycle
21 // ==============================================================================================================================
23 // ==============================================================================================================================
25 protected IDBuilder () {
27 public IDBuilder (Date date) {
28 // -------------------------------
29 SimpleDateFormat get = new SimpleDateFormat("yyyy");
30 String year = get.format(date);
31 cycle = Integer.valueOf(year);
35 // ==============================================================================================================================
36 // Public member functions
37 // ==============================================================================================================================
39 public String buildReference (String pattern, Study study) {
40 // -------------------------------------------------------------
41 char[] format = pattern.toCharArray();
42 char[] ref = new char[80]; // Better evaluate the length of the generated string
46 for (int i=0; i<format.length; i++) {
48 // Insertion of attribute values
49 if (format[i] == '%') {
52 if (format[i] == 'y') { // Insertion of year in format 2 (e.g. 09) or 4 (e.g. 2009) digits
54 while (format[i] == 'y') {
56 if (i == format.length) break;
58 SimpleDateFormat tostring = new SimpleDateFormat("yyyy");
59 String year = tostring.format(study.getDate());
60 year = year.substring(4-(i-n), 4); // 4-(i-n) must be equal to either 0 or 2
61 for (int j=0; j<year.length(); j++) {
62 ref[count] = year.charAt(j);
65 i -= 1; // Back to the last 'y' character
67 if (format[i] == '0') { // Insertion of the index
69 while (format[i] == '0') {
71 if (i == format.length) break;
73 DecimalFormat tostring = new DecimalFormat(pattern.substring(n, i));
74 String number = tostring.format(next);
75 for (int j=0; j<number.length(); j++) {
76 ref[count] = number.charAt(j);
79 i -= 1; // Back to the last '0' character
83 ref[count] = format[i];
87 // Incrementation of the number of study
89 Database.getSession().update(this);
90 return String.copyValueOf(ref, 0, count);