+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.splat.service.StudyService#getComparableStudies()
+ */
+ @Transactional(readOnly = true)
+ public List<StudyFacadeDTO> getComparableStudies(final long userId)
+ throws MismatchException {
+ // retrieve the number of the "Analyze the results" step
+ List<Step> allSteps = _projectSettings.getAllSteps();
+ Step theAnalyzeStep = null;
+ for (Step step : allSteps) {
+ if (step.getKey().equals("postprocessing")) {
+ theAnalyzeStep = step;
+ }
+ }
+ if (theAnalyzeStep == null) { // TODO: throw some other exception
+ throw new MismatchException(
+ "no step with key 'postprocessing' found."
+ + "Probably, customization settings have been changed.");
+ }
+
+ List<Publication> publications = _publicationDAO.getFilteredList(
+ "mydoc", Restrictions.eq("step", Integer.valueOf(theAnalyzeStep
+ .getNumber())));
+
+ // split retrieved publications into groups by studies and scenarios
+ Map<Study, List<ProjectElement>> studyMap = new HashMap<Study, List<ProjectElement>>();
+ Map<ProjectElement, List<Publication>> scenarioMap = new HashMap<ProjectElement, List<Publication>>();
+
+ for (Publication publication : publications) {
+ // filter out publications corresponding to a document of given step which is not a _result_ document
+ if (!publication.value().getType().isResultOf(theAnalyzeStep)
+ || !"srd".equals(publication.getSourceFile().getFormat())) {
+ continue;
+ }
+
+ // check the study visibility to the user
+ if (!isStaffedBy(publication.getOwnerStudy(), _userService
+ .selectUser(userId))
+ && Visibility.PUBLIC.equals(publication.getOwnerStudy()
+ .getVisibility())) {
+ continue;
+ }
+
+ Study study = publication.getOwnerStudy();
+ ProjectElement scenario = publication.getOwner();
+
+ Hibernate.initialize(scenario);
+ if (scenario instanceof HibernateProxy) {
+ scenario = (ProjectElement) ((HibernateProxy) scenario)
+ .getHibernateLazyInitializer().getImplementation();
+ }
+
+ if (!(scenario instanceof Scenario)) {
+ throw new MismatchException(
+ "publications from postprocessing step are supposed to have owner scenario");
+ }
+
+ if (!studyMap.containsKey(study)) {
+ studyMap.put(study, new ArrayList<ProjectElement>());
+ }
+
+ if (!studyMap.get(study).contains(scenario)) {
+ studyMap.get(study).add(scenario);
+ }
+
+ if (!scenarioMap.containsKey(scenario)) {
+ scenarioMap.put(scenario, new ArrayList<Publication>());
+ }
+ scenarioMap.get(scenario).add(publication);
+ }
+
+ // Create the result DTOs
+ List<StudyFacadeDTO> result = new ArrayList<StudyFacadeDTO>();
+ for (Study study : studyMap.keySet()) {
+
+ StudyFacadeDTO studyDTO = new StudyFacadeDTO();
+ studyDTO.setName(study.getTitle());
+ studyDTO.setScenarios(new ArrayList<StudyFacadeDTO.ScenarioDTO>());
+ result.add(studyDTO);
+
+ for (ProjectElement scenario : studyMap.get(study)) {
+
+ StudyFacadeDTO.ScenarioDTO scenarioDTO = new StudyFacadeDTO.ScenarioDTO();
+ scenarioDTO.setName(scenario.getTitle());
+ scenarioDTO.setDocs(new ArrayList<DocumentDTO>());
+ studyDTO.getScenarios().add(scenarioDTO);
+
+ for (Publication publication : scenarioMap.get(scenario)) {
+
+ DocumentDTO documentDTO = new DocumentDTO();
+ documentDTO.setId(publication.getIndex());
+ documentDTO.setTitle(publication.value().getTitle());
+
+ scenarioDTO.getDocs().add(documentDTO);
+ }
+ }
+ }
+ return result;
+ }
+
+ /**
+ * 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) {
+ return resultDocsAtLeast(study, ProgressState.inDRAFT);
+ }
+
+ /**
+ * {@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(ProgressState.APPROVED) >= 0;
+ if (!res) {
+ break;
+ }
+ }
+ return res;
+ }