@Override protected String exec(AppCommandParser parser, Session session, Output out) throws ShellException { if (parser.arguments().size() < 2) { throw new ShellException( "Must supply <from-key> <to-key> " + "arguments, like: mv name \"given_name\""); } String fromKey = parser.arguments().get(0); String toKey = parser.arguments().get(1); boolean mayOverwrite = parser.options().containsKey("o"); NodeOrRelationship thing = getCurrent(session); if (!thing.hasProperty(fromKey)) { throw new ShellException("Property '" + fromKey + "' doesn't exist"); } if (thing.hasProperty(toKey)) { if (!mayOverwrite) { throw new ShellException( "Property '" + toKey + "' already exists, supply -o flag to overwrite"); } else { thing.removeProperty(toKey); } } Object value = thing.removeProperty(fromKey); thing.setProperty(toKey, value); return null; }
public String interpretLine(String line, Session session, Output out) throws ShellException { if (line == null || line.trim().length() == 0) { return ""; } line = replaceAlias(line, session); AppCommandParser parser = new AppCommandParser(this, line); return parser.app().execute(parser, session, out); }
@Override protected String exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException { List<TypedId> paths = readCurrentWorkingDir(session); NodeOrRelationship current = getCurrent(session); NodeOrRelationship newThing = null; if (parser.arguments().isEmpty()) { newThing = NodeOrRelationship.wrap(getServer().getDb().getReferenceNode()); paths.clear(); } else { String arg = parser.arguments().get(0); TypedId newId = current.getTypedId(); if (arg.equals("..")) { if (paths.size() > 0) { newId = paths.remove(paths.size() - 1); } } else if (arg.equals(".")) { } else if (arg.equals(START_ALIAS) || arg.equals(END_ALIAS)) { newId = getStartOrEnd(current, arg); paths.add(current.getTypedId()); } else { long suppliedId = -1; try { suppliedId = Long.parseLong(arg); } catch (NumberFormatException e) { suppliedId = findNodeWithTitle(current.asNode(), arg, session); if (suppliedId == -1) { throw new ShellException("No connected node with title '" + arg + "'"); } } newId = parser.options().containsKey("r") ? new TypedId(NodeOrRelationship.TYPE_RELATIONSHIP, suppliedId) : new TypedId(NodeOrRelationship.TYPE_NODE, suppliedId); if (newId.equals(current.getTypedId())) { throw new ShellException("Can't cd to where you stand"); } boolean absolute = parser.options().containsKey("a"); if (!absolute && !this.isConnected(current, newId)) { throw new ShellException( getDisplayName(getServer(), session, newId, false) + " isn't connected to the current primitive," + " use -a to force it to go there anyway"); } paths.add(current.getTypedId()); } newThing = this.getThingById(newId); } setCurrent(session, newThing); writeCurrentWorkingDir(paths, session); return null; }
public TabCompletion tabComplete(String partOfLine, Session session) throws ShellException, RemoteException { // TODO We can't assume it's an AppShellServer, can we? AppCommandParser parser = new AppCommandParser(this, partOfLine); App app = parser.app(); List<String> appCandidates = app.completionCandidates(partOfLine, session); appCandidates = quote(appCandidates); if (appCandidates.size() == 1) { appCandidates.set(0, appCandidates.get(0) + " "); } int cursor = partOfLine.length() - TextUtil.lastWordOrQuoteOf(partOfLine, true).length(); return new TabCompletion(appCandidates, cursor); }
private App getApp(AppCommandParser parser) throws Exception { String appName = parser.arguments().get(0).toLowerCase(); App app = this.getServer().findApp(appName); if (app == null) { throw new ShellException("No manual entry for '" + appName + "'"); } return app; }
@Override protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException { NodeOrRelationship node = null; if (parser.arguments().isEmpty()) { node = getCurrent(session); } else { long id = parseInt(parser.arguments().get(0)); try { node = wrap(getNodeById(id)); } catch (NotFoundException e) { throw new ShellException("No node " + id + " found"); } } if (!node.isNode()) { out.println("Please select a node to delete"); return Continuation.INPUT_COMPLETE; } boolean forceDeletion = parser.options().containsKey("f"); if (forceDeletion) { for (Relationship relationship : node.asNode().getRelationships()) { out.println( "Relationship " + getDisplayName(getServer(), session, relationship, true, false) + " deleted"); relationship.delete(); } } if (node.asNode().hasRelationship()) { throw new ShellException( getDisplayName(getServer(), session, node.asNode(), false) + " cannot be deleted because it still has relationships. Use -f to force deletion of its relationships"); } node.asNode().delete(); return Continuation.INPUT_COMPLETE; }
protected String replaceAlias(String line, Session session) throws ShellException { boolean changed = true; Set<String> appNames = new HashSet<String>(); while (changed) { changed = false; String appName = AppCommandParser.parseOutAppName(line); String prefixedKey = Alias.ALIAS_PREFIX + appName; String alias = (String) AbstractApp.safeGet(session, prefixedKey); if (alias != null && appNames.add(alias)) { changed = true; line = alias + line.substring(appName.length()); } } return line; }
@Override public Continuation execute(AppCommandParser parser, Session session, Output out) throws Exception { if (parser.arguments().size() == 0) { boolean list = parser.options().containsKey("l"); printHelpString(out, getServer(), list); return Continuation.INPUT_COMPLETE; } App app = this.getApp(parser); out.println(""); for (String line : splitLongLine(fixDesciption(app.getDescription()), CONSOLE_WIDTH)) { out.println(line); } println(out, ""); boolean hasOptions = false; for (String option : app.getAvailableOptions()) { hasOptions = true; String description = fixDesciption(app.getDescription(option)); String[] descriptionLines = splitLongLine(description, CONSOLE_WIDTH); for (int i = 0; i < descriptionLines.length; i++) { String line = ""; if (i == 0) { String optionPrefix = option.length() > 1 ? "--" : "-"; line = optionPrefix + option; } line += "\t "; line += descriptionLines[i]; println(out, line); } } if (hasOptions) { println(out, ""); } return Continuation.INPUT_COMPLETE; }
@Override protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException { assertCurrentIsNode(session); Node node = this.getCurrent(session).asNode(); boolean caseInsensitiveFilters = parser.options().containsKey("i"); boolean looseFilters = parser.options().containsKey("l"); boolean quiet = parser.options().containsKey("q"); // Order TraversalDescription description = Traversal.description(); String order = parser.options().get("o"); if (order != null) { description = description.order(parseOrder(order)); } // Relationship types / expander String relationshipTypes = parser.options().get("r"); if (relationshipTypes != null) { Map<String, Object> types = parseFilter(relationshipTypes, out); description = description.expand( toExpander(getServer().getDb(), null, types, caseInsensitiveFilters, looseFilters)); } // Uniqueness String uniqueness = parser.options().get("u"); if (uniqueness != null) { description = description.uniqueness(parseUniqueness(uniqueness)); } // Depth limit String depthLimit = parser.options().get("d"); if (depthLimit != null) { description = description.evaluator(toDepth(parseInt(depthLimit))); } String filterString = parser.options().get("f"); Map<String, Object> filterMap = filterString != null ? parseFilter(filterString, out) : null; String commandToRun = parser.options().get("c"); Collection<String> commandsToRun = new ArrayList<String>(); if (commandToRun != null) { commandsToRun.addAll(Arrays.asList(commandToRun.split(Pattern.quote("&&")))); } for (Path path : description.traverse(node)) { boolean hit = false; if (filterMap == null) { hit = true; } else { Node endNode = path.endNode(); Map<String, Boolean> matchPerFilterKey = new HashMap<String, Boolean>(); for (String key : endNode.getPropertyKeys()) { for (Map.Entry<String, Object> filterEntry : filterMap.entrySet()) { String filterKey = filterEntry.getKey(); if (matchPerFilterKey.containsKey(filterKey)) { continue; } if (matches( newPattern(filterKey, caseInsensitiveFilters), key, caseInsensitiveFilters, looseFilters)) { Object value = endNode.getProperty(key); String filterPattern = filterEntry.getValue() != null ? filterEntry.getValue().toString() : null; if (matches( newPattern(filterPattern, caseInsensitiveFilters), value.toString(), caseInsensitiveFilters, looseFilters)) { matchPerFilterKey.put(filterKey, true); } } } } if (matchPerFilterKey.size() == filterMap.size()) { hit = true; } } if (hit) { if (commandsToRun.isEmpty()) { printPath(path, quiet, session, out); } else { printAndInterpretTemplateLines( commandsToRun, false, true, NodeOrRelationship.wrap(path.endNode()), getServer(), session, out); } } } return Continuation.INPUT_COMPLETE; }