@ -3,23 +3,27 @@ package com.apside.assist.db.backend.service.implementation;
import com.apside.assist.db.backend.model.LinkInfo ;
import com.apside.assist.db.backend.model.LinkInfo ;
import com.apside.assist.db.backend.model.InfoColumn ;
import com.apside.assist.db.backend.model.InfoColumn ;
import com.apside.assist.db.backend.model.InfoTable ;
import com.apside.assist.db.backend.model.InfoTable ;
import com.apside.assist.db.backend.model.json.Column ;
import com.apside.assist.db.backend.model.json.DataEnum ;
import com.apside.assist.db.backend.model.json.Schema ;
import com.apside.assist.db.backend.service.InfoColumnService ;
import com.apside.assist.db.backend.service.InfoColumnService ;
import com.apside.assist.db.backend.service.InfoTableService ;
import com.apside.assist.db.backend.service.InfoTableService ;
import com.apside.assist.db.backend.service.LinkInfoService ;
import com.apside.assist.db.backend.service.LinkInfoService ;
import com.apside.assist.db.backend.service.ResetDataService ;
import com.apside.assist.db.backend.service.ResetDataService ;
import lombok.Data ;
import com.fasterxml.jackson.core.JsonProcessingException ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import lombok.extern.slf4j.Slf4j ;
import lombok.extern.slf4j.Slf4j ;
import org.json.JSONArray ;
import org.json.JSONObject ;
import org.springframework.beans.factory.InitializingBean ;
import org.springframework.beans.factory.InitializingBean ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.stereotype.Service ;
import org.springframework.stereotype.Service ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import java.io.IOException ;
import java.nio.file.Files ;
import java.nio.file.Files ;
import java.nio.file.Path ;
import java.nio.file.Path ;
import java.nio.file.Paths ;
import java.nio.file.Paths ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.stream.Collectors ;
@Service
@Service
@Slf4j
@Slf4j
@ -47,6 +51,8 @@ public class ResetDataServiceImpl implements ResetDataService, InitializingBean
private static final String LENGTH_STRING = "length" ;
private static final String LENGTH_STRING = "length" ;
private static final String COLUMN_TEXT_STRING = "column_text" ;
private static final String COLUMN_TEXT_STRING = "column_text" ;
@Value ( "${spring.jpa.properties.hibernate.jdbc.batch_size}" )
private int batchSize ;
@Override
@Override
public void afterPropertiesSet ( ) throws Exception {
public void afterPropertiesSet ( ) throws Exception {
@ -62,140 +68,52 @@ public class ResetDataServiceImpl implements ResetDataService, InitializingBean
}
}
@Override
@Override
public void insertEverything ( ) {
public void insertEverything ( ) throws JsonProcessingException {
log . debug ( "Start insert every data into DB" ) ;
log . debug ( "Start insert every data into DB" ) ;
JSONArray jo = new JSONArray ( result ) ;
List < InfoColumn > listOfColumns = new ArrayList < > ( ) ;
for ( int i = 0 ; i < jo . length ( ) ; i + + ) {
List < InfoTable > listOfTables = new ArrayList < > ( ) ;
JSONObject schema = jo . getJSONObject ( i ) ;
ObjectMapper objectMapper = new ObjectMapper ( ) ;
String schemaName = schema . getString ( SCHEMA_STRING ) ;
Schema [ ] jsonData = objectMapper . readValue ( result , Schema [ ] . class ) ;
JSONArray data = schema . getJSONArray ( DATA_STRING ) ;
for ( Schema schema : jsonData ) {
for ( DataEnum dat : schema . getData ( ) ) {
for ( int ia = 0 ; ia < data . length ( ) ; ia + + ) {
listOfTables . add ( new InfoTable ( dat . getTable ( ) , schema . getSchema ( ) , dat . getTable_text ( ) ) ) ;
JSONObject apra = data . getJSONObject ( ia ) ;
for ( Column col : dat . getColumns ( ) ) {
String tableText ;
String descCol = col . getColumn_text ( ) ;
String tableName ;
if ( descCol = = null ) {
if ( apra . has ( TABLE_TEXT_STRING ) ) {
descCol = "" ;
tableText = apra . getString ( TABLE_TEXT_STRING ) ;
} else {
tableText = "" ;
}
if ( apra . has ( TABLE_STRING ) ) {
tableName = apra . getString ( TABLE_STRING ) ;
} else {
tableName = "" ;
}
JSONArray col = apra . getJSONArray ( COLUMN_STRING ) ;
InfoTable infoTable = new InfoTable ( ) ;
infoTable . setNameTable ( tableName ) ;
infoTable . setNameSchema ( schemaName ) ;
infoTable . setTableText ( tableText ) ;
infoTableService . addOrUpdateTable ( infoTable ) ;
for ( int ib = 0 ; ib < col . length ( ) ; ib + + ) {
JSONObject colData = col . getJSONObject ( ib ) ;
String nameCol ;
String dataType ;
String columnText ;
int lengthCol ;
if ( colData . has ( NAME_STRING ) ) {
nameCol = colData . getString ( NAME_STRING ) ;
} else {
nameCol = "" ;
}
if ( colData . has ( DATA_TYPE_STRING ) ) {
dataType = colData . getString ( DATA_TYPE_STRING ) ;
} else {
dataType = "" ;
}
if ( colData . has ( LENGTH_STRING ) ) {
lengthCol = colData . getInt ( LENGTH_STRING ) ;
} else {
lengthCol = 0 ;
}
if ( colData . has ( COLUMN_TEXT_STRING ) ) {
columnText = colData . getString ( COLUMN_TEXT_STRING ) ;
} else {
columnText = "" ;
}
InfoColumn infoColumn = new InfoColumn ( ) ;
infoColumn . setNameColumn ( nameCol ) ;
infoColumn . setDataType ( dataType ) ;
infoColumn . setLengthColumn ( lengthCol ) ;
infoColumn . setColumnText ( columnText ) ;
InfoColumn findCol = infoColumnService . getSpecCol ( nameCol , dataType , lengthCol , columnText ) ;
if ( findCol = = null ) {
infoColumnService . addOrUpdateColumn ( infoColumn ) ;
}
}
listOfColumns . add ( new InfoColumn ( col . getName ( ) , col . getData_type ( ) , col . getLength ( ) , descCol ) ) ;
}
}
}
}
}
}
List < InfoColumn > filteredListOfColumns = listOfColumns . stream ( ) . distinct ( ) . collect ( Collectors . toList ( ) ) ;
infoTableService . addMultipleTables ( listOfTables ) ;
infoColumnService . addMultipleColumns ( filteredListOfColumns ) ;
log . info ( "Insert all data into DB - success" ) ;
log . info ( "Insert all data into DB - success" ) ;
}
}
@Override
@Override
public void checkAndInsertLinks ( ) {
public void checkAndInsertLinks ( ) throws JsonProcessingException {
log . debug ( "Start check data into DB and insert links between table and columns" ) ;
log . debug ( "Start check data into DB and insert links between table and columns" ) ;
JSONArray jo = new JSONArray ( result ) ;
List < LinkInfo > listOfLink = new ArrayList < > ( ) ;
for ( int i = 0 ; i < jo . length ( ) ; i + + ) {
ObjectMapper objectMapper = new ObjectMapper ( ) ;
JSONObject schema = jo . getJSONObject ( i ) ;
Schema [ ] jsonData = objectMapper . readValue ( result , Schema [ ] . class ) ;
String schemaName = schema . getString ( SCHEMA_STRING ) ;
for ( Schema schema : jsonData ) {
JSONArray data = schema . getJSONArray ( DATA_STRING ) ;
for ( DataEnum dat : schema . getData ( ) ) {
for ( Column col : dat . getColumns ( ) ) {
for ( int ia = 0 ; ia < data . length ( ) ; ia + + ) {
String descCol = col . getColumn_text ( ) ;
JSONObject apra = data . getJSONObject ( ia ) ;
if ( descCol = = null ) {
String tableName ;
descCol = "" ;
if ( apra . has ( TABLE_STRING ) ) {
}
tableName = apra . getString ( TABLE_STRING ) ;
InfoColumn findCol = infoColumnService . getSpecCol ( col . getName ( ) , col . getData_type ( ) , col . getLength ( ) , descCol ) ;
} else {
int idCol = Math . toIntExact ( findCol . getId ( ) ) ;
tableName = "" ;
}
JSONArray col = apra . getJSONArray ( COLUMN_STRING ) ;
for ( int ib = 0 ; ib < col . length ( ) ; ib + + ) {
JSONObject colData = col . getJSONObject ( ib ) ;
String nameCol ;
String dataType ;
String columnText ;
int lengthCol ;
if ( colData . has ( NAME_STRING ) ) {
nameCol = colData . getString ( NAME_STRING ) ;
} else {
nameCol = "" ;
}
if ( colData . has ( DATA_TYPE_STRING ) ) {
dataType = colData . getString ( DATA_TYPE_STRING ) ;
} else {
dataType = "" ;
}
if ( colData . has ( LENGTH_STRING ) ) {
lengthCol = colData . getInt ( LENGTH_STRING ) ;
} else {
lengthCol = 0 ;
}
if ( colData . has ( COLUMN_TEXT_STRING ) ) {
columnText = colData . getString ( COLUMN_TEXT_STRING ) ;
} else {
columnText = "" ;
}
InfoColumn findCol = infoColumnService . getSpecCol ( nameCol , dataType , lengthCol , columnText ) ;
Long idCol = findCol . getId ( ) ;
int intId = Math . toIntExact ( idCol ) ;
if ( findCol ! = null ) {
if ( findCol ! = null ) {
LinkInfo linkInfo = new LinkInfo ( ) ;
listOfLink . add ( new LinkInfo ( dat . getTable ( ) , schema . getSchema ( ) , idCol ) ) ;
linkInfo . setNameSchema ( schemaName ) ;
linkInfo . setNameTable ( tableName ) ;
linkInfo . setColumnId ( intId ) ;
linkInfoService . addOrUpdateLink ( linkInfo ) ;
}
}
}
}
}
}
}
}
linkInfoService . addMultipleLinkInfo ( listOfLink ) ;
log . info ( "Check and insert links data into DB - success" ) ;
log . info ( "Check and insert links data into DB - success" ) ;
}
}