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; 027 028import org.apache.avalon.framework.logger.AbstractLogEnabled; 029import org.apache.avalon.framework.service.ServiceException; 030import org.apache.avalon.framework.service.ServiceManager; 031import org.apache.avalon.framework.service.Serviceable; 032import org.apache.commons.lang.StringUtils; 033 034import org.ametys.core.right.RightContextConvertor; 035import org.ametys.odf.ProgramItem; 036import org.ametys.odf.course.Course; 037import org.ametys.odf.courselist.CourseList; 038import org.ametys.odf.orgunit.OrgUnit; 039import org.ametys.odf.program.AbstractProgram; 040import org.ametys.odf.program.Program; 041import org.ametys.odf.program.ProgramPart; 042import org.ametys.plugins.repository.AmetysObjectResolver; 043import org.ametys.plugins.repository.UnknownAmetysObjectException; 044import org.ametys.plugins.repository.provider.AbstractRepository; 045 046 047/** 048 * This implementation convert the ODF contents to a Set of {@link OrgUnit} they belong to. 049 */ 050public class ODFRightsContextConvertor extends AbstractLogEnabled implements RightContextConvertor, Serviceable 051{ 052 private AmetysObjectResolver _resolver; 053 054 private Repository _repository; 055 056 @Override 057 public void service(ServiceManager smanager) throws ServiceException 058 { 059 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 060 _repository = (Repository) smanager.lookup(AbstractRepository.ROLE); 061 } 062 063 @Override 064 public Set<Object> convert(Object object) 065 { 066 Set<Object> convertedObjects = new HashSet<>(); 067 068 if (object instanceof ProgramItem) 069 { 070 Set<String> orgUnitIds = getOrgUnits((ProgramItem) object); 071 072 Session session = null; 073 try 074 { 075 // Force default workspace to get orgunit (we can be in archives workspace) 076 session = _repository.login("default"); 077 078 for (String id : orgUnitIds) 079 { 080 if (StringUtils.isNotEmpty(id)) 081 { 082 try 083 { 084 OrgUnit ou = _resolver.resolveById(id, session); 085 convertedObjects.add(ou); 086 } 087 catch (RepositoryException | UnknownAmetysObjectException e) 088 { 089 getLogger().error("Unable to get rights context for unknown orgunit of id '" + id + "'", e); 090 } 091 } 092 } 093 } 094 catch (RepositoryException e) 095 { 096 getLogger().error("Fail to get converted contexts for object " + object, e); 097 } 098 finally 099 { 100 if (session != null) 101 { 102 session.logout(); 103 } 104 } 105 106 if (object instanceof ProgramPart) 107 { 108 convertedObjects.addAll(((ProgramPart) object).getRootPrograms()); 109 } 110 } 111 112 return convertedObjects; 113 } 114 115 /** 116 * Get the orgunits for the content 117 * @param content the content 118 * @return the orgunits in a List 119 */ 120 protected Set<String> getOrgUnits (ProgramItem content) 121 { 122 List<String> ouIds = new ArrayList<>(); 123 124 if (content instanceof AbstractProgram) 125 { 126 ouIds = ((AbstractProgram) content).getOrgUnits(); 127 Set<Program> rootPrograms = ((AbstractProgram) content).getRootPrograms(); 128 for (Program program : rootPrograms) 129 { 130 ouIds.addAll(program.getOrgUnits()); 131 } 132 } 133 if (content instanceof ProgramPart) 134 { 135 Set<Program> rootPrograms = ((ProgramPart) content).getRootPrograms(); 136 for (Program program : rootPrograms) 137 { 138 ouIds.addAll(program.getOrgUnits()); 139 } 140 } 141 142 if (content instanceof CourseList) 143 { 144 List<Course> parentCourses = ((CourseList) content).getParentCourses(); 145 for (Course parentCourse : parentCourses) 146 { 147 ouIds.addAll(parentCourse.getOrgUnits()); 148 } 149 } 150 151 if (content instanceof Course) 152 { 153 ouIds = ((Course) content).getOrgUnits(); 154 } 155 156 return ouIds.stream().collect(Collectors.toSet()); 157 } 158}