Correction docker + sonar

version_2
VANNEAU 1 year ago
parent 6ea78fb75d
commit be5475868b
  1. 39
      Dockerfile
  2. 1
      pom.xml
  3. 25
      src/main/java/com/apside/assist/db/backend/AssistDBBackendApplication.java
  4. 23
      src/main/java/com/apside/assist/db/backend/controller/InfoColumnController.java
  5. 21
      src/main/java/com/apside/assist/db/backend/controller/InfoTableController.java
  6. 13
      src/main/java/com/apside/assist/db/backend/controller/LinkInfoController.java
  7. 11
      src/main/java/com/apside/assist/db/backend/controller/ResetDataController.java
  8. 14
      src/main/java/com/apside/assist/db/backend/controller/ScriptController.java
  9. 11
      src/main/java/com/apside/assist/db/backend/controller/TagsController.java
  10. 39
      src/main/java/com/apside/assist/db/backend/service/implementation/GitServiceImpl.java
  11. 13
      src/main/java/com/apside/assist/db/backend/service/implementation/InfoColumnServiceImpl.java
  12. 12
      src/main/java/com/apside/assist/db/backend/service/implementation/InfoTableServiceImpl.java
  13. 11
      src/main/java/com/apside/assist/db/backend/service/implementation/LinkInfoServiceImpl.java
  14. 46
      src/main/java/com/apside/assist/db/backend/service/implementation/ResetDataServiceImpl.java
  15. 75
      src/main/java/com/apside/assist/db/backend/service/implementation/ScriptsServiceImpl.java
  16. 27
      src/main/java/com/apside/assist/db/backend/service/implementation/TagsServiceImpl.java
  17. 18
      src/main/resources/application.properties
  18. 0
      src/main/resources/json/assistDbData.json
  19. 0
      src/main/resources/json/assistDbDataTest.json
  20. 0
      src/main/resources/json/pcgp.json

@ -1,11 +1,28 @@
FROM eclipse-temurin:19-jdk-jammy
WORKDIR /app
COPY .mvn/ .mvn
COPY mvnw pom.xml ./
RUN ./mvnw dependency:resolve
COPY src ./src
CMD ["./mvnw", "spring-boot:run"]
# FROM eclipse-temurin:19-jdk-jammy
#
# WORKDIR /app
#
# COPY .mvn/ .mvn
# COPY mvnw pom.xml ./
# RUN ./mvnw dependency:resolve
#
# COPY src ./src
#
# CMD ["./mvnw", "spring-boot:run"]
#
# Build
#
FROM maven:3.9.0-eclipse-temurin-19-focal AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package -DskipTests
#
# Package stage
#
FROM openjdk:19-jdk-alpine
WORKDIR /usr/local/lib
RUN mkdir git
RUN mkdir json
COPY src/main/resources/json /usr/local/lib/json
COPY --from=build /home/app/target/assistDbBackend-0.0.1-SNAPSHOT.jar /usr/local/lib/assistDbBackend.jar
CMD ["java","-jar", "-Dapplication.config.path=classpath:", "/usr/local/lib/assistDbBackend.jar"]

@ -16,6 +16,7 @@
<properties>
<java.version>19</java.version>
</properties>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>

@ -3,38 +3,13 @@ package com.apside.assist.db.backend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import java.util.Arrays;
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
public class AssistDBBackendApplication {
private static final String ACCESS_CONTROL = "Access-Control-Allow-Origin";
public static void main(String[] args){
SpringApplication.run(AssistDBBackendApplication.class, args);
}
@Bean
public CorsFilter corsFilter() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setAllowedOrigins(Arrays.asList("http://178.18.0.4:9002", "http://localhost:9002"));
corsConfiguration.setAllowedHeaders(Arrays.asList("Origin", ACCESS_CONTROL, "Content-Type",
"Accept", "Authorization", "Origin, Accept", "X-Requested-With",
"Access-Control-Request-Method", "Access-Control-Request-Headers"));
corsConfiguration.setExposedHeaders(Arrays.asList("Origin", "Content-Type", "Accept", "Authorization",
ACCESS_CONTROL, ACCESS_CONTROL, "Access-Control-Allow-Credentials"));
corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
return new CorsFilter(urlBasedCorsConfigurationSource);
}
}

@ -3,7 +3,6 @@ package com.apside.assist.db.backend.controller;
import com.apside.assist.db.backend.model.InfoColumn;
import com.apside.assist.db.backend.service.InfoColumnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@ -17,8 +16,12 @@ import java.util.Optional;
@RequestMapping("/api")
@Slf4j
public class InfoColumnController {
@Autowired
private InfoColumnService infoColumnService;
public static final String ERROR_COLUMNS = "Error occurred while fetching columns";
private final InfoColumnService infoColumnService;
public InfoColumnController(InfoColumnService infoColumnService) {
this.infoColumnService = infoColumnService;
}
/**
* Read - Get one column
@ -29,11 +32,7 @@ public class InfoColumnController {
public ResponseEntity<InfoColumn> getColumn(@PathVariable("id") final Long id) {
log.debug("Start GetColumn - Get Request - with id: " + id);
Optional<InfoColumn> infoColumn = infoColumnService.getColumn(id);
if(infoColumn.isPresent()) {
return ResponseEntity.ok(infoColumn.get());
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
return infoColumn.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).build());
}
/**
@ -47,7 +46,7 @@ public class InfoColumnController {
Iterable<InfoColumn> columns = infoColumnService.getAllColumns();
return ResponseEntity.ok(columns);
} catch (Exception e){
log.error("Error occurred while fetching columns", e);
log.error(ERROR_COLUMNS, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@ -63,7 +62,7 @@ public class InfoColumnController {
Iterable<InfoColumn> columnsSelected = infoColumnService.getSelectedColumns(table, schema);
return ResponseEntity.ok(columnsSelected);
} catch (Exception e){
log.error("Error occurred while fetching columns", e);
log.error(ERROR_COLUMNS, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@ -75,7 +74,7 @@ public class InfoColumnController {
Iterable<InfoColumn> columnsForJoin = infoColumnService.getColumnsForJoin(firstTable, secondTable, firstSchema, secondSchema);
return ResponseEntity.ok(columnsForJoin);
} catch (Exception e){
log.error("Error occurred while fetching columns", e);
log.error(ERROR_COLUMNS, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@ -89,7 +88,7 @@ public class InfoColumnController {
Iterable<InfoColumn> columnsForJoin = infoColumnService.getColumnsForJoinTwo(tablesList, schemasList);
return ResponseEntity.ok(columnsForJoin);
} catch (Exception e){
log.error("Error occurred while fetching columns", e);
log.error(ERROR_COLUMNS, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}

@ -3,7 +3,6 @@ package com.apside.assist.db.backend.controller;
import com.apside.assist.db.backend.model.InfoTable;
import com.apside.assist.db.backend.service.InfoTableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@ -14,9 +13,13 @@ import java.util.Optional;
@RequestMapping("/api")
@Slf4j
public class InfoTableController {
@Autowired
private InfoTableService infoTableService;
public static final String ERROR_DELETING_TABLES = "Error occurred while deleting tables";
private final InfoTableService infoTableService;
public InfoTableController(InfoTableService infoTableService) {
this.infoTableService = infoTableService;
}
/**
* Read - Get one table
@ -28,11 +31,7 @@ public class InfoTableController {
log.debug("Start getTable - Get Request - with id: " + id);
Optional<InfoTable> infoTable = infoTableService.getTable(id);
if (infoTable.isPresent()) {
return ResponseEntity.ok(infoTable.get());
} else {
return ResponseEntity.notFound().build();
}
return infoTable.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
/**
@ -100,7 +99,7 @@ public class InfoTableController {
infoTableService.deleteAllTable();
return ResponseEntity.ok().build();
} catch (Exception e) {
log.error("Error occurred while deleting tables", e);
log.error(ERROR_DELETING_TABLES, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@ -112,7 +111,7 @@ public class InfoTableController {
Iterable<InfoTable> tables = infoTableService.getTablesWithFilter(filter);
return ResponseEntity.ok(tables);
} catch (Exception e) {
log.error("Error occurred while deleting tables", e);
log.error(ERROR_DELETING_TABLES, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@ -124,7 +123,7 @@ public class InfoTableController {
Iterable<InfoTable> tables = infoTableService.getTablesWithFilterAndSchema(filter, schema);
return ResponseEntity.ok(tables);
} catch (Exception e) {
log.error("Error occurred while deleting tables", e);
log.error(ERROR_DELETING_TABLES, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}

@ -3,7 +3,6 @@ package com.apside.assist.db.backend.controller;
import com.apside.assist.db.backend.model.LinkInfo;
import com.apside.assist.db.backend.service.LinkInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@ -15,9 +14,11 @@ import java.util.Optional;
@Slf4j
public class LinkInfoController {
@Autowired
private LinkInfoService linkInfoService;
private final LinkInfoService linkInfoService;
public LinkInfoController(LinkInfoService linkInfoService) {
this.linkInfoService = linkInfoService;
}
/**
* Read - Get one Link
@ -28,11 +29,7 @@ public class LinkInfoController {
public ResponseEntity<LinkInfo> getLinkInfo(@PathVariable("id") final Long id) {
log.debug("Start getLinkInfo - Get Request - with id: " + id);
Optional<LinkInfo> linkInfo = linkInfoService.getLinkInfo(id);
if (linkInfo.isPresent()) {
return ResponseEntity.ok(linkInfo.get());
} else {
return ResponseEntity.notFound().build();
}
return linkInfo.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
/**

@ -1,10 +1,7 @@
package com.apside.assist.db.backend.controller;
import java.io.IOException;
import com.apside.assist.db.backend.service.ResetDataService;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@ -13,8 +10,12 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/api")
@Slf4j
public class ResetDataController {
@Autowired
private ResetDataService resetDataService;
private final ResetDataService resetDataService;
public ResetDataController(ResetDataService resetDataService) {
this.resetDataService = resetDataService;
}
@PostMapping("/reset")
public ResponseEntity<Void> resetData() {

@ -1,25 +1,25 @@
package com.apside.assist.db.backend.controller;
import com.apside.assist.db.backend.model.LinkScriptTag;
import com.apside.assist.db.backend.model.Script;;
import com.apside.assist.db.backend.model.Script;
import com.apside.assist.db.backend.service.ScriptsService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
@RestController
@RequestMapping("/api")
@Slf4j
public class ScriptController {
@Autowired
ScriptsService scriptsService;
private final ScriptsService scriptsService;
public ScriptController(ScriptsService scriptsService) {
this.scriptsService = scriptsService;
}
@GetMapping("/scripts")
public ResponseEntity<List<Script>> getAllScripts() {

@ -3,14 +3,10 @@ package com.apside.assist.db.backend.controller;
import com.apside.assist.db.backend.model.Tag;
import com.apside.assist.db.backend.service.TagsService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
@RestController
@ -18,8 +14,11 @@ import java.util.List;
@Slf4j
public class TagsController {
@Autowired
private TagsService tagsService;
private final TagsService tagsService;
public TagsController(TagsService tagsService) {
this.tagsService = tagsService;
}
@GetMapping("/tags/all")
public ResponseEntity<List<Tag>> getTags() {

@ -1,41 +1,56 @@
package com.apside.assist.db.backend.service.implementation;
import com.apside.assist.db.backend.service.GitService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.RemoteAddCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.springframework.beans.factory.InitializingBean;
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;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Objects;
@Service
@Slf4j
public class GitServiceImpl implements GitService {
public class GitServiceImpl implements GitService, InitializingBean {
private final String tempDirectoryPath;
public GitServiceImpl(){
tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "\\AssistDB_AdditionalFiles";
}
@Value("${USERNAME_GIT}")
private String tempDirectoryPath;
@Value("${assistDb.get.username}")
private String userGit;
@Value("${ACCESS_TOKEN_GIT}")
@Value("${assistDb.get.password}")
private String accesToken;
@Value("${URI_GIT}")
@Value("${assistDb.get.uri}")
private String uriGit;
private static final String NAME_URI = "origin";
private static final String COMMIT_MESSAGE = "commited";
private static final String BRANCH_NAME = "main";
@Override
public void afterPropertiesSet() throws Exception {
log.info("Initialisation GIT");
Resource resource = new PathResource("git");
tempDirectoryPath = resource.getURI().getPath();
if (Objects.requireNonNull(resource.getFile().list()).length == 0) {
log.info("Récupération GIT");
Git.cloneRepository()
.setURI(uriGit)
.setDirectory(resource.getFile())
.call();
} else {
log.info("Fichiers GIT déjà existants");
}
}
@Override
public void pushToGit() throws IOException, GitAPIException, URISyntaxException {
log.debug("Starting pushToGit method.");
@ -66,13 +81,13 @@ public class GitServiceImpl implements GitService {
git.pull().setCredentialsProvider(createCredential()).setRemote(NAME_URI).setRemoteBranchName(BRANCH_NAME).call();
log.info("Pull from Git successful.");
} catch (IOException|GitAPIException exception){
log.error("Error push from git", exception);
log.error("Error pull from git", exception);
throw exception;
}
}
public UsernamePasswordCredentialsProvider createCredential() {
private UsernamePasswordCredentialsProvider createCredential() {
return new UsernamePasswordCredentialsProvider(userGit, accesToken);
}
}

@ -1,22 +1,23 @@
package com.apside.assist.db.backend.service.implementation;
import com.apside.assist.db.backend.repository.InfoColumnRepository;
import com.apside.assist.db.backend.model.InfoColumn;
import com.apside.assist.db.backend.repository.InfoColumnRepository;
import com.apside.assist.db.backend.service.InfoColumnService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Data
@Service
@Slf4j
public class InfoColumnServiceImpl implements InfoColumnService {
@Autowired
private InfoColumnRepository infoColumnRepository;
private final InfoColumnRepository infoColumnRepository;
public InfoColumnServiceImpl(InfoColumnRepository infoColumnRepository) {
this.infoColumnRepository = infoColumnRepository;
}
@Override
public Optional<InfoColumn> getColumn(final Long id) {

@ -1,22 +1,22 @@
package com.apside.assist.db.backend.service.implementation;
import com.apside.assist.db.backend.model.InfoColumn;
import com.apside.assist.db.backend.repository.InfoTableRepository;
import com.apside.assist.db.backend.model.InfoTable;
import com.apside.assist.db.backend.service.InfoTableService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Data
@Service
@Slf4j
public class InfoTableServiceImpl implements InfoTableService {
@Autowired
private InfoTableRepository infoTableRepository;
private final InfoTableRepository infoTableRepository;
public InfoTableServiceImpl(InfoTableRepository infoTableRepository) {
this.infoTableRepository = infoTableRepository;
}
@Override
public Optional<InfoTable> getTable(final Long id) {

@ -3,19 +3,20 @@ package com.apside.assist.db.backend.service.implementation;
import com.apside.assist.db.backend.model.LinkInfo;
import com.apside.assist.db.backend.repository.LinkInfoRepository;
import com.apside.assist.db.backend.service.LinkInfoService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Data
@Service
@Slf4j
public class LinkInfoServiceImpl implements LinkInfoService {
@Autowired
private LinkInfoRepository linkInfoRepository;
private final LinkInfoRepository linkInfoRepository;
public LinkInfoServiceImpl(LinkInfoRepository linkInfoRepository) {
this.linkInfoRepository = linkInfoRepository;
}
@Override
public Optional<LinkInfo> getLinkInfo(final Long id) {

@ -1,8 +1,8 @@
package com.apside.assist.db.backend.service.implementation;
import com.apside.assist.db.backend.model.LinkInfo;
import com.apside.assist.db.backend.model.InfoColumn;
import com.apside.assist.db.backend.model.InfoTable;
import com.apside.assist.db.backend.model.LinkInfo;
import com.apside.assist.db.backend.model.json.Column;
import com.apside.assist.db.backend.model.json.DataEnum;
import com.apside.assist.db.backend.model.json.Schema;
@ -15,46 +15,38 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
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 org.springframework.beans.factory.annotation.Autowired;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Objects;
@Service
@Slf4j
public class ResetDataServiceImpl implements ResetDataService, InitializingBean {
@Autowired
private InfoTableService infoTableService;
@Autowired
private InfoColumnService infoColumnService;
@Autowired
private LinkInfoService linkInfoService;
private final InfoTableService infoTableService;
private final InfoColumnService infoColumnService;
private final LinkInfoService linkInfoService;
@Value("${PATH_FOR_DATA}")
private String pathForData;
@Value("${assistDb.db.json.file}")
private String fileName;
private String result;
private Path path;
private static final String TABLE_STRING = "table";
private static final String SCHEMA_STRING = "schema";
private static final String DATA_STRING = "data";
private static final String COLUMN_STRING = "columns";
private static final String TABLE_TEXT_STRING = "table_text";
private static final String NAME_STRING = "name";
private static final String DATA_TYPE_STRING = "data_type";
private static final String LENGTH_STRING = "length";
private static final String COLUMN_TEXT_STRING = "column_text";
public ResetDataServiceImpl(InfoTableService infoTableService, InfoColumnService infoColumnService, LinkInfoService linkInfoService) {
this.infoTableService = infoTableService;
this.infoColumnService = infoColumnService;
this.linkInfoService = linkInfoService;
}
@Override
public void afterPropertiesSet() throws Exception {
path = Paths.get(pathForData);
result = new String(Files.readAllBytes(path));
Resource resource = new PathResource("json/" + fileName);
result = new String(Files.readAllBytes(Paths.get(resource.getURI())));
}
@Override
@ -83,7 +75,7 @@ public class ResetDataServiceImpl implements ResetDataService, InitializingBean
}
}
}
List<InfoColumn> filteredListOfColumns = listOfColumns.stream().distinct().collect(Collectors.toList());
List<InfoColumn> filteredListOfColumns = listOfColumns.stream().distinct().toList();
infoTableService.addMultipleTables(listOfTables);
infoColumnService.addMultipleColumns(filteredListOfColumns);
log.info("Insert all data into DB - success");
@ -103,8 +95,8 @@ public class ResetDataServiceImpl implements ResetDataService, InitializingBean
descCol = "";
}
InfoColumn findCol = infoColumnService.getSpecCol(col.getName(), col.getData_type(), col.getLength(), descCol);
int idCol = Math.toIntExact(findCol.getId());
if (findCol != null){
if (Objects.nonNull(findCol)) {
int idCol = Math.toIntExact(findCol.getId());
listOfLink.add(new LinkInfo(dat.getTable(), schema.getSchema(), idCol));
}
}

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

@ -3,16 +3,16 @@ package com.apside.assist.db.backend.service.implementation;
import com.apside.assist.db.backend.model.Tag;
import com.apside.assist.db.backend.service.GitService;
import com.apside.assist.db.backend.service.TagsService;
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;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
@ -25,29 +25,32 @@ import java.util.Objects;
@Service
@Slf4j
public class TagsServiceImpl implements TagsService, InitializingBean {
@Autowired
private GitService gitService;
private String tempDirectoryPath;
private final GitService gitService;
private Path path;
private String jsonContent;
private JSONObject dataGlobal;
private JSONArray data;
@Value("${PATH_FOR_TAG_JSON}")
private String PATH_FOR_TAG_JSON;
@Value("${assistDb.db.json.tags}")
private String fileName;
private static final String TAG_ID_STRING = "tagId";
private static final String DATA_STRING = "data";
private static final String TAG_STRING = "tag";
private static final String DESCRIPTION_STRING = "description";
public TagsServiceImpl(GitService gitService) {
this.gitService = gitService;
}
@Override
public void afterPropertiesSet() throws Exception {
tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + PATH_FOR_TAG_JSON;
path = Paths.get(tempDirectoryPath);
Resource resource = new PathResource("git/" + fileName);
path = Paths.get(resource.getURI());
}
@Override
public void initialize() throws IOException {
jsonContent = new String(Files.readAllBytes(path));
String jsonContent = new String(Files.readAllBytes(path));
dataGlobal = new JSONObject(jsonContent);
data = dataGlobal.getJSONArray(DATA_STRING);
}

@ -1,19 +1,19 @@
spring.application.name=AssistDBBackend
spring.datasource.url=jdbc:mysql://localhost:3306/prudencetest?rewriteBatchedStatements=true&cachePrepStmts=true&useServerPrepStmts=true
#spring.datasource.url=jdbc:mysql://178.18.0.2:3306/prudencetest?rewriteBatchedStatements=true&cachePrepStmts=true&useServerPrepStmts=true
spring.datasource.url=jdbc:mysql://178.18.0.2:3306/prudencetest
spring.datasource.username=root
spring.datasource.password=Pompom.21
server.port=9001
USERNAME_GIT = fhibert@apside.fr
ACCESS_TOKEN_GIT = accesstokenexemple
assistDb.get.username = *****
assistDb.get.password = *****
assistDb.get.uri = https://gitea.ci.apside-top.fr/Prudence_Creole/AssistDB_AdditionalFiles.git
URI_GIT = https://gitea.ci.apside-top.fr/Prudence_Creole/AssistDB_AdditionalFiles.git
PATH_FOR_DATA = src/main/resources/assistDbData.json
PATH_FOR_SCRIPT_JSON = /AssistDB_AdditionalFiles/scripts.json
PATH_TO_SCRIPT_DIR = /AssistDB_AdditionalFiles/Scripts
PATH_FOR_TAG_JSON = /AssistDB_AdditionalFiles/tags.json
assistDb.db.json.file=assistDbData.json
assistDb.db.json.script=scripts.json
assistDb.db.json.directory=Scripts
assistDb.db.json.tags=tags.json
logging.level.org.springframework.boot.web.embedded.tomcat=INFO
logging.level.org.springframework=error

Loading…
Cancel
Save