CheckArg.isNotNull(indexDefinitions, "indexDefinitions");
// Before we do anything, validate each of the index definitions and throw an exception ...
RepositoryNodeTypeManager nodeTypeManager = repository.nodeTypeManager();
List<IndexDefinition> validated = new ArrayList<>(indexDefinitions.length);
Problems problems = new SimpleProblems();
for (IndexDefinition defn : indexDefinitions) {
String name = defn.getName();
String providerName = defn.getProviderName();
if (name == null) {
problems.addError(JcrI18n.indexMustHaveName, defn, repository.name());
continue;
}
if (indexes.getIndexDefinitions().containsKey(name) && !allowUpdate) {
// Throw this one immediately ...
String msg = JcrI18n.indexAlreadyExists.text(defn.getName(), repository.name());
throw new IndexExistsException(msg);
}
if (providerName == null) {
problems.addError(JcrI18n.indexMustHaveProviderName, defn.getName(), repository.name());
continue;
}
if (defn.hasSingleColumn()) {
IndexColumnDefinition columnDefn = defn.getColumnDefinition(0);
Name propName = context.getValueFactories().getNameFactory().create(columnDefn.getPropertyName());
switch (defn.getKind()) {
case UNIQUE_VALUE:
if (NON_UNIQUE_PROPERTY_NAMES.contains(propName)) {
problems.addError(JcrI18n.unableToCreateUniqueIndexForColumn, defn.getName(),
columnDefn.getPropertyName());
}
break;
case ENUMERATED_VALUE:
if (NON_ENUMERATED_PROPERTY_NAMES.contains(propName)) {
problems.addError(JcrI18n.unableToCreateEnumeratedIndexForColumn, defn.getName(),
columnDefn.getPropertyName());
}
break;
case VALUE:
case NODE_TYPE:
case TEXT:
break;
}
} else {
// Mulitple columns ...
if (defn.getKind() == IndexKind.NODE_TYPE) {
// must be single-column indexes
problems.addError(JcrI18n.nodeTypeIndexMustHaveOneColumn, defn.getName());
}
}
IndexProvider provider = providers.get(providerName);
if (provider == null) {
problems.addError(JcrI18n.indexProviderDoesNotExist, defn, repository.name());
} else {
// Have the provider validate the index
provider.validateProposedIndex(context, defn, nodeTypeManager, problems);
// Create an instance of our own definition implementation ...
defn = RepositoryIndexDefinition.createFrom(defn, true);
validated.add(defn);
}
}
if (problems.hasErrors()) {
String msg = JcrI18n.invalidIndexDefinitions.text(repository.name(), problems);
throw new InvalidIndexDefinitionException(new JcrProblems(problems), msg);
}
SessionCache systemCache = repository.createSystemSession(context, false);