+ * @param aStep
+ * the study step
+ * @param docId
+ * the document id
+ * @return true if removing of the document succeeded
+ * @throws DocumentIsUsedException
+ * if the document is used by other documents
+ */
+ @Override
+ @Transactional
+ public boolean removeDocument(final Step aStep, final long docId)
+ throws DocumentIsUsedException {
+ Publication torem = aStep.getDocument(docId);
+ boolean res = (torem != null);
+ if (res) {
+ if (!torem.value().getRelations(UsedByRelation.class).isEmpty()) {
+ throw new DocumentIsUsedException(torem.value().getTitle());
+ }
+ remove(aStep, torem);
+ Document value = torem.value();
+ if (!value.isPublished() && !value.isVersioned()) { // The referenced document is no more used
+ List<Document> using = new ArrayList<Document>();
+ List<File> files = new ArrayList<File>();
+ for (Relation link : value.getAllRelations()) {
+ if (link.getClass().equals(ConvertsRelation.class)) { // File conversion
+ files.add((File) link.getTo());
+ } else if (link.getClass().equals(UsesRelation.class)) { // Document dependency
+ using.add((Document) link.getTo());
+ }
+ }
+ // Remove relations from depending documents
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Remove " + using.size() + " UsedByRelation(s).");
+ }
+ for (Document doc : using) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Remove UsedByRelation from "
+ + doc.getTitle() + " to " + value.getTitle());
+ LOG.debug("Nb relations of doc " + doc.getTitle()
+ + " before: " + doc.getAllRelations().size());
+ }
+ doc.removeRelation(UsedByRelation.class, value);
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Nb relations of doc " + doc.getTitle()
+ + " after: " + doc.getAllRelations().size());
+ }
+ getDocumentDAO().merge(doc);
+ }
+ // Synchronize deleted objects with the database to avoid hibernate exception
+ // org.hibernate.PropertyValueException: not-null property references a null or transient value
+ getDocumentDAO().flush();
+ // The corresponding physical file is not removed from the vault
+ getDocumentDAO().delete(getDocumentDAO().merge(torem.value()));
+ // Delete document's files
+ for (File file : files) {
+ getFileDAO().delete(getFileDAO().merge(file)); // The corresponding physical file is not removed from the vault
+ }
+ }
+ }
+ return res;
+
+
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.splat.service.StepService#addComment(org.splat.som.Step, org.splat.dal.bo.som.CommentAttribute)
+ */
+ @Override
+ @Transactional
+ public void addStepComment(final StepCommentDTO comment) throws InvalidParameterException {
+
+ if(comment.getId()!= null) {
+ throw new InvalidParameterException("id", String.valueOf(comment.getId()));
+ }
+ User user = getUserDAO().get(comment.getUserId());
+ if (user == null) {
+ throw new InvalidParameterException("userId", String.valueOf(comment.getUserId()));
+ }
+ ProjectElement projectElement = getProjectElementDAO().get(comment.getProjectElementId());
+ if (projectElement==null) {
+ throw new InvalidParameterException("projectElementId", comment.getProjectElementId().toString());
+ }
+ if(comment.getStep() == null || comment.getStep()<0) {
+ throw new InvalidParameterException("step", String.valueOf(comment.getStep()));
+ }
+ if(comment.getDate() == null) {
+ throw new InvalidParameterException("date", String.valueOf(comment.getDate()));
+ }
+ if(comment.getTitle() == null) {
+ throw new InvalidParameterException("title", String.valueOf(comment.getTitle()));
+ }
+
+ StepCommentAttribute newComment = new StepCommentAttribute(
+ projectElement,
+ comment.getText(),
+ comment.getDate(),
+ comment.getStep(),
+ user,
+ comment.getTitle()
+ );
+
+ Long resultKey=getStepCommentAttributeDAO().create(newComment);
+ comment.setId(resultKey);
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.splat.service.StepService#getStepComments(org.splat.som.Step)
+ */
+ @Override
+ @Transactional(readOnly = true)
+ public List<StepCommentDTO> getStepComments(final Step step) throws InvalidParameterException {
+ ProjectElement owner = _projectElementDAO.get(step.getOwner().getRid());
+ if(owner == null) {
+ throw new InvalidParameterException("step owner id",
+ Long.valueOf(step.getOwner().getRid()).toString());
+ }
+ List<StepCommentAttribute> comments = _stepCommentAttributeDAO.getFilteredList(
+ Restrictions.and(
+ Restrictions.eq("step", Integer.valueOf(step.getNumber())),
+ Restrictions.eq("owner", owner)));
+ List<StepCommentDTO> commentDTOs = new ArrayList<StepCommentDTO>();
+ for(StepCommentAttribute comment : comments) {
+ StepCommentDTO stepCommentDTO = BeanHelper.copyBean(comment, StepCommentDTO.class);
+ stepCommentDTO.setText(comment.getValue());
+ stepCommentDTO.setId(Long.valueOf(comment.getRid()));
+ commentDTOs.add(stepCommentDTO);
+ }
+ return commentDTOs;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.splat.service.StepService#removeStepComment(long)
+ */
+ @Override
+ @Transactional
+ public void removeStepComment(final long commentId) throws InvalidParameterException {
+ StepCommentAttribute stepComment = _stepCommentAttributeDAO.get(Long.valueOf(commentId));
+ if(stepComment == null) {
+ throw new InvalidParameterException("commentId",String.valueOf(commentId));
+ }
+ _stepCommentAttributeDAO.delete(stepComment);
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.splat.service.StepService#editStepComment(long, java.lang.String, java.lang.String)
+ */
+ @Override
+ @Transactional
+ public void editStepComment(final long commentId, final String newValue, final String newTitle)
+ throws InvalidParameterException {
+ StepCommentAttribute comment = _stepCommentAttributeDAO.get(Long.valueOf(commentId));
+ if(comment == null) {
+ throw new InvalidParameterException("commentId",String.valueOf(commentId));
+ }
+ if(newTitle != null) {
+ comment.setTitle(newTitle);
+ }
+ if(newValue != null) {
+ comment.setValue(newValue);
+ }
+ _stepCommentAttributeDAO.update(comment);
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.splat.service.StepService#isCommentMadeByUser(long, long)
+ */
+ @Override
+ @Transactional(readOnly = true)
+ public boolean isCommentMadeByUser(final long commentId, final long userId)
+ throws InvalidParameterException {
+ StepCommentAttribute comment = _stepCommentAttributeDAO.get(Long.valueOf(commentId));
+ if(comment == null) {
+ throw new InvalidParameterException("commentId", String.valueOf(commentId));
+ }
+ return comment.getUser().getIndex() == userId;
+ }
+
+ /**
+ * Get the documentService.
+ *