001/*
002 *  Copyright 2016 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.activities.documents;
017
018import java.util.HashSet;
019import java.util.Map;
020import java.util.Set;
021
022import javax.jcr.Node;
023import javax.jcr.RepositoryException;
024import javax.jcr.version.Version;
025import javax.jcr.version.VersionManager;
026
027import org.ametys.plugins.explorer.ObservationConstants;
028import org.ametys.plugins.explorer.resources.jcr.JCRResource;
029import org.ametys.plugins.repository.activities.Activity;
030import org.ametys.plugins.repository.activities.ActivityType;
031import org.ametys.plugins.workspaces.activities.documents.AbstractResourceReferenceElementType.ResourceReference;
032
033/**
034 * {@link ActivityType} implementation for the renaming of a resource 
035 */
036public class ResourceCreatedOrUpdatedActivityType extends DocumentsActivityType
037{
038    @SuppressWarnings("unchecked")
039    @Override
040    public void setAdditionalActivityData(Activity activity, Map<String, Object> parameters) throws RepositoryException
041    {
042        super.setAdditionalActivityData(activity, parameters);
043        
044        // File data
045        Set<String> resourceIds = new HashSet<>();
046        if (parameters.containsKey(ObservationConstants.ARGS_RESOURCES))
047        {
048            resourceIds.addAll(((Map<String, Object>) parameters.get(ObservationConstants.ARGS_RESOURCES)).keySet());
049        }
050        else
051        {
052            resourceIds.add((String) parameters.get(ObservationConstants.ARGS_ID));
053        }
054        
055        ResourceReference[] references = new ResourceReference[resourceIds.size()];
056        int i = 0;
057        for (String resourceId : resourceIds)
058        {
059            JCRResource resource = (JCRResource) _ametysObjectResolver.resolveById(resourceId);
060            Node resourceNode = resource.getNode();
061            
062            VersionManager versionManager = resourceNode.getSession().getWorkspace().getVersionManager();
063            Version baseVersion = versionManager.getBaseVersion(resourceNode.getPath());
064            
065            // Store a reference to the version of the node instead of on the node itself
066            references[i] = new ResourceReference(resourceId, resource.getName(), null, resource.getMimeType(), baseVersion.getName());
067            i++;
068        }
069        activity.setValue(DocumentsActivityType.FILES_DATA_NAME, references);
070    }
071}