diff --git a/src/main/java/it/cnr/isti/epasmed/epas/client/EPASAbsenceTypesClient.java b/src/main/java/it/cnr/isti/epasmed/epas/client/EPASAbsenceTypesClient.java new file mode 100644 index 0000000..21a11ef --- /dev/null +++ b/src/main/java/it/cnr/isti/epasmed/epas/client/EPASAbsenceTypesClient.java @@ -0,0 +1,47 @@ +package it.cnr.isti.epasmed.epas.client; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +import it.cnr.isti.epasmed.config.ApplicationProperties; +import it.cnr.isti.epasmed.epas.model.EPASAbsenceTypes; + +@Component +public class EPASAbsenceTypesClient { + + private final Logger log = LoggerFactory.getLogger(getClass()); + + @Autowired + @Qualifier("RestTemplateForSecondUser") + RestTemplate rt; + + @Autowired + ApplicationProperties appProps; + + public List getAbsenceTypes() { + Map uriVariables = new HashMap<>(); + uriVariables.put("onlyActive", "true"); + uriVariables.put("used", "true"); + + ResponseEntity> responseEntity = rt.exchange( + appProps.getDatasourceEpasRest().getRestUrl() + + "/v3/absenceTypes/list?onlyActive={onlyActive}&used={used}", + HttpMethod.GET, null, new ParameterizedTypeReference>() { + }, uriVariables); + List listEPASAbsenceTypes = responseEntity.getBody(); + log.info("Retrieved EPASAbsenceTypes: {}", listEPASAbsenceTypes); + return listEPASAbsenceTypes; + } + +} diff --git a/src/main/java/it/cnr/isti/epasmed/epas/model/EPASAbsenceTypes.java b/src/main/java/it/cnr/isti/epasmed/epas/model/EPASAbsenceTypes.java new file mode 100644 index 0000000..ac432d0 --- /dev/null +++ b/src/main/java/it/cnr/isti/epasmed/epas/model/EPASAbsenceTypes.java @@ -0,0 +1,27 @@ +package it.cnr.isti.epasmed.epas.model; + +import java.io.Serializable; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class EPASAbsenceTypes implements Serializable { + + private static final long serialVersionUID = 1L; + + private String certificateCode; + private String code; + private String consideredWeekEnd; + private String description; + private String id; + private String internalUse; + private String justifiedTime; + private String validFrom; + private String validTo; + +} \ No newline at end of file diff --git a/src/main/java/it/cnr/isti/epasmed/epas/service/EPASAbsenceTypesService.java b/src/main/java/it/cnr/isti/epasmed/epas/service/EPASAbsenceTypesService.java new file mode 100644 index 0000000..dcd3ea9 --- /dev/null +++ b/src/main/java/it/cnr/isti/epasmed/epas/service/EPASAbsenceTypesService.java @@ -0,0 +1,21 @@ +package it.cnr.isti.epasmed.epas.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import it.cnr.isti.epasmed.epas.client.EPASAbsenceTypesClient; +import it.cnr.isti.epasmed.epas.model.EPASAbsenceTypes; + +@Service +public class EPASAbsenceTypesService { + + @Autowired + EPASAbsenceTypesClient epasAbsenceTypesClient; + + public List getAbsenceTypes() { + return epasAbsenceTypesClient.getAbsenceTypes(); + } + +} diff --git a/src/main/java/it/cnr/isti/epasmed/web/rest/epas/EPASAbsenceTypesResource.java b/src/main/java/it/cnr/isti/epasmed/web/rest/epas/EPASAbsenceTypesResource.java new file mode 100644 index 0000000..edb517a --- /dev/null +++ b/src/main/java/it/cnr/isti/epasmed/web/rest/epas/EPASAbsenceTypesResource.java @@ -0,0 +1,52 @@ +package it.cnr.isti.epasmed.web.rest.epas; + +import java.util.List; +import java.util.Optional; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import io.github.jhipster.web.util.ResponseUtil; +import it.cnr.isti.epasmed.epas.model.EPASAbsenceTypes; +import it.cnr.isti.epasmed.epas.service.EPASAbsenceTypesService; + +@RestController +@RequestMapping("/api/epas") +public class EPASAbsenceTypesResource { + + private final Logger log = LoggerFactory.getLogger(getClass()); + + @Value("${jhipster.clientApp.name}") + private String applicationName; + + private final EPASAbsenceTypesService epasAbsenceTypesService; + + public EPASAbsenceTypesResource(EPASAbsenceTypesService epasAbsenceTypesService) { + this.epasAbsenceTypesService = epasAbsenceTypesService; + + } + + /** + * {@code GET /absenceTypes} : get the absence types list. + * + * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body + * the list of EPAS Absence Types, or with status + * {@code 404 (Not Found)}. + */ + @GetMapping("/absenceTypes") + public ResponseEntity> getEPASAbsencesTypes() { + log.debug("REST request GET EPAS Absences Types"); + List epasAbsenceTypesList = null; + + epasAbsenceTypesList = epasAbsenceTypesService.getAbsenceTypes(); + + return ResponseUtil.wrapOrNotFound(Optional.of(epasAbsenceTypesList)); + } + + +} diff --git a/src/test/java/it/cnr/isti/epasmed/web/rest/epas/EPASAbsenceTypesResourceIT.java b/src/test/java/it/cnr/isti/epasmed/web/rest/epas/EPASAbsenceTypesResourceIT.java new file mode 100644 index 0000000..a22d2d0 --- /dev/null +++ b/src/test/java/it/cnr/isti/epasmed/web/rest/epas/EPASAbsenceTypesResourceIT.java @@ -0,0 +1,84 @@ +package it.cnr.isti.epasmed.web.rest.epas; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.env.Environment; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.junit.jupiter.EnabledIf; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +import it.cnr.isti.epasmed.EpasmedApp; +import it.cnr.isti.epasmed.epas.model.EPASAbsenceTypes; +import it.cnr.isti.epasmed.security.AuthoritiesConstants; + +/** + * Integration tests for the {@link EPASAbsenceTypesResource} REST controller. + */ +@AutoConfigureMockMvc +@WithMockUser(authorities = AuthoritiesConstants.ADMIN) +@SpringBootTest(classes = EpasmedApp.class) +@EnabledIf("false") +public class EPASAbsenceTypesResourceIT { + + private final Logger log = LoggerFactory.getLogger(getClass()); + + // private static final String OFFICE_DEFAULT_ID = "1"; + // private static final String OFFICE_DEFAULT_NAME = "ISTI - Pisa"; + // private static final String OFFICE_DEFAULT_CODE = "074000"; + // private static final String OFFICE_DEFAULT_CODEID = "225200"; + + @Autowired + private MockMvc restEPASAbsenceTypesMockMvc; + + @Autowired + private Environment environment; + + @BeforeEach + public void initTest() { + for (String profileName : environment.getActiveProfiles()) { + log.info("Currently active profile - " + profileName); + } + } + + @Test + public void getAbsenceTypes() throws Exception { + + List epasAbsenceTypes = null; + try { + MvcResult result = restEPASAbsenceTypesMockMvc.perform(get("/api/epas/absenceTypes")) + .andExpect(status().isOk()).andReturn(); + + ObjectMapper mapper = new ObjectMapper(); + epasAbsenceTypes = mapper.readValue(result.getResponse().getContentAsString(), + new TypeReference>() { + }); + + } catch (Exception e) { + log.error(e.getLocalizedMessage(), e); + return; + } + + if (epasAbsenceTypes != null) { + log.info("EPAS Absence Types size: {}", epasAbsenceTypes.size()); + for (EPASAbsenceTypes type : epasAbsenceTypes) { + log.info("{}", type); + } + } + + } + +}