package eu.dnetlib.openaire.info; import java.time.LocalDate; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import eu.dnetlib.openaire.common.AbstractExporterController; import eu.dnetlib.openaire.common.ExporterConstants; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; @RestController @CrossOrigin(origins = { "*" }) @ConditionalOnProperty(value = "openaire.exporter.enable.info", havingValue = "true") @io.swagger.annotations.Api(tags = "OpenAIRE Info API", description = "the OpenAIRE info API") public class InfoController extends AbstractExporterController { private static final Log log = LogFactory.getLog(InfoController.class); // NOPMD by marko on 11/24/08 5:02 PM public final static String UTF8 = "UTF-8"; @Autowired private JdbcInfoDao jdbcInfoDao; @RequestMapping(value = "/info/{infoKey}", produces = { "application/json" }, method = RequestMethod.GET) @ApiOperation(value = "get info date", notes = "get info date", tags = { ExporterConstants.R }, response = LocalDate.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = LocalDate.class), @ApiResponse(code = 500, message = "unexpected error", response = ErrorMessage.class) }) public LocalDate getDate(@PathVariable final String infoKey) { final JdbcInfoDao.DATE_INFO info = JdbcInfoDao.DATE_INFO.valueOf(infoKey); if (info == null) { throw new RuntimeException(infoKey + " not recognized"); } return jdbcInfoDao.getDate(info); } @RequestMapping(value = "/info", produces = { "application/json" }, method = RequestMethod.GET) @ApiOperation(value = "get all the info date", notes = "get all the info date", tags = { ExporterConstants.R }, response = Map.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = LocalDate.class), @ApiResponse(code = 500, message = "unexpected error", response = ErrorMessage.class) }) public Map listInfo() { final Map map = Maps.newHashMap(); for (final JdbcInfoDao.DATE_INFO dateInfo : JdbcInfoDao.DATE_INFO.values()) { map.put(dateInfo.name(), jdbcInfoDao.getDate(dateInfo)); } return map; } @RequestMapping(value = "/info/keys", produces = { "application/json" }, method = RequestMethod.GET) @ApiOperation(value = "get the available keys", notes = "get the available keys", tags = { ExporterConstants.R }, response = String.class, responseContainer = "List") @ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = LocalDate.class), @ApiResponse(code = 500, message = "unexpected error", response = ErrorMessage.class) }) public List listInfoKeys() { final List keys = Lists.newArrayList(); for (final JdbcInfoDao.DATE_INFO dateInfo : JdbcInfoDao.DATE_INFO.values()) { keys.add(dateInfo.name()); } return keys; } @RequestMapping(value = "/info/dropCache", produces = { "application/json" }, method = RequestMethod.GET) @ApiOperation(value = "Drops the info cache", notes = "Drops the info cache", tags = { ExporterConstants.R }) public void dropCache() { jdbcInfoDao.dropCache(); } }