20 changed files with 416 additions and 22 deletions
@ -0,0 +1,50 @@
|
||||
package org.leolo.nrdatad.db; |
||||
|
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
import org.leolo.nrdatad.model.DataSource; |
||||
|
||||
import java.sql.SQLException; |
||||
import java.util.Date; |
||||
|
||||
public abstract class DataSourceDao extends BaseDao{ |
||||
|
||||
private Logger log = LogManager.getLogger(); |
||||
|
||||
public DataSourceDao(DatabaseManager manager) { |
||||
super(manager); |
||||
} |
||||
|
||||
public abstract void insert(DataSource ds) throws SQLException; |
||||
|
||||
public DataSource generateAndInsertUUID(String provider, String streamName, boolean isMessage, Date startDate, String messageId){ |
||||
DataSource ds = new DataSource(provider, streamName, isMessage, startDate, messageId); |
||||
tryInsert(ds); |
||||
return ds; |
||||
} |
||||
|
||||
public DataSource generateAndInsertUUID(String provider, String streamName, boolean isMessage){ |
||||
DataSource ds = new DataSource(provider, streamName, isMessage); |
||||
tryInsert(ds); |
||||
return ds; |
||||
} |
||||
|
||||
public DataSource generateAndInsertUUID(String provider, String streamName, boolean isMessage, String messageId){ |
||||
DataSource ds = new DataSource(provider, streamName, isMessage, messageId); |
||||
tryInsert(ds); |
||||
return ds; |
||||
} |
||||
|
||||
private void tryInsert(DataSource ds) { |
||||
new Thread(()->{ |
||||
log.atInfo().log("Trying to insert record {}", ds.getUuid()); |
||||
try{ |
||||
insert(ds); |
||||
} catch (SQLException e){ |
||||
log.atWarn().withThrowable(e).log("Unable to insert data source record {}", ds.getUuid()); |
||||
} |
||||
}).start(); |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,24 @@
|
||||
package org.leolo.nrdatad.db; |
||||
|
||||
import org.leolo.nrdatad.model.TrainSchedule; |
||||
|
||||
import java.sql.SQLException; |
||||
import java.util.Collection; |
||||
|
||||
public abstract class TrainScheduleDao extends BaseDao { |
||||
|
||||
public TrainScheduleDao(DatabaseManager databaseManager){ |
||||
super(databaseManager); |
||||
} |
||||
|
||||
public abstract void insert(TrainSchedule schedule) throws SQLException; |
||||
|
||||
public void insertAll(Collection<TrainSchedule> schedules) throws SQLException{ |
||||
for(TrainSchedule ts: schedules){ |
||||
insert(ts); |
||||
} |
||||
} |
||||
|
||||
public abstract boolean hasSUID(String suid) throws SQLException; |
||||
|
||||
} |
||||
@ -0,0 +1,36 @@
|
||||
package org.leolo.nrdatad.db.mariadb; |
||||
|
||||
import org.leolo.nrdatad.db.DataSourceDao; |
||||
import org.leolo.nrdatad.db.DatabaseManager; |
||||
import org.leolo.nrdatad.model.DataSource; |
||||
|
||||
import java.sql.Connection; |
||||
import java.sql.PreparedStatement; |
||||
import java.sql.SQLException; |
||||
|
||||
public class DataSourceDaoImpl extends DataSourceDao { |
||||
|
||||
public DataSourceDaoImpl(DatabaseManager manager){ |
||||
super(manager); |
||||
} |
||||
|
||||
@Override |
||||
public void insert(DataSource ds) throws SQLException { |
||||
try( |
||||
Connection conn = getConnection(); |
||||
PreparedStatement stmt = conn.prepareStatement( |
||||
"INSERT INTO data_source (uuid, provider, data_stream, is_messsage, start_date, message_id) " + |
||||
"VALUES (?,?,?,?,?,?)" |
||||
) |
||||
){ |
||||
setString(stmt,1, ds.getUuid().toString()); |
||||
setString(stmt,2, ds.getProvider()); |
||||
setString(stmt,3, ds.getStreamName()); |
||||
setString(stmt,4, ds.isMessage()?"Y":"N"); |
||||
setDate (stmt,5, ds.getStartDate()); |
||||
setString(stmt,6, ds.getMessageId()); |
||||
stmt.executeUpdate(); |
||||
conn.commit(); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,40 @@
|
||||
package org.leolo.nrdatad.db.mariadb; |
||||
|
||||
import org.leolo.nrdatad.db.DatabaseManager; |
||||
import org.leolo.nrdatad.db.TrainScheduleDao; |
||||
import org.leolo.nrdatad.model.TrainSchedule; |
||||
|
||||
import java.sql.Connection; |
||||
import java.sql.PreparedStatement; |
||||
import java.sql.SQLException; |
||||
|
||||
public class TrainScheduleDaoImpl extends TrainScheduleDao { |
||||
public TrainScheduleDaoImpl(DatabaseManager databaseManager) { |
||||
super(databaseManager); |
||||
} |
||||
|
||||
@Override |
||||
public void insert(TrainSchedule schedule) throws SQLException { |
||||
//Insert into train_schedule, train_schedule_location
|
||||
try(Connection conn = getConnection()){ |
||||
//Insert the train_schedule first
|
||||
try ( |
||||
PreparedStatement pstmt = conn.prepareStatement( |
||||
"INSERT INTO train_schedule (suid, train_uid, start_date, end_date, bank_holiday, signal_id, rsid, train_status, train_category, power_type, planned_speed, operating_character, class_avail, sleeper, reservation, catering, atoc_code) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)" |
||||
) |
||||
){ |
||||
setString(pstmt, 1, schedule.getSUID()); |
||||
setInt (pstmt, 2, 1); //This is the first one
|
||||
setString(pstmt, 3, schedule.getTrainUid()); |
||||
char status = schedule.getTrainStatus().charAt(0); |
||||
|
||||
|
||||
} |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public boolean hasSUID(String suid) throws SQLException { |
||||
return false; |
||||
} |
||||
} |
||||
@ -0,0 +1,98 @@
|
||||
package org.leolo.nrdatad.model; |
||||
|
||||
import org.leolo.nrdatad.util.UUIDUtil; |
||||
|
||||
import java.util.Date; |
||||
import java.util.UUID; |
||||
|
||||
public class DataSource { |
||||
|
||||
private UUID uuid; |
||||
private String provider; |
||||
private String streamName; |
||||
private boolean isMessage; |
||||
private Date startDate; |
||||
private String messageId; |
||||
|
||||
public DataSource(){ |
||||
setRandomUuid(); |
||||
} |
||||
|
||||
public DataSource(String provider, String streamName, boolean isMessage, Date startDate, String messageId) { |
||||
setRandomUuid(); |
||||
this.provider = provider; |
||||
this.streamName = streamName; |
||||
this.isMessage = isMessage; |
||||
this.startDate = startDate; |
||||
this.messageId = messageId; |
||||
} |
||||
|
||||
public DataSource(String provider, String streamName, boolean isMessage) { |
||||
setRandomUuid(); |
||||
this.provider = provider; |
||||
this.streamName = streamName; |
||||
this.isMessage = isMessage; |
||||
this.startDate = new Date(); |
||||
} |
||||
|
||||
public DataSource(String provider, String streamName, boolean isMessage, String messageId) { |
||||
setRandomUuid(); |
||||
this.provider = provider; |
||||
this.streamName = streamName; |
||||
this.isMessage = isMessage; |
||||
this.startDate = new Date(); |
||||
this.messageId = messageId; |
||||
} |
||||
|
||||
public String getProvider() { |
||||
return provider; |
||||
} |
||||
|
||||
public void setProvider(String provider) { |
||||
this.provider = provider; |
||||
} |
||||
|
||||
public String getStreamName() { |
||||
return streamName; |
||||
} |
||||
|
||||
public void setStreamName(String streamName) { |
||||
this.streamName = streamName; |
||||
} |
||||
|
||||
public boolean isMessage() { |
||||
return isMessage; |
||||
} |
||||
|
||||
public void setMessage(boolean message) { |
||||
isMessage = message; |
||||
} |
||||
|
||||
public Date getStartDate() { |
||||
return startDate; |
||||
} |
||||
|
||||
public void setStartDate(Date startDate) { |
||||
this.startDate = startDate; |
||||
} |
||||
|
||||
public String getMessageId() { |
||||
return messageId; |
||||
} |
||||
|
||||
public void setMessageId(String messageId) { |
||||
this.messageId = messageId; |
||||
} |
||||
|
||||
public UUID getUuid() { |
||||
return uuid; |
||||
} |
||||
|
||||
public void setUuid(UUID uuid) { |
||||
this.uuid = uuid; |
||||
} |
||||
|
||||
public void setRandomUuid(){ |
||||
this.uuid = UUIDUtil.getNextUUID(); |
||||
} |
||||
} |
||||
@ -1,11 +1,26 @@
|
||||
package org.leolo.nrdatad.util; |
||||
|
||||
import com.fasterxml.uuid.EthernetAddress; |
||||
import com.fasterxml.uuid.Generators; |
||||
import org.leolo.nrdatad.ConfigurationManager; |
||||
|
||||
import java.util.UUID; |
||||
|
||||
public class UUIDUtil { |
||||
|
||||
|
||||
private static EthernetAddress ethernetAddress; |
||||
|
||||
public static UUID getNextUUID(){ |
||||
return null; |
||||
return Generators.timeBasedGenerator(getEthernetAddress()).generate(); |
||||
} |
||||
|
||||
private static synchronized EthernetAddress getEthernetAddress() { |
||||
if(ethernetAddress==null){ |
||||
//Get it from conf
|
||||
String ethAddr = ConfigurationManager.getInstance().getEthAddress(); |
||||
ethernetAddress = new EthernetAddress(ethAddr); |
||||
} |
||||
return ethernetAddress; |
||||
} |
||||
|
||||
} |
||||
|
||||
Loading…
Reference in new issue