Commit e13e8aa7 authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

implement clean method in repository

parent d7e0099b
......@@ -23,7 +23,7 @@ public class RegisterTasksCommand extends ConfiguredCommand<ApplicationConfigura
super(COMMAND_NAME, COMMAND_DESCRIPTION);
}
private void initializeTasks(ApplicationConfiguration applicationConfiguration, Injector injector)
private void initializeTasks(ApplicationConfiguration applicationConfiguration, Injector injector) throws ClassNotFoundException
{
TasksRepository tasksRepository = injector.getInstance(TasksRepository.class);
......
package com.telephoners.krakyournet.ctf.repositories;
import org.mongodb.morphia.Datastore;
import javax.inject.Inject;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
/**
* Created by gpietrus on 20.02.2016.
*/
public interface Repository<T> {
List<T> getAll();
void add(T t);
void clean();
public abstract class Repository<T> {
protected Datastore datastore;
@Inject
public Repository(Datastore datastore)
{
this.datastore = datastore;
}
protected Repository()
{
}
abstract List<T> getAll();
abstract void add(T t); //todo: the same for add
public void clean() throws ClassNotFoundException
{
Type type = ((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
datastore.getCollection(Class.forName(type.getTypeName())).drop();
}
}
......@@ -5,7 +5,6 @@ import com.telephoners.krakyournet.ctf.beans.Team;
import com.telephoners.krakyournet.ctf.beans.tasks.Task;
import org.mongodb.morphia.Datastore;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.List;
import java.util.Map;
......@@ -15,14 +14,15 @@ import java.util.function.Predicate;
import java.util.stream.Collectors;
@Singleton
public class SolutionsRepository implements Repository<Solution>
public class SolutionsRepository extends Repository<Solution>
{
private Datastore datastore;
@Inject
public SolutionsRepository(Datastore datastore)
{
this.datastore = datastore;
super(datastore);
}
public SolutionsRepository()
{
}
public List<Solution> getAll()
......@@ -36,12 +36,6 @@ public class SolutionsRepository implements Repository<Solution>
//todo: do not add if already exists
}
@Override
public void clean()
{
datastore.getCollection(Solution.class).drop(); //todo: move common functions to repository abstract?
}
public List<Solution> getByTeam(Team team)
{
//todo: merge with upper
......
......@@ -21,7 +21,7 @@ import java.util.Optional;
import java.util.stream.Collectors;
@Singleton
public class TasksRepository implements Repository<Task>
public class TasksRepository extends Repository<Task>
{
private ApplicationConfiguration applicationConfiguration;
private Datastore datastore;
......@@ -32,6 +32,7 @@ public class TasksRepository implements Repository<Task>
public TasksRepository(ApplicationConfiguration applicationConfiguration, Datastore datastore,
TeamsRepository teamsRepository, SolutionsRepository solutionsRepository)
{
super(datastore);
this.applicationConfiguration = applicationConfiguration;
this.datastore = datastore;
this.teamsRepository = teamsRepository;
......@@ -131,11 +132,6 @@ public class TasksRepository implements Repository<Task>
return false;
}
public void clean()
{
datastore.getCollection(Task.class).drop();
}
//todo: should it be here?
public List<Integer> getCompletedTasks(Team team)
{
......
package com.telephoners.krakyournet.ctf.repositories;
import com.telephoners.krakyournet.ctf.core.ApplicationConfiguration;
import com.telephoners.krakyournet.ctf.beans.Team;
import com.telephoners.krakyournet.ctf.beans.User;
import org.mongodb.morphia.Datastore;
......@@ -12,19 +11,18 @@ import java.util.Optional;
import java.util.UUID;
@Singleton
public class TeamsRepository implements Repository<Team>
public class TeamsRepository extends Repository<Team>
{
private Datastore datastore;
private ApplicationConfiguration applicationConfiguration;
private UsersRepository usersRepository;
@Inject
public TeamsRepository(Datastore datastore, ApplicationConfiguration applicationConfiguration,
public TeamsRepository(Datastore datastore,
UsersRepository usersRepository)
{
super(datastore);
this.datastore = datastore;
this.applicationConfiguration = applicationConfiguration;
this.usersRepository = usersRepository;
}
......
......@@ -13,15 +13,14 @@ import java.security.NoSuchAlgorithmException;
import java.util.List;
@Singleton
public class UsersRepository implements Repository<User>
public class UsersRepository extends Repository<User>
{
private Datastore datastore;
private MessageDigest messageDigest;
@Inject
public UsersRepository(Datastore datastore)
{
this.datastore = datastore;
super(datastore);
try {
messageDigest = MessageDigest.getInstance("MD5"); //todo
} catch (NoSuchAlgorithmException e) {
......
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