]> SALOME platform Git repositories - tools/siman.git/blobdiff - Workspace/Siman-Common/src/org/splat/service/StudyServiceImpl.java
Salome HOME
Satisfy some PMD rules.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / StudyServiceImpl.java
index 40f106cc693b870862e5d6e894f6c03bc39c9534..d75d3ccfa29cd440919d9bd6ac10de208b6a8753 100644 (file)
@@ -51,6 +51,7 @@ import org.splat.dal.bo.som.IDBuilder;
 import org.splat.dal.bo.som.ProgressState;
 import org.splat.dal.bo.som.ProjectElement;
 import org.splat.dal.bo.som.Publication;
+import org.splat.dal.bo.som.ReaderRelation;
 import org.splat.dal.bo.som.Scenario;
 import org.splat.dal.bo.som.SimulationContext;
 import org.splat.dal.bo.som.Study;
@@ -79,12 +80,14 @@ import org.splat.log.AppLogger;
 import org.splat.service.dto.DocToCompareDTO;
 import org.splat.service.dto.DocumentDTO;
 import org.splat.service.dto.StudyFacadeDTO;
+import org.splat.service.dto.UserDTO;
 import org.splat.service.technical.IndexService;
 import org.splat.service.technical.ProjectSettingsService;
 import org.splat.service.technical.ProjectSettingsServiceImpl;
 import org.splat.service.technical.RepositoryService;
 import org.splat.service.technical.ProjectSettingsService.Step;
 import org.splat.som.Revision;
+import org.splat.util.BeanHelper;
 import org.springframework.transaction.annotation.Transactional;
 
 import com.lowagie.text.Document;
@@ -335,13 +338,25 @@ public class StudyServiceImpl implements StudyService {
         */
        @Transactional
        public boolean addContributor(final Study aStudy, final User user) {
-               List<User> contributor = getModifiableContributors(aStudy); // Initializes contributor
-               for (Iterator<User> i = contributor.iterator(); i.hasNext();) {
-                       User present = i.next();
-                       if (present.equals(user)) {
-                               return false;
+               List<User> contributors = getModifiableContributors(aStudy); // Initializes contributor
+               
+               if(contributors.contains(user)) {
+                       return false;
+               }
+               
+               //Remove user from readers
+               try {
+                       List<UserDTO> readers = getReaders(aStudy.getIndex());
+                       for(UserDTO reader : readers) {
+                               if(reader.getIndex() == user.getIndex()) {
+                                       //user must be the actual user in the relationship object in the aStudy object for this to work
+                                       aStudy.removeRelation(ReaderRelation.class, user);
+                               }
                        }
+               } catch(InvalidParameterException e) {
+                       LOG.error(e.getMessage(), e);   
                }
+               
                boolean absent = getModifiableActors(aStudy).add(user); // User may already be a reviewer or an approver
 
                aStudy.addRelation(new ContributorRelation(aStudy, user));
@@ -362,6 +377,7 @@ public class StudyServiceImpl implements StudyService {
         * @see #isPublic()
         * @see Publication#approve(Date)
         */
+       @Transactional
        public boolean moveToReference(final Study aStudy) {
                if (aStudy.getProgressState() != ProgressState.APPROVED) {
                        return false;
@@ -382,6 +398,7 @@ public class StudyServiceImpl implements StudyService {
         * 
         * @see org.splat.service.StudyService#update(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.Study.Properties)
         */
+       @Transactional
        public boolean update(final Study aStudy, final Properties sprop)
                        throws InvalidPropertyException {
                if (sprop.getTitle() != null) {
@@ -415,20 +432,13 @@ public class StudyServiceImpl implements StudyService {
         */
        @Transactional
        public boolean removeContributor(final Study aStudy, final User... users) {
-               List<User> contributor = getModifiableContributors(aStudy); // Initializes contributor
+               List<User> contributors = getModifiableContributors(aStudy); // Initializes contributor
                Boolean done = false;
-               for (int i = 0; i < users.length; i++) {
-                       User user = users[i];
-                       for (Iterator<User> j = contributor.iterator(); j.hasNext();) {
-                               User present = j.next();
-                               if (!present.equals(user)) {
-                                       continue;
-                               }
-
+               for(User user : users) {
+                       if(contributors.contains(user)) {
                                aStudy.removeRelation(ContributorRelation.class, user);
-                               j.remove(); // Updates the contributor shortcut
+                               contributors.remove(user);
                                done = true;
-                               break;
                        }
                }
                if (done) {
@@ -442,6 +452,7 @@ public class StudyServiceImpl implements StudyService {
         * 
         * @see org.splat.service.StudyService#removeProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext)
         */
+       @Transactional
        public boolean removeProjectContext(final Study aStudy,
                        final SimulationContext context) {
                boolean done = getStepService().removeSimulationContext(
@@ -480,6 +491,7 @@ public class StudyServiceImpl implements StudyService {
 
                                ValidationCycleRelation link = cycle.getContext();
                                aStudy.addRelation(link);
+                               getValidationCycleDAO().flush();
                                aStudyDTO.getAllRelations().add(link); // RKV
 
                                validactor.put(cname, link.getTo()); // Replaces the cycle if exists as default,
@@ -500,6 +512,7 @@ public class StudyServiceImpl implements StudyService {
         *            a study to demote
         * @return true if the demotion succeeded.
         */
+       @Transactional
        public boolean demote(final Study aStudy) {
                if (aStudy.getProgressState() == ProgressState.inCHECK) {
                        aStudy.setProgressState(ProgressState.inDRAFT);
@@ -596,13 +609,12 @@ public class StudyServiceImpl implements StudyService {
         *            the study to update
         * @return true if the study is updated successfully
         */
-       @Transactional
        private boolean update(final Study aStudy) {
                boolean isOk = false;
                try {
                        getStudyDAO().merge(aStudy); // Update of relational base
                        setShortCuts(aStudy); // RKV: initialize transient actors set
-                       // RKV: getIndex().update(aStudy); // Update of Lucene index
+                       //RKV: getIndex().update(aStudy); // Update of Lucene index
                        isOk = true;
                } catch (Exception e) {
                        LOG.error("STD-000001", e, aStudy.getIndex(), e.getMessage());
@@ -750,25 +762,20 @@ public class StudyServiceImpl implements StudyService {
         * @return true if reindexing succeeded
         */
        private boolean updateKnowledgeElementsIndex(final Study aStudy) {
-               // boolean isOk = false;
-               // try {
-               // IndexService lucin = getIndex();
-               //
-               // for (Iterator<Scenario> i = aStudy.getScenariiList().iterator(); i
-               // .hasNext();) {
-               // Scenario scene = i.next();
-               // for (Iterator<KnowledgeElement> j = scene
-               // .getAllKnowledgeElements().iterator(); j.hasNext();) {
-               // KnowledgeElement kelm = j.next();
-               // lucin.update(kelm);
-               // }
-               // }
-               // isOk = true;
-               // } catch (Exception error) {
-               // LOG.error("Unable to re-index Knowledge Elements, reason:",
-               // error);
-               // }
-               // return isOk;
+//             boolean isOk = false;
+//              try {
+//                      IndexService lucin = getIndex();
+//                      for(Scenario scenario : aStudy.getScenariiList()) {
+//                              for (KnowledgeElement element : scenario.getAllKnowledgeElements()) {
+//                                      lucin.update(element);
+//                              }
+//                      }
+//                      isOk = true;
+//              } catch (Exception error) {
+//                      LOG.error("Unable to re-index Knowledge Elements, reason:",
+//                      error);
+//              }
+//              return isOk;
                return true;
        }
 
@@ -1134,7 +1141,6 @@ public class StudyServiceImpl implements StudyService {
         * 
         * @see org.splat.service.StudyService#markStudyAsReference(org.splat.dal.bo.som.Study)
         */
-       @Override
        @Transactional
        public void markStudyAsReference(final Study aStudy) {
 
@@ -1149,7 +1155,6 @@ public class StudyServiceImpl implements StudyService {
         * 
         * @see org.splat.service.StudyService#removeStudyAsReference(org.splat.dal.bo.som.Study)
         */
-       @Override
        @Transactional
        public void removeStudyAsReference(final Study aStudy) {
 
@@ -1183,7 +1188,6 @@ public class StudyServiceImpl implements StudyService {
         * 
         * @see org.splat.service.StudyService#setDescription(java.lang.Long, java.lang.String)
         */
-       @Override
        @Transactional
        public void setDescription(final Long studyId, final String descriptionText)
                        throws InvalidParameterException {
@@ -1203,7 +1207,6 @@ public class StudyServiceImpl implements StudyService {
         * 
         * @see org.splat.service.StudyService#removeStudyDescription(java.lang.Long)
         */
-       @Override
        @Transactional
        public boolean removeDescription(final Long studyId)
                        throws InvalidParameterException {
@@ -1346,10 +1349,73 @@ public class StudyServiceImpl implements StudyService {
                } catch (DocumentException e) {
                        LOG.error("Sorry, the DocumentException is thrown.", e);
                }
-
+       
                return resultPath;
        }
+       
+       /** 
+        * {@inheritDoc}
+        * @see org.splat.service.StudyService#getReaders(long)
+        */
+       @Transactional(readOnly = true)
+       public List<UserDTO> getReaders(final long studyId) throws InvalidParameterException {
+               Study aStudy = selectStudy(studyId);
+               if(aStudy == null){
+                       throw new InvalidParameterException(PARAM_STUDY_ID, String.valueOf(studyId));
+               }
+               List<Relation> relations = aStudy.getRelations(ReaderRelation.class);
+               List<UserDTO> result = new ArrayList<UserDTO>();
+               for(Relation relation : relations){
+                       result.add(BeanHelper.copyBean(relation.getTo(), UserDTO.class));
+               }
+               return Collections.unmodifiableList(result);
+       }
+       
+       /** 
+        * {@inheritDoc}
+        * @see org.splat.service.StudyService#addReader(long, long)
+        */
+       @Transactional
+       public boolean addReader(final long studyId, final long userId) throws InvalidParameterException {
+               Study aStudy = selectStudy(studyId);
+               if(aStudy == null){
+                       throw new InvalidParameterException(PARAM_STUDY_ID, String.valueOf(studyId));
+               }
+               User user = _userService.selectUser(userId);
+               if(user == null){
+                       throw new InvalidParameterException("userId", String.valueOf(userId));
+               }
+
+               for(Relation relation : aStudy.getRelations(ReaderRelation.class)) {
+                       if(user.equals(relation.getTo())) {
+                               return false;
+                       }
+               }
+               aStudy.addRelation(new ReaderRelation(aStudy, user));
+               update(aStudy);
+               return true;
+       }
+
+       /** 
+        * {@inheritDoc}
+        * @see org.splat.service.StudyService#removeReader(long, long)
+        */
+       @Transactional
+       public boolean removeReader(final long studyId, final long userId) throws InvalidParameterException {
+               Study aStudy = selectStudy(studyId);
+               if(aStudy == null){
+                       throw new InvalidParameterException(PARAM_STUDY_ID, String.valueOf(studyId));
+               }
+               User user = _userService.selectUser(userId);
+               if(user == null){
+                       throw new InvalidParameterException("userId", String.valueOf(userId));
+               }
 
+               Relation relation = aStudy.removeRelation(ReaderRelation.class, user);
+               update(aStudy);
+               return relation != null;
+       }
+       
        /**
         * Get project settings.
         *