Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
lhhong committed Feb 22, 2017
1 parent 0097a3a commit 1101d98
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ ethereum/src/main/resources/user.conf
datafile.mv.db
datafile.trace.db
app-main/npm-debug.log
mock_data/Product.pyc
mock_data/Record.pyc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class SalesRecord implements Serializable{
private long id;

private int quantity;
private Date date;
private Date dateStamp;

@ManyToOne (fetch = FetchType.LAZY)
@JoinColumn (name = "product")
Expand Down
2 changes: 1 addition & 1 deletion ethereum/src/main/java/com/crumbs/entities/Shipment.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Shipment implements Serializable {
@GeneratedValue
long id;

private Date shipDate;
private Date dateStamp;
private Date expiry;
private int quantity;

Expand Down
4 changes: 2 additions & 2 deletions ethereum/src/main/java/com/crumbs/models/SalesRecordVM.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class SalesRecordVM {

public SalesRecordVM(SalesRecord record) {
this.quantity = record.getQuantity();
this.date = DateUtil.toLocalDate(record.getDate());
this.dateStamp = DateUtil.toLocalDate(record.getDateStamp());
}

private int quantity;
private LocalDate date;
private LocalDate dateStamp;
}
4 changes: 2 additions & 2 deletions ethereum/src/main/java/com/crumbs/models/ShipmentVM.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public class ShipmentVM {

public ShipmentVM(Shipment shipment) {
quantity = shipment.getQuantity();
shipDate = DateUtil.toLocalDate(shipment.getShipDate());
dateStamp = DateUtil.toLocalDate(shipment.getDateStamp());
expiry = DateUtil.toLocalDate(shipment.getExpiry());
}
private LocalDate shipDate;
private LocalDate dateStamp;
private LocalDate expiry;
private int quantity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
*/
@Repository
public interface SalesRecordRepo extends JpaRepository<SalesRecord, Long> {
List<SalesRecord> findByProductOrderByDateAsc(Product product);
List<SalesRecord> findByProductAndDateBeforeOrderByDateAsc(Product product, Date date);
List<SalesRecord> findByProductOrderByDateStampAsc(Product product);
List<SalesRecord> findByProductAndDateStampBeforeOrderByDateStampAsc(Product product, Date dateStamp);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
@Repository
public interface ShipmentRepo extends JpaRepository<Shipment, Long> {
List<Shipment> findByProductAndQuantityNotAndExpiryAfter(Product product, int quantity, Date expiry);
List<Shipment> findByProductAndQuantityNotAndExpiryAfterAndShipDateBefore(Product product, int quantity, Date expiry, Date shipDate);
List<Shipment> findByProductAndQuantityNotAndExpiryAfterAndDateStampBefore(Product product, int quantity, Date expiry, Date dateStamp);
}
14 changes: 10 additions & 4 deletions ethereum/src/main/java/com/crumbs/rest/TestControllers.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.crumbs.components.AccountBean;
import com.crumbs.components.EthereumBean;
import com.crumbs.entities.Product;
import com.crumbs.repositories.ProductRepo;
import com.crumbs.services.ContractService;
import com.crumbs.services.InventoryService;
import com.crumbs.services.WebSocketSrvc;
Expand Down Expand Up @@ -53,20 +52,27 @@ public long testWebSocket() {
return r.nextLong();
}

@RequestMapping(value = "/delete_products", method = GET)
@ResponseBody
public void deleteAll() {
inventoryService.deleteAll();
}

@RequestMapping(value = "/import", method = POST)
@ResponseBody
public void receive(@RequestBody Product p) {
logger.info(JSON.toJSONString(p, true));
p.getSalesRecord().forEach(r -> {
r.setProduct(p);
r.setDate(DateUtil.toDate(r.getDate()));
r.setDateStamp(DateUtil.toDate(r.getDateStamp()));
});
p.getShipmentsRecord().forEach(r -> {
r.setProduct(p);
r.setExpiry(DateUtil.toDate(r.getExpiry()));
r.setShipDate(DateUtil.toDate(r.getShipDate()));
r.setDateStamp(DateUtil.toDate(r.getDateStamp()));
});
logger.info(JSON.toJSONString(p, true));
//TODO uncomment inventoryService.storeProduct(p);
inventoryService.storeProduct(p);
}

@RequestMapping(value = "/sample-contract", method = GET)
Expand Down
22 changes: 14 additions & 8 deletions ethereum/src/main/java/com/crumbs/services/InventoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public Map<LocalDate,Integer> productToShipmentQuantityArray(Product product) {
Map<LocalDate,Integer> result = new HashMap<>();
List<Shipment> shipments = shipmentRepo.findByProductAndQuantityNotAndExpiryAfter(product,0, DateUtil.today());
for (Shipment record : shipments) {
if (result.containsKey(DateUtil.toLocalDate(record.getShipDate()))) {
result.merge(DateUtil.toLocalDate(record.getShipDate()), record.getQuantity(), (current, addition) -> (current + addition));
if (result.containsKey(DateUtil.toLocalDate(record.getDateStamp()))) {
result.merge(DateUtil.toLocalDate(record.getDateStamp()), record.getQuantity(), (current, addition) -> (current + addition));
}
else {
result.put(DateUtil.toLocalDate(record.getShipDate()), record.getQuantity());
result.put(DateUtil.toLocalDate(record.getDateStamp()), record.getQuantity());
}
}
return result;
Expand Down Expand Up @@ -75,7 +75,7 @@ public Map<LocalDate, StockUpdate> futureStock(String product) {
List<ShipmentVM> shipmentVMS = new ArrayList<>();
shipments.forEach((shipment) -> shipmentVMS.add(new ShipmentVM(shipment)));
for (ShipmentVM shipment : shipmentVMS) {
if (shipment.getShipDate().isBefore(DateUtil.todayLocalDate().plusDays(1))) {
if (shipment.getDateStamp().isBefore(DateUtil.todayLocalDate().plusDays(1))) {
if (result.keySet().contains(shipment.getExpiry())) {
result.get(shipment.getExpiry()).dispose(shipment.getQuantity());
LocalDate date = shipment.getExpiry().minusDays(1);
Expand All @@ -90,9 +90,9 @@ public Map<LocalDate, StockUpdate> futureStock(String product) {
}
}
}
else if (shipment.getShipDate().isBefore(DateUtil.todayLocalDate().plusDays(8))) {
LocalDate date = shipment.getShipDate();
result.get(shipment.getShipDate()).stockUp(shipment.getQuantity());
else if (shipment.getDateStamp().isBefore(DateUtil.todayLocalDate().plusDays(8))) {
LocalDate date = shipment.getDateStamp();
result.get(shipment.getDateStamp()).stockUp(shipment.getQuantity());
date = date.plusDays(1);
while (date.isBefore(DateUtil.todayLocalDate().plusDays(8))) {
if (date.isEqual(shipment.getExpiry())) {
Expand All @@ -109,7 +109,7 @@ else if (shipment.getShipDate().isBefore(DateUtil.todayLocalDate().plusDays(8)))

public List<Integer> productToDateQuantityArray(Product product) {
List<Integer> result = new ArrayList<>();
List<SalesRecord> records = salesRecordRepo.findByProductOrderByDateAsc(product);
List<SalesRecord> records = salesRecordRepo.findByProductOrderByDateStampAsc(product);
for (SalesRecord record : records) {
result.add(record.getQuantity());
}
Expand All @@ -127,4 +127,10 @@ public void storeProduct(Product product) {
public void deleteProduct(String product) {
productRepo.delete(product);
}

public void deleteAll() {
productRepo.deleteAll();
shipmentRepo.deleteAll();
salesRecordRepo.deleteAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ public class PredictionSrvc {
ProductRepo productRepo;

public PredictionVM getAndRankPredictions() {
PredictionVM predictionVM = new PredictionVM();
List<Prediction> predictions = getAllPredictions();
return pullAndRankRelavantPredictions(predictions);
}

public PredictionVM pullAndRankRelavantPredictions(List<Prediction> predictions) {
PredictionVM predictionVM = new PredictionVM();
predictions.forEach(p -> {
p.getShipments().forEach(s -> {
if (!s.getUrgencyLevel().equalsIgnoreCase("green"))
Expand Down Expand Up @@ -68,7 +71,7 @@ public List<Prediction> getAllPredictions() {
public List<Integer> buildArrayQuery(String product) {
Product p = new Product();
p.setName(product);
List<SalesRecord> salesRecords = salesRecordRepo.findByProductAndDateBeforeOrderByDateAsc(p, DateUtil.today());
List<SalesRecord> salesRecords = salesRecordRepo.findByProductAndDateStampBeforeOrderByDateStampAsc(p, DateUtil.today());
List<Integer> query = new ArrayList<>();
salesRecords.forEach((record) -> query.add(record.getQuantity()));
return query;
Expand Down
36 changes: 18 additions & 18 deletions ethereum/src/test/java/com/crumbs/test/ProductRepoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,79 +37,79 @@ public class ProductRepoTest {
@Before
public void setUpApple() {
Product p = new Product();
p.setName("apple");
p.setName("test");
p.setPrice(123);
Shipment s = new Shipment();
s.setProduct(p);
s.setQuantity(1);
s.setExpiry(DateUtil.daysFromToday(2));
s.setShipDate(DateUtil.daysFromToday(-8));
s.setDateStamp(DateUtil.daysFromToday(-8));
Set<Shipment> l = new HashSet<>();
l.add(s);
Shipment s2 = new Shipment();
s2.setProduct(p);
s2.setQuantity(2);
s2.setExpiry(DateUtil.daysFromToday(8));
s2.setShipDate(DateUtil.daysFromToday(2));
s2.setDateStamp(DateUtil.daysFromToday(2));
l.add(s2);
Shipment s3 = new Shipment();
s3.setProduct(p);
s3.setQuantity(3);
s3.setExpiry(DateUtil.daysFromToday(7));
s3.setShipDate(DateUtil.daysFromToday(1));
s3.setDateStamp(DateUtil.daysFromToday(1));
l.add(s3);
Shipment s4 = new Shipment();
s4.setProduct(p);
s4.setQuantity(4);
s4.setExpiry(DateUtil.daysFromToday(9));
s4.setShipDate(DateUtil.today());
s4.setDateStamp(DateUtil.today());
l.add(s4);
Shipment s5 = new Shipment();
s5.setProduct(p);
s5.setQuantity(15);
s5.setExpiry(DateUtil.daysFromToday(6));
s5.setShipDate(DateUtil.daysFromToday(3));
s5.setDateStamp(DateUtil.daysFromToday(3));
l.add(s5);
Shipment s6 = new Shipment();
s6.setProduct(p);
s6.setQuantity(6);
s6.setExpiry(DateUtil.daysFromToday(7));
s6.setShipDate(DateUtil.daysFromToday(0));
s6.setDateStamp(DateUtil.daysFromToday(0));
l.add(s6);
Shipment s7 = new Shipment();
s7.setProduct(p);
s7.setQuantity(36);
s7.setExpiry(DateUtil.daysFromToday(5));
s7.setShipDate(DateUtil.daysFromToday(2));
s7.setDateStamp(DateUtil.daysFromToday(2));
l.add(s7);
Shipment s8 = new Shipment();
s8.setProduct(p);
s8.setQuantity(36);
s8.setExpiry(DateUtil.daysFromToday(4));
s8.setShipDate(DateUtil.daysFromToday(0));
s8.setDateStamp(DateUtil.daysFromToday(0));
l.add(s8);
Shipment s9 = new Shipment();
s9.setProduct(p);
s9.setQuantity(36);
s9.setExpiry(DateUtil.daysFromToday(14));
s9.setShipDate(DateUtil.daysFromToday(7));
s9.setDateStamp(DateUtil.daysFromToday(7));
l.add(s9);
p.setShipmentsRecord(l);
SalesRecord r1 = new SalesRecord();
r1.setQuantity(123456);
r1.setDate(new Date(123456));
r1.setDateStamp(new Date(123456));
r1.setProduct(p);
SalesRecord r4 = new SalesRecord();
r4.setQuantity(12345678);
r4.setDate(new Date(12345678));
r4.setDateStamp(new Date(12345678));
r4.setProduct(p);
SalesRecord r2 = new SalesRecord();
r2.setQuantity(12345);
r2.setDate(new Date(12345));
r2.setDateStamp(new Date(12345));
r2.setProduct(p);
SalesRecord r3 = new SalesRecord();
r3.setQuantity(1234567);
r3.setDate(new Date(1234567));
r3.setDateStamp(new Date(1234567));
r3.setProduct(p);
Set<SalesRecord> records = new HashSet<>();
records.add(r1);
Expand All @@ -119,15 +119,15 @@ public void setUpApple() {
p.setSalesRecord(records);

inventoryService.storeProduct(p);
ProductVM outcome = inventoryService.getProduct("apple");
ProductVM outcome = inventoryService.getProduct("test");
logger.info(JSON.toJSONString(outcome));
logger.info(JSON.toJSONString(inventoryService.productToDateQuantityArray(p)));
logger.info(JSON.toJSONString(inventoryService.productToShipmentQuantityArray(p)));
}

@Test
public void predictionTest() {
String p ="apple";
String p ="test";
List<Integer> list = new ArrayList<>();
list.add(3);
list.add(4);
Expand All @@ -145,14 +145,14 @@ public void predictionTest() {

@Test
public void inventoryMapTest() {
String p = "apple";
String p = "test";
logger.info(JSON.toJSONString(inventoryService.futureStock(p)));
logger.info(JSON.toJSONString(inventoryService.futureStockInArray(p)));
}

@After
public void removeApple() {
inventoryService.deleteProduct("apple");
inventoryService.deleteProduct("test");
}

}

0 comments on commit 1101d98

Please sign in to comment.