Integer storeId = (Integer) request.getData().get("storeId");
Stock s = coreManager.getStock(productId, storeId);
DisplayStock dstock = null;
if(s != null){
dstock = mapper.map(s, DisplayStock.class);
dstock.setProductId(s.getProduct().getId());
}
SgwtRestFetchResponseBase ret = new SgwtRestFetchResponseBase(dstock);
return ret;
}else if(request.getOperationType() == OPERATION_TYPE.ADD){
Float current = RequestUtil.getFloat(request.getData().get("current"));
Integer max = (Integer) request.getData().get("max");
Integer min = (Integer) request.getData().get("min");
Integer order = (Integer) request.getData().get("defaultOrder");
Integer storeId = (Integer) request.getData().get("storeId");
Product product = coreManager.get(Product.class, productId);
Stock s = new Stock();
s.setCurrent(current);
s.setDefaultOrder(order);
s.setMax(max);
s.setMin(min);
s.setLastUpdateReason(StockUpdateReason.WEB);
s.setProduct(product);
s.setStoreId(storeId);
Stock retStock;
DisplayStock dstock = null;
try {
retStock = coreManager.save(Stock.class, s);
dstock = mapper.map(retStock, DisplayStock.class);
dstock.setProductId(retStock.getProduct().getId());
} catch (Exception e) {
SgwtRestErrorResponse resp = new SgwtRestErrorResponse(SgwtRestResponseBase.RESPONSE_FAILURE);
resp.addError("exception", e.getMessage());
return resp;
}
SgwtRestFetchResponseBase ret = new SgwtRestFetchResponseBase(dstock);
return ret;
}else if(request.getOperationType() == OPERATION_TYPE.UPDATE){
Float current = RequestUtil.getFloat(request.getData().get("current"));
Integer max = (Integer) request.getData().get("max");
Integer min = (Integer) request.getData().get("min");
Integer order = (Integer) request.getData().get("defaultOrder");
Integer storeId = (Integer) request.getData().get("storeId");
Stock s = coreManager.getStock(productId, storeId);
s.setCurrent(current);
s.setDefaultOrder(order);
s.setMax(max);
s.setMin(min);
s.setLastUpdateReason(StockUpdateReason.WEB);
Stock retStock;
DisplayStock dstock = null;
try {
retStock = coreManager.save(Stock.class, s);
dstock = mapper.map(retStock, DisplayStock.class);
dstock.setProductId(retStock.getProduct().getId());
} catch (Exception e) {
SgwtRestErrorResponse resp = new SgwtRestErrorResponse(SgwtRestResponseBase.RESPONSE_FAILURE);
resp.addError("exception", e.getMessage());
return resp;
}