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.plugins.workspaces.documents;
017
018import java.util.Date;
019
020import org.apache.avalon.framework.component.Component;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.core.user.CurrentUserProvider;
026import org.ametys.plugins.ai.AIHelper;
027import org.ametys.plugins.explorer.resources.ModifiableResourceCollection;
028import org.ametys.plugins.workspaces.documents.jcr.File;
029import org.ametys.runtime.config.Config;
030import org.ametys.runtime.plugin.component.AbstractLogEnabled;
031
032/**
033 * Helper class for AI-related functionalities in workspace.
034 */
035public class WorkspaceFileAIHelper extends AbstractLogEnabled implements Component, Serviceable
036{
037    /** Avalon role */
038    public static final String ROLE = WorkspaceFileAIHelper.class.getName();
039    
040    /** The summary max lentgh */
041    public static final Integer SUMMARY_MAX_LENGTH = 200;
042    
043    private static final String __CONFIG_IS_AUTOMATIC_GENERATION_ENABLED = "workspaces.ai.docsummary.automatic.enabled";
044    
045    private AIHelper _aiHelper;
046    private CurrentUserProvider _currentUserProvider;
047    
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _aiHelper = (AIHelper) manager.lookup(AIHelper.ROLE);
051        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
052    }
053    
054    /**
055     * Get the value of the configuration param
056     * @return true if it's true, false if it's false
057     */
058    public boolean isAutomaticGenerationEnabled()
059    {
060        return _aiHelper.isEnabled()
061                && Config.getInstance().getValue(__CONFIG_IS_AUTOMATIC_GENERATION_ENABLED, false, false);
062    }
063    
064    /**
065     * Generate the description and set it to the file
066     * @param file the file to summarize
067     * @return String the generated summary
068     * @throws Exception if an error occurred
069     */
070    public String generateFileDescription(File file) throws Exception
071    {
072        String summary = _aiHelper.resourceToSummary(file, SUMMARY_MAX_LENGTH);
073        file.setDescription(summary);
074        file.setIsAutoGeneratedDescription(true);
075        
076        file.setLastContributor(_currentUserProvider.getUser());
077        file.setLastModified(new Date());
078
079        ModifiableResourceCollection parent = file.getParent();
080        parent.saveChanges();
081        file.checkpoint();
082        
083        return summary;
084    }
085}