001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.shell.automation;
020
021import java.util.Collections;
022import java.util.List;
023
024import jline.Completor;
025
026import org.nuxeo.ecm.automation.client.model.Document;
027import org.nuxeo.ecm.automation.client.model.Documents;
028import org.nuxeo.shell.Shell;
029import org.nuxeo.shell.ShellException;
030import org.nuxeo.shell.utils.Path;
031
032/**
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035public class DocRefCompletor implements Completor {
036
037    protected RemoteContext ctx;
038
039    public DocRefCompletor() {
040        this(Shell.get().getContextObject(RemoteContext.class));
041    }
042
043    public DocRefCompletor(RemoteContext ctx) {
044        this.ctx = ctx;
045    }
046
047    protected Document fetchDocument(String path) {
048        try {
049            return ctx.resolveDocument(path);
050        } catch (Exception e) {
051            return null;
052        }
053    }
054
055    @SuppressWarnings({ "rawtypes", "unchecked" })
056    public int complete(String buffer, int cursor, List clist) {
057        Document cdoc = ctx.getDocument();
058        String prefix = "";
059        if (buffer != null) {
060            if (buffer.endsWith("/")) {
061                Path path = new Path(buffer).removeTrailingSeparator();
062                prefix = buffer;
063                buffer = "";
064                cdoc = fetchDocument(path.toString());
065            } else if (buffer.indexOf('/') != -1) {
066                Path path = new Path(buffer);
067                buffer = path.lastSegment();
068                prefix = path.getParent().toString();
069                cdoc = fetchDocument(prefix);
070                prefix += '/';
071            }
072        }
073        if (cdoc == null) {
074            return -1;
075        }
076        try {
077            Documents docs = ctx.getDocumentService().getChildren(cdoc);
078            for (Document doc : docs) {
079                String name = new Path(doc.getPath()).lastSegment();
080                if (buffer == null) {
081                    clist.add(name);
082                } else if (name.startsWith(buffer)) {
083                    clist.add(prefix + name);
084                }
085            }
086            Collections.sort(clist);
087            if (clist.size() == 1) { // TODO add trailing / only if folderish
088                clist.set(0, ((String) clist.get(0)) + '/');
089            }
090            return clist.isEmpty() ? -1 : 0;
091        } catch (Exception e) {
092            throw new ShellException("Failed to gather children for " + buffer, e);
093        }
094    }
095}