Salome HOME
Fix:
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / StudyServiceImpl.java
index 122892593aaffa36f41b229841537a9a7e82681d..5657ba53c9d3aefd859f902a2283aec58a2e91eb 100644 (file)
@@ -12,14 +12,21 @@ package org.splat.service;
 import java.io.IOException;
 import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.Date;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.store.FSDirectory;
+import org.hibernate.Criteria;
+import org.hibernate.criterion.DetachedCriteria;
+import org.hibernate.criterion.Order;
 import org.hibernate.criterion.Restrictions;
 import org.splat.dal.bo.kernel.Relation;
 import org.splat.dal.bo.kernel.User;
@@ -31,6 +38,7 @@ import org.splat.dal.bo.som.IDBuilder;
 import org.splat.dal.bo.som.KnowledgeElement;
 import org.splat.dal.bo.som.ProgressState;
 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;
@@ -40,18 +48,28 @@ import org.splat.dal.bo.som.ValidationStep;
 import org.splat.dal.bo.som.Visibility;
 import org.splat.dal.bo.som.Study.Properties;
 import org.splat.dal.bo.som.ValidationCycle.Actor;
+import org.splat.dal.dao.som.DescriptionAttributeDAO;
+import org.splat.dal.dao.som.DocumentDAO;
+import org.splat.dal.dao.som.DocumentTypeDAO;
 import org.splat.dal.dao.som.IDBuilderDAO;
+import org.splat.dal.dao.som.PublicationDAO;
 import org.splat.dal.dao.som.ScenarioDAO;
 import org.splat.dal.dao.som.StudyDAO;
+import org.splat.dal.dao.som.UsedByRelationDAO;
 import org.splat.dal.dao.som.ValidationCycleDAO;
+import org.splat.exception.BusinessException;
+import org.splat.exception.InvalidParameterException;
 import org.splat.kernel.InvalidPropertyException;
 import org.splat.kernel.MissedPropertyException;
 import org.splat.kernel.MultiplyDefinedException;
 import org.splat.log.AppLogger;
+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.som.Revision;
+import org.splat.util.BeanHelper;
 import org.springframework.transaction.annotation.Transactional;
 
 /**
@@ -67,6 +85,10 @@ public class StudyServiceImpl implements StudyService {
         */
        public final static AppLogger LOG = AppLogger
                        .getLogger(StudyServiceImpl.class);
+       /**
+        * "studyId" parameter name.
+        */
+       public final static String PARAM_STUDY_ID = "studyId";
 
        /**
         * Injected index service.
@@ -93,6 +115,11 @@ public class StudyServiceImpl implements StudyService {
         */
        private StudyDAO _studyDAO;
 
+       /**
+        * Injected usedBy relations DAO.
+        */
+       private UsedByRelationDAO _usedByRelationDAO;
+
        /**
         * Injected scenario DAO.
         */
@@ -118,6 +145,30 @@ public class StudyServiceImpl implements StudyService {
         */
        private UserService _userService;
 
+       /**
+        * Injected publication DAO.
+        */
+       private PublicationDAO _publicationDAO;
+
+       /**
+        * Injected repository service.
+        */
+       private RepositoryService _repositoryService;
+
+       /**
+        * Injected document DAO.
+        */
+       private DocumentDAO _documentDAO;
+
+       /**
+        * Injected description attribute DAO.
+        */
+       private DescriptionAttributeDAO _descriptionAttributeDAO;
+       /**
+        * Injected document type DAO.
+        */
+       private DocumentTypeDAO _documentTypeDAO;
+
        /**
         * {@inheritDoc}
         * 
@@ -126,10 +177,68 @@ public class StudyServiceImpl implements StudyService {
        @Transactional
        public Study selectStudy(final long index) {
                Study result = getStudyDAO().get(index);
-               loadWorkflow(result);
+               if (result != null) {
+                       loadWorkflow(result);
+               }
                return result;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.StudyService#removeStudy(long)
+        */
+       @Transactional
+       public void removeStudy(final Long index) {
+               Study study = getStudyDAO().get(index);
+               Set<org.splat.dal.bo.som.Document> docums = new HashSet<org.splat.dal.bo.som.Document>();
+               if (study != null) {
+                       // Select all documents published in the study and study's scenarios.
+                       DetachedCriteria query = DetachedCriteria.forClass(
+                                       Publication.class, "pub");
+                       query
+                                       .createCriteria("pub.owner", "projelem",
+                                                       Criteria.INNER_JOIN)
+                                       .createAlias("projelem.owner", "study", Criteria.LEFT_JOIN)
+                                       .add(
+                                                       Restrictions
+                                                                       .or(Restrictions.eq("projelem.rid", index),
+                                                                                       Restrictions.eq("study.rid", index)))
+                                       .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
+
+                       if (LOG.isDebugEnabled()) {
+                               LOG.debug("Find study documents: " + query.toString());
+                       }
+                       for (Publication pub : getPublicationDAO().getFilteredList(query)) {
+                               docums.add(pub.value());
+                               // Find also all previous versions of the document
+                               for (org.splat.dal.bo.som.Document prev = pub.value()
+                                               .getPreviousVersion(); prev != null; prev = prev
+                                               .getPreviousVersion()) {
+                                       docums.add(prev);
+                               }
+                       }
+
+                       // Delete the study with its relations, scenarios and publications
+                       getStudyDAO().delete(study);
+
+                       // Remove all relations of study documents
+                       for (org.splat.dal.bo.som.Document doc : docums) {
+                               LOG.debug("Found doc: " + doc.getTitle() + " ["
+                                               + doc.getReference() + "]" + " [" + doc.getRid() + "]");
+                               doc.getAllRelations().clear();
+                       }
+                       getDocumentDAO().flush();
+
+                       // Remove all documents of the study
+                       for (org.splat.dal.bo.som.Document doc : docums) {
+                               LOG.debug("Remove doc: " + doc.getTitle() + " ["
+                                               + doc.getReference() + "]" + " [" + doc.getRid() + "]");
+                               getDocumentDAO().delete(doc);
+                       }
+               }
+       }
+
        /**
         * Get study by its reference.
         * 
@@ -159,14 +268,14 @@ public class StudyServiceImpl implements StudyService {
 
                buildReference(study);
                getStudyDAO().create(study);
-               try {
-                       IndexService lucin = getIndex();
-                       lucin.add(study);
-               } catch (IOException error) {
-                       LOG.error("Unable to index the study '" + study.getIndex()
-                                       + "', reason:", error);
-                       // Continue and try to index later
-               }
+               // try {
+               // IndexService lucin = getIndex();
+               // lucin.add(study);
+               // } catch (IOException error) {
+               // LOG.error("Unable to index the study '" + study.getIndex()
+               // + "', reason:", error);
+               // // Continue and try to index later
+               // }
                return study;
        }
 
@@ -205,55 +314,42 @@ public class StudyServiceImpl implements StudyService {
         * 
         * @see org.splat.service.StudyService#addContributor(org.splat.dal.bo.som.Study, org.splat.dal.bo.kernel.User)
         */
+       @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));
                if (absent) {
                        update(aStudy); // Else, useless to re-index the study
                }
-               contributor.add(user);
                return true;
        }
 
-       /**
-        * Moves this study from the Public to the Reference area of the repository. For being moved to the Reference area, the study must
-        * previously be approved.
-        * 
-        * @param aStudy
-        *            the study to move
-        * @return true if the move succeeded.
-        * @see #moveToPublic()
-        * @see #isPublic()
-        * @see Publication#approve(Date)
-        */
-       public boolean moveToReference(final Study aStudy) {
-               if (aStudy.getProgressState() != ProgressState.APPROVED) {
-                       return false;
-               }
-               if (aStudy.getVisibility() != Visibility.PUBLIC) {
-                       return false;
-               }
-
-               aStudy.setVisibility(Visibility.REFERENCE);
-               if (update(aStudy)) {
-                       return updateKnowledgeElementsIndex(aStudy); // If fails, the database roll-back is under responsibility of the caller
-               }
-               return false;
-       }
-
        /**
         * {@inheritDoc}
         * 
         * @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) {
@@ -276,38 +372,24 @@ public class StudyServiceImpl implements StudyService {
         *            the document
         * @return true if the document is published in the study
         */
-/*     private boolean publishes(final Study aStudy, final Document doc) {
-               if (!aStudy.publishes(doc)) {
-                       Scenario[] scene = aStudy.getScenarii();
-                       for (int i = 0; i < scene.length; i++) {
-                               if (scene[i].publishes(doc)) {
-                                       return true;
-                               }
-                       }
-               }
-               return false;
-       }
-*/
+       /*
+        * private boolean publishes(final Study aStudy, final Document doc) { if (!aStudy.publishes(doc)) { Scenario[] scene =
+        * aStudy.getScenarii(); for (int i = 0; i < scene.length; i++) { if (scene[i].publishes(doc)) { return true; } } } return false; }
+        */
        /**
         * {@inheritDoc}
         * 
         * @see org.splat.service.StudyService#removeContributor(org.splat.dal.bo.som.Study, org.splat.dal.bo.kernel.User[])
         */
+       @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) {
@@ -321,6 +403,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(
@@ -336,15 +419,16 @@ public class StudyServiceImpl implements StudyService {
         *      org.splat.dal.bo.som.ValidationCycle.Properties)
         */
        @Transactional
-       public void setValidationCycle(final Study aStudyDTO, final DocumentType type,
-                       final ValidationCycle.Properties vprop) {
-               Map<String, ValidationCycle> validactor = aStudyDTO.getValidationCycles();
+       public void setValidationCycle(final Study aStudyDTO,
+                       final DocumentType type, final ValidationCycle.Properties vprop) {
+               Map<String, ValidationCycle> validactor = aStudyDTO
+                               .getValidationCycles();
                if (validactor == null) {
                        setShortCuts(aStudyDTO); // Initializes validactor and actor
                }
 
                Study aStudy = selectStudy(aStudyDTO.getIndex());
-               
+
                String cname = type.getName();
                ValidationCycle cycle = validactor.get(cname);
 
@@ -358,12 +442,12 @@ 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,
-                       } catch (Exception error) {
-                               LOG.error("Unable to re-index Knowledge Elements, reason:",
-                                               error);
+                       } catch (BusinessException error) {
+                               LOG.error("Unable to create validation cycle, reason:", error);
                                return;
                        }
                }
@@ -371,25 +455,6 @@ public class StudyServiceImpl implements StudyService {
                update(aStudy); // Re-index the study, just in case
        }
 
-       /**
-        * Demotes this study from In-Check to In-Draft then In-Work states. This function is called internally when demoting the final result
-        * document of the study.
-        * 
-        * @param aStudy
-        *            a study to demote
-        * @return true if the demotion succeeded.
-        */
-       public boolean demote(final Study aStudy) {
-               if (aStudy.getProgressState() == ProgressState.inCHECK) {
-                       aStudy.setProgressState(ProgressState.inDRAFT);
-               } else if (aStudy.getProgressState() == ProgressState.inDRAFT) {
-                       aStudy.setProgressState(ProgressState.inWORK);
-               } else {
-                       return false;
-               }
-               return update(aStudy);
-       }
-
        /**
         * {@inheritDoc}
         * 
@@ -402,31 +467,68 @@ public class StudyServiceImpl implements StudyService {
        }
 
        /**
-        * Promotes this study from In-Work to In-Draft then In-Check and APPROVED states. This function is called internally when promoting the
-        * final result document of the study.
+        * Promotes this study from In-Work to In-Draft then In-Check and APPROVED <BR>
+        * states. This function takes into account statuses of final result<BR>
+        * documents of the study.
         * 
         * @param aStudy
         *            a study to promote
-        * @return true if the demotion succeeded.
+        * @return true if the promotion succeeded.
         */
        @Transactional
        public boolean promote(final Study aStudy) {
-               if (aStudy.getProgressState() == ProgressState.inWORK) {
+               boolean res = true;
+               ValidationCycle cycle = getValidationCycleOf(aStudy,
+                               getStudyResultType(aStudy));
+
+               if (aStudy.getProgressState() == ProgressState.inWORK
+                               && cycle.enables(ValidationStep.REVIEW)
+                               && canBePromoted(aStudy)) {
                        aStudy.setProgressState(ProgressState.inDRAFT);
-               } else if (aStudy.getProgressState() == ProgressState.inDRAFT) {
+               } else if (((!cycle.enables(ValidationStep.REVIEW) && ((aStudy
+                               .getProgressState() == ProgressState.inWORK) || (aStudy
+                               .getProgressState() == ProgressState.inDRAFT))) || (cycle
+                               .enables(ValidationStep.REVIEW) && (aStudy.getProgressState() == ProgressState.inDRAFT)))
+                               && canBeReviewed(aStudy)) {
                        aStudy.setProgressState(ProgressState.inCHECK);
                        Revision myvers = new Revision(aStudy.getVersion());
                        if (myvers.isMinor()) {
                                aStudy.setVersion(myvers.incrementAs(aStudy.getProgressState())
                                                .toString());
                        }
-               } else if (aStudy.getProgressState() == ProgressState.inCHECK) {
+               } else if (aStudy.getProgressState() == ProgressState.inCHECK
+                               && canBeApproved(aStudy)) {
                        aStudy.setProgressState(ProgressState.APPROVED);
+                       updateKnowledgeElementsState(aStudy);
                } else {
-                       return false;
+                       res = false;
+               }
+               if (res) {
+                       res = update(aStudy);
                }
 
-               return update(aStudy);
+               return res;
+       }
+
+       /**
+        * Demotes this study from In-Check or In-Draft to In-Work states.
+        * 
+        * @param aStudy
+        *            a study to demote
+        * @return true if the demotion succeeded.
+        */
+       @Transactional
+       public boolean demote(final Study aStudy) {
+               boolean res;
+
+               if (aStudy.getProgressState() == ProgressState.inCHECK
+                               || aStudy.getProgressState() == ProgressState.inDRAFT) {
+                       aStudy.setProgressState(ProgressState.inWORK);
+                       res = update(aStudy);
+               } else {
+                       res = false;
+               }
+               return res;
        }
 
        /**
@@ -443,12 +545,12 @@ public class StudyServiceImpl implements StudyService {
                if (aStudy.getVisibility() == Visibility.PRIVATE) {
                        aStudy.setVisibility(Visibility.PUBLIC);
                        if (update(aStudy)) {
-                               isOk = updateKnowledgeElementsIndex(aStudy); // If fails, the database roll-back is under responsibility of the caller
+                               isOk = updateKnowledgeElementsState(aStudy); // If fails, the database roll-back is under responsibility of the caller
                        }
                }
                return isOk;
        }
-       
+
        /**
         * Moves this study from the Public to the Private area of the repository.
         * 
@@ -462,7 +564,7 @@ public class StudyServiceImpl implements StudyService {
                if (aStudy.getVisibility() == Visibility.PUBLIC) {
                        aStudy.setVisibility(Visibility.PRIVATE);
                        if (update(aStudy)) {
-                               isOk = updateKnowledgeElementsIndex(aStudy); // If fails, the database roll-back is under responsibility of the caller
+                               isOk = updateKnowledgeElementsState(aStudy); // If fails, the database roll-back is under responsibility of the caller
                        }
                }
                return isOk;
@@ -475,13 +577,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
-                       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());
@@ -541,7 +642,7 @@ public class StudyServiceImpl implements StudyService {
                                                        break;
                                                }
                                        }
-                                       SimpleDateFormat tostring = new SimpleDateFormat("yyyy"); //RKV: NOPMD: TODO: Use locale here?
+                                       SimpleDateFormat tostring = new SimpleDateFormat("yyyy"); // RKV: NOPMD: TODO: Use locale here?
                                        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++) {
@@ -622,32 +723,19 @@ public class StudyServiceImpl implements StudyService {
        }
 
        /**
-        * Update lucene index for the study knowledge elements.
+        * Update knowledge elements states.
         * 
         * @param aStudy
         *            the study
-        * @return true if reindexing succeeded
+        * @return true if 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);
-                               }
+       private boolean updateKnowledgeElementsState(final Study aStudy) {
+               for (Scenario scenario : aStudy.getScenariiList()) {
+                       for (KnowledgeElement element : scenario.getAllKnowledgeElements()) {
+                               element.setProgressState(aStudy.getProgressState());
                        }
-                       isOk = true;
-               } catch (Exception error) {
-                       LOG.error("Unable to re-index Knowledge Elements, reason:",
-                                       error);
                }
-               return isOk;
+               return true;
        }
 
        /**
@@ -659,6 +747,11 @@ public class StudyServiceImpl implements StudyService {
         */
        private IndexService getIndex() throws IOException {
                IndexService lucin = getIndexService();
+               if (IndexWriter.isLocked(FSDirectory.open(getRepositoryService()
+                               .getRepositoryIndexDirectory()))) {
+                       IndexWriter.unlock(FSDirectory.open(getRepositoryService()
+                                       .getRepositoryIndexDirectory()));
+               }
                if (!lucin.exists()) {
                        lucin.create(); // Happens when re-indexing all studies
                }
@@ -863,13 +956,18 @@ public class StudyServiceImpl implements StudyService {
         */
        public ValidationCycle getValidationCycleOf(final Study aStudy,
                        final DocumentType type) {
-               if (aStudy.getValidationCycles() == null || aStudy.getValidationCycles().isEmpty()) {
-                       setShortCuts(aStudy);
+               ValidationCycle result = null;
+               if (aStudy != null) {
+                       if (aStudy.getValidationCycles() == null
+                                       || aStudy.getValidationCycles().isEmpty()) {
+                               setShortCuts(aStudy);
+                       }
+                       if (type != null) {
+                               result = aStudy.getValidationCycles().get(type.getName());
+                       }
                }
-               ValidationCycle result = aStudy.getValidationCycles().get(
-                               type.getName());
-               if (result == null) {
-                       if (type.isStepResult()) {
+               if ((result == null) && (aStudy != null)) {
+                       if ((type == null) || type.isStepResult()) {
                                result = aStudy.getValidationCycles().get("default"); // "default" validation cycle defined in the configuration, if exist
                        }
                        if (result == null) {
@@ -999,35 +1097,173 @@ public class StudyServiceImpl implements StudyService {
                        aStudy.getActor().add(((ActorRelation) link).getTo());
                }
        }
-       
+
        /**
         * 
         * {@inheritDoc}
+        * 
         * @see org.splat.service.StudyService#markStudyAsReference(org.splat.dal.bo.som.Study)
         */
-       @Override
        @Transactional
-       public void markStudyAsReference (final Study aStudy) {
-               
-               aStudy.setMarkreference(1);
-               aStudy.setProgressState(ProgressState.TEMPLATE);
-               getStudyDAO().merge(aStudy);
+       public boolean markStudyAsReference(final Study aStudy) {
+               boolean res = false;
+               if (aStudy.getProgressState() == ProgressState.APPROVED) {
+                       aStudy.setMarkreference(1);
+                       aStudy.setProgressState(ProgressState.TEMPLATE);
+                       res = updateKnowledgeElementsState(aStudy);
+                       getStudyDAO().merge(aStudy);
+               }
+               return res;
        }
-       
+
        /**
         * 
         * {@inheritDoc}
+        * 
         * @see org.splat.service.StudyService#removeStudyAsReference(org.splat.dal.bo.som.Study)
         */
-       @Override
        @Transactional
        public void removeStudyAsReference(final Study aStudy) {
-               
                aStudy.setMarkreference(0);
                aStudy.setProgressState(ProgressState.APPROVED);
+               updateKnowledgeElementsState(aStudy);
                getStudyDAO().merge(aStudy);
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.StudyService#getDescription(java.lang.Long)
+        */
+       @Override
+       @Transactional(readOnly = true)
+       public String getDescription(final Long studyId)
+                       throws InvalidParameterException {
+               if (studyId == null) {
+                       throw new InvalidParameterException(PARAM_STUDY_ID, "null");
+               }
+               Study study = _studyDAO.get(studyId);
+               if (study == null) {
+                       throw new InvalidParameterException(PARAM_STUDY_ID, studyId
+                                       .toString());
+               }
+               return study.getDescription();
+       }
+
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.StudyService#setDescription(java.lang.Long, java.lang.String)
+        */
+       @Transactional
+       public void setDescription(final Long studyId, final String descriptionText)
+                       throws InvalidParameterException {
+               if (studyId == null) {
+                       throw new InvalidParameterException(PARAM_STUDY_ID, "null");
+               }
+               Study study = _studyDAO.get(studyId);
+               if (study == null) {
+                       throw new InvalidParameterException(PARAM_STUDY_ID, studyId
+                                       .toString());
+               }
+               study.setAttribute(new DescriptionAttribute(study, descriptionText));
+       }
+
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.StudyService#removeStudyDescription(java.lang.Long)
+        */
+       @Transactional
+       public boolean removeDescription(final Long studyId)
+                       throws InvalidParameterException {
+               if (studyId == null) {
+                       throw new InvalidParameterException(PARAM_STUDY_ID, String
+                                       .valueOf(studyId));
+               }
+               Study study = _studyDAO.get(studyId);
+               if (study == null) {
+                       throw new InvalidParameterException(PARAM_STUDY_ID, String
+                                       .valueOf(studyId));
+               }
+               return study.removeAttribute(study
+                               .getAttribute(DescriptionAttribute.class));
+       }
+
+       /**
+        * {@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.
         * 
@@ -1221,4 +1457,194 @@ public class StudyServiceImpl implements StudyService {
        public void setUserService(final UserService userService) {
                _userService = userService;
        }
+
+       /**
+        * Get the publicationDAO.
+        * 
+        * @return the publicationDAO
+        */
+       public PublicationDAO getPublicationDAO() {
+               return _publicationDAO;
+       }
+
+       /**
+        * Set the publicationDAO.
+        * 
+        * @param publicationDAO
+        *            the publicationDAO to set
+        */
+       public void setPublicationDAO(final PublicationDAO publicationDAO) {
+               _publicationDAO = publicationDAO;
+       }
+
+       /**
+        * Get the repositoryService.
+        * 
+        * @return the repositoryService
+        */
+       public RepositoryService getRepositoryService() {
+               return _repositoryService;
+       }
+
+       /**
+        * Set the repositoryService.
+        * 
+        * @param repositoryService
+        *            the repositoryService to set
+        */
+       public void setRepositoryService(final RepositoryService repositoryService) {
+               _repositoryService = repositoryService;
+       }
+
+       /**
+        * Get the documentDAO.
+        * 
+        * @return the documentDAO
+        */
+       public DocumentDAO getDocumentDAO() {
+               return _documentDAO;
+       }
+
+       /**
+        * Set the documentDAO.
+        * 
+        * @param documentDAO
+        *            the documentDAO to set
+        */
+       public void setDocumentDAO(final DocumentDAO documentDAO) {
+               _documentDAO = documentDAO;
+       }
+
+       /**
+        * Get the descriptionAttributeDAO.
+        * 
+        * @return the descriptionAttributeDAO
+        */
+       public DescriptionAttributeDAO getDescriptionAttributeDAO() {
+               return _descriptionAttributeDAO;
+       }
+
+       /**
+        * Set the descriptionAttributeDAO.
+        * 
+        * @param descriptionAttributeDAO
+        *            the descriptionAttributeDAO to set
+        */
+       public void setDescriptionAttributeDAO(
+                       final DescriptionAttributeDAO descriptionAttributeDAO) {
+               _descriptionAttributeDAO = descriptionAttributeDAO;
+       }
+
+       /**
+        * Get the usedByRelationDAO.
+        * 
+        * @return the usedByRelationDAO
+        */
+       public UsedByRelationDAO getUsedByRelationDAO() {
+               return _usedByRelationDAO;
+       }
+
+       /**
+        * Set the usedByRelationDAO.
+        * 
+        * @param usedByRelationDAO
+        *            the usedByRelationDAO to set
+        */
+       public void setUsedByRelationDAO(final UsedByRelationDAO usedByRelationDAO) {
+               _usedByRelationDAO = usedByRelationDAO;
+       }
+
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.StudyService#getStudyResultType(org.splat.dal.bo.som.Study)
+        */
+       @Override
+       @Transactional(readOnly = true)
+       public DocumentType getStudyResultType(final Study study) {
+               DetachedCriteria query = DetachedCriteria.forClass(DocumentType.class)
+                               .addOrder(Order.desc("result"));
+               return getDocumentTypeDAO().getFirstResult(query);
+       }
+
+       /**
+        * Get the documentTypeDAO.
+        * 
+        * @return the documentTypeDAO
+        */
+       public DocumentTypeDAO getDocumentTypeDAO() {
+               return _documentTypeDAO;
+       }
+
+       /**
+        * Set the documentTypeDAO.
+        * 
+        * @param documentTypeDAO
+        *            the documentTypeDAO to set
+        */
+       public void setDocumentTypeDAO(final DocumentTypeDAO documentTypeDAO) {
+               _documentTypeDAO = documentTypeDAO;
+       }
+
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.StudyService#canBeApproved(org.splat.dal.bo.som.Study)
+        */
+       @Override
+       @Transactional(readOnly = true)
+       public boolean canBeApproved(final Study study) {
+               return resultDocsAtLeast(study, ProgressState.APPROVED);
+       }
+
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.StudyService#canBePromoted(org.splat.dal.bo.som.Study)
+        */
+       @Override
+       @Transactional(readOnly = true)
+       public boolean canBePromoted(final Study study) {
+               boolean res;
+               if (study.getProgressState().compareTo(ProgressState.inDRAFT) < 0) {
+                       res = resultDocsAtLeast(study, ProgressState.inDRAFT);
+               } else {
+                       res = canBeReviewed(study);
+               }
+               return res;
+       }
+
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.StudyService#canBeReviewed(org.splat.dal.bo.som.Study)
+        */
+       @Override
+       @Transactional(readOnly = true)
+       public boolean canBeReviewed(final Study study) {
+               return resultDocsAtLeast(study, ProgressState.inCHECK);
+       }
+
+       /**
+        * Check that all result documents of the study are at least in the given state.
+        * 
+        * @param study
+        *            the study to check
+        * @param state
+        *            the minimal acceptable state
+        * @return true if study result documents have acceptable states
+        */
+       private boolean resultDocsAtLeast(final Study study,
+                       final ProgressState state) {
+               boolean res = true;
+               // Check that all study result documents have the state APPROVED or more.
+               for (Publication pub : getProjectElementService().getLastStep(study)
+                               .getResultDocuments()) {
+                       res = pub.getProgressState().compareTo(state) >= 0;
+                       if (!res) {
+                               break;
+                       }
+               }
+               return res;
+       }
 }