Commit 3b8692d4 authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

cleanup

parent 9e7e81da
package database;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
/**
* Created by gpietrus on 16.02.16.
*/
//todo: extends DatabaseConnector
public class MongoDatabaseConnector
{
private MongoDatabase mongoDatabase;
public MongoDatabaseConnector()
{
//todo: move to configuration
MongoClient mongo = new MongoClient("localhost", 27017);
//todo: secure mode?
//todo: http://www.mkyong.com/mongodb/java-mongodb-hello-world-example/
this.mongoDatabase = mongo.getDatabase("database name");
}
public FindIterable<Document> getUsers() {
//todo: create user as document extension
MongoCollection<Document> users = mongoDatabase.getCollection("users");//todo; move "users" to configuration
FindIterable<Document> documents = users.find();
return documents;
}
}
package mappings;
import objects.User;
import org.mongolink.domain.mapper.AggregateMap;
/**
* Created by gpietrus on 16.02.16.
*/
public class UserMapping extends AggregateMap<User>
{
@Override
public void map() {
//todo: do not use depracted methods
id(element().getId()).natural();
property(element().getUsername());
property(element().getPassword());
// subclass(new SubclassMap<Banana>(Banana.class) {
//
// @Override
// protected void map() {
//
// }
// });
}
}
//todo: read https://github.com/MongoLink/mongolink-example/blob/master/src/main/java/org/mongolink/example/persistence/mapping/FruitMapping.java
\ 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 objects;
public class Banana extends Fruit {
protected Banana() {
// for mongolink
}
public Banana(String name) {
super(name);
}
}
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 persistence;
import objects.Fruit;
import org.mongolink.MongoSession;
import repositories.FruitRepository;
public class FruitMongoRepository extends MongoRepository<Fruit> implements FruitRepository
{
public FruitMongoRepository(MongoSession mongoSession) {
super(mongoSession);
}
}
/*
* 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 persistence;
import org.mongolink.MongoSession;
import persistence.FruitMongoRepository;
import repositories.FruitRepository;
import repositories.Repositories;
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 persistence;
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 persistence;
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 persistence.mapping;
import objects.Banana;
import objects.Fruit;
import org.mongolink.domain.mapper.AggregateMap;
import org.mongolink.domain.mapper.SubclassMap;
@SuppressWarnings("UnusedDeclaration")
public class FruitMapping extends AggregateMap<Fruit>
{
@Override
public void map() {
id().onProperty(element().getId()).natural();
property().onField("name");
property().onProperty(element().getCreationDate());
subclass(new SubclassMap<Banana>() {
@Override
public void map() {
}
});
}
}
package repositories;
import objects.Fruit;
import persistence.Repository;
public interface FruitRepository extends Repository<Fruit>
{
}
\ No newline at end of file
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 TasksRepository
{
}
package repositories;
/**
* Created by gpietrus on 16.02.16.
*/
public class UsersRepository
{
}
/*
* 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("persistence.mapping");
Settings settings1 = Settings.defaultInstance();
Settings settings = new Properties().addSettings(settings1);
mongoSessionManager = MongoSessionManager.create(builder, settings);
}
private final MongoSessionManager mongoSessionManager;
}
}
package stub;
import com.google.common.collect.Lists;
import com.mongodb.ServerAddress;
import org.mongolink.MongoSession;
import org.mongolink.MongoSessionManager;
import org.mongolink.Settings;
import org.mongolink.UpdateStrategies;
import org.mongolink.domain.mapper.ContextBuilder;
/**
* Created by gpietrus on 16.02.16.
*/
public class MongoSth
{
public void run() {
ContextBuilder builder = new ContextBuilder("org.mongolink.example.persistence.mapping");
Settings settings = Settings.defaultInstance()
.withDefaultUpdateStrategy(UpdateStrategies.DIFF)
.withDbName("db")
.withAddresses(Lists.newArrayList(new ServerAddress("localhost", 27017)))
.withAuthentication("user", "passwd");
MongoSessionManager mongoSessionManager = MongoSessionManager.create(builder, settings);
MongoSession session = mongoSessionManager.createSession();
session.start();
// do stuff
session.stop();
}
}
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