Salome HOME
Refactoring continues: UserService is created instead of UserDirectory. Database...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / StudyServiceImpl.java
index 5a0cbce2e6ae1031eab58576bd18e8347d8043dd..b9cd50e7269971c9be4d47152530775acdcccec9 100644 (file)
@@ -10,6 +10,8 @@
 package org.splat.service;
 
 import java.io.IOException;
+import java.text.DecimalFormat;
+import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.Date;
@@ -46,7 +48,6 @@ import org.splat.dal.dao.som.ValidationCycleDAO;
 import org.splat.kernel.InvalidPropertyException;
 import org.splat.kernel.MissedPropertyException;
 import org.splat.kernel.MultiplyDefinedException;
-import org.splat.kernel.UserDirectory;
 import org.splat.log.AppLogger;
 import org.splat.service.technical.IndexService;
 import org.splat.service.technical.ProjectSettingsService;
@@ -113,6 +114,11 @@ public class StudyServiceImpl implements StudyService {
         */
        private DocumentTypeService _documentTypeService;
 
+       /**
+        * Injected user service.
+        */
+       private UserService _userService;
+
        /**
         * {@inheritDoc}
         * 
@@ -436,7 +442,7 @@ public class StudyServiceImpl implements StudyService {
                boolean isOk = false;
                try {
                        getStudyDAO().update(aStudy); // Update of relational base
-                       setShortCuts(aStudy); //RKV: initialize transient actors set
+                       setShortCuts(aStudy); // RKV: initialize transient actors set
                        getIndex().update(aStudy); // Update of Lucene index
                        isOk = true;
                } catch (Exception error) {
@@ -461,10 +467,77 @@ public class StudyServiceImpl implements StudyService {
                        tool = new IDBuilder(aStudy.getDate());
                        getIDBuilderDAO().create(tool);
                }
-               aStudy.setReference(tool.buildReference(pattern, aStudy));
+               aStudy.setReference(buildReference(tool, pattern, aStudy));
                return true;
        }
 
+       /**
+        * Build reference for the study. The reference of the study is stored as a new reference pattern (IDBuilder).
+        * 
+        * @param aBuilder
+        *            the id builder
+        * @param study
+        *            the study
+        * @param pattern
+        *            the reference pattern
+        * @return true if reference building is succeded
+        */
+       @Transactional
+       public String buildReference(IDBuilder aBuilder, String pattern, Study study) {
+               char[] format = pattern.toCharArray();
+               char[] ref = new char[80]; // Better evaluate the length of the generated string
+               int next = aBuilder.getBase() + 1;
+
+               int count = 0;
+               for (int i = 0; i < format.length; i++) {
+
+                       // Insertion of attribute values
+                       if (format[i] == '%') {
+                               i += 1;
+
+                               if (format[i] == 'y') { // Insertion of year in format 2 (e.g. 09) or 4 (e.g. 2009) digits
+                                       int n = i;
+                                       while (format[i] == 'y') {
+                                               i += 1;
+                                               if (i == format.length)
+                                                       break;
+                                       }
+                                       SimpleDateFormat tostring = new SimpleDateFormat("yyyy");
+                                       String year = tostring.format(study.getDate());
+                                       year = year.substring(4 - (i - n), 4); // 4-(i-n) must be equal to either 0 or 2
+                                       for (int j = 0; j < year.length(); j++) {
+                                               ref[count] = year.charAt(j);
+                                               count += 1;
+                                       }
+                                       i -= 1; // Back to the last 'y' character
+                               } else if (format[i] == '0') { // Insertion of the index
+                                       int n = i;
+                                       while (format[i] == '0') {
+                                               i += 1;
+                                               if (i == format.length)
+                                                       break;
+                                       }
+                                       DecimalFormat tostring = new DecimalFormat(
+                                                       pattern.substring(n, i));
+                                       String number = tostring.format(next);
+                                       for (int j = 0; j < number.length(); j++) {
+                                               ref[count] = number.charAt(j);
+                                               count += 1;
+                                       }
+                                       i -= 1; // Back to the last '0' character
+                               }
+                               // Keep the character
+                       } else {
+                               ref[count] = format[i];
+                               count += 1;
+                       }
+               }
+               // Incrementation of the number of study
+               aBuilder.setBase(next);
+               getIDBuilderDAO().update(aBuilder);
+               return String.copyValueOf(ref, 0, count);
+       }
+
        /**
         * Find an id builder by date.
         * 
@@ -576,13 +649,13 @@ public class StudyServiceImpl implements StudyService {
                                        if (actype[i] == Actor.manager) {
                                                actor = from.getAuthor();
                                        } else if (actype[i] == Actor.Nx1) {
-                                               List<User> manager = UserDirectory
+                                               List<User> manager = getUserService()
                                                                .selectUsersWhere(uprop
                                                                                .setOrganizationName("Nx1"));
                                                if (manager.size() == 1)
                                                        actor = manager.get(0);
                                        } else if (actype[i] == Actor.Nx2) {
-                                               List<User> manager = UserDirectory
+                                               List<User> manager = getUserService()
                                                                .selectUsersWhere(uprop
                                                                                .setOrganizationName("Nx2"));
                                                if (manager.size() == 1)
@@ -1030,4 +1103,20 @@ public class StudyServiceImpl implements StudyService {
        public void setDocumentTypeService(DocumentTypeService documentTypeService) {
                _documentTypeService = documentTypeService;
        }
+
+       /**
+        * Get the userService.
+        * @return the userService
+        */
+       public UserService getUserService() {
+               return _userService;
+       }
+
+       /**
+        * Set the userService.
+        * @param userService the userService to set
+        */
+       public void setUserService(UserService userService) {
+               _userService = userService;
+       }
 }