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.AbstractIndexableContentProperty; 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 the nearest orgunits linked to a program item. 037 * This property seeks the orgunits in the parents when it's not defined in the given program item. 038 * It stop to the first parents who have orgunits 039 */ 040public class NearestOrgUnitsProperty extends AbstractIndexableContentProperty<Content> 041{ 042 /** The ODF Helper */ 043 protected ODFHelper _odfHelper; 044 /** The criterion definition helper */ 045 046 @Override 047 public void service(ServiceManager manager) throws ServiceException 048 { 049 super.service(manager); 050 _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE); 051 } 052 053 @Override 054 protected Set<? extends ModifiableContent> _getLinkedContents(Content content) 055 { 056 if (content instanceof ProgramItem programItem) 057 { 058 return _getSelfAndParentOrgUnits(programItem); 059 } 060 return null; 061 } 062 063 private Set<ModifiableContent> _getSelfAndParentOrgUnits(ProgramItem programItem) 064 { 065 Set<ModifiableContent> orgUnits = Arrays.stream(((Content) programItem).getValue(ProgramItem.ORG_UNITS_REFERENCES, false, new ContentValue[0])) 066 .map(ContentValue::getContentIfExists) 067 .flatMap(Optional::stream) 068 .collect(Collectors.toSet()); 069 070 // If the current program item has an orgUnit, dont search in the parent 071 if (orgUnits.isEmpty()) 072 { 073 List<ProgramItem> parents = _odfHelper.getParentProgramItems(programItem); 074 for (ProgramItem parent : parents) 075 { 076 orgUnits.addAll(_getSelfAndParentOrgUnits(parent)); 077 } 078 } 079 080 return orgUnits; 081 } 082 083 @Override 084 public String getDefaultCriterionWidget() 085 { 086 return "edition.select-orgunit"; 087 } 088 089 @Override 090 public boolean isMultiple() 091 { 092 return true; 093 } 094 095 public String getContentTypeId() 096 { 097 return OrgUnitFactory.ORGUNIT_CONTENT_TYPE; 098 } 099}