001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.odf.rights; 017 018import java.util.ArrayList; 019import java.util.HashSet; 020import java.util.List; 021import java.util.Set; 022import java.util.stream.Collectors; 023 024import javax.jcr.Repository; 025import javax.jcr.RepositoryException; 026import javax.jcr.Session; 027import org.apache.avalon.framework.service.ServiceException; 028import org.apache.avalon.framework.service.ServiceManager; 029import org.apache.avalon.framework.service.Serviceable; 030import org.apache.commons.lang.StringUtils; 031 032import org.ametys.core.right.RightContextConvertor; 033import org.ametys.odf.ProgramItem; 034import org.ametys.odf.course.Course; 035import org.ametys.odf.courselist.CourseList; 036import org.ametys.odf.orgunit.OrgUnit; 037import org.ametys.odf.program.AbstractProgram; 038import org.ametys.odf.program.Program; 039import org.ametys.odf.program.ProgramPart; 040import org.ametys.plugins.repository.AmetysObjectResolver; 041import org.ametys.plugins.repository.UnknownAmetysObjectException; 042import org.ametys.plugins.repository.provider.AbstractRepository; 043import org.ametys.runtime.plugin.component.AbstractLogEnabled; 044 045 046/** 047 * This implementation convert the ODF contents to a Set of {@link OrgUnit} they belong to. 048 */ 049public class ODFRightsContextConvertor extends AbstractLogEnabled implements RightContextConvertor, Serviceable 050{ 051 private AmetysObjectResolver _resolver; 052 053 private Repository _repository; 054 055 @Override 056 public void service(ServiceManager smanager) throws ServiceException 057 { 058 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 059 _repository = (Repository) smanager.lookup(AbstractRepository.ROLE); 060 } 061 062 @Override 063 public Set<Object> convert(Object object) 064 { 065 Set<Object> convertedObjects = new HashSet<>(); 066 067 if (object instanceof ProgramItem) 068 { 069 Set<String> orgUnitIds = getOrgUnits((ProgramItem) object); 070 071 Session session = null; 072 try 073 { 074 // Force default workspace to get orgunit (we can be in archives workspace) 075 session = _repository.login("default"); 076 077 for (String id : orgUnitIds) 078 { 079 if (StringUtils.isNotEmpty(id)) 080 { 081 try 082 { 083 OrgUnit ou = _resolver.resolveById(id, session); 084 convertedObjects.add(ou); 085 } 086 catch (RepositoryException | UnknownAmetysObjectException e) 087 { 088 getLogger().warn("Unable to get rights context for unknown orgunit of id '{}'", id, e); 089 } 090 } 091 } 092 } 093 catch (RepositoryException e) 094 { 095 getLogger().warn("Fail to get converted contexts for object {}", object, e); 096 } 097 finally 098 { 099 if (session != null) 100 { 101 session.logout(); 102 } 103 } 104 105 if (object instanceof ProgramPart) 106 { 107 convertedObjects.addAll(((ProgramPart) object).getRootPrograms()); 108 } 109 } 110 111 return convertedObjects; 112 } 113 114 /** 115 * Get the orgunits for the content 116 * @param content the content 117 * @return the orgunits in a List 118 */ 119 protected Set<String> getOrgUnits (ProgramItem content) 120 { 121 List<String> ouIds = new ArrayList<>(); 122 123 if (content instanceof AbstractProgram) 124 { 125 ouIds = ((AbstractProgram) content).getOrgUnits(); 126 Set<Program> rootPrograms = ((AbstractProgram) content).getRootPrograms(); 127 for (Program program : rootPrograms) 128 { 129 ouIds.addAll(program.getOrgUnits()); 130 } 131 } 132 if (content instanceof ProgramPart) 133 { 134 Set<Program> rootPrograms = ((ProgramPart) content).getRootPrograms(); 135 for (Program program : rootPrograms) 136 { 137 ouIds.addAll(program.getOrgUnits()); 138 } 139 } 140 141 if (content instanceof CourseList) 142 { 143 List<Course> parentCourses = ((CourseList) content).getParentCourses(); 144 for (Course parentCourse : parentCourses) 145 { 146 ouIds.addAll(parentCourse.getOrgUnits()); 147 } 148 } 149 150 if (content instanceof Course) 151 { 152 ouIds = ((Course) content).getOrgUnits(); 153 } 154 155 return ouIds.stream().collect(Collectors.toSet()); 156 } 157}