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.events.documents;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.HashSet;
022import java.util.List;
023import java.util.Map;
024import java.util.Set;
025
026import javax.jcr.Node;
027import javax.jcr.NodeIterator;
028import javax.jcr.RepositoryException;
029import javax.jcr.version.Version;
030import javax.jcr.version.VersionManager;
031
032import org.ametys.cms.transformation.xslt.ResolveURIComponent;
033import org.ametys.plugins.explorer.ObservationConstants;
034import org.ametys.plugins.explorer.resources.Resource;
035import org.ametys.plugins.explorer.resources.jcr.JCRResource;
036import org.ametys.plugins.repository.UnknownAmetysObjectException;
037import org.ametys.plugins.repository.events.EventType;
038
039/**
040 * {@link EventType} implementation for the renaming of a resource 
041 */
042public class ResourceCreatedOrUpdatedEventType extends DocumentsEventType
043{
044    @SuppressWarnings("unchecked")
045    @Override
046    protected void storeAdditionalEventData(Node eventNode, Map<String, Object> parameters) throws RepositoryException
047    {
048        super.storeAdditionalEventData(eventNode, parameters);
049        
050        // File data
051        Set<String> resourceIds = new HashSet<>();
052        if (parameters.containsKey(ObservationConstants.ARGS_RESOURCES))
053        {
054            resourceIds.addAll(((Map<String, Object>) parameters.get(ObservationConstants.ARGS_RESOURCES)).keySet());
055        }
056        else
057        {
058            resourceIds.add((String) parameters.get(ObservationConstants.ARGS_ID));
059        }
060        
061        int count = 1;
062        for (String resourceId : resourceIds)
063        {
064            JCRResource resource = (JCRResource) _ametysObjectResolver.resolveById(resourceId);
065            Node resourceNode = resource.getNode();
066            
067            VersionManager versionManager = resourceNode.getSession().getWorkspace().getVersionManager();
068            Version baseVersion = versionManager.getBaseVersion(resourceNode.getPath());
069            
070            // Store a reference to the version of the node instead of on the node itself
071            Node fileNode = eventNode.addNode("file" + (count > 1 ? "-" + count : ""));
072            resource.getPath();
073            fileNode.setProperty("name", resourceNode.getName());
074            fileNode.setProperty("resourceId", resourceId); 
075            fileNode.setProperty("mimeType", resource.getMimeType());
076            fileNode.setProperty("baseVersionName", baseVersion.getName());
077            
078            count++;
079        }
080    }
081
082    @Override
083    public Map<String, Object> event2JSON(Node eventNode) throws RepositoryException
084    {
085        Map<String, Object> event = super.event2JSON(eventNode);
086        
087        List<Map<String, Object>> files = new ArrayList<>();
088        
089        NodeIterator nodesIt = eventNode.getNodes("file*");
090        while (nodesIt.hasNext())
091        {
092            Node fileNode = (Node) nodesIt.next();
093            
094            String resourceId = fileNode.getProperty("resourceId").getString();
095            
096            try
097            {
098                Resource resource = (Resource) _ametysObjectResolver.resolveById(resourceId);
099                
100                Map<String, Object> fileData = new HashMap<>();
101                String versionName = fileNode.getProperty("baseVersionName").getString();
102                fileData.put("version", versionName);
103                fileData.put("id", resource.getId());
104                fileData.put("name", fileNode.getProperty("name").getString());
105                fileData.put("mimeType", fileNode.getProperty("mimeType").getString());
106                fileData.put("downloadUrl", ResolveURIComponent.resolve("project-resource", resource.getId() + ";" + versionName, true));
107                
108                // TODO version
109                if (resource.getMimeType().startsWith("image/"))
110                {
111                    fileData.put("thumbnailUrl", ResolveURIComponent.resolveBoundedImage("project-resource", resource.getId(), 100, 100));
112                }
113                files.add(fileData);
114            }
115            catch (UnknownAmetysObjectException e)
116            {
117                // The resource does not exist anymore
118            }
119        }
120        
121        if (files.size() == 0)
122        {
123            return Collections.EMPTY_MAP;
124        }
125        else if (files.size() == 1)
126        {
127            event.put("file", files.get(0));
128        }
129        else
130        {
131            event.put("files", files);
132        }
133        
134        return event;
135    }
136}