Commit 1ea08633 authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

first try to cooperate with mongolink

parent 1bd26df1
import api.ExampleResource;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import database.MongoDatabaseConnector;
import io.dropwizard.Application;
import io.dropwizard.setup.Environment;
import objects.Fruit;
import org.bson.Document;
import org.mongolink.MongoSession;
import repositories.MongoRepositories;
import repositories.Repositories;
import stub.MongoConfiguration;
/**
* Created by gpietrus on 16.02.16.
......@@ -21,6 +25,12 @@ public class CTFApplication extends Application<ApplicationConfiguration>
public static void main(String[] args) throws Exception
{
new CTFApplication().run(args);
MongoSession session = MongoConfiguration.createSession();
session.start();
Repositories.initialise(new MongoRepositories(session));
Repositories.fruits().add(new Fruit("apple"));
session.stop();
//new CTFApplication().run(args);
}
}
package objects;
import org.joda.time.DateTime;
import java.util.UUID;
public class Fruit {
@SuppressWarnings("UnusedDeclaration")
protected Fruit() {
// for mongolink
}
public Fruit(String name) {
this.name = name;
this.id = UUID.randomUUID();
}
public UUID getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public DateTime getCreationDate() {
return creationDate;
}
private UUID id;
private String name;
private DateTime creationDate = new DateTime();
}
/*
* MongoLink, Object Document Mapper for Java and MongoDB
*
* Copyright (c) 2012, Arpinum or third-party contributors as
* indicated by the @author tags
*
* MongoLink is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MongoLink 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
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with MongoLink. If not, see <http://www.gnu.org/licenses/>.
*
*/
package repositories;
import objects.Fruit;
import org.mongolink.MongoSession;
public class FruitMongoRepository extends MongoRepository<Fruit> implements FruitRepository {
public FruitMongoRepository(MongoSession mongoSession) {
super(mongoSession);
}
}
package repositories;
import objects.Fruit;
public interface FruitRepository extends Repository<Fruit> {
}
\ No newline at end of file
/*
* MongoLink, Object Document Mapper for Java and MongoDB
*
* Copyright (c) 2012, Arpinum or third-party contributors as
* indicated by the @author tags
*
* MongoLink is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MongoLink 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
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with MongoLink. If not, see <http://www.gnu.org/licenses/>.
*
*/
package repositories;
import org.mongolink.MongoSession;
public class MongoRepositories extends Repositories {
public MongoRepositories(MongoSession session) {
this.session = session;
}
@Override
protected FruitRepository fruitsRepository() {
return new FruitMongoRepository(session);
}
private MongoSession session;
}
/*
* MongoLink, Object Document Mapper for Java and MongoDB
*
* Copyright (c) 2012, Arpinum or third-party contributors as
* indicated by the @author tags
*
* MongoLink is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MongoLink 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
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with MongoLink. If not, see <http://www.gnu.org/licenses/>.
*
*/
package repositories;
import org.mongolink.MongoSession;
import java.lang.reflect.ParameterizedType;
import java.util.List;
public abstract class MongoRepository<T> implements Repository<T> {
protected MongoRepository(MongoSession session) {
this.session = session;
}
@Override
public T get(Object id) {
return session.get(id, persistentType());
}
@Override
public void delete(T entity) {
session.delete(entity);
}
@Override
public void add(T entity) {
session.save(entity);
}
@Override
public List<T> all() {
return session.getAll(persistentType());
}
protected final Class<T> persistentType() {
final ParameterizedType superclass = (ParameterizedType) getClass().getGenericSuperclass();
return (Class<T>) superclass.getActualTypeArguments()[0];
}
protected final MongoSession session;
}
package repositories;
public abstract class Repositories {
public static void initialise(Repositories instance) {
Repositories.instance = instance;
}
public static FruitRepository fruits() {
return instance.fruitsRepository();
}
protected abstract FruitRepository fruitsRepository();
private static Repositories instance;
}
package repositories;
/**
* Created by gpietrus on 16.02.16.
*/
public class Repository
{
import java.util.List;
public interface Repository<T> {
T get(Object id);
void delete(T entité);
void add(T entité);
List<T> all();
}
/*
* MongoLink, Object Document Mapper for Java and MongoDB
*
* Copyright (c) 2012, Arpinum or third-party contributors as
* indicated by the @author tags
*
* MongoLink is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MongoLink 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
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with MongoLink. If not, see <http://www.gnu.org/licenses/>.
*
*/
package stub;
import org.mongolink.MongoSession;
import org.mongolink.MongoSessionManager;
import org.mongolink.Settings;
import org.mongolink.domain.mapper.ContextBuilder;
public class MongoConfiguration
{
public static void stop() {
Singleton.INSTANCE.mongoSessionManager.close();
}
public static MongoSession createSession() {
return Singleton.INSTANCE.mongoSessionManager.createSession();
}
private enum Singleton {
INSTANCE;
private Singleton() {
ContextBuilder builder = new ContextBuilder("org.mongolink.example.persistence.mapping");
mongoSessionManager = MongoSessionManager.create(builder, new Properties().addSettings(Settings.defaultInstance()));
}
private final MongoSessionManager mongoSessionManager;
}
}
package stub;
import org.mongolink.Settings;
import java.io.InputStream;
public class Properties
{
public Settings addSettings(Settings settings) {
return settings.withHost(getDBHost()).withPort(getDBPort()).
withDbName(getDBName()).
withAuthentication(getDBUser(), getDBPassword());
}
public String getDBHost() {
return getProperty("db.host");
}
public String getDBName() {
return getProperty("db.name");
}
public int getDBPort() {
return Integer.valueOf(getProperty("db.port"));
}
public String getDBUser() {
return getProperty("db.user");
}
public String getDBPassword() {
return getProperty("db.password");
}
private String getProperty(String nom) {
return Config.INSTANCE.properties.getProperty(nom);
}
private static enum Config {
INSTANCE;
private Config() {
InputStream stream = getClass().getClassLoader().getResourceAsStream("conf.properties");
properties = new java.util.Properties();
try {
properties.load(stream);
stream.close();
} catch (Exception e) {
//
}
}
private final java.util.Properties properties;
}
}
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