*/
public Version getNextVersion(final JcrPackageManager jcrPackageManager,
final String groupName, final String name,
final String version) throws RepositoryException {
final Node packageRoot = jcrPackageManager.getPackageRoot(false);
final Version configVersion = Version.create(version);
if (!packageRoot.hasNode(groupName)) {
return configVersion;
}
final Node packageGroupNode = packageRoot.getNode(groupName);
if (packageGroupNode == null) {
return configVersion;
} else {
final NodeIterator children = packageGroupNode.getNodes();
Version latestVersion = configVersion;
boolean configVersionEligible = true;
while (children.hasNext()) {
final Node child = children.nextNode();
final JcrPackage jcrPackage = jcrPackageManager.open(child, true);
if (jcrPackage == null
|| jcrPackage.getDefinition() == null
|| jcrPackage.getDefinition().getId() == null) {
log.warn("Could not covert node [ {} ] into a proper JCR Package, moving to next node",
child.getPath());
continue;
} else if (!StringUtils.equals(name, jcrPackage.getDefinition().getId().getName())) {
// Name mismatch - so just skip
continue;
}
final Version packageVersion = jcrPackage.getDefinition().getId().getVersion();
log.debug(packageVersion.toString() + " compareTo " + latestVersion.toString()
+ " = " + packageVersion.compareTo(latestVersion));
if (packageVersion.compareTo(latestVersion) >= 1) {
latestVersion = packageVersion;
log.debug("Found a new latest version: {}", latestVersion.toString());
} else if (packageVersion.compareTo(configVersion) == 0) {
configVersionEligible = false;
log.debug("Found a package with the same version as the config version");
}
}
log.debug("Current latest version: {}", latestVersion.toString());
if (configVersionEligible && latestVersion.equals(configVersion)) {
// If the config-specified version is newer than any existing package, jump to the config version
return configVersion;
} else {
// Else increment the latest known version's minor
latestVersion = this.normalizeVersion(latestVersion);
final String[] segments = latestVersion.getNormalizedSegments();
// Increment minor
segments[1] = String.valueOf(Integer.parseInt(segments[1]) + 1);
final Version nextVersion = Version.create(segments);
log.debug("Next version: {}", nextVersion);
return nextVersion;
}
}
}