Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3'
services:
postgresql:
image: postgres:11.6
environment:
POSTGRES_DB: pulsesurveydb
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- ./server/src/main/resources/db/2019.12.18.14.06.00_createInitialDb.sql:/docker-entrypoint-initdb.d/2019.12.18.14.06.00_createInitialDb.sql
ports:
- "5432:5432"
container_name: pulsesurveydb

2 changes: 2 additions & 0 deletions send-surveys/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ dependencies {
implementation "io.micronaut:micronaut-function-aws:1.3.6"
runtimeOnly "com.amazonaws:aws-lambda-java-log4j2:1.0.0"

compile 'io.micronaut:micronaut-views'
compile 'com.github.jknack:handlebars:4.1.0'
compile 'io.micronaut.data:micronaut-data-jdbc:1.0.0.M5'
compile 'com.github.spullara.mustache.java:compiler:0.9.6'
compile "com.sun.mail:javax.mail:1.6.2"
Expand Down
2 changes: 1 addition & 1 deletion send-surveys/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
micronautVersion=1.2.8
micronautVersion=1.3.1
micronautDataVersion=1.0.0.M5
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package com.objectcomputing.pulsesurvey.model;

import io.micronaut.data.annotation.AutoPopulated;
import io.micronaut.data.annotation.DateCreated;
import io.micronaut.data.annotation.Id;
import org.joda.time.DateTimeZone;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.time.LocalDateTime;
import java.util.UUID;

@Entity
@Table(name = "response")
public class Response {

@Id
@Column(name="responseid")
@AutoPopulated
private UUID responseId;

@Column
@Column(name="responsekey")
private UUID responseKey;

@Column
private int selected;
private String selected;

@Column
private DateTimeZone createdOn;
@Column(name="createdon")
@DateCreated
private LocalDateTime createdOn;

public UUID getResponseId() {
return responseId;
Expand All @@ -36,26 +46,26 @@ public void setResponseKey(UUID responseKey) {
this.responseKey = responseKey;
}

public int getSelected() {
public String getSelected() {
return selected;
}

public void setSelected(int selected) {
public void setSelected(String selected) {
this.selected = selected;
}

public DateTimeZone getCreatedOn() {
public LocalDateTime getCreatedOn() {
return createdOn;
}

public void setCreatedOn(DateTimeZone createdOn) {
public void setCreatedOn(LocalDateTime createdOn) {
this.createdOn = createdOn;
}

public Response() {
}

public Response(UUID responseId, UUID responseKey, int selected, DateTimeZone createdOn) {
public Response(UUID responseId, UUID responseKey, String selected, LocalDateTime createdOn) {
this.responseId = responseId;
this.responseKey = responseKey;
this.selected = selected;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class ResponseKey {
@DateCreated
private LocalDateTime issuedOn;

@Column(name="used")
private boolean used = false;

public UUID getResponseKey() {
return responseKey;
}
Expand All @@ -40,11 +43,16 @@ public void setIssuedOn(LocalDateTime issuedOn) {
this.issuedOn = issuedOn;
}

public boolean isUsed() { return used; }

public void setUsed(boolean used) { this.used = used; }

public ResponseKey() {
}

public ResponseKey(UUID responseKey, LocalDateTime issuedOn) {
public ResponseKey(UUID responseKey, LocalDateTime issuedOn, boolean used) {
this.responseKey = responseKey;
this.issuedOn = issuedOn;
this.used = used;
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package com.objectcomputing.pulsesurvey.model;

import io.micronaut.data.annotation.AutoPopulated;
import io.micronaut.data.annotation.DateCreated;
import io.micronaut.data.annotation.Id;
import org.joda.time.DateTimeZone;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.time.LocalDateTime;
import java.util.UUID;

@Entity
@Table(name = "usercomments")
public class UserComments {

@Id
@Column(name="commentid")
@AutoPopulated
private UUID commentId;

@Column
@Column(name="responsekey")
private UUID responseKey;

@Column
@Column(name="commenttext")
private String commentText;

@Column
private DateTimeZone createdOn;
@Column(name="createdon")
@DateCreated
private LocalDateTime createdOn;

public UUID getCommentId() {
return commentId;
Expand All @@ -44,18 +54,18 @@ public void setCommentText(String commentText) {
this.commentText = commentText;
}

public DateTimeZone getCreatedOn() {
public LocalDateTime getCreatedOn() {
return createdOn;
}

public void setCreatedOn(DateTimeZone createdOn) {
public void setCreatedOn(LocalDateTime createdOn) {
this.createdOn = createdOn;
}

public UserComments() {
}

public UserComments(UUID commentId, UUID responseKey, String commentText, DateTimeZone createdOn) {
public UserComments(UUID commentId, UUID responseKey, String commentText, LocalDateTime createdOn) {
this.commentId = commentId;
this.responseKey = responseKey;
this.commentText = commentText;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package com.objectcomputing.pulsesurvey.receive.responses;

import com.objectcomputing.pulsesurvey.model.Response;
import com.objectcomputing.pulsesurvey.model.ResponseKey;
import com.objectcomputing.pulsesurvey.model.UserComments;
import com.objectcomputing.pulsesurvey.repositories.ResponseKeyRepository;
import com.objectcomputing.pulsesurvey.repositories.ResponseRepository;
import com.objectcomputing.pulsesurvey.repositories.UserCommentsRepository;
import io.micronaut.context.annotation.Value;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.annotation.Produces;
import io.micronaut.views.View;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

@Controller("/happiness")
public class SurveyResponseController {

private static final Logger LOG = LoggerFactory.getLogger(SurveyResponseController.class);

@Inject
private ResponseKeyRepository responseKeyRepo;

@Inject
private ResponseRepository responseRepo;

@Inject
private UserCommentsRepository userCommentsRepo;

public void setResponseKeyRepo(ResponseKeyRepository responseKeyRepository) {
this.responseKeyRepo = responseKeyRepository;
}

public void setResponseRepo(ResponseRepository responseRepository) {
this.responseRepo = responseRepository;
}

public void setUserCommentsRepo(UserCommentsRepository userCommentsRepository) {
this.userCommentsRepo = userCommentsRepository;
}

@Produces(MediaType.TEXT_PLAIN)
@Get("/received")
HttpResponse<String> happinessReceived(String currentEmotion, String surveyKey) {

LOG.info("Hello, I have received your emotion of: " + currentEmotion + "!" +
" with a key of: " + surveyKey);

return HttpResponse.ok("Hello, I have received your emotion of: " + currentEmotion + "!" +
" with a key of: " + surveyKey);
}

@Get
@Produces(MediaType.TEXT_PLAIN)
HttpResponse<String> happiness(String currentEmotion, String surveyKey) {

LOG.info("Hello, your current emotion is " + currentEmotion + "!" +
" with a key of: " + surveyKey);

boolean validKey = false;
validKey = validateKey(surveyKey);

LOG.info("happiness - key is valid? " + validKey);

// if yes - store happiness and comments
if (validKey) {
boolean responseAdded = saveResponse(currentEmotion, surveyKey);

if (responseAdded) {
markKeyAsUsed(surveyKey);

try {
LOG.info("redirecting to /happiness/comment");
return HttpResponse.redirect(new URI("/happiness/comment?surveyKey="+surveyKey));
} catch (URISyntaxException e) {
e.printStackTrace();
LOG.error("unable to redirect to /happiness/comment " + e.getMessage());
}
}
} else {
LOG.warn("This key is not valid: " + surveyKey);
}

return HttpResponse.ok("Hello, your current emotion of " + currentEmotion + "!" +
" is duly noted.");
}

@Get("comment")
@View("comment")
public HttpResponse displayComments(String surveyKey) {

LOG.info("in /comment. surveyKey = " + surveyKey);
return HttpResponse.ok(CollectionUtils.mapOf("surveyKey", surveyKey));
}

@Post("userComments")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@View("thankyou")
public HttpResponse sendThankYouWithCommentBlock
(@Value("userComments") String userComments,
@Value("surveyKey") String surveyKey) {

LOG.info("The user has commented: " + userComments);
LOG.info("With surveyKey: " + surveyKey);
// put comment into the db using the survey key
saveUserComment(surveyKey, userComments);

return HttpResponse.ok();
}

@Get("thanks")
@View("thankyou")
public HttpResponse sendThankYou() {

LOG.info("Sending final thank you web page " );
return HttpResponse.ok();

}

boolean validateKey(String surveyKey) {

LOG.info("Validating key " + surveyKey);
Optional<ResponseKey> responseKey = responseKeyRepo.findById(UUID.fromString(surveyKey));
AtomicBoolean isValid = new AtomicBoolean(false);

responseKey.ifPresent(key -> {
isValid.set(!key.isUsed());
});

LOG.debug("Survey Key " + surveyKey + (isValid.get()?" is used":" is not used"));

LOG.info("key is valid? " + isValid);

return isValid.get();
}

boolean saveResponse(String currentEmotion, String surveyKey) {

boolean responseAdded = false;
Response response = new Response();

response.setResponseKey(UUID.fromString(surveyKey));
response.setSelected(currentEmotion);
response = responseRepo.save(response);

LOG.info("Adding response " + currentEmotion + " ");

if (response.getResponseId() != null) responseAdded = true;

return responseAdded;
}

private void saveUserComment(String surveyKey, String comments) {

UserComments userComments = new UserComments();
userComments.setCommentText(comments);
userComments.setResponseKey(UUID.fromString(surveyKey));
userCommentsRepo.save(userComments);
}

ResponseKey markKeyAsUsed(String surveyKey) {

LOG.info("Marking key as used: " + surveyKey + " from responsekeys");

AtomicReference<ResponseKey> returnedResponseKey = new AtomicReference<>(new ResponseKey());
Optional<ResponseKey> responseKey = responseKeyRepo.findById(UUID.fromString(surveyKey));

responseKey.ifPresent(responseKeyToSave -> {
responseKeyToSave.setUsed(true);
returnedResponseKey.set(responseKeyRepo.update(responseKeyToSave));
});
return returnedResponseKey.get();
}

}
Loading