@SuppressWarnings("unchecked")
public PackageVersion getUploadedPackageVersion(Subject subject, String packageName, int packageTypeId,
String version, int architectureId, InputStream packageBitStream, Map<String, String> packageUploadDetails,
Integer repoId) {
PackageVersion packageVersion = null;
//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();
}
}
} catch (PackageDetailsValidationException e) {
throw e;
} catch (Exception e) {
LOG.error("Failed to get the package type plugin container. This is a bug.", e);
throw new IllegalStateException("Failed to get the package type plugin container.", e);
}
Package existingPackage = null;
Query packageQuery = entityManager.createNamedQuery(Package.QUERY_FIND_BY_NAME_PKG_TYPE_ID);
packageQuery.setParameter("name", packageName);
packageQuery.setParameter("packageTypeId", packageTypeId);
List<Package> existingPackageList = packageQuery.getResultList();
if (existingPackageList.size() == 0) {
// If the package doesn't exist, create that here
existingPackage = new Package(packageName, packageType);
existingPackage = persistOrMergePackageSafely(existingPackage);
} else {
existingPackage = existingPackageList.get(0);
}
//initialize package version if not already
if (packageVersion == null) {
packageVersion = new PackageVersion(existingPackage, version, architecture);
packageVersion.setDisplayName(existingPackage.getName());
entityManager.persist(packageVersion);
}
//get the data
Map<String, String> contentDetails = new HashMap<String, String>();
PackageBits bits = loadPackageBits(packageBitStream, packageVersion.getId(), packageName, version, null,
contentDetails);
packageVersion.setPackageBits(bits);
packageVersion.setFileSize(Long.valueOf(contentDetails.get(UPLOAD_FILE_SIZE)).longValue());
packageVersion.setSHA256(contentDetails.get(UPLOAD_SHA256));
//populate extra details, persist
if (packageUploadDetails != null) {
packageVersion.setFileCreatedDate(Long.valueOf(packageUploadDetails
.get(ContentManagerLocal.UPLOAD_FILE_INSTALL_DATE)));
packageVersion.setFileName(packageUploadDetails.get(ContentManagerLocal.UPLOAD_FILE_NAME));
packageVersion.setMD5(packageUploadDetails.get(ContentManagerLocal.UPLOAD_MD5));
packageVersion.setDisplayVersion(packageUploadDetails.get(ContentManagerLocal.UPLOAD_DISPLAY_VERSION));
}
entityManager.merge(packageVersion);
if (repoId != null) {
int[] packageVersionIds = new int[] { packageVersion.getId() };
repoManager.addPackageVersionsToRepo(subject, repoId, packageVersionIds);
}
entityManager.flush();