Search in sources :

Example 41 with AccessMethod

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;
}
Also used : OrderStatus(org.olat.resource.accesscontrol.OrderStatus) Status(org.olat.resource.accesscontrol.ui.OrderTableItem.Status) AccessTransactionStatus(org.olat.resource.accesscontrol.model.AccessTransactionStatus) PSPTransactionStatus(org.olat.resource.accesscontrol.model.PSPTransactionStatus) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RawOrderItem(org.olat.resource.accesscontrol.model.RawOrderItem) OrderStatus(org.olat.resource.accesscontrol.OrderStatus) AccessMethod(org.olat.resource.accesscontrol.model.AccessMethod) OrderTableItem(org.olat.resource.accesscontrol.ui.OrderTableItem)

Example 42 with AccessMethod

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);
            }
        }
    }
}
Also used : ShibbolethAutoAccessMethod(org.olat.shibboleth.manager.ShibbolethAutoAccessMethod) AccessMethod(org.olat.resource.accesscontrol.model.AccessMethod) FreeAccessMethod(org.olat.resource.accesscontrol.model.FreeAccessMethod) AbstractAccessMethod(org.olat.resource.accesscontrol.model.AbstractAccessMethod) TokenAccessMethod(org.olat.resource.accesscontrol.model.TokenAccessMethod) AbstractAccessMethod(org.olat.resource.accesscontrol.model.AbstractAccessMethod) Date(java.util.Date)

Example 43 with AccessMethod

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());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OLATResource(org.olat.resource.OLATResource) OLATResourceImpl(org.olat.resource.OLATResourceImpl) OLATResourceAccess(org.olat.resource.accesscontrol.model.OLATResourceAccess) ShibbolethAutoAccessMethod(org.olat.shibboleth.manager.ShibbolethAutoAccessMethod) AccessMethod(org.olat.resource.accesscontrol.model.AccessMethod) FreeAccessMethod(org.olat.resource.accesscontrol.model.FreeAccessMethod) AbstractAccessMethod(org.olat.resource.accesscontrol.model.AbstractAccessMethod) TokenAccessMethod(org.olat.resource.accesscontrol.model.TokenAccessMethod) Price(org.olat.resource.accesscontrol.Price)

Example 44 with AccessMethod

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;
}
Also used : AccessMethod(org.olat.resource.accesscontrol.model.AccessMethod) Offer(org.olat.resource.accesscontrol.Offer) ArrayList(java.util.ArrayList)

Example 45 with AccessMethod

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);
    }
}
Also used : AutoAccessMethod(org.olat.resource.accesscontrol.provider.auto.model.AutoAccessMethod) AccessMethod(org.olat.resource.accesscontrol.model.AccessMethod) AdvanceOrder(org.olat.resource.accesscontrol.provider.auto.AdvanceOrder)

Aggregations

AccessMethod (org.olat.resource.accesscontrol.model.AccessMethod)92 TokenAccessMethod (org.olat.resource.accesscontrol.model.TokenAccessMethod)62 Test (org.junit.Test)56 FreeAccessMethod (org.olat.resource.accesscontrol.model.FreeAccessMethod)54 OLATResource (org.olat.resource.OLATResource)40 Identity (org.olat.core.id.Identity)22 ShibbolethAutoAccessMethod (org.olat.shibboleth.manager.ShibbolethAutoAccessMethod)22 BusinessGroup (org.olat.group.BusinessGroup)18 PaypalAccessMethod (org.olat.resource.accesscontrol.provider.paypal.model.PaypalAccessMethod)18 ArrayList (java.util.ArrayList)16 Date (java.util.Date)12 Offer (org.olat.resource.accesscontrol.Offer)12 FormLink (org.olat.core.gui.components.form.flexible.elements.FormLink)10 OfferAccess (org.olat.resource.accesscontrol.OfferAccess)10 AccessMethodHandler (org.olat.resource.accesscontrol.method.AccessMethodHandler)10 AbstractAccessMethod (org.olat.resource.accesscontrol.model.AbstractAccessMethod)10 HashSet (java.util.HashSet)8 FormLayoutContainer (org.olat.core.gui.components.form.flexible.impl.FormLayoutContainer)8 RepositoryEntry (org.olat.repository.RepositoryEntry)8 OLATResourceable (org.olat.core.id.OLATResourceable)6