001/* 002 * Copyright 2025 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.observation.skill; 017 018import org.apache.avalon.framework.service.ServiceException; 019import org.apache.avalon.framework.service.ServiceManager; 020import org.apache.avalon.framework.service.Serviceable; 021 022import org.ametys.cms.ObservationConstants; 023import org.ametys.cms.contenttype.ContentTypesHelper; 024import org.ametys.cms.data.ContentValue; 025import org.ametys.cms.repository.Content; 026import org.ametys.cms.repository.ContentDAO; 027import org.ametys.core.observation.Event; 028import org.ametys.core.observation.Observer; 029import org.ametys.odf.program.Program; 030import org.ametys.odf.skill.workflow.SkillEditionFunction; 031import org.ametys.runtime.plugin.component.AbstractLogEnabled; 032 033/** 034 * Abstract observer to prepare skills observer when a content is updated 035 */ 036public abstract class AbstractSkillsObserver extends AbstractLogEnabled implements Observer, Serviceable 037{ 038 /** The content dao */ 039 protected ContentDAO _contentDAO; 040 /** The content types helper */ 041 protected ContentTypesHelper _contentTypesHelper; 042 043 public void service(ServiceManager manager) throws ServiceException 044 { 045 _contentDAO = (ContentDAO) manager.lookup(ContentDAO.ROLE); 046 _contentTypesHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE); 047 } 048 049 @Override 050 public int getPriority() 051 { 052 return MIN_PRIORITY - 3000; 053 } 054 055 public boolean supports(Event event) 056 { 057 if (event.getId().equals(getSupportedEventId())) 058 { 059 Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT); 060 return supportsContent(content); 061 } 062 063 return false; 064 } 065 066 /** 067 * Get the supported event id 068 * @return the event id 069 */ 070 protected abstract String getSupportedEventId(); 071 072 /** 073 * Check if the content is supported 074 * @param content The content 075 * @return a {@link Boolean}, <code>true</code> if the content is supported, <code>false</code> otherwise 076 */ 077 protected boolean supportsContent(Content content) 078 { 079 // By default, we support Programs and Macro skills 080 return content instanceof Program || _contentTypesHelper.isInstanceOf(content, SkillEditionFunction.MACRO_SKILL_TYPE); 081 } 082 083 /** 084 * Retrieve the skill from a content 085 * @param content The content 086 * @return The skills 087 */ 088 protected ContentValue[] _getSkills(Content content) 089 { 090 return content.getValue(getSkillsAttributeName(content)); 091 } 092 093 /** 094 * Get the skills attribute name 095 * @param content The content 096 * @return The attribute name for the skills 097 */ 098 protected String getSkillsAttributeName(Content content) 099 { 100 // By default, we support Programs and Macro skills 101 if (content instanceof Program) 102 { 103 return "ownSkills"; 104 } 105 else if (_contentTypesHelper.isInstanceOf(content, SkillEditionFunction.MACRO_SKILL_TYPE)) 106 { 107 return "microSkills"; 108 } 109 110 return "skills"; 111 } 112}