Search in sources :

Example 1 with OLATResourceAccess

use of org.olat.resource.accesscontrol.model.OLATResourceAccess in project OpenOLAT by OpenOLAT.

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 2 with OLATResourceAccess

use of org.olat.resource.accesscontrol.model.OLATResourceAccess in project OpenOLAT by OpenOLAT.

the class AuthoringEntryDataSource method processViewModel.

private List<AuthoringEntryRow> processViewModel(List<RepositoryEntryAuthorView> repoEntries) {
    Set<String> newNames = new HashSet<>();
    List<OLATResource> resourcesWithAC = new ArrayList<>(repoEntries.size());
    for (RepositoryEntryAuthorView entry : repoEntries) {
        if (entry.isOfferAvailable()) {
            resourcesWithAC.add(entry.getOlatResource());
        }
        final String author = entry.getAuthor();
        if (StringHelper.containsNonWhitespace(author)) {
            newNames.add(author);
        }
    }
    Map<String, String> fullNames = userManager.getUserDisplayNamesByUserName(newNames);
    List<OLATResourceAccess> resourcesWithOffer = acService.filterResourceWithAC(resourcesWithAC);
    Collection<OLATResourceable> resources = repoEntries.stream().map(RepositoryEntryAuthorView::getOlatResource).collect(Collectors.toList());
    List<ResourceLicense> licenses = licenseService.loadLicenses(resources);
    List<AuthoringEntryRow> items = new ArrayList<>();
    for (RepositoryEntryAuthorView entry : repoEntries) {
        String fullname = fullNames.get(entry.getAuthor());
        if (fullname == null) {
            fullname = entry.getAuthor();
        }
        AuthoringEntryRow row = new AuthoringEntryRow(entry, fullname);
        // bookmark
        row.setMarked(entry.isMarked());
        // access control
        List<PriceMethod> types = new ArrayList<>();
        if (entry.isMembersOnly()) {
            // members only always show lock icon
            types.add(new PriceMethod("", "o_ac_membersonly_icon", uifactory.getTranslator().translate("cif.access.membersonly.short")));
        } else {
            // collect access control method icons
            OLATResource resource = entry.getOlatResource();
            for (OLATResourceAccess resourceAccess : resourcesWithOffer) {
                if (resource.getKey().equals(resourceAccess.getResource().getKey())) {
                    for (PriceMethodBundle bundle : resourceAccess.getMethods()) {
                        String type = (bundle.getMethod().getMethodCssClass() + "_icon").intern();
                        String price = bundle.getPrice() == null || bundle.getPrice().isEmpty() ? "" : PriceFormat.fullFormat(bundle.getPrice());
                        AccessMethodHandler amh = acModule.getAccessMethodHandler(bundle.getMethod().getType());
                        String displayName = amh.getMethodName(uifactory.getTranslator().getLocale());
                        types.add(new PriceMethod(price, type, displayName));
                    }
                }
            }
        }
        if (!types.isEmpty()) {
            row.setAccessTypes(types);
        }
        // license
        for (ResourceLicense license : licenses) {
            OLATResource resource = entry.getOlatResource();
            if (license.getResId().equals(resource.getResourceableId()) && license.getResName().equals(resource.getResourceableTypeName())) {
                row.setLicense(license);
            }
        }
        uifactory.forgeLinks(row);
        items.add(row);
    }
    return items;
}
Also used : OLATResourceable(org.olat.core.id.OLATResourceable) ArrayList(java.util.ArrayList) OLATResource(org.olat.resource.OLATResource) OLATResourceAccess(org.olat.resource.accesscontrol.model.OLATResourceAccess) ResourceLicense(org.olat.core.commons.services.license.ResourceLicense) PriceMethod(org.olat.repository.ui.PriceMethod) RepositoryEntryAuthorView(org.olat.repository.RepositoryEntryAuthorView) AccessMethodHandler(org.olat.resource.accesscontrol.method.AccessMethodHandler) PriceMethodBundle(org.olat.resource.accesscontrol.model.PriceMethodBundle) HashSet(java.util.HashSet)

Example 3 with OLATResourceAccess

use of org.olat.resource.accesscontrol.model.OLATResourceAccess in project OpenOLAT by OpenOLAT.

the class RepositoryEntryACColumnDescriptor method render.

@Override
public void render(StringOutput sb, Renderer renderer, Object val, Locale locale, int alignment, String action) {
    if (val instanceof Collection) {
        Collection<?> accessTypes = (Collection<?>) val;
        for (Object accessType : accessTypes) {
            if (accessType instanceof String) {
                String type = (String) accessType;
                sb.append("<i class='o_icon ").append(type).append("_icon o_icon-lg'></i>");
            }
        }
    } else if (val instanceof Boolean) {
        boolean acessControlled = ((Boolean) val).booleanValue();
        if (acessControlled) {
            sb.append("<i class='o_icon o_ac_group_icon o_icon-lg'></i>");
        }
    } else if (val instanceof OLATResourceAccess) {
        OLATResourceAccess access = (OLATResourceAccess) val;
        for (PriceMethodBundle bundle : access.getMethods()) {
            Price price = bundle.getPrice();
            String type = bundle.getMethod().getMethodCssClass();
            if (price == null || price.isEmpty()) {
                sb.append("<i class='o_icon ").append(type).append("_icon o_icon-lg'></i>");
            } else {
                String p = PriceFormat.fullFormat(price);
                sb.append("<span class='o_nowrap'><i class='o_icon ").append(type).append("_icon o_icon-lg'></i> ").append(p).append("</span>");
            }
        }
    }
}
Also used : Price(org.olat.resource.accesscontrol.Price) Collection(java.util.Collection) OLATResourceAccess(org.olat.resource.accesscontrol.model.OLATResourceAccess) PriceMethodBundle(org.olat.resource.accesscontrol.model.PriceMethodBundle)

Example 4 with OLATResourceAccess

use of org.olat.resource.accesscontrol.model.OLATResourceAccess 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 5 with OLATResourceAccess

use of org.olat.resource.accesscontrol.model.OLATResourceAccess in project openolat by klemens.

the class RepositoryTableModel method addColumnDescriptors.

/**
 * @param tableCtr
 * @param selectButtonLabel Label of action row or null if no action row should be used
 * @param enableDirectLaunch
 * @return the position of the display name column
 */
public ColumnDescriptor addColumnDescriptors(TableController tableCtr, boolean selectTitle, boolean selectIcon, boolean remove, boolean infos) {
    Locale loc = translator.getLocale();
    CustomCellRenderer acRenderer = new RepositoryEntryACColumnDescriptor();
    tableCtr.addColumnDescriptor(new CustomRenderColumnDescriptor("table.header.ac", RepoCols.ac.ordinal(), null, loc, ColumnDescriptor.ALIGNMENT_LEFT, acRenderer) {

        @Override
        public int compareTo(int rowa, int rowb) {
            Object o1 = table.getTableDataModel().getObject(rowa);
            Object o2 = table.getTableDataModel().getObject(rowb);
            if (o1 == null || !(o1 instanceof RepositoryEntry))
                return -1;
            if (o2 == null || !(o2 instanceof RepositoryEntry))
                return 1;
            RepositoryEntry re1 = (RepositoryEntry) o1;
            RepositoryEntry re2 = (RepositoryEntry) o2;
            if (re1.isMembersOnly()) {
                if (!re2.isMembersOnly()) {
                    return 1;
                }
            } else if (re2.isMembersOnly()) {
                return -1;
            }
            OLATResourceAccess ac1 = repoEntriesWithOffer.get(re1.getOlatResource().getKey());
            OLATResourceAccess ac2 = repoEntriesWithOffer.get(re2.getOlatResource().getKey());
            if (ac1 == null && ac2 != null)
                return -1;
            if (ac1 != null && ac2 == null)
                return 1;
            if (ac1 != null && ac2 != null)
                return compareAccess(re1, ac1, re2, ac2);
            return super.compareString(re1.getDisplayname(), re2.getDisplayname());
        }

        private int compareAccess(RepositoryEntry re1, OLATResourceAccess ac1, RepositoryEntry re2, OLATResourceAccess ac2) {
            int s1 = ac1.getMethods().size();
            int s2 = ac2.getMethods().size();
            int compare = s1 - s2;
            if (compare != 0)
                return compare;
            if (s1 > 0 && s2 > 0) {
                String t1 = ac1.getMethods().get(0).getMethod().getType();
                String t2 = ac2.getMethods().get(0).getMethod().getType();
                int compareType = super.compareString(t1, t2);
                if (compareType != 0)
                    return compareType;
            }
            return super.compareString(re1.getDisplayname(), re2.getDisplayname());
        }
    });
    tableCtr.addColumnDescriptor(new RepositoryEntryTypeColumnDescriptor("table.header.typeimg", RepoCols.repoEntry.ordinal(), null, loc, ColumnDescriptor.ALIGNMENT_LEFT));
    String selectAction = selectTitle ? TABLE_ACTION_SELECT_LINK : null;
    if (repositoryModule.isManagedRepositoryEntries()) {
        tableCtr.addColumnDescriptor(false, new DefaultColumnDescriptor("table.header.externalid", RepoCols.externalId.ordinal(), selectAction, loc));
        tableCtr.addColumnDescriptor(new DefaultColumnDescriptor("table.header.externalref", RepoCols.externalRef.ordinal(), selectAction, loc));
    }
    boolean lfVisible = lifecycleDao.countPublicLifecycle() > 0;
    tableCtr.addColumnDescriptor(lfVisible, new DefaultColumnDescriptor("table.header.lifecycle.label", RepoCols.lifecycleLabel.ordinal(), null, loc));
    tableCtr.addColumnDescriptor(false, new DefaultColumnDescriptor("table.header.lifecycle.softkey", RepoCols.lifecycleSoftKey.ordinal(), null, loc));
    ColumnDescriptor nameColDesc = new DefaultColumnDescriptor("table.header.displayname", RepoCols.displayname.ordinal(), selectAction, loc) {

        @Override
        public int compareTo(int rowa, int rowb) {
            Object o1 = table.getTableDataModel().getValueAt(rowa, 1);
            Object o2 = table.getTableDataModel().getValueAt(rowb, 1);
            if (o1 == null || !(o1 instanceof RepositoryEntry))
                return -1;
            if (o2 == null || !(o2 instanceof RepositoryEntry))
                return 1;
            RepositoryEntry re1 = (RepositoryEntry) o1;
            RepositoryEntry re2 = (RepositoryEntry) o2;
            boolean c1 = RepositoryManager.getInstance().createRepositoryEntryStatus(re1.getStatusCode()).isClosed();
            boolean c2 = RepositoryManager.getInstance().createRepositoryEntryStatus(re2.getStatusCode()).isClosed();
            // same as Boolean compare
            int result = (c2 == c1 ? 0 : (c1 ? 1 : -1));
            if (result == 0) {
                Object a = table.getTableDataModel().getValueAt(rowa, dataColumn);
                Object b = table.getTableDataModel().getValueAt(rowb, dataColumn);
                if (a == null || !(a instanceof String))
                    return -1;
                if (b == null || !(b instanceof String))
                    return 1;
                String s1 = (String) a;
                String s2 = (String) b;
                result = compareString(s1, s2);
            }
            return result;
        }
    };
    tableCtr.addColumnDescriptor(nameColDesc);
    CustomCellRenderer dateRenderer = new DateCellRenderer(loc);
    tableCtr.addColumnDescriptor(false, new CustomRenderColumnDescriptor("table.header.lifecycle.start", RepoCols.lifecycleStart.ordinal(), null, loc, ColumnDescriptor.ALIGNMENT_LEFT, dateRenderer));
    tableCtr.addColumnDescriptor(false, new CustomRenderColumnDescriptor("table.header.lifecycle.end", RepoCols.lifecycleEnd.ordinal(), null, loc, ColumnDescriptor.ALIGNMENT_LEFT, dateRenderer));
    tableCtr.addColumnDescriptor(new DefaultColumnDescriptor("table.header.author", RepoCols.author.ordinal(), null, loc));
    CustomCellRenderer accessRenderer = new RepositoryEntryAccessColumnDescriptor(translator);
    ColumnDescriptor accessColDesc = new CustomRenderColumnDescriptor("table.header.access", RepoCols.repoEntry.ordinal(), null, loc, ColumnDescriptor.ALIGNMENT_LEFT, accessRenderer) {

        @Override
        public int compareTo(int rowa, int rowb) {
            Object o1 = table.getTableDataModel().getValueAt(rowa, 1);
            Object o2 = table.getTableDataModel().getValueAt(rowb, 1);
            if (o1 == null || !(o1 instanceof RepositoryEntry))
                return -1;
            if (o2 == null || !(o2 instanceof RepositoryEntry))
                return 1;
            RepositoryEntry re1 = (RepositoryEntry) o1;
            RepositoryEntry re2 = (RepositoryEntry) o2;
            int ar1 = re1.getAccess();
            if (re1.isMembersOnly()) {
                ar1 = 99;
            }
            int ar2 = re2.getAccess();
            if (re2.isMembersOnly()) {
                ar2 = 99;
            }
            if (ar1 < ar2)
                return -1;
            if (ar1 > ar2)
                return 1;
            return super.compareString(re1.getDisplayname(), re2.getDisplayname());
        }
    };
    tableCtr.addColumnDescriptor(accessColDesc);
    tableCtr.addColumnDescriptor(false, new DefaultColumnDescriptor("table.header.date", RepoCols.creationDate.ordinal(), null, loc));
    tableCtr.addColumnDescriptor(false, new DefaultColumnDescriptor("table.header.lastusage", RepoCols.lastUsage.ordinal(), null, loc));
    if (infos) {
        tableCtr.addColumnDescriptor(new StaticColumnDescriptor(TABLE_ACTION_INFOS, "info.header", translator.translate("info.header")) {

            @Override
            public void renderValue(StringOutput so, int row, Renderer renderer) {
                so.append("<i id='o_core").append(row).append("ref' class='o_icon o_icon-lg o_icon_info_resource'> </i>");
            }
        });
    }
    if (selectIcon) {
        tableCtr.addColumnDescriptor(new StaticColumnDescriptor(TABLE_ACTION_SELECT_LINK, "select", translator.translate("table.select")));
    }
    if (remove) {
        tableCtr.addColumnDescriptor(new StaticColumnDescriptor(TABLE_ACTION_REMOVE_LINK, "remove", translator.translate("remove")));
    }
    return nameColDesc;
}
Also used : Locale(java.util.Locale) CustomRenderColumnDescriptor(org.olat.core.gui.components.table.CustomRenderColumnDescriptor) CustomRenderColumnDescriptor(org.olat.core.gui.components.table.CustomRenderColumnDescriptor) DefaultColumnDescriptor(org.olat.core.gui.components.table.DefaultColumnDescriptor) StaticColumnDescriptor(org.olat.core.gui.components.table.StaticColumnDescriptor) ColumnDescriptor(org.olat.core.gui.components.table.ColumnDescriptor) StaticColumnDescriptor(org.olat.core.gui.components.table.StaticColumnDescriptor) StringOutput(org.olat.core.gui.render.StringOutput) RepositoryEntry(org.olat.repository.RepositoryEntry) OLATResourceAccess(org.olat.resource.accesscontrol.model.OLATResourceAccess) CustomCellRenderer(org.olat.core.gui.components.table.CustomCellRenderer) DateCellRenderer(org.olat.core.gui.components.table.DateCellRenderer) CustomCellRenderer(org.olat.core.gui.components.table.CustomCellRenderer) Renderer(org.olat.core.gui.render.Renderer) DateCellRenderer(org.olat.core.gui.components.table.DateCellRenderer) DefaultColumnDescriptor(org.olat.core.gui.components.table.DefaultColumnDescriptor)

Aggregations

OLATResourceAccess (org.olat.resource.accesscontrol.model.OLATResourceAccess)12 ArrayList (java.util.ArrayList)8 OLATResource (org.olat.resource.OLATResource)8 PriceMethodBundle (org.olat.resource.accesscontrol.model.PriceMethodBundle)8 PriceMethod (org.olat.repository.ui.PriceMethod)6 AccessMethodHandler (org.olat.resource.accesscontrol.method.AccessMethodHandler)6 RepositoryEntry (org.olat.repository.RepositoryEntry)4 Price (org.olat.resource.accesscontrol.Price)4 Collection (java.util.Collection)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Locale (java.util.Locale)2 ResourceLicense (org.olat.core.commons.services.license.ResourceLicense)2 ColumnDescriptor (org.olat.core.gui.components.table.ColumnDescriptor)2 CustomCellRenderer (org.olat.core.gui.components.table.CustomCellRenderer)2 CustomRenderColumnDescriptor (org.olat.core.gui.components.table.CustomRenderColumnDescriptor)2 DateCellRenderer (org.olat.core.gui.components.table.DateCellRenderer)2 DefaultColumnDescriptor (org.olat.core.gui.components.table.DefaultColumnDescriptor)2 StaticColumnDescriptor (org.olat.core.gui.components.table.StaticColumnDescriptor)2