* if adding to the index fails
*/
public synchronized ResourceURI add(Resource<?> resource) throws IOException,
ContentRepositoryException {
ResourceURI uri = resource.getURI();
String id = uri.getIdentifier();
String path = StringUtils.trimToNull(uri.getPath());
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);
if (id == null) {
id = (String) ((ResourceSearchResultItem) items[0]).getMetadataByKey(RESOURCE_ID).getValue();
resource.getURI().setIdentifier(id);
}
}
}
// Create an id if necessary. A missing id indicates that the resource
// has never been added to the index before
if (id == null) {
id = UUID.randomUUID().toString();
resource.setIdentifier(id);
uri.setIdentifier(id);
}
try {
searchIdx.add(resource);
} catch (ContentRepositoryException e) {