Commit 5aa0968b authored by Daniel Gerhardt's avatar Daniel Gerhardt

Remove obsolete CouchDB4j code

parent 245f281f
......@@ -185,11 +185,6 @@
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>de.thm.couchdb4j</groupId>
<artifactId>couchdb4j</artifactId>
<version>0.8-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.ektorp</groupId>
<artifactId>org.ektorp</artifactId>
......
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2017 The ARSnova Team
*
* ARSnova Backend 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 Backend 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.dao;
import com.fourspaces.couchdb.Database;
import com.fourspaces.couchdb.Document;
import com.fourspaces.couchdb.View;
import com.fourspaces.couchdb.ViewResults;
import de.thm.arsnova.connector.model.Course;
import de.thm.arsnova.entities.*;
import de.thm.arsnova.persistance.ContentRepository;
import de.thm.arsnova.persistance.LogEntryRepository;
import de.thm.arsnova.persistance.MotdRepository;
import de.thm.arsnova.persistance.SessionRepository;
import de.thm.arsnova.services.ISessionService;
import net.sf.json.JSONObject;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Database implementation based on CouchDB.
*
* Note to developers:
*
* This class makes use of Spring Framework's caching annotations. When you are about to add new functionality,
* you should also think about the possibility of caching. Ideally, your methods should be dependent on domain
* objects like Session or Content, which can be used as cache keys. Relying on plain String objects as a key, e.g.
* by passing only a Session's keyword, will make your cache annotations less readable. You will also need to think
* about cases where your cache needs to be updated and evicted.
*
* In order to use cached methods from within this object, you have to use the getDatabaseDao() method instead of
* using the "this" pointer. This is because caching is only available if the method is called through a Spring Proxy,
* which is not the case when using "this".
*
* @see <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html">Spring Framework's Cache Abstraction</a>
* @see <a href="https://github.com/thm-projects/arsnova-backend/wiki/Caching">Caching in ARSnova explained</a>
*/
@Profile("!test")
@Service("databaseDao")
public class CouchDBDao implements IDatabaseDao {
@Autowired
private ISessionService sessionService;
@Autowired
private LogEntryRepository dbLogger;
@Autowired
private SessionRepository sessionRepository;
@Autowired
private MotdRepository motdRepository;
@Autowired
private ContentRepository contentRepository;
private String databaseHost;
private int databasePort;
private String databaseName;
private Database database;
private static final Logger logger = LoggerFactory.getLogger(CouchDBDao.class);
@Value("${couchdb.host}")
public void setDatabaseHost(final String newDatabaseHost) {
databaseHost = newDatabaseHost;
}
@Value("${couchdb.port}")
public void setDatabasePort(final String newDatabasePort) {
databasePort = Integer.parseInt(newDatabasePort);
}
@Value("${couchdb.name}")
public void setDatabaseName(final String newDatabaseName) {
databaseName = newDatabaseName;
}
public void setSessionService(final ISessionService service) {
sessionService = service;
}
/**
* <strike>
* Allows access to the proxy object. It has to be used instead of <code>this</code> for local calls to public
* methods for caching purposes. This is an ugly but necessary temporary workaround until a better solution is
* implemented (e.g. use of AspectJ's weaving).
* @return the proxy for CouchDBDao
* </strike>
*/
private @NonNull IDatabaseDao getDatabaseDao() {
return this;
}
private Database getDatabase() {
if (database == null) {
try {
final com.fourspaces.couchdb.Session session = new com.fourspaces.couchdb.Session(
databaseHost,
databasePort
);
database = session.getDatabase(databaseName);
} catch (final Exception e) {
logger.error("Cannot connect to CouchDB database '{}' on host '{}' using port {}.",
databaseName, databaseHost, databasePort, e);
}
}
return database;
}
private void deleteDocument(final String documentId) throws IOException {
final Document d = getDatabase().getDocument(documentId);
getDatabase().deleteDocument(d);
}
@SuppressWarnings("unchecked")
@Override
public <T> T getObjectFromId(final String documentId, final Class<T> klass) {
try {
final Document doc = getDatabase().getDocument(documentId);
if (doc == null) {
return null;
}
// TODO: This needs some more error checking...
return (T) JSONObject.toBean(doc.getJSONObject(), klass);
} catch (ClassCastException | IOException | net.sf.json.JSONException e) {
return null;
}
}
private boolean isEmptyResults(final ViewResults results) {
return results == null || results.getResults().isEmpty() || results.getJSONArray("rows").isEmpty();
}
/**
* Adds convenience methods to CouchDB4J's view class.
*/
private static class ExtendedView extends View {
ExtendedView(final String fullname) {
super(fullname);
}
void setCourseIdKeys(final List<Course> courses) {
List<String> courseIds = new ArrayList<>();
for (Course c : courses) {
courseIds.add(c.getId());
}
setKeys(courseIds);
}
void setSessionIdKeys(final List<Session> sessions) {
List<String> sessionIds = new ArrayList<>();
for (Session s : sessions) {
sessionIds.add(s.getId());
}
setKeys(sessionIds);
}
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2017 The ARSnova Team
*
* ARSnova Backend 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 Backend 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.dao;
/**
* All methods the database must support.
*/
public interface IDatabaseDao {
<T> T getObjectFromId(String documentId, Class<T> klass);
}
/**
* Classes and interfaces for accessing the database
*/
package de.thm.arsnova.dao;
......@@ -17,7 +17,6 @@
*/
package de.thm.arsnova.security;
import de.thm.arsnova.dao.IDatabaseDao;
import de.thm.arsnova.entities.Comment;
import de.thm.arsnova.entities.Content;
import de.thm.arsnova.entities.Session;
......@@ -47,9 +46,6 @@ public class ApplicationPermissionEvaluator implements PermissionEvaluator {
@Value("${security.admin-accounts}")
private String[] adminAccounts;
@Autowired
private IDatabaseDao dao;
@Autowired
private SessionRepository sessionRepository;
......
......@@ -18,7 +18,6 @@
package de.thm.arsnova.services;
import de.thm.arsnova.ImageUtils;
import de.thm.arsnova.dao.IDatabaseDao;
import de.thm.arsnova.entities.Answer;
import de.thm.arsnova.entities.Comment;
import de.thm.arsnova.entities.CommentReadingCount;
......@@ -56,10 +55,6 @@ import java.util.TimerTask;
*/
@Service
public class ContentService implements IContentService, ApplicationEventPublisherAware {
@Autowired
private IDatabaseDao databaseDao;
@Autowired
private IUserService userService;
......@@ -87,10 +82,6 @@ public class ContentService implements IContentService, ApplicationEventPublishe
private HashMap<String, Timer> timerList = new HashMap<>();
public void setDatabaseDao(final IDatabaseDao databaseDao) {
this.databaseDao = databaseDao;
}
@Override
@PreAuthorize("isAuthenticated()")
public List<Content> getSkillQuestions(final String sessionkey) {
......
......@@ -18,7 +18,6 @@
package de.thm.arsnova.services;
import de.thm.arsnova.FeedbackStorage;
import de.thm.arsnova.dao.IDatabaseDao;
import de.thm.arsnova.entities.Feedback;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.User;
......@@ -56,9 +55,6 @@ public class FeedbackService implements IFeedbackService, ApplicationEventPublis
@Value("${feedback.cleanup}")
private int cleanupFeedbackDelay;
@Autowired
private IDatabaseDao databaseDao;
@Autowired
private SessionRepository sessionRepository;
......@@ -66,10 +62,6 @@ public class FeedbackService implements IFeedbackService, ApplicationEventPublis
private ApplicationEventPublisher publisher;
public void setDatabaseDao(final IDatabaseDao newDatabaseDao) {
databaseDao = newDatabaseDao;
}
@PostConstruct
public void init() {
feedbackStorage = new FeedbackStorage();
......
......@@ -18,7 +18,6 @@
package de.thm.arsnova.services;
import com.codahale.metrics.annotation.Gauge;
import de.thm.arsnova.dao.IDatabaseDao;
import de.thm.arsnova.entities.DbUser;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.exceptions.BadRequestException;
......@@ -99,9 +98,6 @@ public class UserService implements IUserService {
@Autowired
private UserRepository userRepository;
@Autowired
private IDatabaseDao databaseDao;
@Autowired
private JavaMailSender mailSender;
......
package de.thm.arsnova.config;
import de.thm.arsnova.dao.IDatabaseDao;
import de.thm.arsnova.dao.StubDatabaseDao;
import de.thm.arsnova.services.StubUserService;
import de.thm.arsnova.socket.ARSnovaSocket;
......@@ -61,7 +60,7 @@ public class TestAppConfig {
}
@Bean
public IDatabaseDao databaseDao() {
public StubDatabaseDao databaseDao() {
return new StubDatabaseDao();
}
......
......@@ -28,7 +28,7 @@ import java.util.concurrent.ConcurrentHashMap;
@Profile("test")
@Service("databaseDao")
public class StubDatabaseDao implements IDatabaseDao {
public class StubDatabaseDao {
private static Map<String, Session> stubSessions = new ConcurrentHashMap<>();
private static Map<String, Feedback> stubFeedbacks = new ConcurrentHashMap<>();
......@@ -94,10 +94,4 @@ public class StubDatabaseDao implements IDatabaseDao {
contents.add(new Content());
stubQuestions.put("12345678", contents);
}
@Override
public <T> T getObjectFromId(String documentId, Class<T> klass) {
// TODO Auto-generated method stub
return null;
}
}
......@@ -20,13 +20,11 @@ package de.thm.arsnova.services;
import de.thm.arsnova.config.AppConfig;
import de.thm.arsnova.config.TestAppConfig;
import de.thm.arsnova.config.TestSecurityConfig;
import de.thm.arsnova.dao.IDatabaseDao;
import de.thm.arsnova.dao.StubDatabaseDao;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.exceptions.NotFoundException;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.framework.Advised;
......@@ -41,15 +39,12 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.util.ReflectionTestUtils;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment