parent
2dbfd6d667
commit
dbd113590f
@ -0,0 +1,25 @@ |
||||
package com.secondtest.secondtest.controller; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.Optional; |
||||
import com.secondtest.secondtest.service.InfoColumnService; |
||||
import com.secondtest.secondtest.service.InfoTableService; |
||||
import com.secondtest.secondtest.service.LinkInfoService; |
||||
import com.secondtest.secondtest.service.ResetDataService; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
@RestController |
||||
@RequestMapping("/api") |
||||
public class ResetDataController { |
||||
@Autowired |
||||
private ResetDataService resetDataService; |
||||
|
||||
|
||||
@PostMapping("/reset") |
||||
public void resetData() throws IOException { |
||||
resetDataService.deleteEverything(); |
||||
resetDataService.insertEverything(); |
||||
resetDataService.checkAndInsertLinks(); |
||||
} |
||||
} |
@ -1,10 +1,17 @@ |
||||
package com.secondtest.secondtest.repository; |
||||
|
||||
import com.secondtest.secondtest.model.LinkInfo; |
||||
import org.springframework.data.jpa.repository.Modifying; |
||||
import org.springframework.data.jpa.repository.Query; |
||||
import org.springframework.data.repository.CrudRepository; |
||||
import org.springframework.stereotype.Repository; |
||||
import org.springframework.transaction.annotation.Transactional; |
||||
|
||||
@Repository |
||||
public interface LinkInfoRepository extends CrudRepository<LinkInfo, Long>{ |
||||
|
||||
@Modifying |
||||
@Transactional |
||||
@Query(value = "TRUNCATE TABLE link_informations", nativeQuery = true) |
||||
void truncateMyLink(); |
||||
} |
||||
|
@ -0,0 +1,171 @@ |
||||
package com.secondtest.secondtest.service; |
||||
|
||||
import com.secondtest.secondtest.model.InfoColumn; |
||||
import com.secondtest.secondtest.model.InfoTable; |
||||
import com.secondtest.secondtest.model.LinkInfo; |
||||
import lombok.Data; |
||||
import org.json.JSONArray; |
||||
import org.json.JSONObject; |
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
||||
import java.io.IOException; |
||||
import java.nio.file.Files; |
||||
import java.nio.file.Paths; |
||||
|
||||
@Data |
||||
@Service |
||||
public class ResetDataService { |
||||
|
||||
@Autowired |
||||
private InfoTableService infoTableService; |
||||
@Autowired |
||||
private InfoColumnService infoColumnService; |
||||
@Autowired |
||||
private LinkInfoService linkInfoService; |
||||
|
||||
public void deleteEverything(){ |
||||
infoColumnService.truncateMyColumn(); |
||||
infoTableService.truncateMyTable(); |
||||
linkInfoService.truncateMyLink(); |
||||
} |
||||
|
||||
public void insertEverything() throws IOException { |
||||
String result = new String(Files.readAllBytes(Paths.get("src/main/resources/testTable.json"))); |
||||
JSONArray jo = new JSONArray(result); |
||||
System.out.println("ca marche"); |
||||
for (int i=0; i<jo.length(); i++){ |
||||
JSONObject schema = jo.getJSONObject(i); |
||||
String schemaName = schema.getString("schema"); |
||||
JSONArray data = schema.getJSONArray("data"); |
||||
|
||||
for (int ia = 0; ia<data.length(); ia++){ |
||||
JSONObject apra = data.getJSONObject(ia); |
||||
String tableText, tableName; |
||||
if(apra.has("table_text")){ |
||||
tableText = apra.getString("table_text"); |
||||
} else { |
||||
tableText = ""; |
||||
} |
||||
if(apra.has("table")){ |
||||
tableName = apra.getString("table"); |
||||
} else { |
||||
tableName = ""; |
||||
} |
||||
|
||||
JSONArray col = apra.getJSONArray("columns"); |
||||
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, dataType, columnText; |
||||
int lengthCol; |
||||
if(colData.has("name")){ |
||||
nameCol = colData.getString("name"); |
||||
} else { |
||||
nameCol = ""; |
||||
} |
||||
if (colData.has("data_type")){ |
||||
dataType = colData.getString("data_type"); |
||||
} else { |
||||
dataType = ""; |
||||
} |
||||
if (colData.has("length")){ |
||||
lengthCol = colData.getInt("length"); |
||||
} else { |
||||
lengthCol = 0; |
||||
} |
||||
if(colData.has("column_text")) { |
||||
columnText = colData.getString("column_text"); |
||||
} 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); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
public void checkAndInsertLinks() throws IOException { |
||||
String result = new String(Files.readAllBytes(Paths.get("src/main/resources/testTable.json"))); |
||||
JSONArray jo = new JSONArray(result); |
||||
|
||||
for (int i=0; i<jo.length(); i++){ |
||||
JSONObject schema = jo.getJSONObject(i); |
||||
String schemaName = schema.getString("schema"); |
||||
JSONArray data = schema.getJSONArray("data"); |
||||
|
||||
for (int ia = 0; ia<data.length(); ia++){ |
||||
JSONObject apra = data.getJSONObject(ia); |
||||
String tableText, tableName; |
||||
if(apra.has("table_text")){ |
||||
tableText = apra.getString("table_text"); |
||||
} else { |
||||
tableText = ""; |
||||
} |
||||
if(apra.has("table")){ |
||||
tableName = apra.getString("table"); |
||||
} else { |
||||
tableName = ""; |
||||
} |
||||
JSONArray col = apra.getJSONArray("columns"); |
||||
|
||||
for (int ib =0; ib<col.length(); ib++){ |
||||
JSONObject colData = col.getJSONObject(ib); |
||||
String nameCol, dataType, columnText; |
||||
int lengthCol; |
||||
if(colData.has("name")){ |
||||
nameCol = colData.getString("name"); |
||||
} else { |
||||
nameCol = ""; |
||||
} |
||||
if (colData.has("data_type")){ |
||||
dataType = colData.getString("data_type"); |
||||
} else { |
||||
dataType = ""; |
||||
} |
||||
if (colData.has("length")){ |
||||
lengthCol = colData.getInt("length"); |
||||
} else { |
||||
lengthCol = 0; |
||||
} |
||||
if(colData.has("column_text")) { |
||||
columnText = colData.getString("column_text"); |
||||
} else { |
||||
columnText = ""; |
||||
} |
||||
|
||||
InfoColumn findCol = infoColumnService.getSpecCol(nameCol, dataType, lengthCol, columnText); |
||||
Long idCol = findCol.getId(); |
||||
int intId = Math.toIntExact(idCol); |
||||
|
||||
if (findCol != null){ |
||||
LinkInfo linkInfo = new LinkInfo(); |
||||
linkInfo.setNameSchema(schemaName); |
||||
linkInfo.setNameTable(tableName); |
||||
linkInfo.setColumnId(intId); |
||||
linkInfoService.addOrUpdateLink(linkInfo); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue