LOG.info("Completing deploy package response: " + response);
// Load persisted request
Query query = entityManager.createNamedQuery(ContentServiceRequest.QUERY_FIND_BY_ID);
query.setParameter("id", response.getRequestId());
ContentServiceRequest persistedRequest = (ContentServiceRequest) query.getSingleResult();
Resource resource = persistedRequest.getResource();
int resourceTypeId = persistedRequest.getResource().getResourceType().getId();
// Update the persisted request
persistedRequest.setErrorMessage(response.getOverallRequestErrorMessage());
persistedRequest.setStatus(translateRequestResultStatus(response.getOverallRequestResult()));
// All history entries on the request at this point should be considered "in progress". We need to make
// sure each of these is closed out in some capacity. Typically, this will be done by the response
// explicitly indicating the result of each individual package. However, we can't rely on the plugin
// always doing this, so we need to keep track of which ones are not closed to prevent dangling in progress
// entries.
Set<InstalledPackageHistory> requestInProgressEntries = persistedRequest.getInstalledPackageHistory();
// Convert to a map so we can easily remove entries from it as they are closed by the individual
// package responses.
Map<PackageVersion, InstalledPackageHistory> inProgressEntries = new HashMap<PackageVersion, InstalledPackageHistory>(
requestInProgressEntries.size());
for (InstalledPackageHistory history : requestInProgressEntries) {
inProgressEntries.put(history.getPackageVersion(), history);
}
// Handle each individual package
long timestamp = System.currentTimeMillis();
for (DeployIndividualPackageResponse singleResponse : response.getPackageResponses()) {
// Load the package version for the relationship
PackageDetailsKey key = singleResponse.getKey();
Query packageVersionQuery = entityManager
.createNamedQuery(PackageVersion.QUERY_FIND_BY_PACKAGE_DETAILS_KEY_WITH_NON_NULL_RESOURCE_TYPE);
packageVersionQuery.setParameter("packageName", key.getName());
packageVersionQuery.setParameter("packageTypeName", key.getPackageTypeName());
packageVersionQuery.setParameter("architectureName", key.getArchitectureName());
packageVersionQuery.setParameter("version", key.getVersion());
packageVersionQuery.setParameter("resourceTypeId", resourceTypeId);
PackageVersion packageVersion = (PackageVersion) packageVersionQuery.getSingleResult();
// Create the history entity
InstalledPackageHistory history = new InstalledPackageHistory();
history.setContentServiceRequest(persistedRequest);
history.setPackageVersion(packageVersion);
history.setResource(resource);
history.setTimestamp(timestamp);
// Link the deployment configuration values that were saved for the initial history entity for this
// package with this entity as well. This will let us show the user the configuration values on the
// last entry in the audit trail (i.e. for a failed package, the configuration values will be accessible).
Query deploymentConfigurationQuery = entityManager
.createNamedQuery(InstalledPackageHistory.QUERY_FIND_CONFIG_BY_PACKAGE_VERSION_AND_REQ);
deploymentConfigurationQuery.setParameter("packageVersion", packageVersion);
deploymentConfigurationQuery.setParameter("contentServiceRequest", persistedRequest);
deploymentConfigurationQuery.setMaxResults(1);
Configuration deploymentConfiguration = null;
List deploymentConfigurationResults = deploymentConfigurationQuery.getResultList();
if (deploymentConfigurationResults.size() > 0) {
deploymentConfiguration = (Configuration) deploymentConfigurationResults.get(0);
deploymentConfiguration = deploymentConfiguration.deepCopy(false);
}
history.setDeploymentConfigurationValues(deploymentConfiguration);
// If the package indicated installation steps, link them to the resulting history entry
List<DeployPackageStep> transferObjectSteps = singleResponse.getDeploymentSteps();
if (transferObjectSteps != null) {
List<PackageInstallationStep> installationSteps = translateInstallationSteps(transferObjectSteps,
history);
history.setInstallationSteps(installationSteps);
}
if (singleResponse.getResult() == ContentResponseResult.SUCCESS) {
history.setStatus(InstalledPackageHistoryStatus.INSTALLED);
} else {
history.setStatus(InstalledPackageHistoryStatus.FAILED);
history.setErrorMessage(singleResponse.getErrorMessage());
}
entityManager.persist(history);
persistedRequest.addInstalledPackageHistory(history);
// We're closing out the package request for this package version, so remove it from the cache of entries
// that need to be closed
inProgressEntries.remove(packageVersion);
}
// For any entries that were not closed, add closing entries
for (InstalledPackageHistory unclosed : inProgressEntries.values()) {
PackageVersion packageVersion = unclosed.getPackageVersion();
// Create the history entity
InstalledPackageHistory history = new InstalledPackageHistory();
history.setContentServiceRequest(persistedRequest);
history.setPackageVersion(packageVersion);
history.setResource(resource);
history.setTimestamp(timestamp);
// One option is to create a new status that indicates unknown. For now, just give them the same result
// as the overall request result
if (response.getOverallRequestResult() == ContentResponseResult.SUCCESS) {
history.setStatus(InstalledPackageHistoryStatus.INSTALLED);
} else {
history.setStatus(InstalledPackageHistoryStatus.FAILED);
history.setErrorMessage(response.getOverallRequestErrorMessage());
}
entityManager.persist(history);
persistedRequest.addInstalledPackageHistory(history);
}
}