1+ package com .objectcomputing .pulsesurvey .receive .responses ;
2+
3+ import com .objectcomputing .pulsesurvey .model .Response ;
4+ import com .objectcomputing .pulsesurvey .model .ResponseKey ;
5+ import com .objectcomputing .pulsesurvey .model .UserComments ;
6+ import com .objectcomputing .pulsesurvey .repositories .ResponseKeyRepository ;
7+ import com .objectcomputing .pulsesurvey .repositories .ResponseRepository ;
8+ import com .objectcomputing .pulsesurvey .repositories .UserCommentsRepository ;
9+ import io .micronaut .context .annotation .Value ;
10+ import io .micronaut .core .util .CollectionUtils ;
11+ import io .micronaut .http .HttpResponse ;
12+ import io .micronaut .http .MediaType ;
13+ import io .micronaut .http .annotation .Consumes ;
14+ import io .micronaut .http .annotation .Controller ;
15+ import io .micronaut .http .annotation .Get ;
16+ import io .micronaut .http .annotation .Post ;
17+ import io .micronaut .http .annotation .Produces ;
18+ import io .micronaut .views .View ;
19+ import org .slf4j .Logger ;
20+ import org .slf4j .LoggerFactory ;
21+
22+ import javax .inject .Inject ;
23+ import java .net .URI ;
24+ import java .net .URISyntaxException ;
25+ import java .util .Optional ;
26+ import java .util .UUID ;
27+ import java .util .concurrent .atomic .AtomicBoolean ;
28+ import java .util .concurrent .atomic .AtomicReference ;
29+
30+ @ Controller ("/happiness" )
31+ public class SurveyResponseController {
32+
33+ private static final Logger LOG = LoggerFactory .getLogger (SurveyResponseController .class );
34+
35+ @ Inject
36+ private ResponseKeyRepository responseKeyRepo ;
37+
38+ @ Inject
39+ private ResponseRepository responseRepo ;
40+
41+ @ Inject
42+ private UserCommentsRepository userCommentsRepo ;
43+
44+ public void setResponseKeyRepo (ResponseKeyRepository responseKeyRepository ) {
45+ this .responseKeyRepo = responseKeyRepository ;
46+ }
47+
48+ public void setResponseRepo (ResponseRepository responseRepository ) {
49+ this .responseRepo = responseRepository ;
50+ }
51+
52+ public void setUserCommentsRepo (UserCommentsRepository userCommentsRepository ) {
53+ this .userCommentsRepo = userCommentsRepository ;
54+ }
55+
56+ @ Produces (MediaType .TEXT_PLAIN )
57+ @ Get ("/received" )
58+ HttpResponse <String > happinessReceived (String currentEmotion , String surveyKey ) {
59+
60+ LOG .info ("Hello, I have received your emotion of: " + currentEmotion + "!" +
61+ " with a key of: " + surveyKey );
62+
63+ return HttpResponse .ok ("Hello, I have received your emotion of: " + currentEmotion + "!" +
64+ " with a key of: " + surveyKey );
65+ }
66+
67+ @ Get
68+ @ Produces (MediaType .TEXT_PLAIN )
69+ HttpResponse <String > happiness (String currentEmotion , String surveyKey ) {
70+
71+ LOG .info ("Hello, your current emotion is " + currentEmotion + "!" +
72+ " with a key of: " + surveyKey );
73+
74+ boolean validKey = false ;
75+ validKey = validateKey (surveyKey );
76+
77+ LOG .info ("happiness - key is valid? " + validKey );
78+
79+ // if yes - store happiness and comments
80+ if (validKey ) {
81+ boolean responseAdded = saveResponse (currentEmotion , surveyKey );
82+
83+ if (responseAdded ) {
84+ markKeyAsUsed (surveyKey );
85+
86+ try {
87+ LOG .info ("redirecting to /happiness/comment" );
88+ return HttpResponse .redirect (new URI ("/happiness/comment?surveyKey=" +surveyKey ));
89+ } catch (URISyntaxException e ) {
90+ e .printStackTrace ();
91+ LOG .error ("unable to redirect to /happiness/comment " + e .getMessage ());
92+ }
93+ }
94+ } else {
95+ LOG .warn ("This key is not valid: " + surveyKey );
96+ }
97+
98+ return HttpResponse .ok ("Hello, your current emotion of " + currentEmotion + "!" +
99+ " is duly noted." );
100+ }
101+
102+ @ Get ("comment" )
103+ @ View ("comment" )
104+ public HttpResponse displayComments (String surveyKey ) {
105+
106+ LOG .info ("in /comment. surveyKey = " + surveyKey );
107+ return HttpResponse .ok (CollectionUtils .mapOf ("surveyKey" , surveyKey ));
108+ }
109+
110+ @ Post ("userComments" )
111+ @ Consumes (MediaType .APPLICATION_FORM_URLENCODED )
112+ @ View ("thankyou" )
113+ public HttpResponse sendThankYouWithCommentBlock
114+ (@ Value ("userComments" ) String userComments ,
115+ @ Value ("surveyKey" ) String surveyKey ) {
116+
117+ LOG .info ("The user has commented: " + userComments );
118+ LOG .info ("With surveyKey: " + surveyKey );
119+ // put comment into the db using the survey key
120+ saveUserComment (surveyKey , userComments );
121+
122+ return HttpResponse .ok ();
123+ }
124+
125+ @ Get ("thanks" )
126+ @ View ("thankyou" )
127+ public HttpResponse sendThankYou () {
128+
129+ LOG .info ("Sending final thank you web page " );
130+ return HttpResponse .ok ();
131+
132+ }
133+
134+ boolean validateKey (String surveyKey ) {
135+
136+ LOG .info ("Validating key " + surveyKey );
137+ Optional <ResponseKey > responseKey = responseKeyRepo .findById (UUID .fromString (surveyKey ));
138+ AtomicBoolean isValid = new AtomicBoolean (false );
139+
140+ responseKey .ifPresent (key -> {
141+ isValid .set (!key .isUsed ());
142+ });
143+
144+ LOG .debug ("Survey Key " + surveyKey + (isValid .get ()?" is used" :" is not used" ));
145+
146+ LOG .info ("key is valid? " + isValid );
147+
148+ return isValid .get ();
149+ }
150+
151+ boolean saveResponse (String currentEmotion , String surveyKey ) {
152+
153+ boolean responseAdded = false ;
154+ Response response = new Response ();
155+
156+ response .setResponseKey (UUID .fromString (surveyKey ));
157+ response .setSelected (currentEmotion );
158+ response = responseRepo .save (response );
159+
160+ LOG .info ("Adding response " + currentEmotion + " " );
161+
162+ if (response .getResponseId () != null ) responseAdded = true ;
163+
164+ return responseAdded ;
165+ }
166+
167+ private void saveUserComment (String surveyKey , String comments ) {
168+
169+ UserComments userComments = new UserComments ();
170+ userComments .setCommentText (comments );
171+ userComments .setResponseKey (UUID .fromString (surveyKey ));
172+ userCommentsRepo .save (userComments );
173+ }
174+
175+ ResponseKey markKeyAsUsed (String surveyKey ) {
176+
177+ LOG .info ("Marking key as used: " + surveyKey + " from responsekeys" );
178+
179+ AtomicReference <ResponseKey > returnedResponseKey = new AtomicReference <>(new ResponseKey ());
180+ Optional <ResponseKey > responseKey = responseKeyRepo .findById (UUID .fromString (surveyKey ));
181+
182+ responseKey .ifPresent (responseKeyToSave -> {
183+ responseKeyToSave .setUsed (true );
184+ returnedResponseKey .set (responseKeyRepo .update (responseKeyToSave ));
185+ });
186+ return returnedResponseKey .get ();
187+ }
188+
189+ }
0 commit comments