001/* 002 * Copyright 2024 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.program.properties; 017 018import java.util.Arrays; 019import java.util.List; 020import java.util.Optional; 021import java.util.Set; 022import java.util.stream.Collectors; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026 027import org.ametys.cms.data.ContentValue; 028import org.ametys.cms.model.properties.AbstractContentProperty; 029import org.ametys.cms.repository.Content; 030import org.ametys.cms.repository.ModifiableContent; 031import org.ametys.odf.ODFHelper; 032import org.ametys.odf.ProgramItem; 033import org.ametys.odf.orgunit.OrgUnitFactory; 034 035/** 036 * Property to get all the orgunits linked to a program item (hold by it self and its parents) 037 */ 038public class SelfAndParentOrgUnitsProperty extends AbstractContentProperty<Content> 039{ 040 /** The ODF Helper */ 041 protected ODFHelper _odfHelper; 042 043 @Override 044 public void service(ServiceManager manager) throws ServiceException 045 { 046 super.service(manager); 047 _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE); 048 } 049 050 @Override 051 protected Set<? extends ModifiableContent> _getLinkedContents(Content content) 052 { 053 if (content instanceof ProgramItem programItem) 054 { 055 return _getSelfAndParentOrgUnits(programItem); 056 } 057 return null; 058 } 059 060 private Set<ModifiableContent> _getSelfAndParentOrgUnits(ProgramItem programItem) 061 { 062 Set<ModifiableContent> orgUnits = Arrays.stream(((Content) programItem).getValue(ProgramItem.ORG_UNITS_REFERENCES, false, new ContentValue[0])) 063 .map(ContentValue::getContentIfExists) 064 .flatMap(Optional::stream) 065 .collect(Collectors.toSet()); 066 067 List<ProgramItem> parents = _odfHelper.getParentProgramItems(programItem); 068 for (ProgramItem parent : parents) 069 { 070 orgUnits.addAll(_getSelfAndParentOrgUnits(parent)); 071 } 072 073 return orgUnits; 074 } 075 076 @Override 077 public String getCriterionWidget() 078 { 079 return "edition.select-orgunit"; 080 } 081 082 @Override 083 public boolean isMultiple() 084 { 085 return true; 086 } 087 088 public String getContentTypeId() 089 { 090 return OrgUnitFactory.ORGUNIT_CONTENT_TYPE; 091 } 092}