use of com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException in project registry by hortonworks.
the class SchemaVersionLifecycleStates method transitionToEnableState.
public static void transitionToEnableState(SchemaVersionLifecycleContext context) throws SchemaNotFoundException, IncompatibleSchemaException, SchemaLifecycleException, SchemaBranchNotFoundException {
Long schemaVersionId = context.getSchemaVersionId();
SchemaVersionService schemaVersionService = context.getSchemaVersionService();
SchemaMetadataInfo schemaMetadataInfo = schemaVersionService.getSchemaMetadata(schemaVersionId);
SchemaMetadata schemaMetadata = schemaMetadataInfo.getSchemaMetadata();
String schemaName = schemaMetadata.getName();
SchemaValidationLevel validationLevel = schemaMetadata.getValidationLevel();
SchemaVersionInfo schemaVersionInfo = schemaVersionService.getSchemaVersionInfo(schemaVersionId);
int schemaVersion = schemaVersionInfo.getVersion();
String schemaText = schemaVersionInfo.getSchemaText();
List<SchemaVersionInfo> allEnabledSchemaVersions = schemaVersionService.getAllSchemaVersions(SchemaBranch.MASTER_BRANCH, schemaName).stream().filter(x -> SchemaVersionLifecycleStates.ENABLED.getId().equals(x.getStateId())).collect(Collectors.toList());
if (!allEnabledSchemaVersions.isEmpty()) {
if (validationLevel.equals(SchemaValidationLevel.ALL)) {
for (SchemaVersionInfo curSchemaVersionInfo : allEnabledSchemaVersions) {
int curVersion = curSchemaVersionInfo.getVersion();
if (curVersion < schemaVersion) {
checkCompatibility(schemaVersionService, schemaMetadata, schemaText, curSchemaVersionInfo.getSchemaText());
} else {
checkCompatibility(schemaVersionService, schemaMetadata, curSchemaVersionInfo.getSchemaText(), schemaText);
}
}
} else if (validationLevel.equals(SchemaValidationLevel.LATEST)) {
List<SchemaVersionInfo> sortedSchemaVersionInfos = new ArrayList<>(allEnabledSchemaVersions);
sortedSchemaVersionInfos.sort(Comparator.comparingInt(SchemaVersionInfo::getVersion));
int i = 0;
int size = sortedSchemaVersionInfos.size();
for (; i < size && sortedSchemaVersionInfos.get(i).getVersion() < schemaVersion; i++) {
String fromSchemaText = sortedSchemaVersionInfos.get(i).getSchemaText();
checkCompatibility(schemaVersionService, schemaMetadata, schemaText, fromSchemaText);
}
for (; i < size && sortedSchemaVersionInfos.get(i).getVersion() > schemaVersion; i++) {
String toSchemaText = sortedSchemaVersionInfos.get(i).getSchemaText();
checkCompatibility(schemaVersionService, schemaMetadata, toSchemaText, schemaText);
}
}
}
context.setState(ENABLED);
context.updateSchemaVersionState();
}
Aggregations