diff --git a/src/main/java/de/thm/arsnova/SessionController.java b/src/main/java/de/thm/arsnova/SessionController.java index 497fbfc8b00eff815a46b1346bccdf546ec58f94..57572481fdfdc0b382c7898ca45067d405b85cbd 100644 --- a/src/main/java/de/thm/arsnova/SessionController.java +++ b/src/main/java/de/thm/arsnova/SessionController.java @@ -47,6 +47,18 @@ public class SessionController { return null; } + @RequestMapping(value="/session", method=RequestMethod.POST) + public Session postNewSession(@RequestBody Session session, HttpServletResponse response) { + Session newSession = sessionService.saveSession(session); + if (session != null) { + response.setStatus(HttpStatus.CREATED.value()); + return newSession; + } + + response.setStatus(HttpStatus.SERVICE_UNAVAILABLE.value()); + return null; + } + @RequestMapping(value="/session/{sessionkey}/feedback", method=RequestMethod.GET) public Feedback getFeedback(@PathVariable String sessionkey, HttpServletResponse response) { Feedback feedback = sessionService.getFeedback(sessionkey); diff --git a/src/main/java/de/thm/arsnova/services/ISessionService.java b/src/main/java/de/thm/arsnova/services/ISessionService.java index 9f3bb3942980e6678bbd78d2a7756bb01e8959d3..bc5a680eec7d2d673985753600d7848edb4aa234 100644 --- a/src/main/java/de/thm/arsnova/services/ISessionService.java +++ b/src/main/java/de/thm/arsnova/services/ISessionService.java @@ -25,7 +25,10 @@ import de.thm.arsnova.entities.Session; public interface ISessionService { public Session getSession(String keyword); + public Session saveSession(Session session); public Feedback getFeedback(String keyword); public boolean postFeedback(String keyword, int value); + public boolean sessionKeyAvailable(String keyword); + public String generateKeyword(); } \ 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 23969d7105200dc095e28ff05d9d33c6bcf3c771..ff7897917c5430132a43448800ba92b11643dbf4 100644 --- a/src/main/java/de/thm/arsnova/services/SessionService.java +++ b/src/main/java/de/thm/arsnova/services/SessionService.java @@ -31,6 +31,8 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.User; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Isolation; +import org.springframework.transaction.annotation.Transactional; import com.fourspaces.couchdb.Database; import com.fourspaces.couchdb.Document; @@ -69,6 +71,25 @@ public class SessionService implements ISessionService { return null; } + + @Override + public Session saveSession(Session session) { + + Document sessionDocument = new Document(); + sessionDocument.put("type","session"); + sessionDocument.put("name", session.getName()); + sessionDocument.put("shortName", session.getShortName()); + sessionDocument.put("keyword", this.generateKeyword()); + sessionDocument.put("creator", this.actualUserName()); + sessionDocument.put("active", true); + try { + database.saveDocument(sessionDocument); + } catch (IOException e) { + return null; + } + + return this.getSession(sessionDocument.getString("keyword")); + } @Override public Feedback getFeedback(String keyword) { @@ -160,6 +181,15 @@ public class SessionService implements ISessionService { return true; } + @Override + @Transactional(isolation=Isolation.READ_COMMITTED) + public boolean sessionKeyAvailable(String keyword) { + View view = new View("session/by_keyword"); + ViewResults results = database.view(view); + + return ! results.containsKey(keyword); + } + private String getSessionId(String keyword) { View view = new View("session/by_keyword"); view.setKey(URLEncoder.encode("\"" + keyword + "\"")); @@ -184,4 +214,14 @@ public class SessionService implements ISessionService { } catch (ClassCastException e) {} return null; } + + @Override + public String generateKeyword() { + final int low = 10000000; + final int high = 100000000; + String keyword = String.valueOf((int)(Math.random() * (high - low) + low)); + + if (this.sessionKeyAvailable(keyword)) return keyword; + return generateKeyword(); + } } diff --git a/src/test/java/de/thm/arsnova/services/SessionServiceTest.java b/src/test/java/de/thm/arsnova/services/SessionServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9bf9798332842bdd8e90a416b38367261c5c2e19 --- /dev/null +++ b/src/test/java/de/thm/arsnova/services/SessionServiceTest.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2012 THM webMedia + * + * This file is part of ARSnova. + * + * ARSnova is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ARSnova is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package de.thm.arsnova.services; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class SessionServiceTest { + + @Test + public void shouldGenerateSessionKeyword() { + SessionService session = new SessionService(); + System.out.println(session.generateKeyword()); + assertTrue(session.generateKeyword().matches("^[0-9]{8}$")); + } +} \ No newline at end of file