//default version to 1.0 if is null, not provided for any reason.
if ((version == null) || (version.trim().length() == 0)) {
version = "1.0";
}
Architecture architecture = entityManager.find(Architecture.class, architectureId);
PackageType packageType = entityManager.find(PackageType.class, packageTypeId);
// See if package version already exists for the resource package
Query packageVersionQuery = null;
if (packageType.getResourceType() != null) {
packageVersionQuery = entityManager
.createNamedQuery(PackageVersion.QUERY_FIND_BY_PACKAGE_DETAILS_KEY_WITH_NON_NULL_RESOURCE_TYPE);
packageVersionQuery.setParameter("resourceTypeId", packageType.getResourceType().getId());
} else {
packageVersionQuery = entityManager.createNamedQuery(PackageVersion.QUERY_FIND_BY_PACKAGE_DETAILS_KEY);
packageVersionQuery.setParameter("resourceType", null);
}
packageVersionQuery.setFlushMode(FlushModeType.COMMIT);
packageVersionQuery.setParameter("packageName", packageName);
packageVersionQuery.setParameter("packageTypeName", packageType.getName());
packageVersionQuery.setParameter("architectureName", architecture.getName());
packageVersionQuery.setParameter("version", version);
// Result of the query should be either 0 or 1
List<PackageVersion> existingPackageVersionList = packageVersionQuery.getResultList();
if (existingPackageVersionList.size() > 0) {
packageVersion = existingPackageVersionList.get(0);
}
try {
PackageTypeBehavior behavior = ContentManagerHelper.getPackageTypeBehavior(packageTypeId);
if (behavior != null) {
String packageTypeName = packageType.getName();
String archName = architecture.getName();
ValidatablePackageDetailsKey key = new ValidatablePackageDetailsKey(packageName, version,
packageTypeName, archName);
behavior.validateDetails(key, subject);
//update the details from the validation results
packageName = key.getName();
version = key.getVersion();
if (!architecture.getName().equals(key.getArchitectureName())) {
Query q = entityManager.createNamedQuery(Architecture.QUERY_FIND_BY_NAME);
q.setParameter("name", key.getArchitectureName());
architecture = (Architecture) q.getSingleResult();
}
}