@Transactional
 @Override
 public ResponseDTO createResponse(ResponseDTO response) {
   JpaResponse jpaResponse = surveyMapper.toJpaResponse(response);
   jpaSurveyDao.createResponse(jpaResponse);
   return surveyMapper.toResponse(jpaResponse);
 }
 /**
  * Create a {@link JpaSurvey} from the data in survey
  *
  * @param survey
  * @return
  */
 @Transactional
 @Override
 public SurveyDTO createSurvey(SurveyDTO survey) {
   // remove questions/answers if they are present - only create the survey
   JpaSurvey jpaSurvey = surveyMapper.toJpaSurvey(survey);
   jpaSurvey.setLastUpdateDate(new Timestamp(new Date().getTime()));
   jpaSurvey = jpaSurveyDao.createSurvey(jpaSurvey);
   return surveyMapper.toSurvey(jpaSurvey);
 }
  /**
   * Create a {@link JpaQuestion} from the data in question
   *
   * @param question
   * @return
   */
  @Transactional
  @Override
  public QuestionDTO createQuestion(QuestionDTO question) {
    JpaQuestion jpaQuestion = surveyMapper.toJpaQuestion(question);
    jpaQuestion = jpaSurveyDao.createQuestion(jpaQuestion);

    QuestionDTO newQuestion = surveyMapper.toQuestion(jpaQuestion);

    return newQuestion;
  }
  /**
   * Update question details including embedded answer data
   *
   * @param question
   * @return {@link QuestionDTO} or null on error
   */
  @Transactional
  @Override
  public QuestionDTO updateQuestion(QuestionDTO question) {
    JpaQuestion existingQuestion = jpaSurveyDao.getQuestion(question.getId());
    if (existingQuestion.getStatus() == PublishedState.PUBLISHED) {
      log.warn("Cannot update question in PUBLISHED state");
      return null;
    }

    JpaQuestion jpaQuestion = surveyMapper.toJpaQuestion(question);
    jpaSurveyDao.updateQuestion(jpaQuestion);

    return surveyMapper.toQuestion(jpaQuestion);
  }
 @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
 @Override
 public ResponseDTO getResponse(long id) {
   JpaResponse jpaResponse = jpaSurveyDao.getResponse(id);
   log.debug(jpaResponse.toString());
   return jpaResponse == null ? null : surveyMapper.toResponse(jpaResponse);
 }
  @Transactional
  @Override
  public ResponseDTO updateResponse(ResponseDTO response) {
    JpaResponse existingResponse = jpaSurveyDao.getResponse(response.getId());
    if (existingResponse == null) {
      log.warn("Cannot update response - does not exist", response.toString());
      return null;
    }

    JpaResponse jpaResponse = surveyMapper.toJpaResponse(response);
    log.debug("existing response: " + existingResponse.toString());
    log.debug("source DTO response: " + response.toString());
    log.debug("mapped response: " + jpaResponse.toString());
    jpaResponse = jpaSurveyDao.updateResponse(jpaResponse);
    log.debug("updated response: " + jpaResponse.toString());
    return surveyMapper.toResponse(jpaResponse);
  }
 @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
 @Override
 public List<ResponseDTO> getResponseByUser(String user) {
   List<JpaResponse> responseList = jpaSurveyDao.getResponseByUser(user);
   if (responseList == null) {
     return null;
   }
   return surveyMapper.toResponseList(responseList);
 }
  /**
   * Update base survey data. Questions/answer relationships will not be included in the update.
   *
   * @param survey
   * @return
   */
  @Transactional
  @Override
  public SurveyDTO updateSurvey(SurveyDTO survey) {
    JpaSurvey existingSurvey = jpaSurveyDao.getSurvey(survey.getId());
    if (existingSurvey == null || existingSurvey.getStatus() == PublishedState.PUBLISHED) {
      log.warn("Cannot update survey");
      return null;
    }

    // remove question/answer elements
    survey.setSurveyQuestions(null);

    JpaSurvey jpaSurvey = surveyMapper.toJpaSurvey(survey);
    jpaSurvey.setLastUpdateDate(new Timestamp(new Date().getTime()));
    jpaSurvey = jpaSurveyDao.updateSurvey(jpaSurvey);

    return surveyMapper.toSurvey(jpaSurvey);
  }
 @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
 @Override
 public SurveyDTO getSurveyByName(String surveyName) {
   JpaSurvey jpaSurvey = jpaSurveyDao.getSurveyByCanonicalName(surveyName);
   if (jpaSurvey == null) {
     return null;
   }
   SurveyDTO result = surveyMapper.toSurvey(jpaSurvey);
   result.retrieveText(this);
   return result;
 }
 /**
  * Search for {@link JpaSurvey} specified by id.
  *
  * @param id
  * @return
  */
 @Override
 @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
 public SurveyDTO getSurvey(long id) {
   JpaSurvey jpaSurvey = jpaSurveyDao.getSurvey(id);
   if (jpaSurvey == null) {
     return null;
   }
   SurveyDTO result = surveyMapper.toSurvey(jpaSurvey);
   result.retrieveText(this);
   return result;
 }
  /**
   * Search for survey questions for the specified survey. Return only the question data.
   *
   * @param surveyId
   * @return
   */
  @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
  @Override
  public List<SurveyQuestionDTO> getSurveyQuestions(Long surveyId) {
    JpaSurvey survey = jpaSurveyDao.getSurvey(surveyId);
    if (survey == null) {
      return null;
    }

    SurveyDTO surveyDTO = surveyMapper.toSurvey(survey);

    return Lists.newArrayList(surveyDTO.getSurveyQuestions());
  }
 /**
  * Return all surveys
  *
  * @return
  */
 @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
 @Override
 public List<SurveyDTO> getAllSurveys() {
   List<JpaSurvey> surveyList = jpaSurveyDao.getAllSurveys();
   if (surveyList == null) {
     return null;
   }
   List<SurveyDTO> results = surveyMapper.toSurveyList(surveyList);
   for (SurveyDTO surveyDTO : results) {
     surveyDTO.retrieveText(this);
   }
   return results;
 }
 @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
 @Override
 public ResponseDTO getResponseByUserAndSurvey(String user, long surveyId) {
   JpaResponse jpaResponse = jpaSurveyDao.getResponseByUserAndSurvey(user, surveyId);
   return jpaResponse == null ? null : surveyMapper.toResponse(jpaResponse);
 }