|
|
@ -1,5 +1,6 @@ |
|
|
|
package com.apside.assistDbBackend.service; |
|
|
|
package com.apside.assistDbBackend.service; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.Git; |
|
|
@ -9,6 +10,8 @@ import org.eclipse.jgit.api.errors.TransportException; |
|
|
|
import org.eclipse.jgit.transport.PushResult; |
|
|
|
import org.eclipse.jgit.transport.PushResult; |
|
|
|
import org.eclipse.jgit.transport.URIish; |
|
|
|
import org.eclipse.jgit.transport.URIish; |
|
|
|
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; |
|
|
|
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.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties; |
|
|
|
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties; |
|
|
@ -21,15 +24,31 @@ import java.io.IOException; |
|
|
|
import java.net.URISyntaxException; |
|
|
|
import java.net.URISyntaxException; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.nio.file.Paths; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.*; |
|
|
|
import java.util.Iterator; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Data |
|
|
|
@Data |
|
|
|
@Service |
|
|
|
@Service |
|
|
|
public class ScriptsService { |
|
|
|
public class ScriptsService { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final String linkScriptTagPath; |
|
|
|
|
|
|
|
private final Path pathOfLink; |
|
|
|
|
|
|
|
private String allScriptsContent; |
|
|
|
|
|
|
|
private JSONObject dataGlobalScripts; |
|
|
|
|
|
|
|
private JSONArray dataLinkScriptsTags; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ScriptsService() throws IOException { |
|
|
|
|
|
|
|
linkScriptTagPath = new File(System.getProperty("user.dir")).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"); |
|
|
|
|
|
|
|
System.out.println(dataLinkScriptsTags); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public List<Script> retrieveScripts() throws IOException, GitAPIException { |
|
|
|
public List<Script> retrieveScripts() throws IOException, GitAPIException { |
|
|
|
pullFromGit(); |
|
|
|
pullFromGit(); |
|
|
|
List<Script> listOfScripts = new ArrayList<>(); |
|
|
|
List<Script> listOfScripts = new ArrayList<>(); |
|
|
@ -49,6 +68,29 @@ public class ScriptsService { |
|
|
|
return listOfScripts; |
|
|
|
return listOfScripts; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<LinkScriptTag> getAllScriptTag() throws IOException { |
|
|
|
|
|
|
|
initialize(); |
|
|
|
|
|
|
|
List<LinkScriptTag> listLinkScriptTag = new ArrayList<>(); |
|
|
|
|
|
|
|
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"); |
|
|
|
|
|
|
|
List<String> tagList = new ArrayList<>(); |
|
|
|
|
|
|
|
JSONArray tags = link.getJSONArray("tags"); |
|
|
|
|
|
|
|
for (int j=0; j<tags.length(); j++){ |
|
|
|
|
|
|
|
JSONObject unitTag = tags.getJSONObject(j); |
|
|
|
|
|
|
|
String tagName = unitTag.getString("tagname"); |
|
|
|
|
|
|
|
tagList.add(tagName); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
LinkScriptTag newLinkScriptTag = new LinkScriptTag(linkId, scriptName, tagList); |
|
|
|
|
|
|
|
listLinkScriptTag.add(newLinkScriptTag); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return listLinkScriptTag; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts"; |
|
|
|
try { |
|
|
|
try { |
|
|
@ -79,9 +121,60 @@ public class ScriptsService { |
|
|
|
writerDataFile.write(content); |
|
|
|
writerDataFile.write(content); |
|
|
|
writerDataFile.close(); |
|
|
|
writerDataFile.close(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void addOneLink(final String name, final List<String> tagList, final int linkId) throws IOException, GitAPIException, URISyntaxException { |
|
|
|
|
|
|
|
initialize(); |
|
|
|
|
|
|
|
JSONObject newLink = new JSONObject(); |
|
|
|
|
|
|
|
newLink.put("id", linkId); |
|
|
|
|
|
|
|
newLink.put("filename", name); |
|
|
|
|
|
|
|
JSONArray tagArray = new JSONArray(); |
|
|
|
|
|
|
|
for(int i=0; i<tagList.size(); i++){ |
|
|
|
|
|
|
|
JSONObject tempTag = new JSONObject(); |
|
|
|
|
|
|
|
tempTag.put("tagname", tagList.get(i).toString()); |
|
|
|
|
|
|
|
tagArray.put(tempTag); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
newLink.put("tags", tagArray); |
|
|
|
|
|
|
|
JSONArray newArr = dataLinkScriptsTags.put(newLink); |
|
|
|
|
|
|
|
JSONObject newObj = dataGlobalScripts.put("data", newArr); |
|
|
|
|
|
|
|
Files.write(pathOfLink, newObj.toString().getBytes()); |
|
|
|
pushToGit(); |
|
|
|
pushToGit(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void updateOneLink(final String name, final List<String> tagList, final int linkId) throws IOException, GitAPIException, URISyntaxException { |
|
|
|
|
|
|
|
initialize(); |
|
|
|
|
|
|
|
JSONArray newArr = new JSONArray(); |
|
|
|
|
|
|
|
for (int d = 0; d<dataLinkScriptsTags.length(); d++){ |
|
|
|
|
|
|
|
JSONObject actualLink = dataLinkScriptsTags.getJSONObject(d); |
|
|
|
|
|
|
|
int actualId = actualLink.getInt("id"); |
|
|
|
|
|
|
|
if (!Objects.equals(linkId, actualId)){ |
|
|
|
|
|
|
|
newArr.put(actualLink); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
JSONObject newLink = new JSONObject(); |
|
|
|
|
|
|
|
newLink.put("id", linkId); |
|
|
|
|
|
|
|
newLink.put("filename", name); |
|
|
|
|
|
|
|
JSONArray tagArray = new JSONArray(); |
|
|
|
|
|
|
|
for(int i=0; i<tagList.size(); i++){ |
|
|
|
|
|
|
|
JSONObject tempTag = new JSONObject(); |
|
|
|
|
|
|
|
tempTag.put("tagname", tagList.get(i).toString()); |
|
|
|
|
|
|
|
tagArray.put(tempTag); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
newLink.put("tags", tagArray); |
|
|
|
|
|
|
|
newArr.put(newLink); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
JSONObject newObj = dataGlobalScripts.put("data", newArr); |
|
|
|
|
|
|
|
Files.write(pathOfLink, newObj.toString().getBytes()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pushToGit(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${USERNAME_GIT}") |
|
|
|
@Value("${USERNAME_GIT}") |
|
|
|