001    /*
002     * Copyright (C) 2010 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.jcr.command;
021    
022    import org.crsh.cmdline.IntrospectionException;
023    import org.crsh.cmdline.ParameterDescriptor;
024    import org.crsh.cmdline.completers.AbstractPathCompleter;
025    import org.crsh.cmdline.spi.Completer;
026    import org.crsh.cmdline.spi.ValueCompletion;
027    import org.crsh.command.CRaSHCommand;
028    
029    import javax.jcr.Node;
030    import javax.jcr.NodeIterator;
031    import javax.jcr.PathNotFoundException;
032    import javax.jcr.RepositoryException;
033    import javax.jcr.Session;
034    import java.util.ArrayList;
035    import java.util.Collection;
036    import java.util.List;
037    
038    /**
039     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
040     * @version $Revision$
041     */
042    public abstract class JCRCommand extends CRaSHCommand implements Completer {
043    
044      protected JCRCommand() throws IntrospectionException {
045      }
046    
047      public ValueCompletion complete(ParameterDescriptor<?> parameter, String prefix) throws Exception {
048        if (parameter.getJavaValueType() == Path.class) {
049    
050          final Path path = (Path)getProperty("currentPath");
051          final Session session = (Session)getProperty("session");
052    
053          //
054          if (session != null) {
055    
056            AbstractPathCompleter<Node> pc = new AbstractPathCompleter<Node>() {
057              @Override
058              protected String getCurrentPath() throws Exception {
059                return path != null ? path.getString() : "/";
060              }
061    
062              @Override
063              protected Node getPath(String path) throws Exception {
064                try {
065                  return (Node)session.getItem(path);
066                }
067                catch (RepositoryException e) {
068                  return null;
069                }
070              }
071    
072              @Override
073              protected boolean exists(Node path) throws Exception {
074                return path != null;
075              }
076    
077              @Override
078              protected boolean isDirectory(Node path) throws Exception {
079                return true;
080              }
081    
082              @Override
083              protected boolean isFile(Node path) throws Exception {
084                return false;
085              }
086    
087              @Override
088              protected Collection<Node> getChilren(Node path) throws Exception {
089                List<Node> children = new ArrayList<Node>();
090                for (NodeIterator i = path.getNodes();i.hasNext();) {
091                  Node child = i.nextNode();
092                  children.add(child);
093                }
094                return children;
095              }
096    
097              @Override
098              protected String getName(Node path) throws Exception {
099                return path.getName();
100              }
101            };
102    
103            //
104            return pc.complete(parameter, prefix);
105          }
106        }
107    
108        //
109        return ValueCompletion.create();
110      }
111    }