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. 7
      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. 77
      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") @GetMapping("/columns/joins")
public Iterable<InfoColumn> getColumnsForJoinTwo(@RequestParam("tables") String tables, @RequestParam("schemas") String schemas) { public Iterable<InfoColumn> getColumnsForJoinTwo(@RequestParam("tables") String tables, @RequestParam("schemas") String schemas) {
List<String> tablesList = new ArrayList<String>(Arrays.asList(tables.split(","))); List<String> tablesList = new ArrayList<>(Arrays.asList(tables.split(",")));
List<String> schemasList = new ArrayList<String>(Arrays.asList(schemas.split(","))); List<String> schemasList = new ArrayList<>(Arrays.asList(schemas.split(",")));
return infoColumnService.getColumnsForJoinTwo(tablesList, schemasList); return infoColumnService.getColumnsForJoinTwo(tablesList, schemasList);
} }

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

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

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

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

@ -29,7 +29,7 @@ public class GitService {
private String accesToken; private String accesToken;
public void pushToGit() throws IOException, GitAPIException, URISyntaxException { public void pushToGit() throws IOException, GitAPIException, URISyntaxException {
Git git = Git.open(new File(tempDirectoryPath)); try (Git git = Git.open(new File(tempDirectoryPath))){
RemoteAddCommand remoteAddCommand = git.remoteAdd(); RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName("origin"); remoteAddCommand.setName("origin");
remoteAddCommand.setUri(new URIish("https://gitea.ci.apside-top.fr/Prudence_Creole/AssistDB_AdditionalFiles.git")); remoteAddCommand.setUri(new URIish("https://gitea.ci.apside-top.fr/Prudence_Creole/AssistDB_AdditionalFiles.git"));
@ -42,9 +42,12 @@ public class GitService {
git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).call(); git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).call();
} }
}
public void pullFromGit() throws IOException, GitAPIException { public void pullFromGit() throws IOException, GitAPIException {
Git git = Git.open(new File(tempDirectoryPath)); try (Git git = Git.open(new File(tempDirectoryPath))){
git.pull().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).setRemote("origin").setRemoteBranchName("main").call(); git.pull().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).setRemote("origin").setRemoteBranchName("main").call();
} }
}
} }

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

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

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

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

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

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

Loading…
Cancel
Save