|
|
|
@ -4,14 +4,14 @@ import com.apside.assist.db.backend.model.LinkScriptTag; |
|
|
|
|
import com.apside.assist.db.backend.model.Script; |
|
|
|
|
import com.apside.assist.db.backend.service.GitService; |
|
|
|
|
import com.apside.assist.db.backend.service.ScriptsService; |
|
|
|
|
import lombok.Data; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.eclipse.jgit.api.errors.GitAPIException; |
|
|
|
|
import org.json.JSONArray; |
|
|
|
|
import org.json.JSONObject; |
|
|
|
|
import org.springframework.beans.factory.InitializingBean; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
import org.springframework.core.io.PathResource; |
|
|
|
|
import org.springframework.core.io.Resource; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
@ -21,27 +21,28 @@ import java.net.URISyntaxException; |
|
|
|
|
import java.nio.file.Files; |
|
|
|
|
import java.nio.file.Path; |
|
|
|
|
import java.nio.file.Paths; |
|
|
|
|
import java.util.*; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
|
|
@Service |
|
|
|
|
@Slf4j |
|
|
|
|
public class ScriptsServiceImpl implements ScriptsService, InitializingBean { |
|
|
|
|
@Autowired |
|
|
|
|
private GitService gitService; |
|
|
|
|
private String linkScriptTagPath; |
|
|
|
|
|
|
|
|
|
private final GitService gitService; |
|
|
|
|
private Path pathOfLink; |
|
|
|
|
private String allScriptsContent; |
|
|
|
|
|
|
|
|
|
private JSONObject dataGlobalScripts; |
|
|
|
|
private JSONArray dataLinkScriptsTags; |
|
|
|
|
private String tempDirectoryPath; |
|
|
|
|
private File scriptDirectory; |
|
|
|
|
|
|
|
|
|
@Value("${PATH_FOR_SCRIPT_JSON}") |
|
|
|
|
private String PATH_FOR_SCRIPT_JSON; |
|
|
|
|
@Value("${assistDb.db.json.script}") |
|
|
|
|
private String fileName; |
|
|
|
|
|
|
|
|
|
@Value("${assistDb.db.json.directory}") |
|
|
|
|
private String directory; |
|
|
|
|
|
|
|
|
|
@Value("${PATH_TO_SCRIPT_DIR}") |
|
|
|
|
private String PATH_TO_SCRIPT_DIR; |
|
|
|
|
private static final String USER_DIR_STRING = "user.dir"; |
|
|
|
|
private static final String FILE_NAME_STRING = "filename"; |
|
|
|
|
private static final String DESCRIPTION_STRING = "description"; |
|
|
|
|
private static final String TAG_NAME_STRING = "tagname"; |
|
|
|
@ -49,17 +50,22 @@ public class ScriptsServiceImpl implements ScriptsService, InitializingBean { |
|
|
|
|
private static final String TAGS_STRING = "tags"; |
|
|
|
|
private static final String DATA_STRING = "data"; |
|
|
|
|
|
|
|
|
|
public ScriptsServiceImpl(GitService gitService) { |
|
|
|
|
this.gitService = gitService; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void afterPropertiesSet() throws Exception { |
|
|
|
|
linkScriptTagPath = new File(System.getProperty(USER_DIR_STRING)).getParent() + PATH_FOR_SCRIPT_JSON; |
|
|
|
|
pathOfLink = Paths.get(linkScriptTagPath); |
|
|
|
|
tempDirectoryPath = new File(System.getProperty(USER_DIR_STRING)).getParent() + PATH_TO_SCRIPT_DIR; |
|
|
|
|
scriptDirectory = new File(tempDirectoryPath); |
|
|
|
|
Resource resource = new PathResource("git/" + fileName); |
|
|
|
|
pathOfLink = Paths.get(resource.getURI()); |
|
|
|
|
resource = new PathResource("git/" + directory); |
|
|
|
|
tempDirectoryPath = resource.getURI().getPath(); |
|
|
|
|
scriptDirectory = resource.getFile(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void initialize() throws IOException { |
|
|
|
|
allScriptsContent = new String((Files.readAllBytes(pathOfLink))); |
|
|
|
|
String allScriptsContent = new String((Files.readAllBytes(pathOfLink))); |
|
|
|
|
dataGlobalScripts = new JSONObject(allScriptsContent); |
|
|
|
|
dataLinkScriptsTags = dataGlobalScripts.getJSONArray(DATA_STRING); |
|
|
|
|
} |
|
|
|
@ -71,14 +77,16 @@ public class ScriptsServiceImpl implements ScriptsService, InitializingBean { |
|
|
|
|
List<Script> listOfScripts = new ArrayList<>(); |
|
|
|
|
//Creating a File object for directory
|
|
|
|
|
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); |
|
|
|
|
if (Objects.nonNull(contents)) { |
|
|
|
|
for (String content : contents) { |
|
|
|
|
Path filePath = Path.of(tempDirectoryPath, content); |
|
|
|
|
String allData = Files.readString(filePath); |
|
|
|
|
String extension = content.substring(content.lastIndexOf(".") + 1); |
|
|
|
|
Script tempScript = new Script(extension, content, allData); |
|
|
|
|
listOfScripts.add(tempScript); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
log.info("Get all Scripts successful"); |
|
|
|
|
log.info("Get all Scripts successful : {}", listOfScripts.size()); |
|
|
|
|
return listOfScripts; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -112,8 +120,8 @@ public class ScriptsServiceImpl implements ScriptsService, InitializingBean { |
|
|
|
|
public void deleteOneScript(final String name){ |
|
|
|
|
log.debug("Start delete one script with Git"); |
|
|
|
|
try { |
|
|
|
|
File scriptDirectoryToDelete = new File(tempDirectoryPath + "/" + name); |
|
|
|
|
scriptDirectoryToDelete.delete(); |
|
|
|
|
File scriptDirectoryToDelete = new File(tempDirectoryPath, name); |
|
|
|
|
Files.delete(scriptDirectoryToDelete.toPath()); |
|
|
|
|
gitService.pushToGit(); |
|
|
|
|
log.info("Delete one Script successful"); |
|
|
|
|
} catch (Exception e){ |
|
|
|
@ -126,8 +134,8 @@ public class ScriptsServiceImpl implements ScriptsService, InitializingBean { |
|
|
|
|
public void simpleDeleteScript(final String name){ |
|
|
|
|
log.debug("Start delete one script without Git"); |
|
|
|
|
try { |
|
|
|
|
File scriptDirectoryToSimpleDelete = new File(tempDirectoryPath + "/" + name); |
|
|
|
|
scriptDirectoryToSimpleDelete.delete(); |
|
|
|
|
File scriptDirectoryToSimpleDelete = new File(tempDirectoryPath, name); |
|
|
|
|
Files.delete(scriptDirectoryToSimpleDelete.toPath()); |
|
|
|
|
log.info("Delete one Script successful"); |
|
|
|
|
} catch (Exception e){ |
|
|
|
|
e.printStackTrace(); |
|
|
|
@ -138,8 +146,6 @@ public class ScriptsServiceImpl implements ScriptsService, InitializingBean { |
|
|
|
|
@Override |
|
|
|
|
public void addOneScript(final String content, final String name) throws IOException { |
|
|
|
|
log.debug("Start add one scripts"); |
|
|
|
|
File newFile = new File(tempDirectoryPath + "/" + name); |
|
|
|
|
newFile.createNewFile(); |
|
|
|
|
try (FileWriter writerDataFile = new FileWriter(tempDirectoryPath + "/" + name);) { |
|
|
|
|
writerDataFile.write(content); |
|
|
|
|
log.info("Add one Script successful"); |
|
|
|
@ -159,9 +165,9 @@ public class ScriptsServiceImpl implements ScriptsService, InitializingBean { |
|
|
|
|
newLink.put(FILE_NAME_STRING, name); |
|
|
|
|
newLink.put(DESCRIPTION_STRING, description); |
|
|
|
|
JSONArray tagArray = new JSONArray(); |
|
|
|
|
for(int i=0; i<tagList.size(); i++){ |
|
|
|
|
for (String s : tagList) { |
|
|
|
|
JSONObject tempTag = new JSONObject(); |
|
|
|
|
tempTag.put(TAG_NAME_STRING, tagList.get(i)); |
|
|
|
|
tempTag.put(TAG_NAME_STRING, s); |
|
|
|
|
tagArray.put(tempTag); |
|
|
|
|
} |
|
|
|
|
newLink.put(TAGS_STRING, tagArray); |
|
|
|
@ -189,9 +195,10 @@ public class ScriptsServiceImpl implements ScriptsService, InitializingBean { |
|
|
|
|
newLink.put(FILE_NAME_STRING, name); |
|
|
|
|
newLink.put(DESCRIPTION_STRING, description); |
|
|
|
|
JSONArray tagArray = new JSONArray(); |
|
|
|
|
for(int i=0; i<tagList.size(); i++){ |
|
|
|
|
|
|
|
|
|
for (String s : tagList) { |
|
|
|
|
JSONObject tempTag = new JSONObject(); |
|
|
|
|
tempTag.put(TAG_NAME_STRING, tagList.get(i)); |
|
|
|
|
tempTag.put(TAG_NAME_STRING, s); |
|
|
|
|
tagArray.put(tempTag); |
|
|
|
|
} |
|
|
|
|
newLink.put(TAGS_STRING, tagArray); |
|
|
|
|