mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-09-30 09:26:44 +00:00
refactoring of DomainObjectFactories to a more service oriented version (SwissTimingAdapeterPersistence) V2
This commit is contained in:
-55
@@ -1,55 +0,0 @@
|
||||
package com.sap.sailing.domain.swisstimingadapter.persistence;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.sap.sailing.domain.swisstimingadapter.Race;
|
||||
import com.sap.sailing.domain.swisstimingadapter.RaceSpecificMessageLoader;
|
||||
import com.sap.sailing.domain.swisstimingadapter.SailMasterMessage;
|
||||
import com.sap.sailing.domain.swisstimingadapter.SwissTimingConfiguration;
|
||||
import com.sap.sailing.domain.swisstimingadapter.SwissTimingFactory;
|
||||
import com.sap.sailing.domain.swisstimingadapter.persistence.impl.DomainObjectFactoryImpl;
|
||||
import com.sap.sailing.mongodb.Activator;
|
||||
|
||||
/**
|
||||
* Offers methods to load domain objects from a Mongo DB
|
||||
*
|
||||
* @author Axel Uhl (d043530)
|
||||
*
|
||||
*/
|
||||
public interface DomainObjectFactory extends RaceSpecificMessageLoader {
|
||||
DomainObjectFactory INSTANCE = new DomainObjectFactoryImpl(Activator.getDefaultInstance().getDB(), SwissTimingFactory.INSTANCE);
|
||||
|
||||
Iterable<SwissTimingConfiguration> getSwissTimingConfigurations();
|
||||
|
||||
/**
|
||||
* Loads all messages received and stored to the DB starting with sequence number <code>firstSequenceNumber</code>.
|
||||
* If <code>firstSequenceNumber</code> is -1, all messages are loaded.
|
||||
*
|
||||
* @return messages in ascending sequence number order; always a valid but perhaps empty list
|
||||
*/
|
||||
List<SailMasterMessage> loadMessages(int firstSequenceNumber);
|
||||
|
||||
/**
|
||||
* Loads all messages received and stored to the DB for a specific race.
|
||||
* @return messages of the specified race; null if the race does not exist
|
||||
*/
|
||||
List<SailMasterMessage> loadRaceMessages(String raceID);
|
||||
|
||||
/**
|
||||
* Loads all command messages (not race specific) received and stored to the DB.
|
||||
* @return command messages; always a valid but perhaps empty list
|
||||
*/
|
||||
List<SailMasterMessage> loadCommandMessages();
|
||||
|
||||
/**
|
||||
* Gets all races stored in the DB
|
||||
* @return the list of races
|
||||
*/
|
||||
Iterable<Race> getRaces();
|
||||
|
||||
/**
|
||||
* Gets the race masterdata for a given raceID
|
||||
* @return the race or null if the race does not exist
|
||||
*/
|
||||
Race getRace(String raceID);
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.sap.sailing.domain.swisstimingadapter.persistence;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
import com.sap.sailing.domain.swisstimingadapter.Race;
|
||||
import com.sap.sailing.domain.swisstimingadapter.SailMasterMessage;
|
||||
import com.sap.sailing.domain.swisstimingadapter.SwissTimingConfiguration;
|
||||
import com.sap.sailing.domain.swisstimingadapter.persistence.impl.MongoObjectFactoryImpl;
|
||||
import com.sap.sailing.mongodb.Activator;
|
||||
|
||||
/**
|
||||
* Offers methods to construct {@link DBObject MongoDB objects} from domain objects.
|
||||
*
|
||||
* @author Axel Uhl (d043530)
|
||||
*
|
||||
*/
|
||||
public interface MongoObjectFactory {
|
||||
MongoObjectFactory INSTANCE = new MongoObjectFactoryImpl(Activator.getDefaultInstance().getDB());
|
||||
|
||||
void storeSwissTimingConfiguration(SwissTimingConfiguration swissTimingConfiguration);
|
||||
|
||||
void storeRawSailMasterMessage(SailMasterMessage message);
|
||||
|
||||
void storeSailMasterMessage(SailMasterMessage message);
|
||||
|
||||
void storeRace(Race race);
|
||||
}
|
||||
-141
@@ -1,141 +0,0 @@
|
||||
package com.sap.sailing.domain.swisstimingadapter.persistence.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DB;
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBCursor;
|
||||
import com.mongodb.DBObject;
|
||||
import com.sap.sailing.domain.base.impl.MillisecondsTimePoint;
|
||||
import com.sap.sailing.domain.swisstimingadapter.Race;
|
||||
import com.sap.sailing.domain.swisstimingadapter.SailMasterMessage;
|
||||
import com.sap.sailing.domain.swisstimingadapter.SwissTimingConfiguration;
|
||||
import com.sap.sailing.domain.swisstimingadapter.SwissTimingFactory;
|
||||
import com.sap.sailing.domain.swisstimingadapter.persistence.DomainObjectFactory;
|
||||
|
||||
public class DomainObjectFactoryImpl implements DomainObjectFactory {
|
||||
private static final Logger logger = Logger.getLogger(DomainObjectFactoryImpl.class.getName());
|
||||
|
||||
private final DB database;
|
||||
|
||||
private final SwissTimingFactory swissTimingFactory;
|
||||
|
||||
public DomainObjectFactoryImpl(DB db, SwissTimingFactory swissTimingFactory) {
|
||||
super();
|
||||
this.database = db;
|
||||
this.swissTimingFactory = swissTimingFactory;
|
||||
DBCollection rawMessages = database.getCollection(CollectionNames.RAW_MESSAGES.name());
|
||||
rawMessages.ensureIndex(new BasicDBObject().append(FieldNames.MESSAGE_SEQUENCE_NUMBER.name(), 1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<SwissTimingConfiguration> getSwissTimingConfigurations() {
|
||||
List<SwissTimingConfiguration> result = new ArrayList<SwissTimingConfiguration>();
|
||||
try {
|
||||
DBCollection stConfigs = database.getCollection(CollectionNames.SWISSTIMING_CONFIGURATIONS.name());
|
||||
for (DBObject o : stConfigs.find()) {
|
||||
SwissTimingConfiguration stConfig = loadSwissTimingConfiguration(o);
|
||||
result.add(stConfig);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
// something went wrong during DB access; report, then use empty new wind track
|
||||
logger.log(Level.SEVERE, "Error connecting to MongoDB, unable to load recorded TracTrac configurations. Check MongoDB settings.");
|
||||
logger.throwing(DomainObjectFactoryImpl.class.getName(), "getTracTracConfigurations", t);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private SwissTimingConfiguration loadSwissTimingConfiguration(DBObject object) {
|
||||
return swissTimingFactory.createSwissTimingConfiguration((String) object.get(FieldNames.ST_CONFIG_NAME.name()),
|
||||
(String) object.get(FieldNames.ST_CONFIG_HOSTNAME.name()),
|
||||
(Integer) object.get(FieldNames.ST_CONFIG_PORT.name()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SailMasterMessage> loadMessages(int firstSequenceNumber) {
|
||||
DBCollection rawMessages = database.getCollection(CollectionNames.RAW_MESSAGES.name());
|
||||
BasicDBObject query = new BasicDBObject();
|
||||
if (firstSequenceNumber != -1) {
|
||||
query.append(FieldNames.MESSAGE_SEQUENCE_NUMBER.name(), new BasicDBObject("$gte", firstSequenceNumber));
|
||||
}
|
||||
DBCursor results = rawMessages.find(query);
|
||||
List<SailMasterMessage> result = new ArrayList<SailMasterMessage>();
|
||||
for (DBObject o : results) {
|
||||
result.add(swissTimingFactory.createMessage((String) o.get(FieldNames.MESSAGE_CONTENT.name()),
|
||||
(Long) o.get(FieldNames.MESSAGE_SEQUENCE_NUMBER.name())));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SailMasterMessage> loadRaceMessages(String raceID) {
|
||||
|
||||
DBCollection racesMessagesCollection = database.getCollection(CollectionNames.RACES_MESSAGES.name());
|
||||
|
||||
BasicDBObject query = new BasicDBObject();
|
||||
query.append(FieldNames.RACE_ID.name(), raceID);
|
||||
|
||||
DBCursor results = racesMessagesCollection.find(query);
|
||||
List<SailMasterMessage> result = new ArrayList<SailMasterMessage>();
|
||||
|
||||
for (DBObject o : results) {
|
||||
SailMasterMessage msg = swissTimingFactory.createMessage((String) o.get(FieldNames.MESSAGE_CONTENT.name()),
|
||||
(Long) o.get(FieldNames.MESSAGE_SEQUENCE_NUMBER.name()));
|
||||
result.add(msg);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SailMasterMessage> loadCommandMessages() {
|
||||
DBCollection cmdMessagesCollection = database.getCollection(CollectionNames.COMMAND_MESSAGES.name());
|
||||
|
||||
DBCursor results = cmdMessagesCollection.find();
|
||||
List<SailMasterMessage> result = new ArrayList<SailMasterMessage>();
|
||||
|
||||
for (DBObject o : results) {
|
||||
SailMasterMessage msg = swissTimingFactory.createMessage((String) o.get(FieldNames.MESSAGE_CONTENT.name()),
|
||||
(Long) o.get(FieldNames.MESSAGE_SEQUENCE_NUMBER.name()));
|
||||
result.add(msg);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public Race getRace(String raceID) {
|
||||
DBCollection races = database.getCollection(CollectionNames.RACES_MASTERDATA.name());
|
||||
|
||||
BasicDBObject query = new BasicDBObject();
|
||||
query.append(FieldNames.RACE_ID.name(), raceID);
|
||||
|
||||
DBObject o = races.findOne(query);
|
||||
|
||||
if(o != null) {
|
||||
Race race = swissTimingFactory.createRace((String) o.get(FieldNames.RACE_ID.name()),
|
||||
(String) o.get(FieldNames.RACE_DESCRIPTION.name()), new MillisecondsTimePoint((Long) o.get(FieldNames.RACE_STARTTIME.name())));
|
||||
|
||||
return race;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Iterable<Race> getRaces() {
|
||||
DBCollection races = database.getCollection(CollectionNames.RACES_MASTERDATA.name());
|
||||
|
||||
DBCursor results = races.find();
|
||||
List<Race> result = new ArrayList<Race>();
|
||||
|
||||
for (DBObject o : results) {
|
||||
Race race = swissTimingFactory.createRace((String) o.get(FieldNames.RACE_ID.name()),
|
||||
(String) o.get(FieldNames.RACE_DESCRIPTION.name()),
|
||||
new MillisecondsTimePoint((Long) o.get(FieldNames.RACE_STARTTIME.name())));
|
||||
result.add(race);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
-98
@@ -1,98 +0,0 @@
|
||||
package com.sap.sailing.domain.swisstimingadapter.persistence.impl;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DB;
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBObject;
|
||||
import com.sap.sailing.domain.swisstimingadapter.MessageType;
|
||||
import com.sap.sailing.domain.swisstimingadapter.Race;
|
||||
import com.sap.sailing.domain.swisstimingadapter.SailMasterMessage;
|
||||
import com.sap.sailing.domain.swisstimingadapter.SwissTimingConfiguration;
|
||||
import com.sap.sailing.domain.swisstimingadapter.persistence.MongoObjectFactory;
|
||||
|
||||
public class MongoObjectFactoryImpl implements MongoObjectFactory {
|
||||
private final DB database;
|
||||
|
||||
public MongoObjectFactoryImpl(DB database) {
|
||||
super();
|
||||
this.database = database;
|
||||
|
||||
// ensure the required indexes for the collection of race specific messages
|
||||
DBCollection racesMessageCollection = database.getCollection(CollectionNames.RACES_MESSAGES.name());
|
||||
|
||||
BasicDBObject indexKeysRaceMsgs = new BasicDBObject();
|
||||
indexKeysRaceMsgs.put(FieldNames.MESSAGE_SEQUENCE_NUMBER.name(), 1);
|
||||
indexKeysRaceMsgs.put(FieldNames.RACE_ID.name(), 1);
|
||||
|
||||
racesMessageCollection.ensureIndex(indexKeysRaceMsgs, IndexNames.INDEX_RACES_MESSAGES.name(), true);
|
||||
|
||||
// ensure the required indexes for the collection of command messages
|
||||
DBCollection cmdMessagesCollection = database.getCollection(CollectionNames.COMMAND_MESSAGES.name());
|
||||
|
||||
BasicDBObject indexKeysCmdMsgs = new BasicDBObject();
|
||||
indexKeysCmdMsgs.put(FieldNames.MESSAGE_SEQUENCE_NUMBER.name(), 1);
|
||||
|
||||
cmdMessagesCollection.ensureIndex(indexKeysCmdMsgs, IndexNames.INDEX_COMMAND_MESSAGES.name(), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeSwissTimingConfiguration(SwissTimingConfiguration swissTimingConfiguration) {
|
||||
DBCollection stConfigCollection = database.getCollection(CollectionNames.SWISSTIMING_CONFIGURATIONS.name());
|
||||
stConfigCollection.ensureIndex(CollectionNames.SWISSTIMING_CONFIGURATIONS.name());
|
||||
BasicDBObject result = new BasicDBObject();
|
||||
result.put(FieldNames.ST_CONFIG_NAME.name(), swissTimingConfiguration.getName());
|
||||
for (DBObject equallyNamedConfig : stConfigCollection.find(result)) {
|
||||
stConfigCollection.remove(equallyNamedConfig);
|
||||
}
|
||||
result.put(FieldNames.ST_CONFIG_HOSTNAME.name(), swissTimingConfiguration.getHostname());
|
||||
result.put(FieldNames.ST_CONFIG_PORT.name(), swissTimingConfiguration.getPort());
|
||||
stConfigCollection.insert(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeRawSailMasterMessage(SailMasterMessage message) {
|
||||
DBCollection rawMessageCollection = database.getCollection(CollectionNames.RAW_MESSAGES.name());
|
||||
rawMessageCollection.insert(new BasicDBObject().append(FieldNames.MESSAGE_SEQUENCE_NUMBER.name(), message.getSequenceNumber()).
|
||||
append(FieldNames.MESSAGE_CONTENT.name(), message.getMessage()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeSailMasterMessage(SailMasterMessage message) {
|
||||
|
||||
DBCollection messageCollection = null;
|
||||
MessageType type = message.getType();
|
||||
|
||||
BasicDBObject objToInsert = new BasicDBObject();
|
||||
|
||||
objToInsert.put(FieldNames.MESSAGE_COMMAND.name(), message.getType().name());
|
||||
objToInsert.put(FieldNames.MESSAGE_SEQUENCE_NUMBER.name(), message.getSequenceNumber());
|
||||
objToInsert.put(FieldNames.MESSAGE_CONTENT.name(), message.getMessage());
|
||||
|
||||
if(type.isRaceSpecific()) {
|
||||
objToInsert.put(FieldNames.RACE_ID.name(), message.getRaceID());
|
||||
messageCollection = database.getCollection(CollectionNames.RACES_MESSAGES.name());
|
||||
}
|
||||
else {
|
||||
messageCollection = database.getCollection(CollectionNames.COMMAND_MESSAGES.name());
|
||||
}
|
||||
|
||||
messageCollection.insert(objToInsert);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeRace(Race race) {
|
||||
DBCollection racesCollection = database.getCollection(CollectionNames.RACES_MASTERDATA.name());
|
||||
|
||||
BasicDBObject query = new BasicDBObject();
|
||||
query.append(FieldNames.RACE_ID.name(), race.getRaceID());
|
||||
|
||||
BasicDBObject result = new BasicDBObject();
|
||||
|
||||
result.put(FieldNames.RACE_ID.name(), race.getRaceID());
|
||||
result.put(FieldNames.RACE_DESCRIPTION.name(), race.getDescription());
|
||||
result.put(FieldNames.RACE_STARTTIME.name(), new Long(race.getStartTime().asMillis()));
|
||||
|
||||
racesCollection.update(query,result);
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -79,7 +79,7 @@ public class SwissTimingAdapterPersistenceImpl implements SwissTimingAdapterPers
|
||||
} catch (Throwable t) {
|
||||
// something went wrong during DB access; report, then use empty new wind track
|
||||
logger.log(Level.SEVERE, "Error connecting to MongoDB, unable to load recorded TracTrac configurations. Check MongoDB settings.");
|
||||
logger.throwing(DomainObjectFactoryImpl.class.getName(), "getTracTracConfigurations", t);
|
||||
logger.throwing(SwissTimingAdapterPersistenceImpl.class.getName(), "getTracTracConfigurations", t);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user