use of org.apache.camel.component.salesforce.api.dto.SObject in project camel by apache.
the class CamelSalesforceMojo method execute.
/**
* Execute the mojo to generate SObject DTOs
*
* @throws MojoExecutionException
*/
public void execute() throws MojoExecutionException {
engine = createVelocityEngine();
// make sure we can load both templates
if (!engine.resourceExists(SOBJECT_POJO_VM) || !engine.resourceExists(SOBJECT_QUERY_RECORDS_VM) || !engine.resourceExists(SOBJECT_POJO_OPTIONAL_VM) || !engine.resourceExists(SOBJECT_QUERY_RECORDS_OPTIONAL_VM)) {
throw new MojoExecutionException("Velocity templates not found");
}
// connect to Salesforce
final SalesforceHttpClient httpClient = createHttpClient();
final SalesforceSession session = httpClient.getSession();
getLog().info("Salesforce login...");
try {
session.login(null);
} catch (SalesforceException e) {
String msg = "Salesforce login error " + e.getMessage();
throw new MojoExecutionException(msg, e);
}
getLog().info("Salesforce login successful");
// create rest client
RestClient restClient;
try {
restClient = new DefaultRestClient(httpClient, version, PayloadFormat.JSON, session);
// remember to start the active client object
((DefaultRestClient) restClient).start();
} catch (Exception e) {
final String msg = "Unexpected exception creating Rest client: " + e.getMessage();
throw new MojoExecutionException(msg, e);
}
try {
// use Jackson json
final ObjectMapper mapper = JsonUtils.createObjectMapper();
// call getGlobalObjects to get all SObjects
final Set<String> objectNames = new TreeSet<String>();
final SyncResponseCallback callback = new SyncResponseCallback();
try {
getLog().info("Getting Salesforce Objects...");
restClient.getGlobalObjects(callback);
if (!callback.await(responseTimeout, TimeUnit.MILLISECONDS)) {
throw new MojoExecutionException("Timeout waiting for getGlobalObjects!");
}
final SalesforceException ex = callback.getException();
if (ex != null) {
throw ex;
}
final GlobalObjects globalObjects = mapper.readValue(callback.getResponse(), GlobalObjects.class);
// create a list of object names
for (SObject sObject : globalObjects.getSobjects()) {
objectNames.add(sObject.getName());
}
} catch (Exception e) {
String msg = "Error getting global Objects: " + e.getMessage();
throw new MojoExecutionException(msg, e);
}
// check if we are generating POJOs for all objects or not
if ((includes != null && includes.length > 0) || (excludes != null && excludes.length > 0) || ObjectHelper.isNotEmpty(includePattern) || ObjectHelper.isNotEmpty(excludePattern)) {
filterObjectNames(objectNames);
} else {
getLog().warn(String.format("Generating Java classes for all %s Objects, this may take a while...", objectNames.size()));
}
// for every accepted name, get SObject description
final Set<SObjectDescription> descriptions = new HashSet<SObjectDescription>();
getLog().info("Retrieving Object descriptions...");
for (String name : objectNames) {
try {
callback.reset();
restClient.getDescription(name, callback);
if (!callback.await(responseTimeout, TimeUnit.MILLISECONDS)) {
throw new MojoExecutionException("Timeout waiting for getDescription for sObject " + name);
}
final SalesforceException ex = callback.getException();
if (ex != null) {
throw ex;
}
descriptions.add(mapper.readValue(callback.getResponse(), SObjectDescription.class));
} catch (Exception e) {
String msg = "Error getting SObject description for '" + name + "': " + e.getMessage();
throw new MojoExecutionException(msg, e);
}
}
// validate package name
if (!packageName.matches(PACKAGE_NAME_PATTERN)) {
throw new MojoExecutionException("Invalid package name " + packageName);
}
if (outputDirectory.getAbsolutePath().contains("$")) {
outputDirectory = new File("generated-sources/camel-salesforce");
}
final File pkgDir = new File(outputDirectory, packageName.trim().replace('.', File.separatorChar));
if (!pkgDir.exists()) {
if (!pkgDir.mkdirs()) {
throw new MojoExecutionException("Unable to create " + pkgDir);
}
}
getLog().info("Generating Java Classes...");
// generate POJOs for every object description
final GeneratorUtility utility = new GeneratorUtility(useStringsForPicklists);
// should we provide a flag to control timestamp generation?
final String generatedDate = new Date().toString();
for (SObjectDescription description : descriptions) {
try {
processDescription(pkgDir, description, utility, generatedDate);
} catch (IOException e) {
throw new MojoExecutionException("Unable to generate source files for: " + description.getName(), e);
}
}
getLog().info(String.format("Successfully generated %s Java Classes", descriptions.size() * 2));
} finally {
// remember to stop the client
try {
((DefaultRestClient) restClient).stop();
} catch (Exception ignore) {
}
// Salesforce session stop
try {
session.stop();
} catch (Exception ignore) {
}
// release HttpConnections
try {
httpClient.stop();
} catch (Exception ignore) {
}
}
}
Aggregations