001/*
002 *  Copyright 2020 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.repository.script;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import javax.jcr.Node;
024import javax.jcr.NodeIterator;
025import javax.jcr.RepositoryException;
026
027import org.ametys.plugins.core.ui.script.ScriptHandler;
028import org.ametys.plugins.repository.AmetysObject;
029import org.ametys.plugins.repository.AmetysObjectIterable;
030import org.ametys.plugins.repository.AmetysObjectIterator;
031
032/**
033 * {@link ScriptHandler} aware of {@link Node} and {@link AmetysObject}.
034 */
035public class RepositoryScriptHandler extends ScriptHandler
036{
037    @Override
038    protected ResultProcessor getProcessor()
039    {
040        return new RepositoryResultProcessor();
041    }
042    
043    static class RepositoryResultProcessor extends ResultProcessor
044    {
045        @Override
046        protected Object process(Map<String, Object> results, Object scriptResult)
047        {
048            try
049            {
050                if (scriptResult instanceof Node) 
051                {
052                    // JCR Node
053                    Node node = (Node) scriptResult;
054                    return _process(node);
055                } 
056                else if (scriptResult instanceof NodeIterator) 
057                {
058                    // JCR NodeIterator
059                    List<Object> nodePaths = new ArrayList<>();
060                    
061                    NodeIterator nodeIt = (NodeIterator) scriptResult;
062                    while (nodeIt.hasNext())
063                    {
064                        nodePaths.add(_process(nodeIt.nextNode()));
065                    }
066                    
067                    return nodePaths;
068                }
069                else if (scriptResult instanceof AmetysObject)
070                {
071                    // Ametys Object
072                    AmetysObject object = (AmetysObject) scriptResult;
073                    return _process(object);
074                }
075                else if (scriptResult instanceof AmetysObjectIterable<?>)
076                {
077                    AmetysObjectIterator<?> iterator = ((AmetysObjectIterable<?>) scriptResult).iterator();
078                    List<Object> objects = new ArrayList<>();
079                    
080                    while (iterator.hasNext())
081                    {
082                        objects.add(_process(iterator.next()));
083                    }
084                    
085                    return objects;
086                }
087            }
088            catch (RepositoryException e)
089            {
090                throw new RuntimeException(e);
091            }
092
093            return super.process(results, scriptResult);
094        }
095        
096        private Object _process(Node node) throws RepositoryException
097        {
098            Map<String, String> props = new HashMap<>();
099            props.put("type", "node");
100            props.put("path", node.getPath());
101            props.put("workspace", node.getSession().getWorkspace().getName());
102            
103            return props;
104        }
105        
106        private Object _process(AmetysObject object)
107        {
108            Map<String, String> props = new HashMap<>();
109            props.put("type", "ametys-object");
110            props.put("path", object.getPath());
111            props.put("id", object.getId());
112            
113            return props;
114        }
115    }
116}