Commit 682e68f2 authored by Aman Verma's avatar Aman Verma
Browse files

refactoring methods

parent ddb41bd9
Pipeline #62444 failed with stages
in 34 minutes and 20 seconds
......@@ -12,6 +12,7 @@ import java.util.Map;
import java.util.stream.Collectors;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
......@@ -132,44 +133,7 @@ public class SchemaService implements ISchemaService {
*/
@Override
public SchemaInfo createSchema(SchemaRequest schemaRequest) throws ApplicationException, BadRequestException {
String dataPartitionId = headers.getPartitionId();
String schemaId = createAndSetSchemaId(schemaRequest);
if (schemaInfoStore.isUnique(schemaId, dataPartitionId)) {
setScope(schemaRequest,false);
String schema = resolveAndCheckBreakingChanges(schemaRequest, false);
Boolean authority = authorityService.checkAndRegisterAuthorityIfNotPresent(
schemaRequest.getSchemaInfo().getSchemaIdentity().getAuthority());
Boolean source = sourceService
.checkAndRegisterSourceIfNotPresent(schemaRequest.getSchemaInfo().getSchemaIdentity().getSource());
Boolean entity = entityTypeService.checkAndRegisterEntityTypeIfNotPresent(
schemaRequest.getSchemaInfo().getSchemaIdentity().getEntityType());
if (authority && source && entity) {
log.info(SchemaConstants.SCHEMA_CREATION_STARTED);
try {
SchemaInfo schemaInfo = schemaInfoStore.createSchemaInfo(schemaRequest);
schemaStore.createSchema(schemaId, schema);
auditLogger.schemaRegisteredSuccess(Collections.singletonList(schemaRequest.toString()));
messageBus.publishMessage(schemaId, SchemaConstants.SCHEMA_CREATE_EVENT_TYPE);
return schemaInfo;
} catch (ApplicationException ex) {
auditLogger.schemaRegisteredFailure(
Collections.singletonList(schemaRequest.toString()));
log.warning(SchemaConstants.SCHEMA_CREATION_FAILED);
schemaInfoStore.cleanSchema(schemaId);
schemaStore.cleanSchemaProject(schemaId);
log.info(SchemaConstants.SCHEMA_CREATE_CLEAN);
throw ex;
}
} else {
log.error("The schema could not be created due invalid authority,source or entityType");
throw new ApplicationException(SchemaConstants.INTERNAL_SERVER_ERROR);
}
} else {
throw new BadRequestException(SchemaConstants.SCHEMA_ID_EXISTS);
}
return this.createSchemaInternal(schemaRequest, false);
}
private SchemaInfo createSchemaInternal(SchemaRequest schemaRequest, Boolean isSystemSchema) throws ApplicationException, BadRequestException {
......@@ -190,8 +154,8 @@ public class SchemaService implements ISchemaService {
if (authority && source && entity) {
log.info(SchemaConstants.SCHEMA_CREATION_STARTED);
try {
SchemaInfo schemaInfo = schemaInfoStore.createSchemaInfo(schemaRequest);
schemaStore.createSchema(schemaId, schema);
SchemaInfo schemaInfo = this.createSchemaInfo(schemaRequest, isSystemSchema);
this.createSchema(schemaId, schema, isSystemSchema);
auditLogger.schemaRegisteredSuccess(Collections.singletonList(schemaRequest.toString()));
messageBus.publishMessage(schemaId, SchemaConstants.SCHEMA_CREATE_EVENT_TYPE);
return schemaInfo;
......@@ -199,8 +163,8 @@ public class SchemaService implements ISchemaService {
auditLogger.schemaRegisteredFailure(
Collections.singletonList(schemaRequest.toString()));
log.warning(SchemaConstants.SCHEMA_CREATION_FAILED);
schemaInfoStore.cleanSchema(schemaId);
schemaStore.cleanSchemaProject(schemaId);
this.cleanSchema(schemaId, isSystemSchema);
this.cleanSchemaProject(schemaId, isSystemSchema);
log.info(SchemaConstants.SCHEMA_CREATE_CLEAN);
throw ex;
}
......@@ -213,76 +177,8 @@ public class SchemaService implements ISchemaService {
}
}
private Boolean checkAndRegisterAuthorityIfNotPresent(String authorityId, Boolean isSystemSchema) {
if (isSystemSchema) {
return authorityService.checkAndRegisterSystemAuthorityIfNotPresent(authorityId);
} else {
return authorityService.checkAndRegisterAuthorityIfNotPresent(authorityId);
}
}
private Boolean checkAndRegisterSourceIfNotPresent(String sourceId, Boolean isSystemSchema) {
if (isSystemSchema) {
return sourceService.checkAndRegisterSystemSourceIfNotPresent(sourceId);
} else {
return sourceService.checkAndRegisterSourceIfNotPresent(sourceId);
}
}
private Boolean checkAndRegisterEntityTypeIfNotPresent(String entityTypeId, Boolean isSystemSchema) {
if (isSystemSchema) {
return entityTypeService.checkAndRegisterSystemEntityTypeIfNotPresent(entityTypeId);
} else {
return entityTypeService.checkAndRegisterEntityTypeIfNotPresent(entityTypeId);
}
}
private Boolean isUniqueSchema(String schemaId, Boolean isSystemSchema) throws ApplicationException {
if (isSystemSchema) {
return schemaInfoStore.isUniqueSystemSchema(schemaId);
} else {
return schemaInfoStore.isUnique(schemaId, headers.getPartitionId());
}
}
private SchemaInfo createSystemSchema(SchemaRequest schemaRequest) throws ApplicationException, BadRequestException {
String schemaId = createAndSetSchemaId(schemaRequest);
if (schemaInfoStore.isUniqueSystemSchema(schemaId)) {
setScope(schemaRequest,false);
String schema = resolveAndCheckBreakingChanges(schemaRequest, true);
Boolean authority = authorityService.checkAndRegisterAuthorityIfNotPresent(
schemaRequest.getSchemaInfo().getSchemaIdentity().getAuthority());
Boolean source = sourceService
.checkAndRegisterSourceIfNotPresent(schemaRequest.getSchemaInfo().getSchemaIdentity().getSource());
Boolean entity = entityTypeService.checkAndRegisterEntityTypeIfNotPresent(
schemaRequest.getSchemaInfo().getSchemaIdentity().getEntityType());
if (authority && source && entity) {
log.info(SchemaConstants.SCHEMA_CREATION_STARTED);
try {
SchemaInfo schemaInfo = schemaInfoStore.createSchemaInfo(schemaRequest);
schemaStore.createSchema(schemaId, schema);
auditLogger.schemaRegisteredSuccess(Collections.singletonList(schemaRequest.toString()));
messageBus.publishMessage(schemaId, SchemaConstants.SCHEMA_CREATE_EVENT_TYPE);
return schemaInfo;
} catch (ApplicationException ex) {
auditLogger.schemaRegisteredFailure(
Collections.singletonList(schemaRequest.toString()));
log.warning(SchemaConstants.SCHEMA_CREATION_FAILED);
schemaInfoStore.cleanSchema(schemaId);
schemaStore.cleanSchemaProject(schemaId);
log.info(SchemaConstants.SCHEMA_CREATE_CLEAN);
throw ex;
}
} else {
log.error("The schema could not be created due invalid authority,source or entityType");
throw new ApplicationException(SchemaConstants.INTERNAL_SERVER_ERROR);
}
} else {
throw new BadRequestException(SchemaConstants.SCHEMA_ID_EXISTS);
}
return this.createSchemaInternal(schemaRequest, true);
}
/**
......@@ -580,5 +476,69 @@ public class SchemaService implements ISchemaService {
}
}
private SchemaInfo createSchemaInfo(SchemaRequest schemaRequest, Boolean isSystemSchema) throws ApplicationException, BadRequestException {
if (isSystemSchema) {
return schemaInfoStore.createSystemSchemaInfo(schemaRequest);
} else {
return schemaInfoStore.createSchemaInfo(schemaRequest);
}
}
private Boolean cleanSchema(String schemaId, Boolean isSystemSchema) throws ApplicationException {
if(isSystemSchema) {
return schemaInfoStore.cleanSystemSchema(schemaId);
} else {
return schemaInfoStore.cleanSchema(schemaId);
}
}
private String createSchema(String schemaId, String schema, Boolean isSystemSchema) throws ApplicationException {
if (isSystemSchema) {
return schemaStore.createSystemSchema(schemaId, schema);
} else {
return schemaStore.createSchema(schemaId, schema);
}
}
private Boolean cleanSchemaProject(String schemaId, Boolean isSystemSchema) throws ApplicationException {
if (isSystemSchema) {
return schemaStore.cleanSystemSchemaProject(schemaId);
} else {
return schemaStore.cleanSchemaProject(schemaId);
}
}
private Boolean checkAndRegisterAuthorityIfNotPresent(String authorityId, Boolean isSystemSchema) {
if (isSystemSchema) {
return authorityService.checkAndRegisterSystemAuthorityIfNotPresent(authorityId);
} else {
return authorityService.checkAndRegisterAuthorityIfNotPresent(authorityId);
}
}
private Boolean checkAndRegisterSourceIfNotPresent(String sourceId, Boolean isSystemSchema) {
if (isSystemSchema) {
return sourceService.checkAndRegisterSystemSourceIfNotPresent(sourceId);
} else {
return sourceService.checkAndRegisterSourceIfNotPresent(sourceId);
}
}
private Boolean checkAndRegisterEntityTypeIfNotPresent(String entityTypeId, Boolean isSystemSchema) {
if (isSystemSchema) {
return entityTypeService.checkAndRegisterSystemEntityTypeIfNotPresent(entityTypeId);
} else {
return entityTypeService.checkAndRegisterEntityTypeIfNotPresent(entityTypeId);
}
}
private Boolean isUniqueSchema(String schemaId, Boolean isSystemSchema) throws ApplicationException {
if (isSystemSchema) {
return schemaInfoStore.isUniqueSystemSchema(schemaId);
} else {
return schemaInfoStore.isUnique(schemaId, headers.getPartitionId());
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment