Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.wadpam.survey.service.SurveyService Maven / Gradle / Ivy
package com.wadpam.survey.service;
import java.io.Serializable;
import java.util.Collection;
import net.sf.mardao.core.CursorPage;
import net.sf.mardao.core.dao.DaoImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.wadpam.open.exceptions.ConflictException;
import com.wadpam.open.exceptions.NotFoundException;
import com.wadpam.survey.dao.DAnswerDao;
import com.wadpam.survey.dao.DOptionDao;
import com.wadpam.survey.dao.DQuestionDao;
import com.wadpam.survey.dao.DResponseDao;
import com.wadpam.survey.dao.DSurveyDao;
import com.wadpam.survey.dao.DVersionDao;
import com.wadpam.survey.dao.Di18nDao;
import com.wadpam.survey.domain.DAnswer;
import com.wadpam.survey.domain.DOption;
import com.wadpam.survey.domain.DQuestion;
import com.wadpam.survey.domain.DResponse;
import com.wadpam.survey.domain.DSurvey;
import com.wadpam.survey.domain.DVersion;
import com.wadpam.survey.json.JAnswer;
import com.wadpam.survey.json.JResponse;
import com.wadpam.survey.json.JSurvey;
import com.wadpam.survey.json.JVersion;
import com.wadpam.survey.web.AnswerController;
import com.wadpam.survey.web.Converter;
import com.wadpam.survey.web.OptionController;
import com.wadpam.survey.web.QuestionController;
import com.wadpam.survey.web.ResponseController;
import com.wadpam.survey.web.SurveyController;
/**
*
* @author os
*/
public class SurveyService {
public static final String DOMAIN_APIDOCS = "apidocs";
/** Base offset for survey resource errors (101000) */
public static final int ERR_SURVEY = 101000;
/** Base offset for response resource errors (102000) */
public static final int ERR_RESPONSE = 102000;
/** Base offset for answer resource errors (103000) */
public static final int ERR_ANSWER = 103000;
/** Base offset for question resource errors (104000) */
public static final int ERR_QUESTION = 104000;
/** Base offset for option resource errors (105000) */
public static final int ERR_OPTION = 105000;
/** Base offset for version resource errors (106010) */
public static final int ERR_VERSION = 106010;
public static final int ERR_VERSION_CREATE_NOT_FOUND = 106001;
static final Logger LOG = LoggerFactory.getLogger(SurveyService.class);
private DAnswerDao answerDao;
private Di18nDao i18nDao;
private DOptionDao optionDao;
private DQuestionDao questionDao;
private DResponseDao responseDao;
private DSurveyDao surveyDao;
private DVersionDao versionDao;
public void init() {
}
public DOption cloneOption(DQuestion question, DOption from) {
final DOption to = new DOption();
to.setLabel(from.getLabel());
to.setQuestion(question);
to.setSurvey(from.getSurvey());
to.setVersion(from.getVersion());
optionDao.persist(to);
return to;
}
public DQuestion cloneQuestion(DVersion version, DQuestion from) {
final DQuestion to = new DQuestion();
to.setOrdering(from.getOrdering());
to.setQuestion(from.getQuestion());
to.setRequired(from.getRequired());
to.setSurvey(from.getSurvey());
to.setType(from.getType());
to.setVersion(version);
questionDao.persist(to);
for (DOption fo : optionDao.queryByQuestion(from)) {
cloneOption(to, fo);
}
return to;
}
public DVersion cloneVersion(Long surveyId, Long fromVersionId, String description) {
LOG.debug("clone version from {} for surveyId {}", fromVersionId, surveyId);
final DSurvey survey = new DSurvey();
survey.setId(surveyId);
final DVersion to = new DVersion();
to.setDescription(description);
to.setState(JVersion.STATE_DRAFT);
to.setSurvey(survey);
versionDao.persist(to);
// clone version
if (null != fromVersionId) {
final DVersion from = new DVersion();
from.setId(fromVersionId);
for (DQuestion fq : questionDao.queryByVersion(from)) {
cloneQuestion(to, fq);
}
}
return to;
}
public DAnswer createAnswer(DAnswer dEntity) {
// id must be generated
if (null != dEntity.getId()) {
throw new ConflictException(AnswerController.ERR_CREATE_CONFLICT,
String.format("id {} must not be specified", dEntity.getId()),
null,
"id must be generated");
}
// check that question exists
final DQuestion question = questionDao.findByPrimaryKey(dEntity.getQuestion().getId());
if (null == question) {
throw new NotFoundException(AnswerController.ERR_CREATE_NOT_FOUND,
String.format("Question {} not found", dEntity.getQuestion().getId()),
null,
"Cannot answer non-existing question");
}
answerDao.persist(dEntity);
return dEntity;
}
public DOption createOption(DOption dEntity) {
// id must be generated
if (null != dEntity.getId()) {
throw new ConflictException(OptionController.ERR_CREATE_CONFLICT,
String.format("id {} must not be specified", dEntity.getId()),
null,
"id must be generated");
}
// check that question exists
final DQuestion question = questionDao.findByPrimaryKey(dEntity.getQuestion().getId());
if (null == question) {
throw new NotFoundException(OptionController.ERR_CREATE_NOT_FOUND,
String.format("Question {} not found", dEntity.getQuestion().getId()),
null,
"Cannot answer non-existing question");
}
optionDao.persist(dEntity);
return dEntity;
}
public DQuestion createQuestion(DQuestion dEntity) {
// id must be generated
if (null != dEntity.getId()) {
throw new ConflictException(QuestionController.ERR_CREATE_CONFLICT,
String.format("id {} must not be specified", dEntity.getId()),
null,
"id must be generated");
}
// check that survey exists
final DSurvey survey = surveyDao.findByPrimaryKey(dEntity.getSurvey().getId());
if (null == survey) {
throw new NotFoundException(QuestionController.ERR_CREATE_NOT_FOUND,
String.format("Survey {} not found", dEntity.getSurvey().getId()),
null,
"Cannot respond to non-existing survey");
}
questionDao.persist(dEntity);
return dEntity;
}
public DSurvey createSurvey(DSurvey dEntity) {
// id must be generated
if (null != dEntity.getId()) {
throw new ConflictException(SurveyController.ERR_CREATE_CONFLICT,
String.format("id {} must not be specified", dEntity.getId()),
null,
"id must be generated");
}
// patch state if missing
if (null == dEntity.getState()) {
dEntity.setState(JSurvey.STATE_IN_DESIGN);
}
surveyDao.persist(dEntity);
// create the first version
cloneVersion(dEntity.getId(), null, "0.1");
return dEntity;
}
public DVersion createVersion(Long surveyId, Long fromVersionId, String description) {
final DSurvey survey = surveyDao.findByPrimaryKey(surveyId);
if (null == survey) {
throw new NotFoundException(ERR_VERSION_CREATE_NOT_FOUND, null, null,
String.format("Cannot find survey with id %d", surveyId));
}
final DVersion fromVersion = versionDao.findByPrimaryKey(fromVersionId);
if (null == fromVersion) {
throw new NotFoundException(ERR_VERSION_CREATE_NOT_FOUND, null, null,
String.format("Cannot find from-version with id %d", fromVersionId));
}
return cloneVersion(surveyId, fromVersionId, description);
}
public Integer deleteAll() {
int count = answerDao.deleteAll();
count += i18nDao.deleteAll();
count += optionDao.deleteAll();
count += responseDao.deleteAll();
count += questionDao.deleteAll();
count += versionDao.deleteAll();
count += surveyDao.deleteAll();
// for GAE, will do nothing
((DaoImpl) answerDao).dropTable();
((DaoImpl) i18nDao).dropTable();
((DaoImpl) optionDao).dropTable();
((DaoImpl) responseDao).dropTable();
((DaoImpl) questionDao).dropTable();
((DaoImpl) versionDao).dropTable();
((DaoImpl) surveyDao).dropTable();
return count;
}
public void deleteResponse(Long id) {
// TODO: cascade
responseDao.delete(id);
}
public void deleteSurvey(Long id) {
// TODO: cascade
surveyDao.delete(id);
}
public DAnswer getAnswer(Long id) {
final DAnswer entity = answerDao.findByPrimaryKey(id);
return entity;
}
public Iterable getAnswersByResponse(Long id) {
final DResponse response = new DResponse();
response.setId(id);
return answerDao.queryByResponse(response);
}
public Iterable getAnswersByResponseIds(Collection ids) {
return answerDao.queryByResponseIds(ids);
}
public Iterable getAnswersByVersion(Long versionId) {
final DVersion version = new DVersion();
version.setId(versionId);
return answerDao.queryByVersion(version);
}
@Deprecated
public CursorPage getAnswersPage(Long responseId, int pageSize, Serializable cursorKey) {
return getAnswersPage(responseId, pageSize, null != cursorKey ? cursorKey.toString() : (String) null);
}
public CursorPage getAnswersPage(Long responseId, int pageSize, String cursorKey) {
final DResponse response = new DResponse();
response.setId(responseId);
final CursorPage page = answerDao.queryPageByResponse(response, pageSize, cursorKey);
return page;
}
public DOption getOption(Long id) {
final DOption entity = optionDao.findByPrimaryKey(id);
return entity;
}
public Iterable getOptionsBySurvey(Long surveyId) {
DSurvey survey = new DSurvey();
survey.setId(surveyId);
return optionDao.queryBySurvey(survey);
}
public Iterable getOptionsByVersion(Long versionId) {
DVersion version = new DVersion();
version.setId(versionId);
return optionDao.queryByVersion(version);
}
@Deprecated
public CursorPage getOptionsPage(Long questionId, int pageSize, Serializable cursorKey) {
return getOptionsPage(questionId, pageSize, null != cursorKey ? cursorKey.toString() : (String) null);
}
public CursorPage getOptionsPage(Long questionId, int pageSize, String cursorKey) {
DQuestion question = new DQuestion();
question.setId(questionId);
final CursorPage page = optionDao.queryPageByQuestion(question, pageSize, cursorKey);
return page;
}
public DQuestion getQuestion(Long id) {
final DQuestion entity = questionDao.findByPrimaryKey(id);
return entity;
}
public Iterable getVersionsBySurvey(Long surveyId) {
DSurvey survey = new DSurvey();
survey.setId(surveyId);
return versionDao.queryBySurvey(survey);
}
public Iterable getQuestionsBySurvey(Long surveyId) {
DSurvey survey = new DSurvey();
survey.setId(surveyId);
return questionDao.queryBySurvey(survey);
}
public Iterable getQuestionsByVersion(Long versionId) {
DVersion version = new DVersion();
version.setId(versionId);
return questionDao.queryByVersion(version);
}
public CursorPage getQuestionsPage(Long versionId, int pageSize, String cursorKey) {
final DVersion version = new DVersion();
version.setId(versionId);
final CursorPage page = questionDao.queryPageByVersion(version, pageSize, cursorKey);
return page;
}
public DResponse getResponse(Long id) {
final DResponse entity = responseDao.findByPrimaryKey(id);
return entity;
}
public CursorPage getResponsesPage(Long versionId, int pageSize, String cursorKey) {
DVersion version = new DVersion();
version.setId(versionId);
final CursorPage page = responseDao.queryPageByVersion(version, pageSize, cursorKey);
return page;
}
public DSurvey getSurvey(Long id) {
final DSurvey entity = surveyDao.findByPrimaryKey(id);
return entity;
}
@Deprecated
public CursorPage getSurveysPage(int pageSize, Serializable cursorKey) {
return getSurveysPage(pageSize, null != cursorKey ? cursorKey.toString() : (String) null);
}
public CursorPage getSurveysPage(int pageSize, String cursorKey) {
final CursorPage page = surveyDao.queryPage(pageSize, cursorKey);
return page;
}
public DVersion getVersion(Long id) {
final DVersion entity = versionDao.findByPrimaryKey(id);
return entity;
}
public CursorPage getVersionsPage(Long surveyId, int pageSize, String cursorKey) {
DSurvey survey = new DSurvey();
survey.setId(surveyId);
final CursorPage page = versionDao.queryPageBySurvey(survey, pageSize, cursorKey);
return page;
}
public DOption updateOption(DOption dEntity) {
optionDao.update(dEntity);
return dEntity;
}
public DQuestion updateQuestion(DQuestion dEntity) {
questionDao.update(dEntity);
return dEntity;
}
public DSurvey updateSurvey(DSurvey dEntity) {
surveyDao.update(dEntity);
return dEntity;
}
public DVersion updateVersion(DVersion dEntity) {
versionDao.update(dEntity);
return dEntity;
}
public DAnswer upsertAnswer(DAnswer dEntity, Iterable existing) {
boolean existed = null != dEntity.getId();
// check if found by Question and Response
if (!existed && null != existing) {
for (DAnswer da : existing) {
if (dEntity.getQuestion().getId().equals(
da.getQuestion().getId())) {
da.setAnswer(dEntity.getAnswer());
dEntity = da;
existed = true;
break;
}
}
}
if (existed) {
answerDao.update(dEntity);
}
else {
answerDao.persist(dEntity);
}
return dEntity;
}
public DResponse upsertResponse(JResponse jResponse) {
final DResponse dEntity = Converter.convert(jResponse);
return upsertResponse(dEntity, jResponse.getAnswers());
}
public DResponse upsertResponse(DResponse dEntity, Collection innerAnswers) {
// check that survey exists
final DSurvey survey = surveyDao.findByPrimaryKey(dEntity.getSurvey().getId());
if (null == survey) {
throw new NotFoundException(ResponseController.ERR_CREATE_NOT_FOUND,
String.format("Survey {} not found", dEntity.getSurvey().getId()),
null,
"Cannot respond to non-existing survey");
}
// patch state if missing
if (null == dEntity.getState()) {
dEntity.setState(JResponse.STATE_ACTIVE);
}
final boolean existed = null != dEntity.getId();
if (existed) {
responseDao.update(dEntity);
}
else {
responseDao.persist(dEntity);
}
LOG.info("saved response {}, inner answers = {}", dEntity, innerAnswers);
// inner Answers to this response?
if (null != innerAnswers) {
final Iterable existing = answerDao.queryByResponse(dEntity);
for (JAnswer answer : innerAnswers) {
// if ID generated above, populate on inner answers
answer.setResponseId(dEntity.getId());
upsertAnswer(Converter.convert(answer), existing);
}
}
return dEntity;
}
public void setAnswerDao(DAnswerDao answerDao) {
this.answerDao = answerDao;
}
public void setI18nDao(Di18nDao i18nDao) {
this.i18nDao = i18nDao;
}
public void setOptionDao(DOptionDao optionDao) {
this.optionDao = optionDao;
}
public void setQuestionDao(DQuestionDao questionDao) {
this.questionDao = questionDao;
}
public void setResponseDao(DResponseDao responseDao) {
this.responseDao = responseDao;
}
public void setSurveyDao(DSurveyDao surveyDao) {
this.surveyDao = surveyDao;
}
public void setVersionDao(DVersionDao versionDao) {
this.versionDao = versionDao;
}
}