use of org.olat.resource.accesscontrol.model.AccessMethod 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.AccessMethod in project openolat by klemens.
the class ACMethodDAO method enableMethod.
public void enableMethod(Class<? extends AccessMethod> type, boolean enable) {
StringBuilder sb = new StringBuilder();
sb.append("select method from ").append(type.getName()).append(" method");
List<AccessMethod> methods = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), AccessMethod.class).getResultList();
if (methods.isEmpty() && enable) {
try {
AccessMethod method = type.newInstance();
Date now = new Date();
((AbstractAccessMethod) method).setCreationDate(now);
((AbstractAccessMethod) method).setLastModified(now);
dbInstance.getCurrentEntityManager().persist(method);
} catch (InstantiationException e) {
log.error("Failed to instantiate an access method", e);
} catch (IllegalAccessException e) {
log.error("Failed to instantiate an access method", e);
}
} else {
for (AccessMethod method : methods) {
if (method.isEnabled() != enable) {
((AbstractAccessMethod) method).setEnabled(enable);
((AbstractAccessMethod) method).setLastModified(new Date());
dbInstance.getCurrentEntityManager().merge(method);
}
}
}
}
use of org.olat.resource.accesscontrol.model.AccessMethod in project openolat by klemens.
the class ACMethodDAO method getAccessMethodForResources.
public List<OLATResourceAccess> getAccessMethodForResources(Collection<Long> resourceKeys, String resourceType, String excludedResourceType, boolean valid, Date atDate) {
// quicker to filter in java, numerous keys in "in" are slow
final int maxResourcesEntries = 250;
StringBuilder sb = new StringBuilder();
sb.append("select access.method, resource, offer.price from acofferaccess access, ").append(OLATResourceImpl.class.getName()).append(" resource").append(" inner join access.offer offer").append(" inner join offer.resource oResource").append(" where access.valid=").append(valid).append(" and offer.valid=").append(valid).append(" and resource.key=oResource.key");
if (resourceKeys != null && !resourceKeys.isEmpty()) {
if (resourceKeys.size() < maxResourcesEntries) {
sb.append(" and resource.key in (:resourceKeys) ");
}
sb.append(" and oResource.key=resource.key");
}
if (StringHelper.containsNonWhitespace(resourceType)) {
sb.append(" and oResource.resName =:resourceType ");
}
if (StringHelper.containsNonWhitespace(excludedResourceType)) {
sb.append(" and not(oResource.resName=:excludedResourceType)");
}
if (atDate != null) {
sb.append(" and (offer.validFrom is null or offer.validFrom<=:atDate)").append(" and (offer.validTo is null or offer.validTo>=:atDate)");
}
TypedQuery<Object[]> query = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), Object[].class);
if (atDate != null) {
query.setParameter("atDate", atDate, TemporalType.TIMESTAMP);
}
Set<Long> resourceKeysSet = null;
if (resourceKeys != null && !resourceKeys.isEmpty()) {
if (resourceKeys.size() < maxResourcesEntries) {
query.setParameter("resourceKeys", resourceKeys);
} else {
resourceKeysSet = new HashSet<Long>(resourceKeys);
}
}
if (StringHelper.containsNonWhitespace(resourceType)) {
query.setParameter("resourceType", resourceType);
}
if (StringHelper.containsNonWhitespace(excludedResourceType)) {
query.setParameter("excludedResourceType", excludedResourceType);
}
List<Object[]> rawResults = query.getResultList();
Map<Long, OLATResourceAccess> rawResultsMap = new HashMap<Long, OLATResourceAccess>();
for (Object[] rawResult : rawResults) {
AccessMethod method = (AccessMethod) rawResult[0];
OLATResource resource = (OLATResource) rawResult[1];
if (resourceKeysSet != null && !resourceKeysSet.contains(resource.getKey())) {
continue;
}
if (!method.isVisibleInGui()) {
continue;
}
Price price = (Price) rawResult[2];
if (rawResultsMap.containsKey(resource.getKey())) {
rawResultsMap.get(resource.getKey()).addBundle(price, method);
} else {
rawResultsMap.put(resource.getKey(), new OLATResourceAccess(resource, price, method));
}
}
return new ArrayList<OLATResourceAccess>(rawResultsMap.values());
}
use of org.olat.resource.accesscontrol.model.AccessMethod in project openolat by klemens.
the class ACOfferDAO method findOfferByResource.
public List<Offer> findOfferByResource(OLATResource resource, boolean valid, Date atDate) {
StringBuilder sb = new StringBuilder();
sb.append("select offer, access.method from acofferaccess access ").append(" inner join access.offer offer").append(" left join offer.resource resource").append(" where resource.key=:resourceKey").append(" and offer.valid=").append(valid);
if (atDate != null) {
sb.append(" and (offer.validFrom is null or offer.validFrom<=:atDate)").append(" and (offer.validTo is null or offer.validTo>=:atDate)");
}
TypedQuery<Object[]> query = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), Object[].class).setParameter("resourceKey", resource.getKey());
if (atDate != null) {
query.setParameter("atDate", atDate, TemporalType.TIMESTAMP);
}
List<Object[]> loadedObjects = query.getResultList();
List<Offer> offers = new ArrayList<>();
for (Object[] objects : loadedObjects) {
Offer offer = (Offer) objects[0];
AccessMethod method = (AccessMethod) objects[1];
if (method.isVisibleInGui()) {
offers.add(offer);
}
}
return offers;
}
use of org.olat.resource.accesscontrol.model.AccessMethod in project openolat by klemens.
the class AutoAccessManagerImpl method createAndPersistAdvanceOrderIfNotExist.
private void createAndPersistAdvanceOrderIfNotExist(Identity identity, IdentifierKey key, String value, Class<? extends AutoAccessMethod> type) {
List<AccessMethod> methods = acService.getAvailableMethodsByType(type);
AccessMethod method = methods.get(0);
if (doesNotExist(identity, key, value, method)) {
AdvanceOrder advanceOrder = advanceOrderDAO.create(identity, key, value, method);
advanceOrderDAO.save(advanceOrder);
}
}
Aggregations