001/*
002 *  Copyright 2024 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.cms.properties.section.resource;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.LinkedHashMap;
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;
029
030import org.apache.avalon.framework.service.ServiceException;
031import org.apache.avalon.framework.service.ServiceManager;
032import org.apache.avalon.framework.service.Serviceable;
033
034import org.ametys.cms.content.references.OutgoingReferencesHelper;
035import org.ametys.cms.properties.section.AbstractDefaultPropertySection;
036import org.ametys.cms.repository.Content;
037import org.ametys.plugins.explorer.resources.Resource;
038import org.ametys.plugins.repository.AmetysObject;
039import org.ametys.plugins.repository.AmetysObjectResolver;
040import org.ametys.plugins.repository.jcr.JCRAmetysObject;
041
042/**
043 * Section to display references of the resource.
044 */
045public class ResourceReferencerSection extends AbstractDefaultPropertySection implements Serviceable
046{
047    private AmetysObjectResolver _resolver;
048    
049    public void service(ServiceManager manager) throws ServiceException
050    {
051        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
052    }
053    
054    public boolean supports(AmetysObject ametysObject)
055    {
056        return ametysObject instanceof Resource;
057    }
058    
059    @Override
060    protected Map<String, Object> buildData(AmetysObject ametysObject)
061    {
062        Map<String, Object> resultMap = new LinkedHashMap<>();
063        resultMap.put("contents", _incomingContents((Resource) ametysObject));
064        return resultMap;
065    }
066    
067    private List<Map<String, Object>> _incomingContents(Resource resource)
068    {
069        List<Map<String, Object>> json = new ArrayList<>();
070        
071        if (resource instanceof JCRAmetysObject ametysObject)
072        {
073            // Avoid duplicates
074            Set<Content> refContents = new HashSet<>();
075            
076            try
077            {
078                NodeIterator results = OutgoingReferencesHelper.getContentOutgoingReferences(ametysObject, "explorer");
079                while (results.hasNext())
080                {
081                    Node node = results.nextNode();
082                    
083                    try
084                    {
085                        Node contentNode = node.getParent()  // go up towards node 'ametys-internal:outgoing-references
086                                               .getParent()  // go up towards node 'ametys-internal:root-outgoing-references
087                                               .getParent(); // go up towards node of the content
088                        Content refContent = _resolver.resolve(contentNode, false);
089                        
090                        if (refContents.add(refContent))
091                        {
092                            Map<String, Object> contentJson = new HashMap<>();
093                            contentJson.put("id", refContent.getId());
094                            contentJson.put("title", refContent.getTitle());
095                            json.add(contentJson);
096                        }
097                    }
098                    catch (RepositoryException e)
099                    {
100                        getLogger().error("Impossible to get the linked content in the node {}", node.getPath());
101                    }
102                }
103            }
104            catch (RepositoryException e)
105            {
106                // Should not happen
107            }
108        }
109        
110        return json;
111    }
112}