public class ReportSyncopeOperations {

  private final ReportService reportService = SyncopeServices.get(ReportService.class);

  public ReportExecTO readExecution(final String executionid) {
    return reportService.readExecution(Long.valueOf(executionid));
  }

  public ReportTO read(final String reportId) {
    return reportService.read(Long.valueOf(reportId));
  }

  public List<ReportExecTO> listJobs(final String jobStatusType) {
    return reportService.listJobs(JobStatusType.valueOf(jobStatusType));
  }

  public List<ReportTO> list() {
    return reportService.list();
  }

  public String exportExecutionResult(
      final String executionKey, final String reportExecExportFormat)
      throws TransformerException, SAXException, IOException, ParserConfigurationException {
    final ReportExecExportFormat format = ReportExecExportFormat.valueOf(reportExecExportFormat);
    final SequenceInputStream report =
        (SequenceInputStream)
            reportService.exportExecutionResult(Long.valueOf(executionKey), format).getEntity();
    final String xmlFinalName = "export_" + executionKey + ".xml";
    switch (format) {
      case XML:
        XMLUtils.createXMLFile(report, xmlFinalName);
        break;
      case CSV:
        return format + " doesn't supported";
      case PDF:
        return format + " doesn't supported";
      case HTML:
        return format + " doesn't supported";
      case RTF:
        return format + " doesn't supported";
      default:
        return format + " doesn't supported";
    }
    return xmlFinalName;
  }

  public void execute(final String reportId) {
    reportService.execute(Long.valueOf(reportId));
  }

  public void deleteExecution(final String executionId) {
    reportService.deleteExecution(Long.valueOf(executionId));
  }

  public void delete(final String reportId) {
    reportService.delete(Long.valueOf(reportId));
  }
}
public class UserSyncopeOperations {

  private final UserService userService = SyncopeServices.get(UserService.class);

  public List<UserTO> searchByRole(final String realm, final String role) {
    return userService
        .search(
            SyncopeClient.getAnySearchQueryBuilder()
                .realm(realm)
                .fiql(
                    SyncopeClient.getUserSearchConditionBuilder()
                        .inRoles(Long.valueOf(role))
                        .query())
                .build())
        .getResult();
  }

  public List<UserTO> searchByResource(final String realm, final String resource) {
    return userService
        .search(
            SyncopeClient.getAnySearchQueryBuilder()
                .realm(realm)
                .fiql(SyncopeClient.getUserSearchConditionBuilder().hasResources(resource).query())
                .build())
        .getResult();
  }

  public List<UserTO> searchByAttribute(
      final String realm, final String attributeName, final String attributeValue) {
    return userService
        .search(
            SyncopeClient.getAnySearchQueryBuilder()
                .realm(realm)
                .fiql(
                    SyncopeClient.getUserSearchConditionBuilder()
                        .is(attributeName)
                        .equalTo(attributeValue)
                        .query())
                .build())
        .getResult();
  }

  public PagedResult<UserTO> list() {
    return userService.list(new AnyListQuery());
  }

  public UserTO read(final String userId) {
    return userService.read(Long.valueOf(userId));
  }

  public void delete(final String userId) {
    userService.delete(Long.valueOf(userId));
  }

  public int count() {
    return list().getResult().size();
  }

  public String getUsernameFromId(final String userId) {
    return userService.getUsername(Long.valueOf(userId)).getHeaderString(RESTHeaders.USERNAME);
  }

  public String getIdFromUsername(final String username) {
    return userService.getUserKey(username).getHeaderString(RESTHeaders.USER_KEY);
  }
}
  public void setup() throws FileNotFoundException, IllegalAccessException {
    installResultManager.printWelcome();

    System.out.println(
        "Path to config files of Syncope CLI client will be: "
            + InstallConfigFileTemplate.dirPath());

    if (!FileSystemUtils.exists(InstallConfigFileTemplate.dirPath())) {
      throw new FileNotFoundException(
          "Directory: " + InstallConfigFileTemplate.dirPath() + " does not exists!");
    }

    if (!FileSystemUtils.canWrite(InstallConfigFileTemplate.dirPath())) {
      throw new IllegalAccessException(
          "Permission denied on " + InstallConfigFileTemplate.dirPath());
    }
    System.out.println("- File system permission checked");
    System.out.println("");

    try (Scanner scanIn = new Scanner(System.in)) {
      System.out.print("Syncope server schema [http/https]: ");
      String syncopeServerSchemaFromSystemIn = scanIn.nextLine();
      boolean schemaFound = false;
      while (!schemaFound) {
        if (("http".equalsIgnoreCase(syncopeServerSchemaFromSystemIn))
            || ("https".equalsIgnoreCase(syncopeServerSchemaFromSystemIn))) {
          syncopeServerSchema = syncopeServerSchemaFromSystemIn;
          schemaFound = true;
        } else {
          System.out.println("Please use one of below values: ");
          System.out.println("   - http");
          System.out.println("   - https");
          syncopeServerSchemaFromSystemIn = scanIn.nextLine();
        }
      }

      System.out.print("Syncope server hostname [e.g. " + syncopeServerHostname + "]: ");
      String syncopeServerHostnameFromSystemIn = scanIn.nextLine();
      boolean syncopeServerHostnameFound = false;
      while (!syncopeServerHostnameFound) {
        if (StringUtils.isNotBlank(syncopeServerHostnameFromSystemIn)) {
          syncopeServerHostname = syncopeServerHostnameFromSystemIn;
          syncopeServerHostnameFound = true;
        } else {
          System.out.print("Syncope server hostname [e.g. " + syncopeServerHostname + "]: ");
          syncopeServerHostnameFromSystemIn = scanIn.nextLine();
        }
      }

      System.out.print("Syncope server port [e.g. " + syncopeServerPort + "]: ");
      String syncopeServerPortFromSystemIn = scanIn.nextLine();
      boolean syncopeServerPortFound = false;
      while (!syncopeServerPortFound) {
        if (StringUtils.isNotBlank(syncopeServerPortFromSystemIn)) {
          syncopeServerPort = syncopeServerPortFromSystemIn;
          syncopeServerPortFound = true;
        } else if (!StringUtils.isNumeric(syncopeServerPortFromSystemIn)) {
          System.err.println(syncopeServerPortFromSystemIn + " is not a numeric string, try again");
          syncopeServerPortFromSystemIn = scanIn.nextLine();
        } else {
          System.out.print("Syncope server port [e.g. " + syncopeServerPort + "]: ");
          syncopeServerPortFromSystemIn = scanIn.nextLine();
        }
      }

      System.out.print("Syncope server rest context [e.g. " + syncopeServerRestContext + "]: ");
      String syncopeServerRestContextFromSystemIn = scanIn.nextLine();
      boolean syncopeServerRestContextFound = false;
      while (!syncopeServerRestContextFound) {
        if (StringUtils.isNotBlank(syncopeServerRestContextFromSystemIn)) {
          syncopeServerRestContext = syncopeServerRestContextFromSystemIn;
          syncopeServerRestContextFound = true;
        } else {
          System.out.print("Syncope server port [e.g. " + syncopeServerRestContext + "]: ");
          syncopeServerRestContextFromSystemIn = scanIn.nextLine();
        }
      }

      System.out.print("Syncope admin user: "******"Syncope admin user: "******"Syncope admin password: "******"Syncope admin user: "******"Error installing CLI", ex);
      installResultManager.manageProcessingException(ex);
    } catch (final Exception e) {
      LOG.error("Error installing CLI", e);
      installResultManager.manageException(e);
    }
  }