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