Optimization Back

version_2
floxx2112 1 year ago
parent 8512a97924
commit c0100c5d68
  1. 4
      src/main/java/com/apside/assistDbBackend/controller/InfoColumnController.java
  2. 2
      src/main/java/com/apside/assistDbBackend/controller/ScriptController.java
  3. 7
      src/main/java/com/apside/assistDbBackend/controller/TagsController.java
  4. 1
      src/main/java/com/apside/assistDbBackend/model/Script.java
  5. 2
      src/main/java/com/apside/assistDbBackend/model/Tag.java
  6. 31
      src/main/java/com/apside/assistDbBackend/service/GitService.java
  7. 3
      src/main/java/com/apside/assistDbBackend/service/InfoColumnService.java
  8. 3
      src/main/java/com/apside/assistDbBackend/service/InfoTableService.java
  9. 3
      src/main/java/com/apside/assistDbBackend/service/LinkInfoService.java
  10. 69
      src/main/java/com/apside/assistDbBackend/service/ResetDataService.java
  11. 79
      src/main/java/com/apside/assistDbBackend/service/ScriptsService.java
  12. 38
      src/main/java/com/apside/assistDbBackend/service/TagsService.java

@ -56,8 +56,8 @@ public class InfoColumnController {
@GetMapping("/columns/joins")
public Iterable<InfoColumn> getColumnsForJoinTwo(@RequestParam("tables") String tables, @RequestParam("schemas") String schemas) {
List<String> tablesList = new ArrayList<String>(Arrays.asList(tables.split(",")));
List<String> schemasList = new ArrayList<String>(Arrays.asList(schemas.split(",")));
List<String> tablesList = new ArrayList<>(Arrays.asList(tables.split(",")));
List<String> schemasList = new ArrayList<>(Arrays.asList(schemas.split(",")));
return infoColumnService.getColumnsForJoinTwo(tablesList, schemasList);
}

@ -23,7 +23,7 @@ public class ScriptController {
}
@GetMapping("/scripts/link")
public List<LinkScriptTag> getAllLinkScriptsTags() throws IOException, GitAPIException {
public List<LinkScriptTag> getAllLinkScriptsTags() throws IOException {
return scriptsService.getAllScriptTag();
}

@ -1,6 +1,5 @@
package com.apside.assistDbBackend.controller;
import com.apside.assistDbBackend.model.InfoColumn;
import com.apside.assistDbBackend.model.Tag;
import com.apside.assistDbBackend.service.TagsService;
import org.eclipse.jgit.api.errors.GitAPIException;
@ -10,7 +9,6 @@ import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api")
@ -24,11 +22,6 @@ public class TagsController {
return tagsService.getAllTags();
}
/*@DeleteMapping("/tags/deleteAll")
public void deleteAllTags() {
tagsService.deleteAllTags();
}*/
@DeleteMapping("/tag/delete/{nameTag}")
public void deleteTag(@PathVariable("nameTag") final String nameTag) throws IOException, GitAPIException, URISyntaxException {
tagsService.deleteTag(nameTag);

@ -1,6 +1,5 @@
package com.apside.assistDbBackend.model;
import jakarta.persistence.Entity;
import lombok.Data;
@Data

@ -1,7 +1,5 @@
package com.apside.assistDbBackend.model;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Data;
@Data

@ -29,22 +29,25 @@ public class GitService {
private String accesToken;
public void pushToGit() throws IOException, GitAPIException, URISyntaxException {
Git git = Git.open(new File(tempDirectoryPath));
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName("origin");
remoteAddCommand.setUri(new URIish("https://gitea.ci.apside-top.fr/Prudence_Creole/AssistDB_AdditionalFiles.git"));
// you can add more settings here if needed
remoteAddCommand.call();
git.add().setUpdate(true).addFilepattern(".").call();
git.add().addFilepattern(".").call();
git.commit().setMessage("commited").call();
git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).call();
try (Git git = Git.open(new File(tempDirectoryPath))){
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName("origin");
remoteAddCommand.setUri(new URIish("https://gitea.ci.apside-top.fr/Prudence_Creole/AssistDB_AdditionalFiles.git"));
// you can add more settings here if needed
remoteAddCommand.call();
git.add().setUpdate(true).addFilepattern(".").call();
git.add().addFilepattern(".").call();
git.commit().setMessage("commited").call();
git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).call();
}
}
public void pullFromGit() throws IOException, GitAPIException {
Git git = Git.open(new File(tempDirectoryPath));
git.pull().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).setRemote("origin").setRemoteBranchName("main").call();
try (Git git = Git.open(new File(tempDirectoryPath))){
git.pull().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).setRemote("origin").setRemoteBranchName("main").call();
}
}
}

@ -44,8 +44,7 @@ public class InfoColumnService {
}
public InfoColumn addOrUpdateColumn(InfoColumn infoColumn) {
InfoColumn savedInfoColumn = infoColumnRepository.save(infoColumn);
return savedInfoColumn;
return infoColumnRepository.save(infoColumn);
}
public InfoColumn getSpecCol(String nameCol, String dataType, int lengthCol, String columnText){

@ -40,8 +40,7 @@ public class InfoTableService {
}
public InfoTable addOrUpdateTable(InfoTable infoTable) {
InfoTable savedInfoTable = infoTableRepository.save(infoTable);
return savedInfoTable;
return infoTableRepository.save(infoTable);
}
public void truncateMyTable(){

@ -31,8 +31,7 @@ public class LinkInfoService {
}
public LinkInfo addOrUpdateLink(LinkInfo linkInfo) {
LinkInfo savedLinkInfo = linkInfoRepository.save(linkInfo);
return savedLinkInfo;
return linkInfoRepository.save(linkInfo);
}

@ -24,6 +24,12 @@ public class ResetDataService {
@Autowired
private LinkInfoService linkInfoService;
private static final String tableStr = "table";
private static final String nameStr = "name";
private static final String dataTypeStr = "data_type";
private static final String lengthStr = "length";
private static final String columnTextStr = "column_text";
public void deleteEverything(){
infoColumnService.truncateMyColumn();
infoTableService.truncateMyTable();
@ -31,10 +37,8 @@ public class ResetDataService {
}
public void insertEverything() throws IOException {
int hopli = 0;
String result = new String(Files.readAllBytes(Paths.get("src/main/resources/assistDbData.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");
@ -42,14 +46,15 @@ public class ResetDataService {
for (int ia = 0; ia<data.length(); ia++){
JSONObject apra = data.getJSONObject(ia);
String tableText, tableName;
String tableText;
String tableName;
if(apra.has("table_text")){
tableText = apra.getString("table_text");
} else {
tableText = "";
}
if(apra.has("table")){
tableName = apra.getString("table");
if(apra.has(tableStr)){
tableName = apra.getString(tableStr);
} else {
tableName = "";
}
@ -63,25 +68,27 @@ public class ResetDataService {
for (int ib =0; ib<col.length(); ib++){
JSONObject colData = col.getJSONObject(ib);
String nameCol, dataType, columnText;
String nameCol;
String dataType;
String columnText;
int lengthCol;
if(colData.has("name")){
nameCol = colData.getString("name");
if(colData.has(nameStr)){
nameCol = colData.getString(nameStr);
} else {
nameCol = "";
}
if (colData.has("data_type")){
dataType = colData.getString("data_type");
if (colData.has(dataTypeStr)){
dataType = colData.getString(dataTypeStr);
} else {
dataType = "";
}
if (colData.has("length")){
lengthCol = colData.getInt("length");
if (colData.has(lengthStr)){
lengthCol = colData.getInt(lengthStr);
} else {
lengthCol = 0;
}
if(colData.has("column_text")) {
columnText = colData.getString("column_text");
if(colData.has(columnTextStr)) {
columnText = colData.getString(columnTextStr);
} else {
columnText = "";
}
@ -97,12 +104,9 @@ public class ResetDataService {
if(findCol == null){
infoColumnService.addOrUpdateColumn(infoColumn);
}
hopli++;
}
}
}
System.out.println(hopli);
}
public void checkAndInsertLinks() throws IOException {
@ -116,14 +120,9 @@ public class ResetDataService {
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");
String tableName;
if(apra.has(tableStr)){
tableName = apra.getString(tableStr);
} else {
tableName = "";
}
@ -131,25 +130,27 @@ public class ResetDataService {
for (int ib =0; ib<col.length(); ib++){
JSONObject colData = col.getJSONObject(ib);
String nameCol, dataType, columnText;
String nameCol;
String dataType;
String columnText;
int lengthCol;
if(colData.has("name")){
nameCol = colData.getString("name");
if(colData.has(nameStr)){
nameCol = colData.getString(nameStr);
} else {
nameCol = "";
}
if (colData.has("data_type")){
dataType = colData.getString("data_type");
if (colData.has(dataTypeStr)){
dataType = colData.getString(dataTypeStr);
} else {
dataType = "";
}
if (colData.has("length")){
lengthCol = colData.getInt("length");
if (colData.has(lengthStr)){
lengthCol = colData.getInt(lengthStr);
} else {
lengthCol = 0;
}
if(colData.has("column_text")) {
columnText = colData.getString("column_text");
if(colData.has(columnTextStr)) {
columnText = colData.getString(columnTextStr);
} else {
columnText = "";
}

@ -3,19 +3,10 @@ package com.apside.assistDbBackend.service;
import com.apside.assistDbBackend.model.LinkScriptTag;
import com.apside.assistDbBackend.model.Script;
import lombok.Data;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.RemoteAddCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import java.io.File;
@ -38,32 +29,40 @@ public class ScriptsService {
private JSONObject dataGlobalScripts;
private JSONArray dataLinkScriptsTags;
private static final String pathToScriptDir = "/AssistDB_AdditionalFiles/Scripts";
private static final String userDirString = "user.dir";
private static final String filenameString = "filename";
private static final String descriptionString = "description";
private static final String tagNameString = "tagname";
private static final String idString = "id";
private static final String tagsString = "tags";
private static final String dataString = "data";
public ScriptsService() throws IOException {
linkScriptTagPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/scripts.json";
linkScriptTagPath = new File(System.getProperty(userDirString)).getParent() + "/AssistDB_AdditionalFiles/scripts.json";
pathOfLink = Paths.get(linkScriptTagPath);
}
private void initialize() throws IOException {
allScriptsContent = new String((Files.readAllBytes(pathOfLink)));
dataGlobalScripts = new JSONObject(allScriptsContent);
dataLinkScriptsTags = dataGlobalScripts.getJSONArray("data");
dataLinkScriptsTags = dataGlobalScripts.getJSONArray(dataString);
}
public List<Script> retrieveScripts() throws IOException, GitAPIException {
gitService.pullFromGit();
List<Script> listOfScripts = new ArrayList<>();
//Creating a File object for directory
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts";
String tempDirectoryPath = new File(System.getProperty(userDirString)).getParent() + pathToScriptDir;
//List of all files and directories
File scriptDirectory = new File(tempDirectoryPath);
String contents[] = scriptDirectory.list();
String[] contents = scriptDirectory.list();
for(int i=0; i<contents.length; i++) {
Path filePath = Path.of(tempDirectoryPath + "/" + contents[i]);
String allData = Files.readString(filePath);
String extension = contents[i].substring(contents[i].lastIndexOf(".") + 1);
Script tempScript = new Script(extension, contents[i], allData);
listOfScripts.add(tempScript);
//System.out.println(contents[i] + " Extension : " + extension);
}
return listOfScripts;
}
@ -74,14 +73,14 @@ public class ScriptsService {
if (dataLinkScriptsTags.length()>0){
for (int i = 0; i<dataLinkScriptsTags.length(); i++){
JSONObject link = dataLinkScriptsTags.getJSONObject(i);
int linkId = link.getInt("id");
String scriptName = link.getString("filename");
String desc = link.getString("description");
int linkId = link.getInt(idString);
String scriptName = link.getString(filenameString);
String desc = link.getString(descriptionString);
List<String> tagList = new ArrayList<>();
JSONArray tags = link.getJSONArray("tags");
JSONArray tags = link.getJSONArray(tagsString);
for (int j=0; j<tags.length(); j++){
JSONObject unitTag = tags.getJSONObject(j);
String tagName = unitTag.getString("tagname");
String tagName = unitTag.getString(tagNameString);
tagList.add(tagName);
}
LinkScriptTag newLinkScriptTag = new LinkScriptTag(linkId, scriptName, desc, tagList);
@ -93,7 +92,7 @@ public class ScriptsService {
}
public void deleteOneScript(final String name){
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts";
String tempDirectoryPath = new File(System.getProperty(userDirString)).getParent() + pathToScriptDir;
try {
File scriptDirectory = new File(tempDirectoryPath + "/" + name);
scriptDirectory.delete();
@ -105,22 +104,24 @@ public class ScriptsService {
}
public void simpleDeleteScript(final String name){
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts";
String tempDirectoryPath = new File(System.getProperty(userDirString)).getParent() + pathToScriptDir;
try {
File scriptDirectory = new File(tempDirectoryPath + "/" + name);
scriptDirectory.delete();
} catch (Exception e){
e.printStackTrace();
}
}
public void addOneScript(final String content, final String name) throws IOException, GitAPIException, URISyntaxException {
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts";
public void addOneScript(final String content, final String name) throws IOException {
String tempDirectoryPath = new File(System.getProperty(userDirString)).getParent() + pathToScriptDir;
File newFile = new File(tempDirectoryPath + "/" + name);
newFile.createNewFile();
FileWriter writerDataFile = new FileWriter(tempDirectoryPath + "/" + name);
writerDataFile.write(content);
writerDataFile.close();
try (FileWriter writerDataFile = new FileWriter(tempDirectoryPath + "/" + name);) {
writerDataFile.write(content);
}
}
@ -129,18 +130,18 @@ public class ScriptsService {
initialize();
gitService.pullFromGit();
JSONObject newLink = new JSONObject();
newLink.put("id", linkId);
newLink.put("filename", name);
newLink.put("description", description);
newLink.put(idString, linkId);
newLink.put(filenameString, name);
newLink.put(descriptionString, description);
JSONArray tagArray = new JSONArray();
for(int i=0; i<tagList.size(); i++){
JSONObject tempTag = new JSONObject();
tempTag.put("tagname", tagList.get(i).toString());
tempTag.put(tagNameString, tagList.get(i));
tagArray.put(tempTag);
}
newLink.put("tags", tagArray);
newLink.put(tagsString, tagArray);
JSONArray newArr = dataLinkScriptsTags.put(newLink);
JSONObject newObj = dataGlobalScripts.put("data", newArr);
JSONObject newObj = dataGlobalScripts.put(dataString, newArr);
Files.write(pathOfLink, newObj.toString().getBytes());
gitService.pushToGit();
}
@ -151,25 +152,25 @@ public class ScriptsService {
JSONArray newArr = new JSONArray();
for (int d = 0; d<dataLinkScriptsTags.length(); d++){
JSONObject actualLink = dataLinkScriptsTags.getJSONObject(d);
int actualId = actualLink.getInt("id");
int actualId = actualLink.getInt(idString);
if (!Objects.equals(linkId, actualId)){
newArr.put(actualLink);
} else {
JSONObject newLink = new JSONObject();
newLink.put("id", linkId);
newLink.put("filename", name);
newLink.put("description", description);
newLink.put(idString, linkId);
newLink.put(filenameString, name);
newLink.put(descriptionString, description);
JSONArray tagArray = new JSONArray();
for(int i=0; i<tagList.size(); i++){
JSONObject tempTag = new JSONObject();
tempTag.put("tagname", tagList.get(i).toString());
tempTag.put(tagNameString, tagList.get(i));
tagArray.put(tempTag);
}
newLink.put("tags", tagArray);
newLink.put(tagsString, tagArray);
newArr.put(newLink);
}
}
JSONObject newObj = dataGlobalScripts.put("data", newArr);
JSONObject newObj = dataGlobalScripts.put(dataString, newArr);
Files.write(pathOfLink, newObj.toString().getBytes());
gitService.pushToGit();

@ -7,9 +7,7 @@ import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
@ -18,7 +16,6 @@ import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@Data
@Service
@ -30,6 +27,10 @@ public class TagsService {
private String jsonContent;
private JSONObject dataGlobal;
private JSONArray data;
private static final String tagIdStr = "tagId";
private static final String dataStr = "data";
private static final String tagStr = "tag";
private static final String descriptionStr = "description";
public TagsService() throws IOException {
tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/tags.json";
path = Paths.get(tempDirectoryPath);
@ -38,7 +39,7 @@ public class TagsService {
private void initialize() throws IOException {
jsonContent = new String(Files.readAllBytes(path));
dataGlobal = new JSONObject(jsonContent);
data = dataGlobal.getJSONArray("data");
data = dataGlobal.getJSONArray(dataStr);
}
public List<Tag> getAllTags() throws IOException, GitAPIException {
@ -47,9 +48,9 @@ public class TagsService {
List<Tag> listOfTag = new ArrayList<>();
for (int i=0; i<data.length(); i++){
JSONObject tag = data.getJSONObject(i);
int tagId = tag.getInt("tagId");
String tagName = tag.getString("tag");
String tagDescription = tag.getString("description");
int tagId = tag.getInt(tagIdStr);
String tagName = tag.getString(tagStr);
String tagDescription = tag.getString(descriptionStr);
Tag tempTag = new Tag(tagId, tagName, tagDescription);
listOfTag.add(tempTag);
}
@ -61,13 +62,12 @@ public class TagsService {
JSONArray newArr = new JSONArray();
for (int i=0; i<data.length(); i++) {
JSONObject tag = data.getJSONObject(i);
String tagName = tag.getString("tag");
String tagName = tag.getString(tagStr);
if(!Objects.equals(tagName, deleteNameTag)){
newArr.put(tag);
}
}
System.out.println(newArr);
JSONObject newObj = dataGlobal.put("data", newArr);
JSONObject newObj = dataGlobal.put(dataStr, newArr);
Files.write(path, newObj.toString().getBytes());
gitService.pushToGit();
gitService.pullFromGit();
@ -77,11 +77,11 @@ public class TagsService {
initialize();
gitService.pullFromGit();
JSONObject newTag = new JSONObject();
newTag.put("tagId", tag.getTagId());
newTag.put("tag", tag.getNameTag().toString());
newTag.put("description", tag.getDescriptionTag().toString());
newTag.put(tagIdStr, tag.getTagId());
newTag.put(tagStr, tag.getNameTag());
newTag.put(descriptionStr, tag.getDescriptionTag());
JSONArray newArr = data.put(newTag);
JSONObject newObj = dataGlobal.put("data", newArr);
JSONObject newObj = dataGlobal.put(dataStr, newArr);
Files.write(path, newObj.toString().getBytes());
gitService.pushToGit();
gitService.pullFromGit();
@ -92,18 +92,18 @@ public class TagsService {
JSONArray newArr = new JSONArray();
for (int i=0; i<data.length(); i++) {
JSONObject tag = data.getJSONObject(i);
String tagName = tag.getString("tag");
String tagName = tag.getString(tagStr);
if(!Objects.equals(tagName, prevTag)){
newArr.put(tag);
} else {
JSONObject newTag = new JSONObject();
newTag.put("tagId", modTag.getTagId());
newTag.put("tag", modTag.getNameTag().toString());
newTag.put("description", modTag.getDescriptionTag().toString());
newTag.put(tagIdStr, modTag.getTagId());
newTag.put(tagStr, modTag.getNameTag());
newTag.put(descriptionStr, modTag.getDescriptionTag());
newArr.put(newTag);
}
}
JSONObject newObj = dataGlobal.put("data", newArr);
JSONObject newObj = dataGlobal.put(dataStr, newArr);
Files.write(path, newObj.toString().getBytes());
gitService.pushToGit();
gitService.pullFromGit();

Loading…
Cancel
Save