|
|
|
@ -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(); |
|
|
|
|