/**
   * {@inheritDoc}
   *
   * @see org.komodo.shell.BuiltInShellCommand#doExecute()
   */
  @Override
  protected CommandResult doExecute() {
    CommandResult result = null;

    try {
      final String sourceTypeName =
          requiredArgument(0, I18n.bind(ServerCommandsI18n.missingDatasourceTypeName));

      // Validates that a server is connected
      CommandResult validationResult = validateHasConnectedWorkspaceServer();
      if (!validationResult.isOk()) {
        return validationResult;
      }

      Collection<TeiidPropertyDefinition> propDefns =
          getWorkspaceTeiidInstance().getTemplatePropertyDefns(sourceTypeName);
      if (propDefns == null) {
        return new CommandResultImpl(
            false,
            I18n.bind(ServerCommandsI18n.serverDatasourceTypeNotFound, sourceTypeName),
            null);
      }

      // Print title
      final String title =
          I18n.bind(
              ServerCommandsI18n.infoMessageDatasourceType,
              sourceTypeName,
              getWorkspaceServerName());
      print(MESSAGE_INDENT, title);
      print(MESSAGE_INDENT, I18n.bind(ServerCommandsI18n.datasourceTypePropertiesHeader));

      // Print DataSource Template Info
      ServerObjPrintUtils.printDatasourceTemplateProperties(
          getWriter(),
          MESSAGE_INDENT,
          propDefns,
          I18n.bind(ServerCommandsI18n.datasourceTypeNameLabel),
          I18n.bind(ServerCommandsI18n.datasourceTypeDefaultValueLabel));

      result = CommandResult.SUCCESS;
    } catch (final Exception e) {
      result = new CommandResultImpl(e);
    }

    return result;
  }
Ejemplo n.º 2
0
  /*
   * Create a Schema to place imported model temporarily
   */
  private KomodoObject createSchema(String schemaName) {
    KomodoObject resultSchema = null;
    try {
      // Save original context
      KomodoObject origContext = getContext();

      // Cd into workspace to create temp Schema
      CdCommand cdCommand = new CdCommand(getWorkspaceStatus());
      cdCommand.setArguments(new Arguments("/")); // $NON-NLS-1$
      CommandResult result = cdCommand.execute();

      if (!result.isOk()) {
        return resultSchema;
      }

      // Create the temp schema
      CreateSchemaCommand createCommand = new CreateSchemaCommand(getWorkspaceStatus());

      createCommand.setArguments(new Arguments(schemaName));
      result = createCommand.execute();

      if (result.isOk()) {
        final KomodoObject[] schemas = getWorkspaceManager().findSchemas(getTransaction());
        for (KomodoObject schema : schemas) {
          if (schema.getName(getTransaction()).equals(schemaName)) {
            resultSchema = schema;
            break;
          }
        }
      }

      // Cd back into the original Context
      String path = getWorkspaceStatus().getDisplayPath(origContext, null);
      cdCommand.setArguments(new Arguments(path));
      cdCommand.execute();
    } catch (Exception e) {
      // Result tempContext is null
    }
    return resultSchema;
  }
  /**
   * {@inheritDoc}
   *
   * @see org.komodo.shell.BuiltInShellCommand#doExecute()
   */
  @Override
  protected CommandResult doExecute() {
    CommandResult result = null;

    try {
      String datasourceName =
          requiredArgument(0, I18n.bind(ServerCommandsI18n.missingDatasourceName));

      final String overwriteArg = optionalArgument(1, null);
      final boolean overwrite = !StringUtils.isBlank(overwriteArg);
      // make sure overwrite arg is valid
      if (overwrite && !VALID_OVERWRITE_ARGS.contains(overwriteArg)) {
        return new CommandResultImpl(
            false, I18n.bind(WorkspaceCommandsI18n.overwriteArgInvalid, overwriteArg), null);
      }

      // If datasource with same name is in workspace, make sure we can overwrite
      boolean hasDS =
          getWorkspaceManager(getTransaction())
              .hasChild(getTransaction(), datasourceName, DataVirtLexicon.Connection.NODE_TYPE);
      if (hasDS && !overwrite) {
        return new CommandResultImpl(
            false,
            I18n.bind(ServerCommandsI18n.datasourceOverwriteNotEnabled, datasourceName),
            null);
      }

      // Validates that a server is connected
      CommandResult validationResult = validateHasConnectedWorkspaceServer();
      if (!validationResult.isOk()) {
        return validationResult;
      }

      // Get the Data Source from the server
      TeiidDataSource serverDS = null;
      try {
        // Check the data source name to make sure its valid
        List<String> existingSourceNames =
            ServerUtils.getDatasourceNames(getWorkspaceTeiidInstance());
        if (!existingSourceNames.contains(datasourceName)) {
          return new CommandResultImpl(
              false, I18n.bind(ServerCommandsI18n.serverDatasourceNotFound, datasourceName), null);
        }
        // Get the data source
        serverDS = getWorkspaceTeiidInstance().getDataSource(datasourceName);
      } catch (Exception ex) {
        result =
            new CommandResultImpl(
                false, I18n.bind(ServerCommandsI18n.connectionErrorWillDisconnect), ex);
        WkspStatusServerManager.getInstance(getWorkspaceStatus()).disconnectDefaultServer();
        return result;
      }
      if (serverDS == null) {
        return new CommandResultImpl(
            false, I18n.bind(ServerCommandsI18n.serverDatasourceNotFound, datasourceName), null);
      }

      // If overwriting, delete existing first
      if (hasDS) {
        final KomodoObject datasourceToDelete =
            getWorkspaceManager(getTransaction())
                .getChild(getTransaction(), datasourceName, DataVirtLexicon.Connection.NODE_TYPE);
        getWorkspaceManager(getTransaction()).delete(getTransaction(), datasourceToDelete);
      }
      // Create the Data Source and set properties
      Datasource newDatasource =
          getWorkspaceManager(getTransaction())
              .createDatasource(getTransaction(), null, datasourceName);
      setRepoDatasourceProperties(newDatasource, serverDS.getProperties());

      print(MESSAGE_INDENT, I18n.bind(ServerCommandsI18n.datasourceCopyToRepoFinished));
      result = CommandResult.SUCCESS;
    } catch (final Exception e) {
      result = new CommandResultImpl(e);
    }

    return result;
  }