This is an automated email from the git hooks/post-receive script. New commit to branch feature/6016-add-question in repository coselmar. See http://git.codelutin.com/coselmar.git commit 7998ad35c062f3f5d163265499a87650ef8263fa Author: Yannick Martel <martel@©odelutin.com> Date: Mon Dec 1 17:51:36 2014 +0100 prepare web service to add question --- .../src/main/xmi/coselmar-model.zargo | Bin 9618 -> 9640 bytes .../fr/ifremer/coselmar/beans/QuestionBean.java | 188 +++++++++++++++++++++ .../services/CoselmarWebServiceSupport.java | 10 ++ .../coselmar/services/v1/QuestionsWebService.java | 178 +++++++++++++++++++ 4 files changed, 376 insertions(+) diff --git a/coselmar-persistence/src/main/xmi/coselmar-model.zargo b/coselmar-persistence/src/main/xmi/coselmar-model.zargo index 009697d..f4d34ec 100644 Binary files a/coselmar-persistence/src/main/xmi/coselmar-model.zargo and b/coselmar-persistence/src/main/xmi/coselmar-model.zargo differ diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/beans/QuestionBean.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/beans/QuestionBean.java new file mode 100644 index 0000000..22efff4 --- /dev/null +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/beans/QuestionBean.java @@ -0,0 +1,188 @@ +package fr.ifremer.coselmar.beans; + +import java.io.Serializable; +import java.util.Date; +import java.util.Set; + +public class QuestionBean implements Serializable { + + protected String id; + + protected String title; + + protected Date submissionDate; + + protected Date deadline; + + protected Set<String> theme; + + protected String summary; + + protected Date closingDate; + + protected String questionType; + + protected Set<UserBean> participants; + + protected Set<UserBean> supervisors; + + protected Set<UserBean> contributors; + + protected Set<UserBean> clients; + + protected Set<QuestionBean> parents; + protected Set<QuestionBean> children; + + protected String privacy; + + protected Set<DocumentBean> relatedDocuments; + + protected String status; + + protected Set<String> externalExperts; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Date getSubmissionDate() { + return submissionDate; + } + + public void setSubmissionDate(Date submissionDate) { + this.submissionDate = submissionDate; + } + + public Date getDeadline() { + return deadline; + } + + public void setDeadline(Date deadline) { + this.deadline = deadline; + } + + public Set<String> getTheme() { + return theme; + } + + public void setTheme(Set<String> theme) { + this.theme = theme; + } + + public String getSummary() { + return summary; + } + + public void setSummary(String summary) { + this.summary = summary; + } + + public Date getClosingDate() { + return closingDate; + } + + public void setClosingDate(Date closingDate) { + this.closingDate = closingDate; + } + + public String getQuestionType() { + return questionType; + } + + public void setQuestionType(String questionType) { + this.questionType = questionType; + } + + public Set<UserBean> getParticipants() { + return participants; + } + + public void setParticipants(Set<UserBean> participants) { + this.participants = participants; + } + + public Set<UserBean> getSupervisors() { + return supervisors; + } + + public void setSupervisors(Set<UserBean> supervisors) { + this.supervisors = supervisors; + } + + public Set<UserBean> getContributors() { + return contributors; + } + + public void setContributors(Set<UserBean> contributors) { + this.contributors = contributors; + } + + public Set<UserBean> getClients() { + return clients; + } + + public void setClients(Set<UserBean> clients) { + this.clients = clients; + } + + public Set<QuestionBean> getParents() { + return parents; + } + + public void setParents(Set<QuestionBean> parents) { + this.parents = parents; + } + + public Set<QuestionBean> getChildren() { + return children; + } + + public void setChildren(Set<QuestionBean> children) { + this.children = children; + } + + public String getPrivacy() { + return privacy; + } + + public void setPrivacy(String privacy) { + this.privacy = privacy; + } + + public Set<DocumentBean> getRelatedDocuments() { + return relatedDocuments; + } + + public void setRelatedDocuments(Set<DocumentBean> relatedDocuments) { + this.relatedDocuments = relatedDocuments; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public Set<String> getExternalExperts() { + return externalExperts; + } + + public void setExternalExperts(Set<String> externalExperts) { + this.externalExperts = externalExperts; + } + +} //Question diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarWebServiceSupport.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarWebServiceSupport.java index 0817cde..17e5af2 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarWebServiceSupport.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarWebServiceSupport.java @@ -36,8 +36,10 @@ import com.auth0.jwt.JWTVerifier; import fr.ifremer.coselmar.beans.UserWebToken; import fr.ifremer.coselmar.persistence.CoselmarPersistenceContext; import fr.ifremer.coselmar.persistence.entity.CoselmarUser; +import fr.ifremer.coselmar.persistence.entity.CoselmarUserGroupTopiaDao; import fr.ifremer.coselmar.persistence.entity.CoselmarUserTopiaDao; import fr.ifremer.coselmar.persistence.entity.DocumentTopiaDao; +import fr.ifremer.coselmar.persistence.entity.QuestionTopiaDao; import fr.ifremer.coselmar.services.config.CoselmarServicesConfig; import fr.ifremer.coselmar.services.errors.InvalidCredentialException; import fr.ifremer.coselmar.services.v1.DocumentsWebService; @@ -113,6 +115,14 @@ public abstract class CoselmarWebServiceSupport extends WebMotionController impl return getPersistenceContext().getCoselmarUserDao(); } + protected CoselmarUserGroupTopiaDao getCoselmarUserGroupDao() { + return getPersistenceContext().getCoselmarUserGroupDao(); + } + + protected QuestionTopiaDao getQuestionDao() { + return getPersistenceContext().getQuestionDao(); + } + public void commit() { getPersistenceContext().commit(); } diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/QuestionsWebService.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/QuestionsWebService.java new file mode 100644 index 0000000..b88b12e --- /dev/null +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/QuestionsWebService.java @@ -0,0 +1,178 @@ +package fr.ifremer.coselmar.services.v1; + +import java.util.Collection; +import java.util.Date; +import java.util.List; +import java.util.Set; + +import com.google.common.base.Function; +import com.google.common.collect.Collections2; +import fr.ifremer.coselmar.beans.QuestionBean; +import fr.ifremer.coselmar.beans.UserBean; +import fr.ifremer.coselmar.beans.UserWebToken; +import fr.ifremer.coselmar.persistence.entity.CoselmarUser; +import fr.ifremer.coselmar.persistence.entity.CoselmarUserGroup; +import fr.ifremer.coselmar.persistence.entity.CoselmarUserRole; +import fr.ifremer.coselmar.persistence.entity.Privacy; +import fr.ifremer.coselmar.persistence.entity.Question; +import fr.ifremer.coselmar.persistence.entity.Status; +import fr.ifremer.coselmar.services.CoselmarTechnicalException; +import fr.ifremer.coselmar.services.CoselmarWebServiceSupport; +import fr.ifremer.coselmar.services.errors.InvalidCredentialException; +import fr.ifremer.coselmar.services.errors.UnauthorizedException; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.persistence.TopiaNoResultException; + +/** + * @author ymartel <martel@codelutin.com> + */ +public class QuestionsWebService extends CoselmarWebServiceSupport { + + private static final Log log = LogFactory.getLog(QuestionsWebService.class); + + public void addQuestion(QuestionBean questionBean) throws InvalidCredentialException, UnauthorizedException { + + // Check authentication + String authorization = getContext().getHeader("Authorization"); + UserWebToken userWebToken = checkAuthentication(authorization); + + // Only Expert or Supervisor can add document + String userRole = userWebToken.getRole(); + + if (!StringUtils.equalsIgnoreCase(CoselmarUserRole.ADMIN.name(), userRole)) { + String message = String.format("User %s %s ('%s') is not allowed to add question", + userWebToken.getFirstName(), userWebToken.getLastName(), userWebToken.getUserId()); + if (log.isWarnEnabled()) { + log.warn(message); + } + throw new UnauthorizedException(message); + + } + + // retrieve user who will be assigned as question supervisor + String fullId = getFullUserIdFromShort(userWebToken.getUserId()); + + CoselmarUser supervisor; + try { + supervisor = getCoselmarUserDao().forTopiaIdEquals(fullId).findUnique(); + } catch (TopiaNoResultException tnre) { + // Should not happened, cause user are not really deleted + String message = String.format("Seems that logged user ('%s') does not exist anymore.", fullId); + if (log.isErrorEnabled()) { + log.equals(message); + } + throw new CoselmarTechnicalException(message); + } + + // let's go + Question question = getQuestionDao().create(); + + question.setTitle(questionBean.getTitle()); + + question.setSummary(questionBean.getSummary()); + + question.setQuestionType(questionBean.getQuestionType()); + + // By default, privacy is private + String privacy = questionBean.getPrivacy(); + Privacy realPrivacy = privacy != null ? Privacy.valueOf(privacy.toUpperCase()) : Privacy.PRIVATE; + question.setPrivacy(realPrivacy); + + // On creation, Status is Open + question.setStatus(Status.OPEN); + + // Manage Dates : submission & deadline + Date submissionDate = questionBean.getSubmissionDate(); + if (submissionDate != null) { + question.setSubmissionDate(new Date(submissionDate.getTime())); + } + + Date deadline = questionBean.getDeadline(); + if (deadline != null) { + question.setDeadline(new Date(deadline.getTime())); + } + + + // Users around the question + + // First Supervisor is the one creating this question + question.addSupervisors(supervisor); + + question.addAllExternalExperts(questionBean.getExternalExperts()); + // Retrieve the clients + Set<UserBean> clients = questionBean.getClients(); + if (clients != null && !clients.isEmpty()) { + List<CoselmarUser> clientEntities = retrieveUsers(clients); + question.addAllClients(clientEntities); + } + + // For participants, should create a dedicated group, + // that could be used to restrict documents access + Set<UserBean> participants = questionBean.getParticipants(); + if (participants != null && !participants.isEmpty()) { + CoselmarUserGroup userGroup = getCoselmarUserGroupDao().create(); + userGroup.setName(questionBean.getTitle()); + + List<CoselmarUser> expertEntities = retrieveUsers(participants); + userGroup.addAllMembers(expertEntities); + + question.setParticipants(userGroup); + } + + // Note : no contributors now, contributors are old participants, + // excluded from process + + + // Hierarchy of questions + Set<QuestionBean> parents = questionBean.getParents(); + if (parents != null && !parents.isEmpty()) { + List<Question> questions = retrieveQuestions(parents); + question.addAllParents(questions); + } + + +// // Documents on init +// question.addAllRelatedDocuments(); + + } + + + //////////////////////////////////////////////////////////////////////////// + /////////////////////// Internal Parts ///////////////////////////// + //////////////////////////////////////////////////////////////////////////// + + + protected List<CoselmarUser> retrieveUsers(Collection<UserBean> userBeans) { + Function<UserBean, String> getIds = new Function<UserBean, String>() { + @Override + public String apply(UserBean userBean) { + return getFullUserIdFromShort(userBean.getId()); + } + }; + + Collection<String> userIds = Collections2.transform(userBeans, getIds); + List<CoselmarUser> coselmarUsers = getCoselmarUserDao().forTopiaIdIn(userIds).findAll(); + return coselmarUsers; + + } + + protected List<Question> retrieveQuestions(Collection<QuestionBean> questionBeans) { + Function<QuestionBean, String> getIds = new Function<QuestionBean, String>() { + @Override + public String apply(QuestionBean questionBean) { + return getFullUserIdFromShort(questionBean.getId()); + } + }; + + Collection<String> questionIds = Collections2.transform(questionBeans, getIds); + List<Question> questions = getQuestionDao().forTopiaIdIn(questionIds).findAll(); + return questions; + + } + + protected String getFullQuestionIdFromShort(String shortQuestionId) { + return Question.class.getCanonicalName() + getPersistenceContext().getTopiaIdFactory().getSeparator() + shortQuestionId; + } +} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.