Search in sources :

Example 1 with MaintenanceCommand

use of org.structr.core.graph.MaintenanceCommand in project structr by structr.

the class InitConsoleCommand method run.

@Override
public void run(final SecurityContext securityContext, final List<String> parameters, final Writable writable) throws FrameworkException, IOException {
    final Principal user = securityContext.getUser(false);
    final int paramCount = parameters.size();
    if (user != null && user.isAdmin()) {
        NodeServiceCommand command = null;
        boolean allNodes = true;
        boolean allRels = true;
        boolean isCreateLabels = false;
        boolean isIndex = false;
        boolean isIds = false;
        boolean hasCommand = false;
        boolean hasFor = false;
        boolean error = false;
        String type = null;
        String typeKey = null;
        String mode = null;
        // parse parameters
        for (int i = 1; i < paramCount && !error; i++) {
            final String param = getParameter(parameters, i);
            switch(param) {
                case "index":
                    if (hasCommand) {
                        writable.println("Syntax error, too many parameters.");
                        error = true;
                    } else {
                        command = StructrApp.getInstance(securityContext).command(BulkRebuildIndexCommand.class);
                        command.setLogBuffer(writable);
                        isIndex = true;
                        hasCommand = true;
                    }
                    break;
                case "ids":
                    if (hasCommand) {
                        writable.println("Syntax error, too many parameters.");
                        error = true;
                    } else {
                        command = StructrApp.getInstance(securityContext).command(BulkSetUuidCommand.class);
                        command.setLogBuffer(writable);
                        isIds = true;
                        hasCommand = true;
                    }
                    break;
                case "labels":
                    if (hasCommand) {
                        writable.println("Syntax error, too many parameters.");
                        error = true;
                    } else {
                        if ("relsOnly".equals(mode)) {
                            writable.println("Cannot set labels on relationships.");
                            error = true;
                        } else {
                            command = StructrApp.getInstance(securityContext).command(BulkCreateLabelsCommand.class);
                            command.setLogBuffer(writable);
                            isCreateLabels = true;
                            hasCommand = true;
                        }
                    }
                    break;
                case "node":
                    if (hasCommand) {
                        if (isIndex) {
                            writable.println("Index type must be specified before the 'index' keyword.");
                            error = true;
                        } else if (isIds) {
                            writable.println("Entity type must be specified before the 'ids' keyword.");
                            error = true;
                        }
                    } else {
                        mode = "nodesOnly";
                        typeKey = "type";
                        allRels = false;
                    }
                    break;
                case "rel":
                case "relationship":
                    if (hasCommand) {
                        if (isIndex) {
                            writable.println("Index type must be specified before the 'index' keyword.");
                            error = true;
                        } else if (isIds) {
                            writable.println("Entity type must be specified before the 'ids' keyword.");
                            error = true;
                        }
                    } else {
                        if (isCreateLabels) {
                            writable.println("Cannot set labels on relationships.");
                            error = true;
                        }
                        mode = "relsOnly";
                        typeKey = "relType";
                        allNodes = false;
                    }
                    break;
                case "for":
                    if (!hasCommand) {
                        writable.println("Unknown init mode 'for'.");
                        error = true;
                    }
                    hasFor = true;
                    break;
                default:
                    // specify node or rel type
                    if (hasCommand && hasFor) {
                        // prevent too many parameters from being accepted
                        if (StringUtils.isNotBlank(type)) {
                            writable.println("Syntax error, too many parameters.");
                            error = true;
                            break;
                        }
                        type = param;
                        // only set type key if not already set, default is "type" not "relType"
                        if (typeKey == null) {
                            typeKey = "type";
                        }
                    } else {
                        if (!hasCommand) {
                            writable.println("Unknown init mode '" + param + "'.");
                            error = true;
                        } else {
                            writable.println("Syntax error, please specify something like 'init node index for User'.");
                            error = true;
                        }
                    }
                    break;
            }
            // break early on errors
            if (error) {
                break;
            }
        }
        if (!error && !hasCommand) {
            writable.println("Please specify what to initialize.");
            error = true;
        }
        if (!error && hasCommand && hasFor && StringUtils.isEmpty(type)) {
            writable.println("Missing type specification, please specify something like 'init node index for User'.");
            error = true;
        }
        if (!error) {
            if (command instanceof MaintenanceCommand) {
                final Map<String, Object> data = toMap("mode", mode, typeKey, type);
                if (type == null) {
                    data.put("allNodes", allNodes);
                    data.put("allRels", allRels);
                }
                ((MaintenanceCommand) command).execute(data);
            } else if (command != null) {
                writable.println("Cannot execute command '" + command.getClass().getSimpleName() + "', wrong type.");
            } else {
                writable.println("Cannot execute null command.");
            }
        }
    } else {
        writable.println("You must be admin user to use this command.");
    }
}
Also used : BulkCreateLabelsCommand(org.structr.core.graph.BulkCreateLabelsCommand) NodeServiceCommand(org.structr.core.graph.NodeServiceCommand) MaintenanceCommand(org.structr.core.graph.MaintenanceCommand) BulkSetUuidCommand(org.structr.core.graph.BulkSetUuidCommand) Principal(org.structr.core.entity.Principal) BulkRebuildIndexCommand(org.structr.core.graph.BulkRebuildIndexCommand)

Example 2 with MaintenanceCommand

use of org.structr.core.graph.MaintenanceCommand in project structr by structr.

the class MaintenanceResource method doPost.

@Override
public RestMethodResult doPost(Map<String, Object> propertySet) throws FrameworkException {
    if ((securityContext != null) && isSuperUser()) {
        if (this.taskOrCommand != null) {
            try {
                final App app = StructrApp.getInstance(securityContext);
                if (Task.class.isAssignableFrom(taskOrCommand)) {
                    Task task = (Task) taskOrCommand.newInstance();
                    app.processTasks(task);
                } else if (MaintenanceCommand.class.isAssignableFrom(taskOrCommand)) {
                    MaintenanceCommand cmd = (MaintenanceCommand) StructrApp.getInstance(securityContext).command(taskOrCommand);
                    // flush caches if required
                    if (cmd.requiresFlushingOfCaches()) {
                        app.command(FlushCachesCommand.class).execute(Collections.EMPTY_MAP);
                    }
                    // create enclosing transaction if required
                    if (cmd.requiresEnclosingTransaction()) {
                        try (final Tx tx = app.tx()) {
                            cmd.execute(propertySet);
                            tx.success();
                        }
                    } else {
                        cmd.execute(propertySet);
                    }
                    final RestMethodResult result = new RestMethodResult(HttpServletResponse.SC_OK);
                    cmd.getCustomHeaders().forEach((final String headerName, final String headerValue) -> {
                        result.addHeader(headerName, headerValue);
                    });
                    cmd.getCustomHeaders().clear();
                    return result;
                } else {
                    return new RestMethodResult(HttpServletResponse.SC_NOT_FOUND);
                }
                // return 200 OK
                return new RestMethodResult(HttpServletResponse.SC_OK);
            } catch (InstantiationException iex) {
                throw new SystemException(iex.getMessage());
            } catch (IllegalAccessException iaex) {
                throw new SystemException(iaex.getMessage());
            }
        } else {
            if (taskOrCommandName != null) {
                throw new NotFoundException("No such task or command: " + this.taskOrCommandName);
            } else {
                throw new IllegalPathException("Maintenance resource needs parameter");
            }
        }
    } else {
        throw new NotAllowedException("Use of the maintenance endpoint is restricted to admin users");
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Task(org.structr.agent.Task) IllegalPathException(org.structr.rest.exception.IllegalPathException) Tx(org.structr.core.graph.Tx) SystemException(org.structr.rest.exception.SystemException) NotAllowedException(org.structr.rest.exception.NotAllowedException) NotFoundException(org.structr.rest.exception.NotFoundException) MaintenanceCommand(org.structr.core.graph.MaintenanceCommand) RestMethodResult(org.structr.rest.RestMethodResult)

Aggregations

MaintenanceCommand (org.structr.core.graph.MaintenanceCommand)2 Task (org.structr.agent.Task)1 App (org.structr.core.app.App)1 StructrApp (org.structr.core.app.StructrApp)1 Principal (org.structr.core.entity.Principal)1 BulkCreateLabelsCommand (org.structr.core.graph.BulkCreateLabelsCommand)1 BulkRebuildIndexCommand (org.structr.core.graph.BulkRebuildIndexCommand)1 BulkSetUuidCommand (org.structr.core.graph.BulkSetUuidCommand)1 NodeServiceCommand (org.structr.core.graph.NodeServiceCommand)1 Tx (org.structr.core.graph.Tx)1 RestMethodResult (org.structr.rest.RestMethodResult)1 IllegalPathException (org.structr.rest.exception.IllegalPathException)1 NotAllowedException (org.structr.rest.exception.NotAllowedException)1 NotFoundException (org.structr.rest.exception.NotFoundException)1 SystemException (org.structr.rest.exception.SystemException)1