* {@inheritDoc}
*/
public JcrPackage rename(JcrPackage pack, String group, String name, String version)
throws PackageException, RepositoryException {
if (!pack.isValid()) {
throw new PackageException("Package is not valid.");
}
if (pack.getSize() > 0 && !pack.getDefinition().isUnwrapped()) {
throw new PackageException("Package definition not unwrapped.");
}
if (!PackageId.isValid(group, name, version)) {
throw new RepositoryException("Unable to rename package. Illegal package name.");
}
JcrPackageDefinition def = pack.getDefinition();
PackageId id = def.getId();
PackageId newId = new PackageId(
group == null ? id.getGroup() : group,
name == null ? id.getName() : name,
version == null ? id.getVersion() : Version.create(version)
);
String dstPath = newId.getInstallationPath() + ".zip";
if (id.equals(newId) && pack.getNode().getPath().equals(dstPath)) {
log.info("Package id not changed. won't rename.");
return pack;
}
def.setId(newId, false);
// only move if not already at correct location
if (!pack.getNode().getPath().equals(dstPath)) {
if (session.nodeExists(dstPath)) {
throw new PackageException("Node at " + dstPath + " already exists.");
}
// ensure parent path exists
mkdir(Text.getRelativeParent(dstPath, 1), false);
session.move(pack.getNode().getPath(), dstPath);
}