use of org.olat.resource.accesscontrol.model.RawOrderItem in project OpenOLAT by OpenOLAT.
the class ACOrderManagerTest method findOrderItems_1.
@Test
public void findOrderItems_1() {
// create an offer to buy
List<AccessMethod> methods = acMethodManager.getAvailableMethodsByType(TokenAccessMethod.class);
assertNotNull(methods);
assertEquals(1, methods.size());
AccessMethod tokenMethod = methods.get(0);
OLATResource randomOres = createResource();
Offer offer = acService.createOffer(randomOres, "TestSaveOrder");
offer = acService.save(offer);
dbInstance.commitAndCloseSession();
// create and save an order
Order order = acOrderManager.createOrder(ident1);
OrderPart part = acOrderManager.addOrderPart(order);
OrderLine line = acOrderManager.addOrderLine(part, offer);
order = acOrderManager.save(order);
Assert.assertNotNull(order);
Assert.assertNotNull(order.getDelivery());
Assert.assertNotNull(line);
Assert.assertEquals(ident1, order.getDelivery());
dbInstance.commitAndCloseSession();
AccessTransaction accessTransaction = acTransactionManager.createTransaction(order, part, tokenMethod);
assertNotNull(accessTransaction);
acTransactionManager.save(accessTransaction);
AccessTransaction accessTransaction2 = acTransactionManager.createTransaction(order, part, tokenMethod);
assertNotNull(accessTransaction2);
acTransactionManager.save(accessTransaction2);
dbInstance.commitAndCloseSession();
acTransactionManager.update(accessTransaction, AccessTransactionStatus.NEW);
acTransactionManager.update(accessTransaction2, AccessTransactionStatus.CANCELED);
long start = System.nanoTime();
List<RawOrderItem> items = acOrderManager.findNativeOrderItems(randomOres, null, null, null, null, null, 0, -1, null);
CodeHelper.printNanoTime(start, "Order itemized");
Assert.assertNotNull(items);
// check the order by
for (OrderCol col : OrderCol.values()) {
List<RawOrderItem> rawItems = acOrderManager.findNativeOrderItems(randomOres, null, null, null, null, null, 0, -1, null, new SortKey(col.sortKey(), false));
Assert.assertNotNull(rawItems);
}
}
use of org.olat.resource.accesscontrol.model.RawOrderItem in project OpenOLAT by OpenOLAT.
the class ACFrontendManager method findOrderItems.
@Override
public List<OrderTableItem> findOrderItems(OLATResource resource, IdentityRef delivery, Long orderNr, Date from, Date to, OrderStatus[] status, int firstResult, int maxResults, List<UserPropertyHandler> userPropertyHandlers, SortKey... orderBy) {
List<AccessMethod> methods = methodManager.getAllMethods();
Map<String, AccessMethod> methodMap = new HashMap<>();
for (AccessMethod method : methods) {
methodMap.put(method.getKey().toString(), method);
}
List<RawOrderItem> rawOrders = orderManager.findNativeOrderItems(resource, delivery, orderNr, from, to, status, firstResult, maxResults, userPropertyHandlers, orderBy);
List<OrderTableItem> items = new ArrayList<>(rawOrders.size());
for (RawOrderItem rawOrder : rawOrders) {
String orderStatusStr = rawOrder.getOrderStatus();
OrderStatus orderStatus = OrderStatus.valueOf(orderStatusStr);
Status finalStatus = getStatus(orderStatusStr, rawOrder.getTrxStatus(), rawOrder.getPspTrxStatus());
String methodIds = rawOrder.getTrxMethodIds();
List<AccessMethod> orderMethods = new ArrayList<>(2);
if (StringHelper.containsNonWhitespace(methodIds)) {
String[] methodIdArr = methodIds.split(",");
for (String methodId : methodIdArr) {
if (methodMap.containsKey(methodId)) {
orderMethods.add(methodMap.get(methodId));
}
}
}
OrderTableItem item = new OrderTableItem(rawOrder.getOrderKey(), rawOrder.getOrderNr(), rawOrder.getTotal(), rawOrder.getCreationDate(), orderStatus, finalStatus, rawOrder.getDeliveryKey(), rawOrder.getUsername(), rawOrder.getUserProperties(), orderMethods);
item.setResourceDisplayname(rawOrder.getResourceName());
items.add(item);
}
return items;
}
use of org.olat.resource.accesscontrol.model.RawOrderItem in project openolat by klemens.
the class ACFrontendManager method findOrderItems.
@Override
public List<OrderTableItem> findOrderItems(OLATResource resource, IdentityRef delivery, Long orderNr, Date from, Date to, OrderStatus[] status, int firstResult, int maxResults, List<UserPropertyHandler> userPropertyHandlers, SortKey... orderBy) {
List<AccessMethod> methods = methodManager.getAllMethods();
Map<String, AccessMethod> methodMap = new HashMap<>();
for (AccessMethod method : methods) {
methodMap.put(method.getKey().toString(), method);
}
List<RawOrderItem> rawOrders = orderManager.findNativeOrderItems(resource, delivery, orderNr, from, to, status, firstResult, maxResults, userPropertyHandlers, orderBy);
List<OrderTableItem> items = new ArrayList<>(rawOrders.size());
for (RawOrderItem rawOrder : rawOrders) {
String orderStatusStr = rawOrder.getOrderStatus();
OrderStatus orderStatus = OrderStatus.valueOf(orderStatusStr);
Status finalStatus = getStatus(orderStatusStr, rawOrder.getTrxStatus(), rawOrder.getPspTrxStatus());
String methodIds = rawOrder.getTrxMethodIds();
List<AccessMethod> orderMethods = new ArrayList<>(2);
if (StringHelper.containsNonWhitespace(methodIds)) {
String[] methodIdArr = methodIds.split(",");
for (String methodId : methodIdArr) {
if (methodMap.containsKey(methodId)) {
orderMethods.add(methodMap.get(methodId));
}
}
}
OrderTableItem item = new OrderTableItem(rawOrder.getOrderKey(), rawOrder.getOrderNr(), rawOrder.getTotal(), rawOrder.getCreationDate(), orderStatus, finalStatus, rawOrder.getDeliveryKey(), rawOrder.getUsername(), rawOrder.getUserProperties(), orderMethods);
item.setResourceDisplayname(rawOrder.getResourceName());
items.add(item);
}
return items;
}
use of org.olat.resource.accesscontrol.model.RawOrderItem in project openolat by klemens.
the class ACOrderDAO method findNativeOrderItems.
/**
* The method is optimized for our settings: 1 order -> 1 order part -> 1 order line
*
* @param resource
* @param delivery
* @return
*/
public List<RawOrderItem> findNativeOrderItems(OLATResource resource, IdentityRef delivery, Long orderNr, Date from, Date to, OrderStatus[] status, int firstResult, int maxResults, List<UserPropertyHandler> userPropertyHandlers, SortKey... orderBy) {
NativeQueryBuilder sb = new NativeQueryBuilder(1024, dbInstance);
sb.append("select").append(" o.order_id as order_id,").append(" o.total_currency_code as total_currency_code,").append(" o.total_amount as total_amount,").append(" o.creationdate as creationdate,").append(" o.order_status as o_status,").append(" o.fk_delivery_id as delivery_id,").append(" ").appendToArray("offer.resourcedisplayname").append(" as resDisplaynames,").append(" ").appendToArray("trx.trx_status").append(" as trxStatus,").append(" ").appendToArray("trx.fk_method_id").append(" as trxMethodIds,").append(" ").appendToArray("pspTrx.trx_status").append(" as pspTrxStatus");
if (delivery == null) {
sb.append(" ,delivery.id as delivery_ident_id").append(" ,delivery.name as delivery_ident_name").append(" ,delivery_user.user_id as delivery_user_id");
if (userPropertyHandlers != null) {
for (UserPropertyHandler handler : userPropertyHandlers) {
sb.append(" ,delivery_user.").append(handler.getDatabaseColumnName()).append(" as ").append(handler.getName());
}
}
}
sb.append(" from o_ac_order o").append(" inner join o_ac_order_part order_part on (o.order_id=order_part.fk_order_id and order_part.pos=0)").append(" inner join o_ac_order_line order_line on (order_part.order_part_id=order_line.fk_order_part_id and order_line.pos=0)").append(" inner join o_ac_offer offer on (order_line.fk_offer_id=offer.offer_id)");
if (delivery == null) {
sb.append(" inner join o_bs_identity delivery on (delivery.id=o.fk_delivery_id)").append(" inner join o_user delivery_user on (delivery_user.fk_identity=delivery.id)");
}
sb.append(" left join o_ac_paypal_transaction pspTrx on (o.order_id = pspTrx.order_id)").append(" left join o_ac_transaction trx on (o.order_id = trx.fk_order_id)");
boolean where = false;
if (resource != null) {
where = PersistenceHelper.appendAnd(sb, where);
sb.append(" offer.fk_resource_id=:resourceKey ");
}
if (delivery != null) {
where = PersistenceHelper.appendAnd(sb, where);
sb.append(" o.fk_delivery_id=:deliveryKey ");
}
if (status != null && status.length > 0 && status[0] != null) {
where = PersistenceHelper.appendAnd(sb, where);
sb.append("o.order_status in (:status)");
}
if (from != null) {
where = PersistenceHelper.appendAnd(sb, where);
sb.append("o.creationdate >=:from");
}
if (to != null) {
where = PersistenceHelper.appendAnd(sb, where);
sb.append("o.creationdate <=:to");
}
if (orderNr != null) {
where = PersistenceHelper.appendAnd(sb, where);
sb.append("o.order_id=:orderNr");
}
sb.append(" group by o.order_id");
if (dbInstance.isOracle()) {
sb.append(", o.total_currency_code, o.total_amount, o.creationdate, o.order_status, o.fk_delivery_id");
}
if (delivery == null) {
sb.append(", delivery.id, delivery_user.user_id");
if (dbInstance.isOracle()) {
sb.append(", delivery.name");
if (userPropertyHandlers != null) {
for (UserPropertyHandler handler : userPropertyHandlers) {
sb.append(", delivery_user.").append(handler.getDatabaseColumnName());
}
}
}
}
if (orderBy != null && orderBy.length > 0 && orderBy[0] != null) {
sb.appendOrderBy(orderBy[0]);
}
Query query = dbInstance.getCurrentEntityManager().createNativeQuery(sb.toString());
if (resource != null) {
query.setParameter("resourceKey", resource.getKey());
}
if (delivery != null) {
query.setParameter("deliveryKey", delivery.getKey());
}
if (status != null && status.length > 0 && status[0] != null) {
List<String> statusStr = new ArrayList<String>();
for (OrderStatus s : status) {
statusStr.add(s.name());
}
query.setParameter("status", statusStr);
}
if (orderNr != null) {
query.setParameter("orderNr", orderNr);
}
if (from != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(from);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
query.setParameter("from", cal.getTime(), TemporalType.TIMESTAMP);
}
if (to != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(to);
cal.set(Calendar.HOUR, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 0);
query.setParameter("to", cal.getTime(), TemporalType.TIMESTAMP);
}
if (maxResults > 0) {
query.setFirstResult(firstResult).setMaxResults(maxResults);
}
int numOfProperties = userPropertyHandlers == null ? 0 : userPropertyHandlers.size();
List<?> rawOrders = query.getResultList();
List<RawOrderItem> items = new ArrayList<>(rawOrders.size());
for (Object rawOrder : rawOrders) {
Object[] order = (Object[]) rawOrder;
int pos = 0;
Long orderKey = ((Number) order[pos++]).longValue();
String totalCurrencyCode = (String) order[pos++];
BigDecimal totalAmount = (BigDecimal) order[pos++];
Date creationDate = (Date) order[pos++];
String orderStatus = (String) order[pos++];
Long deliveryKey = ((Number) order[pos++]).longValue();
String resourceName = (String) order[pos++];
String trxStatus = (String) order[pos++];
String trxMethodIds = (String) order[pos++];
String pspTrxStatus = (String) order[pos++];
String username = null;
String[] userProperties = null;
if (numOfProperties > 0) {
// identityKey
pos++;
username = (String) order[pos++];
// userKey
pos++;
userProperties = new String[numOfProperties];
for (int i = 0; i < numOfProperties; i++) {
userProperties[i] = (String) order[pos++];
}
}
RawOrderItem item = new RawOrderItem(orderKey, orderKey.toString(), totalCurrencyCode, totalAmount, creationDate, orderStatus, deliveryKey, resourceName, trxStatus, trxMethodIds, pspTrxStatus, username, userProperties);
items.add(item);
}
return items;
}
use of org.olat.resource.accesscontrol.model.RawOrderItem in project OpenOLAT by OpenOLAT.
the class ACOrderDAO method findNativeOrderItems.
/**
* The method is optimized for our settings: 1 order -> 1 order part -> 1 order line
*
* @param resource
* @param delivery
* @return
*/
public List<RawOrderItem> findNativeOrderItems(OLATResource resource, IdentityRef delivery, Long orderNr, Date from, Date to, OrderStatus[] status, int firstResult, int maxResults, List<UserPropertyHandler> userPropertyHandlers, SortKey... orderBy) {
NativeQueryBuilder sb = new NativeQueryBuilder(1024, dbInstance);
sb.append("select").append(" o.order_id as order_id,").append(" o.total_currency_code as total_currency_code,").append(" o.total_amount as total_amount,").append(" o.creationdate as creationdate,").append(" o.order_status as o_status,").append(" o.fk_delivery_id as delivery_id,").append(" ").appendToArray("offer.resourcedisplayname").append(" as resDisplaynames,").append(" ").appendToArray("trx.trx_status").append(" as trxStatus,").append(" ").appendToArray("trx.fk_method_id").append(" as trxMethodIds,").append(" ").appendToArray("pspTrx.trx_status").append(" as pspTrxStatus");
if (delivery == null) {
sb.append(" ,delivery.id as delivery_ident_id").append(" ,delivery.name as delivery_ident_name").append(" ,delivery_user.user_id as delivery_user_id");
if (userPropertyHandlers != null) {
for (UserPropertyHandler handler : userPropertyHandlers) {
sb.append(" ,delivery_user.").append(handler.getDatabaseColumnName()).append(" as ").append(handler.getName());
}
}
}
sb.append(" from o_ac_order o").append(" inner join o_ac_order_part order_part on (o.order_id=order_part.fk_order_id and order_part.pos=0)").append(" inner join o_ac_order_line order_line on (order_part.order_part_id=order_line.fk_order_part_id and order_line.pos=0)").append(" inner join o_ac_offer offer on (order_line.fk_offer_id=offer.offer_id)");
if (delivery == null) {
sb.append(" inner join o_bs_identity delivery on (delivery.id=o.fk_delivery_id)").append(" inner join o_user delivery_user on (delivery_user.fk_identity=delivery.id)");
}
sb.append(" left join o_ac_paypal_transaction pspTrx on (o.order_id = pspTrx.order_id)").append(" left join o_ac_transaction trx on (o.order_id = trx.fk_order_id)");
boolean where = false;
if (resource != null) {
where = PersistenceHelper.appendAnd(sb, where);
sb.append(" offer.fk_resource_id=:resourceKey ");
}
if (delivery != null) {
where = PersistenceHelper.appendAnd(sb, where);
sb.append(" o.fk_delivery_id=:deliveryKey ");
}
if (status != null && status.length > 0 && status[0] != null) {
where = PersistenceHelper.appendAnd(sb, where);
sb.append("o.order_status in (:status)");
}
if (from != null) {
where = PersistenceHelper.appendAnd(sb, where);
sb.append("o.creationdate >=:from");
}
if (to != null) {
where = PersistenceHelper.appendAnd(sb, where);
sb.append("o.creationdate <=:to");
}
if (orderNr != null) {
where = PersistenceHelper.appendAnd(sb, where);
sb.append("o.order_id=:orderNr");
}
sb.append(" group by o.order_id");
if (dbInstance.isOracle()) {
sb.append(", o.total_currency_code, o.total_amount, o.creationdate, o.order_status, o.fk_delivery_id");
}
if (delivery == null) {
sb.append(", delivery.id, delivery_user.user_id");
if (dbInstance.isOracle()) {
sb.append(", delivery.name");
if (userPropertyHandlers != null) {
for (UserPropertyHandler handler : userPropertyHandlers) {
sb.append(", delivery_user.").append(handler.getDatabaseColumnName());
}
}
}
}
if (orderBy != null && orderBy.length > 0 && orderBy[0] != null) {
sb.appendOrderBy(orderBy[0]);
}
Query query = dbInstance.getCurrentEntityManager().createNativeQuery(sb.toString());
if (resource != null) {
query.setParameter("resourceKey", resource.getKey());
}
if (delivery != null) {
query.setParameter("deliveryKey", delivery.getKey());
}
if (status != null && status.length > 0 && status[0] != null) {
List<String> statusStr = new ArrayList<String>();
for (OrderStatus s : status) {
statusStr.add(s.name());
}
query.setParameter("status", statusStr);
}
if (orderNr != null) {
query.setParameter("orderNr", orderNr);
}
if (from != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(from);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
query.setParameter("from", cal.getTime(), TemporalType.TIMESTAMP);
}
if (to != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(to);
cal.set(Calendar.HOUR, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 0);
query.setParameter("to", cal.getTime(), TemporalType.TIMESTAMP);
}
if (maxResults > 0) {
query.setFirstResult(firstResult).setMaxResults(maxResults);
}
int numOfProperties = userPropertyHandlers == null ? 0 : userPropertyHandlers.size();
List<?> rawOrders = query.getResultList();
List<RawOrderItem> items = new ArrayList<>(rawOrders.size());
for (Object rawOrder : rawOrders) {
Object[] order = (Object[]) rawOrder;
int pos = 0;
Long orderKey = ((Number) order[pos++]).longValue();
String totalCurrencyCode = (String) order[pos++];
BigDecimal totalAmount = (BigDecimal) order[pos++];
Date creationDate = (Date) order[pos++];
String orderStatus = (String) order[pos++];
Long deliveryKey = ((Number) order[pos++]).longValue();
String resourceName = (String) order[pos++];
String trxStatus = (String) order[pos++];
String trxMethodIds = (String) order[pos++];
String pspTrxStatus = (String) order[pos++];
String username = null;
String[] userProperties = null;
if (numOfProperties > 0) {
// identityKey
pos++;
username = (String) order[pos++];
// userKey
pos++;
userProperties = new String[numOfProperties];
for (int i = 0; i < numOfProperties; i++) {
userProperties[i] = (String) order[pos++];
}
}
RawOrderItem item = new RawOrderItem(orderKey, orderKey.toString(), totalCurrencyCode, totalAmount, creationDate, orderStatus, deliveryKey, resourceName, trxStatus, trxMethodIds, pspTrxStatus, username, userProperties);
items.add(item);
}
return items;
}
Aggregations