Browse Source

return results

master
DemiSel 2 years ago
parent
commit
6b63a1d07c

+ 5
- 1
backend/src/main/java/api/JsonKeys.java View File

9
     KEY_ANSWERS("answers"),
9
     KEY_ANSWERS("answers"),
10
     KEY_DESCRIPTION("description"),
10
     KEY_DESCRIPTION("description"),
11
     KEY_NAME("name"),
11
     KEY_NAME("name"),
12
-    KEY_ID("id");
12
+    KEY_ID("id"),
13
+    KEY_TRAIT("trait"),
14
+    KEY_TRAITS("traits"),
15
+    KEY_WEIGHT("weight"),
16
+    KEY_RESULTS("results");
13
 
17
 
14
 
18
 
15
     private final Object value;
19
     private final Object value;

+ 5
- 0
backend/src/main/java/controller/SessionController.java View File

16
         return new Session().asJsonObject();
16
         return new Session().asJsonObject();
17
     }
17
     }
18
 
18
 
19
+    public JsonObject getEntriesForId(String iId, JsonObject iBody) {
20
+        Session vSession = Session.getSessionForUUID(UUID.fromString(iId));
21
+        return vSession.asJsonObject();
22
+    }
23
+
19
     public JsonObject updateEntry(String iID, JsonObject iBody) throws Exception{
24
     public JsonObject updateEntry(String iID, JsonObject iBody) throws Exception{
20
         Session vSession = Session.getSessionForUUID(UUID.fromString(iID));
25
         Session vSession = Session.getSessionForUUID(UUID.fromString(iID));
21
         if(null == vSession)
26
         if(null == vSession)

+ 40
- 1
backend/src/main/java/model/personnality/Trait.java View File

1
 package model.personnality;
1
 package model.personnality;
2
 
2
 
3
-public class Trait {
3
+import com.github.cliftonlabs.json_simple.JsonObject;
4
+import com.github.cliftonlabs.json_simple.Jsonable;
5
+
6
+import java.io.IOException;
7
+import java.io.StringWriter;
8
+import java.io.Writer;
9
+import java.nio.charset.StandardCharsets;
10
+
11
+import static api.JsonKeys.KEY_DESCRIPTION;
12
+import static api.JsonKeys.KEY_NAME;
13
+
14
+public class Trait implements Jsonable {
4
 
15
 
5
     private String fName;
16
     private String fName;
6
     private String fDescription;
17
     private String fDescription;
9
         fName           = iName;
20
         fName           = iName;
10
         fDescription    = iDescription;
21
         fDescription    = iDescription;
11
     }
22
     }
23
+
24
+    // ------------------------------------------------------------------
25
+    // Jsonable Implementation
26
+    // ------------------------------------------------------------------
27
+    public JsonObject asJsonObject(){
28
+        JsonObject vResult = new JsonObject();
29
+        vResult.put(KEY_NAME, fName);
30
+        vResult.put(KEY_DESCRIPTION, fDescription);
31
+        return vResult;
32
+    }
33
+
34
+    @Override
35
+    public String toJson() {
36
+        final StringWriter writable = new StringWriter();
37
+        try{
38
+            this.toJson(writable);
39
+        }catch(final IOException caught)
40
+        {
41
+            caught.printStackTrace();
42
+        }
43
+        return writable.toString();
44
+    }
45
+
46
+    @Override
47
+    public void toJson(Writer writable) throws IOException {
48
+
49
+        writable.write(new String(asJsonObject().toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
50
+    }
12
 }
51
 }

+ 16
- 6
backend/src/main/java/model/questionnaire/Answer.java View File

10
 import java.nio.charset.StandardCharsets;
10
 import java.nio.charset.StandardCharsets;
11
 import java.util.UUID;
11
 import java.util.UUID;
12
 
12
 
13
-import static api.JsonKeys.KEY_DESCRIPTION;
14
-import static api.JsonKeys.KEY_ID;
13
+import static api.JsonKeys.*;
15
 
14
 
16
 public class Answer implements Jsonable {
15
 public class Answer implements Jsonable {
17
 
16
 
33
     public float getWeight() { return fWeight; }
32
     public float getWeight() { return fWeight; }
34
     public UUID getId() { return fId; }
33
     public UUID getId() { return fId; }
35
 
34
 
35
+
36
+    // ------------------------------------------------------------------
37
+    // Jsonable Implementation
38
+    // ------------------------------------------------------------------
39
+    public JsonObject asJsonObject(){
40
+        final JsonObject vResult = new JsonObject();
41
+        vResult.put(KEY_DESCRIPTION, fDescription);
42
+        vResult.put(KEY_ID, fId.toString());
43
+        return vResult;
44
+    }
45
+
46
+
36
     @Override
47
     @Override
37
     public String toJson() {
48
     public String toJson() {
38
         final StringWriter writable = new StringWriter();
49
         final StringWriter writable = new StringWriter();
45
         return writable.toString();
56
         return writable.toString();
46
     }
57
     }
47
 
58
 
59
+
48
     @Override
60
     @Override
49
     public void toJson(Writer writable) throws IOException {
61
     public void toJson(Writer writable) throws IOException {
50
-        final JsonObject vResult = new JsonObject();
51
-        vResult.put(KEY_DESCRIPTION, fDescription);
52
-        vResult.put(KEY_ID, fId.toString());
53
-        writable.write(new String(vResult.toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
62
+
63
+        writable.write(new String(asJsonObject().toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
54
     }
64
     }
55
 }
65
 }

+ 9
- 6
backend/src/main/java/model/questionnaire/Question.java View File

41
     // ------------------------------------------------------------------
41
     // ------------------------------------------------------------------
42
     // Jsonable Implementation
42
     // Jsonable Implementation
43
     // ------------------------------------------------------------------
43
     // ------------------------------------------------------------------
44
-
44
+    public JsonObject asJsonObject()
45
+    {
46
+        JsonObject vResult = new JsonObject();
47
+        vResult.put(KEY_NAME, fName);
48
+        vResult.put(KEY_ANSWERS, fAnswers);
49
+        vResult.put(KEY_ID, fId.toString());
50
+        return vResult;
51
+    }
45
     @Override
52
     @Override
46
     public String toJson() {
53
     public String toJson() {
47
         final StringWriter writable = new StringWriter();
54
         final StringWriter writable = new StringWriter();
56
 
63
 
57
     @Override
64
     @Override
58
     public void toJson(Writer writable) throws IOException {
65
     public void toJson(Writer writable) throws IOException {
59
-        JsonObject vResult = new JsonObject();
60
-        vResult.put(KEY_NAME, fName);
61
-        vResult.put(KEY_ANSWERS, fAnswers);
62
-        vResult.put(KEY_ID, fId.toString());
63
-        writable.write(new String(vResult.toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
66
+        writable.write(new String(asJsonObject().toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
64
     }
67
     }
65
 }
68
 }

+ 2
- 2
backend/src/main/java/model/questionnaire/Session.java View File

11
 import java.util.HashMap;
11
 import java.util.HashMap;
12
 import java.util.UUID;
12
 import java.util.UUID;
13
 
13
 
14
-import static api.JsonKeys.KEY_ID;
14
+import static api.JsonKeys.*;
15
 
15
 
16
 public class Session implements Jsonable {
16
 public class Session implements Jsonable {
17
 
17
 
41
     public JsonObject asJsonObject() {
41
     public JsonObject asJsonObject() {
42
         JsonObject vResult = new JsonObject();
42
         JsonObject vResult = new JsonObject();
43
         vResult.put(KEY_ID, fId.toString());
43
         vResult.put(KEY_ID, fId.toString());
44
-
44
+        vResult.put(KEY_RESULTS, fAnswers.toJson());
45
         return vResult;
45
         return vResult;
46
     }
46
     }
47
 
47
 

+ 42
- 1
backend/src/main/java/model/questionnaire/SessionAnswers.java View File

1
 package model.questionnaire;
1
 package model.questionnaire;
2
 
2
 
3
+import com.github.cliftonlabs.json_simple.JsonArray;
4
+import com.github.cliftonlabs.json_simple.JsonObject;
5
+import com.github.cliftonlabs.json_simple.Jsonable;
3
 import model.personnality.Personality;
6
 import model.personnality.Personality;
4
 import model.personnality.Trait;
7
 import model.personnality.Trait;
5
 
8
 
9
+import java.io.IOException;
10
+import java.io.StringWriter;
11
+import java.io.Writer;
12
+import java.nio.charset.StandardCharsets;
13
+import java.util.ArrayList;
6
 import java.util.HashMap;
14
 import java.util.HashMap;
7
 
15
 
16
+import static api.JsonKeys.*;
17
+
8
 /**
18
 /**
9
  * Stores answers provided for a session of answers
19
  * Stores answers provided for a session of answers
10
  */
20
  */
11
-public class SessionAnswers {
21
+public class SessionAnswers implements Jsonable {
12
 
22
 
13
     private HashMap<Question, Answer> fAnswers;
23
     private HashMap<Question, Answer> fAnswers;
14
     private HashMap<Trait, Float> fTraits;
24
     private HashMap<Trait, Float> fTraits;
47
         fTraits.put(vNewTrait, vNewTraitWeight);
57
         fTraits.put(vNewTrait, vNewTraitWeight);
48
     }
58
     }
49
 
59
 
60
+    // ------------------------------------------------------------------
61
+    // Jsonable Implementation
62
+    // ------------------------------------------------------------------
63
+    @Override
64
+    public String toJson() {
65
+        final StringWriter writable = new StringWriter();
66
+        try{
67
+            this.toJson(writable);
68
+        }catch(final IOException caught)
69
+        {
70
+            caught.printStackTrace();
71
+        }
72
+        return writable.toString();
73
+    }
74
+
75
+    @Override
76
+    public void toJson(Writer writable) throws IOException {
77
+        JsonObject vResult = new JsonObject();
78
+        JsonArray vAnswers = new JsonArray();
79
+        fAnswers.forEach( (question, answer) -> {
80
+            vAnswers.add(answer.asJsonObject().putChain(KEY_QUESTION, question.asJsonObject()));
81
+        });
82
+        vResult.put(KEY_ANSWERS, vAnswers);
83
+        JsonArray vTraits = new JsonArray();
84
+        fTraits.forEach( (trait, weight) -> {
85
+            vTraits.add(trait.asJsonObject().putChain(KEY_WEIGHT, weight));
86
+        });
87
+        vResult.put(KEY_TRAITS, vTraits);
88
+        writable.write(new String(vResult.toJson().getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
89
+    }
90
+
50
 }
91
 }

+ 1
- 1
frontend/src/component/questionnaire.js View File

52
     )
52
     )
53
     else
53
     else
54
     return(
54
     return(
55
-        <QuestionnaireResult/>
55
+        <QuestionnaireResult sessionId={props.sessionId}/>
56
     )
56
     )
57
 }
57
 }
58
 
58
 

+ 6
- 1
frontend/src/component/questionnaire_result.js View File

6
 
6
 
7
     if(null === resultsFetched)
7
     if(null === resultsFetched)
8
     {
8
     {
9
-        
9
+        fetch(url+"/session/"+props.sessionId) 
10
+        .then(response => response.json())
11
+        .then(result => {
12
+            onResultsFetched(result);
13
+        })
14
+        .catch( e => console.error(e));
10
     }
15
     }
11
     return(
16
     return(
12
         <div>
17
         <div>

Loading…
Cancel
Save