long version = uri.getVersion();
// Make sure we are not asked to add a resource to the index that has the
// same id as an existing one
if (id != null) {
SearchQuery q = new SearchQueryImpl(site).withIdentifier(id).withPreferredVersion(version).withLimit(1).withField(PATH);
SearchResultItem[] items = searchIdx.getByQuery(q).getItems();
if (items.length > 0) {
long versionInIndex = (Long) ((ResourceSearchResultItem) items[0]).getMetadataByKey(VERSION).getValue();
if (items.length == 1 && versionInIndex == version)
throw new ContentRepositoryException("Resource '" + id + "' already exists in version " + version);
if (path == null) {
path = (String) ((ResourceSearchResultItem) items[0]).getMetadataByKey(PATH).getValue();
resource.getURI().setPath(path);
}
}
}
// Make sure we are not asked to add a resource to the index that has the
// same path as an existing one
if (path != null) {
SearchQuery q = new SearchQueryImpl(site).withPath(path).withPreferredVersion(version).withLimit(1).withField(RESOURCE_ID);
SearchResultItem[] items = searchIdx.getByQuery(q).getItems();
if (items.length > 0) {
long versionInIndex = (Long) ((ResourceSearchResultItem) items[0]).getMetadataByKey(VERSION).getValue();
if (items.length == 1 && versionInIndex == version)
throw new ContentRepositoryException("Resource '" + id + "' already exists in version " + version);