001/*
002 *  Copyright 2021 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.observers;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.core.observation.AsyncObserver;
025import org.ametys.core.observation.Event;
026import org.ametys.core.user.UserIdentity;
027import org.ametys.plugins.explorer.ObservationConstants;
028import org.ametys.plugins.explorer.resources.Resource;
029import org.ametys.plugins.explorer.resources.ResourceCollection;
030import org.ametys.plugins.repository.AmetysObject;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.plugins.workspaces.documents.onlyoffice.OnlyOfficeManager;
033import org.ametys.plugins.workspaces.project.objects.Project;
034
035/**
036 * This observer observes event of resources to generate thumbnail if only office is enabled
037 */
038public class FileThumbnailObserver implements AsyncObserver, Serviceable
039{
040    //FIXME pass this observer in async when currentUserProvider return not null current user in async observers
041    
042    /** The only office manager */
043    protected OnlyOfficeManager _onlyOfficeManager;
044    
045    /** The Ametys object resolver */
046    protected AmetysObjectResolver _resolver;
047    
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _onlyOfficeManager = (OnlyOfficeManager) manager.lookup(OnlyOfficeManager.ROLE);
051        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
052    }
053    
054    public int getPriority(Event event)
055    {
056        return MAX_PRIORITY;
057    }
058
059    public boolean supports(Event event)
060    {
061        return _onlyOfficeManager.isOnlyOfficeAvailable()
062            && (
063                event.getId().equals(ObservationConstants.EVENT_RESOURCE_CREATED) 
064             || event.getId().equals(ObservationConstants.EVENT_RESOURCE_UPDATED)
065             || event.getId().equals(ObservationConstants.EVENT_RESOURCE_DELETED)
066             || event.getId().equals(ObservationConstants.EVENT_COLLECTION_DELETING)
067             || event.getId().equals(org.ametys.plugins.workspaces.ObservationConstants.EVENT_PROJECT_DELETED)
068            );
069    }
070
071    public void observe(Event event, Map<String, Object> transientVars) throws Exception
072    {
073        Map<String, Object> args = event.getArguments();
074        UserIdentity currentUser = event.getIssuer();
075        
076        if (event.getId().equals(org.ametys.plugins.workspaces.ObservationConstants.EVENT_PROJECT_DELETED))
077        {
078            String projectName = (String) event.getArguments().get(org.ametys.plugins.workspaces.ObservationConstants.ARGS_PROJECT_NAME);
079            _onlyOfficeManager.deleteProjectThumbnailsInCache(projectName);
080        }
081        else
082        {
083            String parentId = (String) args.get(ObservationConstants.ARGS_PARENT_ID);
084            AmetysObject ao = _resolver.resolveById(parentId);
085            
086            Project project = getProject(ao);
087            if (project != null)
088            {
089                String projectName = project.getName();
090                if (event.getId().equals(ObservationConstants.EVENT_RESOURCE_CREATED))
091                {
092                    @SuppressWarnings("unchecked")
093                    Map<String, Resource> resources = (Map<String, Resource>) args.get(ObservationConstants.ARGS_RESOURCES);
094                    for (String resourceId : resources.keySet())
095                    {
096                        _onlyOfficeManager.generateThumbnailInCache(projectName, resourceId, currentUser);
097                    }
098                }
099                else if (event.getId().equals(ObservationConstants.EVENT_RESOURCE_UPDATED))
100                {
101                    String resourceId = (String) args.get(ObservationConstants.ARGS_ID);
102                    _onlyOfficeManager.deleteThumbnailInCache(projectName, resourceId);
103                    _onlyOfficeManager.generateThumbnailInCache(projectName, resourceId, currentUser);
104                }
105                else if (event.getId().equals(ObservationConstants.EVENT_RESOURCE_DELETED))
106                {
107                    String resourceId = (String) args.get(ObservationConstants.ARGS_ID);
108                    _onlyOfficeManager.deleteThumbnailInCache(projectName, resourceId);
109                }
110                else if (event.getId().equals(ObservationConstants.EVENT_COLLECTION_DELETING))
111                {
112                    String resourceCollectionId = (String) args.get(ObservationConstants.ARGS_ID);
113                    ResourceCollection resourceCollection = _resolver.resolveById(resourceCollectionId);
114                    _deleteResourcesInCaches(projectName, resourceCollection);
115                }
116            }
117        }
118    }
119    
120    /**
121     * Delete resource thumbnail in cache from the a resource collection
122     * @param projectName the project name
123     * @param resourceCollection the resource collection
124     */
125    protected void _deleteResourcesInCaches(String projectName, ResourceCollection resourceCollection)
126    {
127        for (AmetysObject child : resourceCollection.getChildren())
128        {
129            if (child instanceof ResourceCollection)
130            {
131                _deleteResourcesInCaches(projectName, (ResourceCollection) child);
132            }
133            else if (child instanceof Resource)
134            {
135                _onlyOfficeManager.deleteThumbnailInCache(projectName, child.getId());
136            }
137        }
138    }
139    
140    /**
141     * Get the parent project
142     * @param ao The ametys object
143     * @return The parent project or <code>null</code> if not found
144     */
145    protected Project getProject(AmetysObject ao)
146    {
147        AmetysObject parentAO = ao;
148        while (parentAO != null)
149        {
150            if (parentAO instanceof Project)
151            {
152                return (Project) parentAO;
153            } 
154            parentAO = parentAO.getParent();
155        }
156        
157        return null;
158    }
159}