diff --git a/src/main/java/de/thm/arsnova/SessionController.java b/src/main/java/de/thm/arsnova/SessionController.java index 57c07d402b25f7f2882b0395be75c315dc93e177..5b7cdb27f909b281d9dc3f648a2439d2381cc335 100644 --- a/src/main/java/de/thm/arsnova/SessionController.java +++ b/src/main/java/de/thm/arsnova/SessionController.java @@ -18,8 +18,6 @@ */ package de.thm.arsnova; -import java.util.List; - import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; @@ -30,6 +28,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import de.thm.arsnova.entities.Feedback; import de.thm.arsnova.entities.Session; import de.thm.arsnova.services.ISessionService; @@ -49,8 +48,8 @@ public class SessionController { } @RequestMapping(value="/session/{sessionkey}/feedback", method=RequestMethod.GET) - public List<Integer> getFeedback(@PathVariable String sessionkey, HttpServletResponse response) { - List<Integer> feedback = sessionService.getFeedback(sessionkey); + public Feedback getFeedback(@PathVariable String sessionkey, HttpServletResponse response) { + Feedback feedback = sessionService.getFeedback(sessionkey); if (feedback != null) return feedback; response.setStatus(HttpStatus.NOT_FOUND.value()); @@ -58,11 +57,7 @@ public class SessionController { } @RequestMapping(value="/session/{sessionkey}/feedback", method=RequestMethod.POST) - public List<Integer> postFeedback(@PathVariable String sessionkey, @RequestBody int value, HttpServletResponse response) { - List<Integer> feedback = sessionService.getFeedback(sessionkey); - if (feedback != null) return feedback; - - response.setStatus(HttpStatus.NOT_FOUND.value()); - return null; + public boolean postFeedback(@PathVariable String sessionkey, @RequestBody int value, HttpServletResponse response) { + return false; } } diff --git a/src/main/java/de/thm/arsnova/entities/Feedback.java b/src/main/java/de/thm/arsnova/entities/Feedback.java new file mode 100644 index 0000000000000000000000000000000000000000..46870c33c968c5099357c1fa8336165e9886b586 --- /dev/null +++ b/src/main/java/de/thm/arsnova/entities/Feedback.java @@ -0,0 +1,20 @@ +package de.thm.arsnova.entities; + +import java.util.ArrayList; +import java.util.List; + +public class Feedback { + private List<Integer> values; + + public Feedback(int a, int b, int c, int d) { + values = new ArrayList<Integer>(); + values.add(a); + values.add(b); + values.add(c); + values.add(d); + } + + public List<Integer> getValues() { + return values; + } +} diff --git a/src/main/java/de/thm/arsnova/services/ISessionService.java b/src/main/java/de/thm/arsnova/services/ISessionService.java index 949ebee34a2354671619568ee2d919d0950844af..b14242b55ff657cb133ca7c56ee86d138da6514f 100644 --- a/src/main/java/de/thm/arsnova/services/ISessionService.java +++ b/src/main/java/de/thm/arsnova/services/ISessionService.java @@ -18,15 +18,14 @@ */ package de.thm.arsnova.services; -import java.util.List; - +import de.thm.arsnova.entities.Feedback; import de.thm.arsnova.entities.Session; public interface ISessionService { public Session getSession(String keyword); - public List<Integer> getFeedback(String keyword); + public Feedback getFeedback(String keyword); public void postFeedback(String keyword, int value); } \ No newline at end of file diff --git a/src/main/java/de/thm/arsnova/services/SessionService.java b/src/main/java/de/thm/arsnova/services/SessionService.java index 962a923e39230e5240b9580b2eb59a674fd6a599..eaa6f8767c727dc25d865e0e65d75115d2d34439 100644 --- a/src/main/java/de/thm/arsnova/services/SessionService.java +++ b/src/main/java/de/thm/arsnova/services/SessionService.java @@ -32,62 +32,109 @@ import com.fourspaces.couchdb.Database; import com.fourspaces.couchdb.View; import com.fourspaces.couchdb.ViewResults; +import de.thm.arsnova.entities.Feedback; import de.thm.arsnova.entities.Session; @Service public class SessionService implements ISessionService { - private final com.fourspaces.couchdb.Session session = new com.fourspaces.couchdb.Session("localhost", 5984); + private final com.fourspaces.couchdb.Session session = new com.fourspaces.couchdb.Session( + "localhost", 5984); private final Database database = session.getDatabase("arsnova"); - - public static final Logger logger = LoggerFactory.getLogger(SessionService.class); - + + public static final Logger logger = LoggerFactory + .getLogger(SessionService.class); + @Override public Session getSession(String keyword) { View view = new View("session/by_keyword"); - view.setKey(URLEncoder.encode("\""+keyword+"\"")); + view.setKey(URLEncoder.encode("\"" + keyword + "\"")); ViewResults results = database.view(view); - if (results.getJSONArray("rows").optJSONObject(0) == null) return null; - - Session result = (Session)JSONObject.toBean(results.getJSONArray("rows").optJSONObject(0).optJSONObject("value"), Session.class); - - if (result.isActive()) return result; - + if (results.getJSONArray("rows").optJSONObject(0) == null) + return null; + + Session result = (Session) JSONObject.toBean( + results.getJSONArray("rows").optJSONObject(0) + .optJSONObject("value"), Session.class); + + if (result.isActive()) + return result; + return null; } @Override - public List<Integer> getFeedback(String keyword) { + public Feedback getFeedback(String keyword) { String sessionId = this.getSessionId(keyword); - if (sessionId == null) return null; - + if (sessionId == null) + return null; + + logger.info("Time: {}", this.currentTimestamp()); + View view = new View("understanding/by_session"); view.setGroup(true); - view.setStartKey(URLEncoder.encode("[\""+sessionId+"\"]")); - view.setEndKey(URLEncoder.encode("[\""+sessionId+"\"],{}")); + view.setStartKey(URLEncoder.encode("[\"" + sessionId + "\"]")); + view.setEndKey(URLEncoder.encode("[\"" + sessionId + "\",{}]")); ViewResults results = database.view(view); + + logger.info("Feedback: {}", results.getJSONArray("rows")); + + int values[] = { 0, 0, 0, 0 }; + List<Integer> result = new ArrayList<Integer>(); - //if (results.getJSONArray("rows").optJSONObject(0) == null) return null; - - logger.info("Feedback: {}", results.getJSONObject("rows")); - - return null; + try { + for (int i = 0; i <= 3; i++) { + String key = results.getJSONArray("rows").optJSONObject(i) + .optJSONArray("key").getString(1); + if (key.equals("Bitte schneller")) + values[0] = results.getJSONArray("rows").optJSONObject(i) + .getInt("value"); + if (key.equals("Kann folgen")) + values[1] = results.getJSONArray("rows").optJSONObject(i) + .getInt("value"); + if (key.equals("Zu schnell")) + values[2] = results.getJSONArray("rows").optJSONObject(i) + .getInt("value"); + if (key.equals("Nicht mehr dabei")) + values[3] = results.getJSONArray("rows").optJSONObject(i) + .getInt("value"); + } + } catch (Exception e) { + return new Feedback( + values[0], + values[1], + values[2], + values[3] + ); + } + + return new Feedback( + values[0], + values[1], + values[2], + values[3] + ); } - + @Override public void postFeedback(String keyword, int value) { - + } - + private String getSessionId(String keyword) { View view = new View("session/by_keyword"); - view.setKey(URLEncoder.encode("\""+keyword+"\"")); + view.setKey(URLEncoder.encode("\"" + keyword + "\"")); ViewResults results = database.view(view); - if (results.getJSONArray("rows").optJSONObject(0) == null) return null; - - return results.getJSONArray("rows").optJSONObject(0).optJSONObject("value").getString("_id"); + if (results.getJSONArray("rows").optJSONObject(0) == null) + return null; + + return results.getJSONArray("rows").optJSONObject(0) + .optJSONObject("value").getString("_id"); + } + + private String currentTimestamp() { + return Long.toString(System.currentTimeMillis()); } - }