/*============================================================================
Name : FMEServerAPIDemo.cpp
System : FME Server API
Language : C++
Purpose : FMEServerAPIDemo sample application
Author Date Changes made
------------------ ------------ -------------------------------
Sample Jun 19, 2008 Original definition
Copyright (c) 1994 - 2011, Safe Software Inc.
All Rights Reserved
This software may not be copied or reproduced, in all or in part,
without the prior written consent of Safe Software Inc.
The entire risk as to the results and performance of the software,
supporting text and other information contained in this file
(collectively called the "Software") is with the user.
In no event will Safe Software Incorporated be liable for damages,
including loss of profits or consequential damages, arising out of
the use of the Software.
============================================================================ */
#define WIN32
#include <fmeservertypes.h>
#include <ifmeserverattribute.h>
#include <ifmeserverdataset.h>
#include <ifmeserverparameter.h>
#include <ifmeserverrepository.h>
#include <ifmeserverrepositorymanager.h>
#include <ifmeserversession.h>
#include <ifmeserverrepositorymanager.h>
#include <ifmeserverrepository.h>
#include <ifmeserverstring.h>
#include <ifmeserverstatus.h>
#include <ifmeserverworkspacesummary.h>
#include <ifmeservertransformationrequest.h>
#include <ifmeservertransformationmanager.h>
#include <ifmeservertransformationresult.h>
#include <ifmeserverworkspace.h>
#include <ifmeserverresource.h>
#include <ifmeserverservice.h>
#include <iostream>
#include <string>
using namespace std;
class FMEServerAPIDemo
{
private:
IFMEServerSession* serverSession_;
IFMEServerRepositoryManager* repositoryMgr_;
public:
//------------------------------------------------------------------
// Constructor.
FMEServerAPIDemo(const char* hostArg, const char* portArg);
//------------------------------------------------------------------
// Destructor.
virtual ~FMEServerAPIDemo();
//------------------------------------------------------------------
// Will display the menu and execute the user's choice.
void execute();
private:
void displayMainMenu();
int getMenuChoice();
void executeMenuChoice(int choice);
void listRepositories();
void addRepository();
void removeRepository();
void listWorkspaces();
void addWorkspace();
void updateWorkspace();
void getWorkspace();
void removeWorkspace();
void runWorkspace();
void listResources();
void addResource();
void updateResource();
void getResource();
void removeResource();
void listServices();
void addService();
void removeService();
void updateService();
void addCustomFormat();
void remCustomFormat();
void updateCustomFormat();
void addCustomTransformer();
void remCustomTransformer();
void updateCustomTransformer();
// ---------------------------------------------------------------------
// Sets the IFMEServerString input content to what the user enters.
// promptMessage will be displayed to tell the user what to enter.
void getInput(IFMEServerString& input, const char* promptMessage);
// ---------------------------------------------------------------------
// Returns true if serverStatus is NULL. If serverStatus isn't NULL,
// its content is displayed and the object is deleted.
FME_Boolean isSuccess(IFMEServerStatus*& serverStatus);
};
//=========================================================================
//
FMEServerAPIDemo::FMEServerAPIDemo(const char* hostArg, const char* portArg)
{
// From ifmeserversession.h, factory class
FME_createServerSession(serverSession_);
IFMEServerString* host = serverSession_->createServerString();
IFMEServerString* userID = serverSession_->createServerString();
IFMEServerString* password = serverSession_->createServerString();
IFMEServerString* clientId = serverSession_->createServerString();
IFMEServerStringArray* directives = serverSession_->createServerStringArray();
*host = hostArg;
FME_UInt16 port = atoi(portArg);
cout << "Please enter server connection credentials." << endl;
getInput(*userID, "UserId:");
if (userID->length() > 0)
{
getInput(*password, "Password:");
directives->append(kKeywordClientID);
directives->append("app_apidemo");
}
IFMEServerConnectionInfo* connectionInfo_ = serverSession_->createServerConnectionInfo(*host, port, *userID, *password);
serverSession_->init(*connectionInfo_, directives);
repositoryMgr_ = serverSession_->getRepositoryManager();
serverSession_->destroyServerConnectionInfo(connectionInfo_);
serverSession_->destroyServerStringArray(directives);
serverSession_->destroyServerString(clientId);
serverSession_->destroyServerString(password);
serverSession_->destroyServerString(userID);
serverSession_->destroyServerString(host);
}
//=========================================================================
//
FMEServerAPIDemo::~FMEServerAPIDemo()
{
IFMEServerStatus* status = serverSession_->disconnect();
isSuccess(status);
FME_destroyServerSession(serverSession_);
}
//=========================================================================
//
void FMEServerAPIDemo::execute()
{
displayMainMenu();
int choice = getMenuChoice();
executeMenuChoice(choice);
}
//=========================================================================
//
int FMEServerAPIDemo::getMenuChoice()
{
int choice;
// Check if input is valid, and if not clear and ignore the stream
if (!(cin >> choice))
{
cin.clear();
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return choice;
}
//=========================================================================
//
void FMEServerAPIDemo::executeMenuChoice(int choice)
{
switch(choice)
{
case 1:
listRepositories();
break;
case 2:
addRepository();
break;
case 3:
removeRepository();
break;
case 4:
listWorkspaces();
break;
case 5:
addWorkspace();
break;
case 6:
updateWorkspace();
break;
case 7:
getWorkspace();
break;
case 8:
removeWorkspace();
break;
case 9:
listResources();
break;
case 10:
addResource();
break;
case 11:
updateResource();
break;
case 12:
getResource();
break;
case 13:
removeResource();
break;
case 14:
listServices();
break;
case 15:
addService();
break;
case 16:
updateService();
break;
case 17:
removeService();
break;
case 18:
runWorkspace();
break;
case 0:
exit(0);
break;
default:
cout << "Invalid option!\n";
break;
}
}
//=========================================================================
//
void FMEServerAPIDemo::displayMainMenu()
{
cout << "\n\n" <<
"===FME Server API Demo===\n" <<
"1. List available repositories\n" <<
"2. Add a repository\n" <<
"3. Remove a repository\n" <<
"4. List available workspaces\n" <<
"5. Add a workspace\n" <<
"6. Update a workspace\n" <<
"7. Get a workspace\n" <<
"8. Remove a workspace\n" <<
"9. List resources of a workspace\n" <<
"10. Add a resource\n" <<
"11. Update a resource\n" <<
"12. Get a resource\n" <<
"13. Remove a resource\n" <<
"14. List available services\n" <<
"15. Add a service\n" <<
"16. Update a service\n" <<
"17. Remove a service\n" <<
"18. Run a workspace\n" <<
"0. Quit\n" <<
"\nSELECT: ";
}
//=========================================================================
//
void FMEServerAPIDemo::listRepositories()
{
IFMEServerRepositoryVector* repositories = serverSession_->createRepositoryVector();
IFMEServerString* description = serverSession_->createServerString();
IFMEServerString* name = serverSession_->createServerString();
IFMEServerStatus* status = repositoryMgr_->getRepositories(NULL, *repositories);
if (isSuccess(status))
{
for (FME_UInt32 i = 0; i < repositories->entries(); i++)
{
IFMEServerRepository* repository = (*repositories)(i);
repository->getName(*name);
repository->getDescription(*description);
cout << name->data() << " " << description->data() << endl;
}
}
serverSession_->destroyRepositoryVector(repositories);
serverSession_->destroyServerString(description);
serverSession_->destroyServerString(name);
}
//=========================================================================
//
void FMEServerAPIDemo::addRepository()
{
IFMEServerString* name = serverSession_->createServerString();
IFMEServerString* description = serverSession_->createServerString();
getInput(*name, "Please enter the name for the new repository:\t");
getInput(*description, "Please enter the description for the new repository:\t");
IFMEServerStatus* status = repositoryMgr_->addRepository(*name, *description);
if (isSuccess(status))
{
cout << "Added the repository\n";
}
serverSession_->destroyServerString(name);
serverSession_->destroyServerString(description);
}
//=========================================================================
//
void FMEServerAPIDemo::removeRepository()
{
FME_Boolean exists = FME_FALSE;
IFMEServerString* name = serverSession_->createServerString();
getInput(*name, "Please enter the name for the repository to remove:\t");
IFMEServerStatus* status = repositoryMgr_->removeRepository(*name, exists);
if (isSuccess(status))
{
cout << "Removed the repository\n";
}
serverSession_->destroyServerString(name);
}
//=========================================================================
//
void FMEServerAPIDemo::listWorkspaces()
{
IFMEServerRepositoryVector* repositories = serverSession_->createRepositoryVector();
IFMEServerWorkspaceSummaryVector* workspaceSummaries = serverSession_->createWorkspaceSummaryVector();
IFMEServerString* repositoryName = serverSession_->createServerString();
IFMEServerStatus* status = repositoryMgr_->getRepositories(NULL, *repositories);
if (isSuccess(status))
{
for (FME_UInt32 i = 0; i < repositories->entries(); i++)
{
IFMEServerRepository* repository = (*repositories)(i);
status = repository->getWorkspaceSummaries(NULL, *workspaceSummaries);
if (isSuccess(status))
{
for (FME_UInt32 j = 0; j < workspaceSummaries->entries(); j++)
{
IFMEServerString* workspaceTitle = serverSession_->createServerString();
IFMEServerString* workspaceName = serverSession_->createServerString();
IFMEServerWorkspaceSummary* workspaceSummary = (*workspaceSummaries)(j);
repository->getName(*repositoryName);
workspaceSummary->getTitle(*workspaceTitle);
workspaceSummary->getName(*workspaceName);
cout << "Repository: "<< repositoryName->data() << " " << workspaceTitle->data() << " " << workspaceName->data() << " ON:" << workspaceSummary->getIsEnabled() << endl;
serverSession_->destroyServerString(workspaceName);
serverSession_->destroyServerString(workspaceTitle);
}
}
}
}
serverSession_->destroyRepositoryVector(repositories);
serverSession_->destroyWorkspaceSummaryVector(workspaceSummaries);
serverSession_->destroyServerString(repositoryName);
}
//=========================================================================
//
void FMEServerAPIDemo::addWorkspace()
{
IFMEServerRepository* repository = serverSession_->createRepository();
FME_Boolean exists = FME_FALSE;
IFMEServerString* repositoryName = serverSession_->createServerString();
IFMEServerString* workspaceName = serverSession_->createServerString();
IFMEServerString* workspaceFilePath = serverSession_->createServerString();
getInput(*repositoryName, "Please enter the repository name for the new workspace:\t");
getInput(*workspaceName, "Please enter the name for the new workspace:\t");
getInput(*workspaceFilePath, "Please enter the file path for the workspace to add:\t");
IFMEServerStatus* status = repositoryMgr_->getRepository(*repositoryName, *repository, exists);
if (isSuccess(status))
{
status = repository->addItem(*workspaceName, *workspaceFilePath);
if (isSuccess(status))
{
cout << "Added the workspace\n";
}
}
serverSession_->destroyRepository(repository);
serverSession_->destroyServerString(repositoryName);
serverSession_->destroyServerString(workspaceName);
serverSession_->destroyServerString(workspaceFilePath);
}
//=========================================================================
//
void FMEServerAPIDemo::updateWorkspace()
{
IFMEServerRepository* repository = serverSession_->createRepository();
FME_Boolean exists = FME_FALSE;
IFMEServerString* repositoryName = serverSession_->createServerString();
IFMEServerString* workspaceName = serverSession_->createServerString();
IFMEServerString* workspaceFilePath = serverSession_->createServerString();
getInput(*repositoryName, "Please enter the repository name for the workspace to update:\t");
getInput(*workspaceName, "Please enter the name for the workspace to update:\t");
getInput(*workspaceFilePath, "Please enter the file path for the workspace to update:\t");
IFMEServerStatus* status = repositoryMgr_->getRepository(*repositoryName, *repository, exists);
if (isSuccess(status))
{
status = repository->updateItem(*workspaceName, *workspaceFilePath);
if (isSuccess(status))
{
cout << "Updated the workspace\n";
}
}
serverSession_->destroyRepository(repository);
serverSession_->destroyServerString(repositoryName);
serverSession_->destroyServerString(workspaceName);
serverSession_->destroyServerString(workspaceFilePath);
}
//=========================================================================
//
void FMEServerAPIDemo::getWorkspace()
{
IFMEServerRepository* repository = serverSession_->createRepository();
FME_Boolean exists = FME_FALSE;
IFMEServerString* repositoryName = serverSession_->createServerString();
IFMEServerString* workspaceName = serverSession_->createServerString();
IFMEServerString* workspaceFilePath = serverSession_->createServerString();
getInput(*repositoryName, "Please enter the repository name for the workspace to get:\t");
getInput(*workspaceName, "Please enter the name for the workspace to get:\t");
getInput(*workspaceFilePath, "Please enter the local file path for the workspace to get:\t");
IFMEServerStatus* status = repositoryMgr_->getRepository(*repositoryName, *repository, exists);
if (isSuccess(status))
{
status = repository->getItem(*workspaceName, *workspaceFilePath, exists);
if (isSuccess(status))
{
cout << "Get workspace succeeded\n";
}
}
serverSession_->destroyRepository(repository);
serverSession_->destroyServerString(repositoryName);
serverSession_->destroyServerString(workspaceName);
serverSession_->destroyServerString(workspaceFilePath);
}
//=========================================================================
//
void FMEServerAPIDemo::removeWorkspace()
{
IFMEServerRepository* repository = serverSession_->createRepository();
FME_Boolean existsRepository = FME_FALSE;
IFMEServerString* repositoryName = serverSession_->createServerString();
IFMEServerString* workspaceName = serverSession_->createServerString();
getInput(*repositoryName, "Please enter the repository name for the workspace to remove:\t");
getInput(*workspaceName, "Please enter the name of the workspace to remove:\t");
IFMEServerStatus* status = repositoryMgr_->getRepository(*repositoryName, *repository, existsRepository);
if (isSuccess(status))
{
FME_Boolean existsWorkspace = FME_FALSE;
status = repository->removeItem(*workspaceName, existsWorkspace);
if (isSuccess(status))
{
cout << "Removed the workspace\n";
}
}
serverSession_->destroyRepository(repository);
serverSession_->destroyServerString(repositoryName);
serverSession_->destroyServerString(workspaceName);
}
//=========================================================================
//
void FMEServerAPIDemo::runWorkspace()
{
IFMEServerString* subsectionName = serverSession_->createServerString();
IFMEServerString* repositoryName = serverSession_->createServerString();
IFMEServerString* workspaceName = serverSession_->createServerString();
IFMEServerString* parmName = serverSession_->createServerString();
IFMEServerString* parmValue = serverSession_->createServerString();
IFMEServerString* directiveName = serverSession_->createServerString();
IFMEServerString* directiveValue = serverSession_->createServerString();
IFMEServerString* fmeServerResponse = serverSession_->createServerString();
IFMEServerTransformationManager* transformationMgr = serverSession_->getTransformationManager();
// Create an IFMETransformationRequest object to encapsulate our
// transformation request that will be submitted to the server.
// The createTransformationRequest(...) method expects three parameters:
// 1. The name of an FME Server node subsection, as defined in the
// node's configuration. SERVER_CONSOLE_CLIENT is one such
// subsection that is part of the default configuration that ships
// with the FME Server. Each subsection specifies various
// processing directives (e.g., the folder where output should
// be placed). For information on the different subsections, please
// refer to <FMEServer-InstallDir>\Server\fmeServerConfig.txt
// 2. The unique name for the repository that contains the mapping file
// or command file to be executed by FME.
*subsectionName = "SERVER_CONSOLE_CLIENT";
getInput(*repositoryName, "Please enter the repository name for the workspace to run:\t");
getInput(*workspaceName, "Please enter the name of the workspace to run:\t");
IFMEServerTransformationRequest* req =
serverSession_->createTransformationRequest(*subsectionName, *repositoryName, *workspaceName);
// Set published parameters that have been exposed by the workspace.
// Both the name of the published parameter and the desired value
// are simple Strings, even if the value happens to have a numeric
// interpretation.
// Here, we set a published parameter called MAX_FEATURES that
// controls how many features will be read in from the source dataset(s).
// Published parameters depend on the workspace that is being run.
*parmName = "MAX_FEATURES";
*parmValue = "2000";
req->setPublishedParameter(*parmName, *parmValue);
// We can also set certain transformation manager directives that determine how
// our transformation request will be processed. Unlike published
// parameters, which are determined by each workspace's author and
// thus vary from one workspace to another, the available directives
// are constant for all transformation requests. A complete
// list of directives can be found in the FME Server documentation.
// Here, we set a high priority for this job to suggest that it
// should be given precedence over other, lower-priority, jobs.
*directiveName = "priority";
*directiveValue = "42";
req->setTMDirective(*directiveName, *directiveValue);
cout << "Sending a transformation request to the FME Server...\n";
// Submit the transformation request. This method will
// block until transformation results are available. (Users who wish
// to submit jobs asynchronously should instead use the submitJob(...)
// method.) The returned results are stored in
// an IFMEServerTransformationResult object, which is the companion to the
// IFMEServerTransformationRequest object that we submitted.
IFMEServerTransformationResult* result = serverSession_->createTransformationResult();
transformationMgr->transactJob(*req, *result);
// The transformation result object receives a result string from the
// server and parses it to extract many different pieces of
// information about the job result. However, users who wish to
// do their own parsing of the raw server response, or wish to
// log this string, may obtain the unparsed response string using
// the getFMEServerResponse() method. Here, we use that method to
// print out the response string to the standard output.
result->getFMEServerResponse(*fmeServerResponse);
cout << "Response received:\n" << fmeServerResponse->data() << endl;
// The IFMEServerTransformationResult interface defines many accessor methods
// for retrieving specific information from the parsed result. We
// only demonstrate a small subset of those methods here; a full list
// is available in the FME Server API documentation.
// The getTransformationSuccess() method returns true if, and only if,
// the transformation was successful. Here, we check to see if the
// transformation was successful and then print out a message accordingly.
if (!result->getTransformationSuccess())
{
// The transformation was not successful. We inform the user of this
// and then use getStatusMessage() to get the error message that
// was returned by the server (e.g., "File austin.fmw could not
// be found.")
IFMEServerString* status = serverSession_->createServerString();
result->getStatusMessage(*status);
cout << "Transformation failed.";
cout << "The error was: " << status->data();
serverSession_->destroyServerString(status);
}
else
{
// The transformation was successful. We inform the user of this.
cout << "Transformation successful!\n";
// The transformation result object contains many name-value pairs
// that are extracted from the SUCCESS_RESPONSE defined in the
// FME Server node subsection. In addition to these pairs, there
// are also several name-value pairs added by the server:
// the time that the job was sent to an FME Server node for
// processing (timeStarted), the job ID (id), etcetera.
// The getAllProperties() method returns a map of all these
// name-value pairs. Here, we iterate over this map -- using its
// entry set -- and simply print out its contents in the form
// "name = value".
cout << "The following information was parsed from the response:\n";
cout << "============\n";
// display a few of the properties
IFMEServerString* timeReq = serverSession_->createServerString();
IFMEServerString* timeStart = serverSession_->createServerString();
IFMEServerString* timeEnd = serverSession_->createServerString();
IFMEServerString* reqHost = serverSession_->createServerString();
IFMEServerString* reqKwd = serverSession_->createServerString();
FME_Int32 id;
FME_Int32 statNo;
result->getID(id);
result->getTimeRequested(*timeReq);
result->getTimeStarted(*timeStart);
result->getTimeFinished(*timeEnd);
result->getRequesterHost(*reqHost);
result->getRequestKeyword(*reqKwd);
result->getStatusNumber(statNo);
cout
<< "ID: " << id << endl
<< "Time Requested: " << timeReq->data() << endl
<< "Time Started: " << timeStart->data() << endl
<< "Time Finished: " << timeEnd->data() << endl
<< "Request Host: " << reqHost->data() << endl
<< "Request Keyword: " << reqKwd->data() << endl
<< "Status Number: " << statNo << endl;
serverSession_->destroyServerString(timeReq);
serverSession_->destroyServerString(timeStart);
serverSession_->destroyServerString(timeEnd);
serverSession_->destroyServerString(reqHost);
serverSession_->destroyServerString(reqKwd);
}
serverSession_->destroyServerString(subsectionName);
serverSession_->destroyServerString(repositoryName);
serverSession_->destroyServerString(workspaceName);
serverSession_->destroyServerString(parmName);
serverSession_->destroyServerString(parmValue);
serverSession_->destroyServerString(directiveName);
serverSession_->destroyServerString(directiveValue);
serverSession_->destroyServerString(fmeServerResponse);
}
//=========================================================================
//
void FMEServerAPIDemo::listResources()
{
IFMEServerRepository* repository = serverSession_->createRepository();
IFMEServerResourceVector* resources = serverSession_->createResourceVector();
FME_Boolean exists = FME_FALSE;
IFMEServerString* repositoryName = serverSession_->createServerString();
IFMEServerString* workspaceName = serverSession_->createServerString();
getInput(*repositoryName, "Please enter the repository name for the resources:\t");
getInput(*workspaceName, "Please enter the workspace name for the resources:\t");
IFMEServerStatus* status = repositoryMgr_->getRepository(*repositoryName, *repository, exists);
if (isSuccess(status))
{
status = repository->getResources(*workspaceName, NULL, *resources);
if (isSuccess(status))
{
for (FME_UInt32 i = 0; i < resources->entries(); i++)
{
IFMEServerString* resourceName = serverSession_->createServerString();
IFMEServerString* resourceDescription = serverSession_->createServerString();
IFMEServerResource* resource = (*resources)(i);
resource->getName(*resourceName);
resource->getDescription(*resourceDescription);
cout << resourceName->data() << " " << resourceDescription->data() << endl;
serverSession_->destroyServerString(resourceDescription);
serverSession_->destroyServerString(resourceName);
}
}
}
serverSession_->destroyServerString(workspaceName);
serverSession_->destroyServerString(repositoryName);
serverSession_->destroyResourceVector(resources);
serverSession_->destroyRepository(repository);
}
//=========================================================================
//
void FMEServerAPIDemo::addResource()
{
IFMEServerRepository* repository = serverSession_->createRepository();
IFMEServerResource* resource = serverSession_->createResource();
FME_Boolean exists = FME_FALSE;
IFMEServerString* repositoryName = serverSession_->createServerString();
IFMEServerString* workspaceName = serverSession_->createServerString();
IFMEServerString* resourceName = serverSession_->createServerString();
IFMEServerString* resourceFilePath = serverSession_->createServerString();
getInput(*repositoryName, "Please enter the repository name for the new resource:\t");
getInput(*workspaceName, "Please enter the workspace name for the new resource:\t");
getInput(*resourceName, "Please enter the name for the new resource:\t");
getInput(*resourceFilePath, "Please enter the file path for the new resource to add:\t");
IFMEServerStatus* status = repositoryMgr_->getRepository(*repositoryName, *repository, exists);
if (isSuccess(status))
{
status = repository->addResource(*workspaceName, *resourceName, *resourceFilePath);
if (isSuccess(status))
{
cout << "Added the resource\n";
}
}
serverSession_->destroyRepository(repository);
serverSession_->destroyResource(resource);
serverSession_->destroyServerString(repositoryName);
serverSession_->destroyServerString(workspaceName);
serverSession_->destroyServerString(resourceName);
serverSession_->destroyServerString(resourceFilePath);
}
//=========================================================================
//
void FMEServerAPIDemo::updateResource()
{
IFMEServerRepository* repository = serverSession_->createRepository();
IFMEServerResource* resource = serverSession_->createResource();
FME_Boolean exists = FME_FALSE;
IFMEServerString* repositoryName = serverSession_->createServerString();
IFMEServerString* workspaceName = serverSession_->createServerString();
IFMEServerString* resourceName = serverSession_->createServerString();
IFMEServerString* resourceFilePath = serverSession_->createServerString();
getInput(*repositoryName, "Please enter the repository name for the resource to update:\t");
getInput(*workspaceName, "Please enter the workspace name for the resource to update:\t");
getInput(*resourceName, "Please enter the name for the resource to update:\t");
getInput(*resourceFilePath, "Please enter the file path for the resource to update:\t");
IFMEServerStatus* status = repositoryMgr_->getRepository(*repositoryName, *repository, exists);
if (isSuccess(status))
{
status = repository->updateResource(*workspaceName, *resourceName, *resourceFilePath);
if (isSuccess(status))
{
cout << "Updated the resource\n";
}
}
serverSession_->destroyRepository(repository);
serverSession_->destroyResource(resource);
serverSession_->destroyServerString(repositoryName);
serverSession_->destroyServerString(workspaceName);
serverSession_->destroyServerString(resourceName);
serverSession_->destroyServerString(resourceFilePath);
}
//=========================================================================
//
void FMEServerAPIDemo::getResource()
{
IFMEServerRepository* repository = serverSession_->createRepository();
IFMEServerResource* resource = serverSession_->createResource();
FME_Boolean exists = FME_FALSE;
IFMEServerString* repositoryName = serverSession_->createServerString();
IFMEServerString* workspaceName = serverSession_->createServerString();
IFMEServerString* resourceName = serverSession_->createServerString();
IFMEServerString* resourceFilePath = serverSession_->createServerString();
getInput(*repositoryName, "Please enter the repository name for the resource to get:\t");
getInput(*workspaceName, "Please enter the workspace name for the resource to get:\t");
getInput(*resourceName, "Please enter the resource name for the resource to get:\t");
getInput(*resourceFilePath, "Please enter the file path for the resource to get:\t");
IFMEServerStatus* status = repositoryMgr_->getRepository(*repositoryName, *repository, exists);
if (isSuccess(status))
{
status = repository->getResource(*workspaceName, *resourceName, *resourceFilePath, exists);
if (isSuccess(status))
{
cout << "Get resource succeeded\n";
}
}
serverSession_->destroyRepository(repository);
serverSession_->destroyResource(resource);
serverSession_->destroyServerString(repositoryName);
serverSession_->destroyServerString(workspaceName);
serverSession_->destroyServerString(resourceName);
serverSession_->destroyServerString(resourceFilePath);
}
//=========================================================================
//
void FMEServerAPIDemo::removeResource()
{
IFMEServerRepository* repository = serverSession_->createRepository();
IFMEServerResource* resource = serverSession_->createResource();
FME_Boolean existsRepository = FME_FALSE;
IFMEServerString* repositoryName = serverSession_->createServerString();
IFMEServerString* workspaceName = serverSession_->createServerString();
IFMEServerString* resourceName = serverSession_->createServerString();
getInput(*repositoryName, "Please enter the repository name for the resource to remove:\t");
getInput(*workspaceName, "Please enter the workspace name for the resource to remove:\t");
getInput(*resourceName, "Please enter the resource name for the resource to remove:\t");
IFMEServerStatus* status = repositoryMgr_->getRepository(*repositoryName, *repository, existsRepository);
if (isSuccess(status))
{
FME_Boolean existsResource = FME_FALSE;
status = repository->removeResource(*workspaceName,*resourceName, existsResource);
if (isSuccess(status))
{
cout << "Removed the resource\n";
}
}
serverSession_->destroyRepository(repository);
serverSession_->destroyResource(resource);
serverSession_->destroyServerString(repositoryName);
serverSession_->destroyServerString(workspaceName);
serverSession_->destroyServerString(resourceName);
}
//=========================================================================
//
void FMEServerAPIDemo::listServices()
{
IFMEServerServiceVector* services = serverSession_->createServiceVector();
IFMEServerStatus* status = repositoryMgr_->getServices(NULL, *services);
if (isSuccess(status))
{
IFMEServerString* name = serverSession_->createServerString();
IFMEServerString* urlPattern = serverSession_->createServerString();
IFMEServerString* description = serverSession_->createServerString();
IFMEServerString* displayName = serverSession_->createServerString();
for (FME_UInt32 i = 0; i < services->entries(); i++)
{
IFMEServerService* service = (*services)(i);
service->getName(*name);
service->getURLPattern(*urlPattern);
service->getDescription(*description);
service->getDisplayName(*displayName);
cout << name->data()
<< " " << displayName->data()
<< " " << urlPattern->data()
<< " ON:" << service->getIsEnabled()
<< " " << description->data() << endl;
}
serverSession_->destroyServerString(name);
serverSession_->destroyServerString(urlPattern);
serverSession_->destroyServerString(description);
serverSession_->destroyServerString(displayName);
}
serverSession_->destroyServiceVector(services);
}
//=========================================================================
//
void FMEServerAPIDemo::addService()
{
IFMEServerService* service = serverSession_->createService();
IFMEServerString* name = serverSession_->createServerString();
IFMEServerString* urlPattern = serverSession_->createServerString();
IFMEServerString* displayName = serverSession_->createServerString();
IFMEServerString* description = serverSession_->createServerString();
getInput(*name, "Please enter the name of the new service:\t");
getInput(*urlPattern, "Please enter the URL pattern of the new service:\t");
getInput(*displayName, "Please enter the display name of the new service:\t");
getInput(*description, "Please enter the description of the new service:\t");
service->setName(*name);
service->setURLPattern(*urlPattern);
service->setDisplayName(*displayName);
service->setDescription(*description);
service->setIsEnabled(FME_TRUE);
FME_Boolean existsService = FME_FALSE;
IFMEServerStatus* status = repositoryMgr_->serviceExists(*name, existsService);
if (isSuccess(status))
{
if (!existsService)
{
status = repositoryMgr_->addService(*service);
if (isSuccess(status))
{
cout << "Added the service\n";
}
}
else
{
cout << "A service with this name already exists!";
}
}
serverSession_->destroyServerString(name);
serverSession_->destroyServerString(urlPattern);
serverSession_->destroyServerString(displayName);
serverSession_->destroyServerString(description);
serverSession_->destroyService(service);
}
//=========================================================================
//
void FMEServerAPIDemo::updateService()
{
FME_Boolean exists = FME_FALSE;
IFMEServerService* service = serverSession_->createService();
IFMEServerString* name = serverSession_->createServerString();
IFMEServerString* urlPattern = serverSession_->createServerString();
IFMEServerString* displayName = serverSession_->createServerString();
IFMEServerString* description = serverSession_->createServerString();
getInput(*name, "Please enter the name of the service to update:\t");
getInput(*urlPattern, "Please enter the URL pattern of the service to update:\t");
getInput(*displayName, "Please enter the display name of the service to update:\t");
getInput(*description, "Please enter the description of the service to update:\t");
IFMEServerStatus* status = repositoryMgr_->getService(*name, *service, exists);
if (isSuccess(status))
{
service->setURLPattern(*urlPattern);
service->setDisplayName(*displayName);
service->setDescription(*description);
status = repositoryMgr_->updateService(*service);
if (isSuccess(status))
{
cout << "Updated the service\n";
}
}
serverSession_->destroyServerString(name);
serverSession_->destroyServerString(urlPattern);
serverSession_->destroyServerString(displayName);
serverSession_->destroyServerString(description);
serverSession_->destroyService(service);
}
//=========================================================================
//
void FMEServerAPIDemo::removeService()
{
FME_Boolean exists = FME_FALSE;
IFMEServerString* name = serverSession_->createServerString();
getInput(*name, "Please enter the name of the service to remove:\t");
IFMEServerStatus* status = repositoryMgr_->removeService(*name, exists);
if (isSuccess(status))
{
if (!exists)
{
cout << "There is no service with this name!" ;
}
}
serverSession_->destroyServerString(name);
}
//=========================================================================
//
FME_Boolean FMEServerAPIDemo::isSuccess(IFMEServerStatus*& serverStatus)
{
FME_Boolean isSuccessful = FME_TRUE;
if (serverStatus)
{
isSuccessful = FME_FALSE;
cout << serverStatus->getErrorMessage();
serverSession_->destroyServerStatus(serverStatus);
}
return isSuccessful;
}
//=========================================================================
//
void FMEServerAPIDemo::getInput(IFMEServerString& input, const char* promptMessage)
{
string stringInput;
cout << promptMessage;
getline(cin, stringInput, '\n');
input.set(stringInput.c_str(), (FME_UInt32)stringInput.length());
}
//=========================================================================
//
void argError()
{
cout << "USAGE:\n\tFMEServerAPIDemo <host> <port>";
exit(-1);
};
//=========================================================================
// This program accepts two parameters.
//
// First parameter is the FME Server host. ie. localhost
// Second parameter is the FME Server port. ie. 7071
//
int main(int argc, char *argv[])
{
if (argc != 3)
{
argError();
}
else
{
FMEServerAPIDemo demo(argv[1], argv[2]);
while (true)
{
demo.execute();
}
}
return 0;
}