2017-12-07 18:57:12 +01:00
package eu.dnetlib.repo.manager.service.controllers ;
import eu.dnetlib.domain.data.PiwikInfo ;
import eu.dnetlib.utils.md5.MD5 ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Qualifier ;
2017-12-22 11:29:02 +01:00
import org.springframework.dao.EmptyResultDataAccessException ;
2017-12-07 18:57:12 +01:00
import org.springframework.jdbc.core.JdbcTemplate ;
import org.springframework.jdbc.core.RowMapper ;
import org.springframework.stereotype.Component ;
import org.springframework.web.bind.annotation.PathVariable ;
2018-02-22 11:55:56 +01:00
import org.springframework.web.bind.annotation.RequestBody ;
2017-12-07 18:57:12 +01:00
import javax.annotation.PostConstruct ;
import javax.sql.DataSource ;
import java.security.NoSuchAlgorithmException ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.sql.Types ;
import java.util.List ;
@Component
public class PiWikApiImpl implements PiWikApi {
@Autowired
@Qualifier ( " repomanager.dataSource " )
private DataSource dataSource ;
private static final org . apache . log4j . Logger LOGGER = org . apache . log4j . Logger
. getLogger ( PiWikApiImpl . class ) ;
private final static String GET_PIWIK_SITE = " select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site where repositoryid = ?; " ;
private final static String INSERT_PIWIK_INFO = " insert into piwik_site (repositoryid, siteid, creationdate, requestorname, requestoremail, validated, repositoryname, country, authenticationtoken) values (?, ?, now(), ?, ?, ?, ?, ?, ?) " ;
private final static String GET_PIWIK_SITES = " select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site order by repositoryname " ;
private final static String APPROVE_PIWIK_SITE = " update piwik_site set validated=true, validationdate=now() where repositoryid = ?; " ;
private RowMapper < PiwikInfo > piwikRowMapper = ( rs , i ) - > new PiwikInfo ( rs . getString ( " repositoryid " ) , getOpenaireId ( rs . getString ( " repositoryid " ) ) , rs . getString ( " repositoryname " ) , rs . getString ( " country " ) ,
rs . getString ( " siteid " ) , rs . getString ( " authenticationtoken " ) , rs . getTimestamp ( " creationdate " ) , rs . getString ( " requestorname " ) , rs . getString ( " requestoremail " ) ,
rs . getBoolean ( " validated " ) , rs . getTimestamp ( " validationdate " ) , rs . getString ( " comment " ) ) ;
@Override
public PiwikInfo getPiwikSiteForRepo ( @PathVariable ( " repositoryId " ) String repositoryId ) {
2017-12-22 11:29:02 +01:00
try {
return new JdbcTemplate ( dataSource ) . queryForObject ( GET_PIWIK_SITE , new String [ ] { repositoryId } , new int [ ] { Types . VARCHAR } , piwikRowMapper ) ;
} catch ( EmptyResultDataAccessException e ) {
return null ;
}
2017-12-07 18:57:12 +01:00
}
@Override
2018-02-22 11:55:56 +01:00
public PiwikInfo savePiwikInfo ( @RequestBody PiwikInfo piwikInfo ) {
2017-12-07 18:57:12 +01:00
JdbcTemplate jdbcTemplate = new JdbcTemplate ( dataSource ) ;
jdbcTemplate . update ( INSERT_PIWIK_INFO , new Object [ ] { piwikInfo . getRepositoryId ( ) , piwikInfo . getSiteId ( ) , piwikInfo . getRequestorName ( ) ,
piwikInfo . getRequestorEmail ( ) , piwikInfo . isValidated ( ) , piwikInfo . getRepositoryName ( ) , piwikInfo . getCountry ( ) , piwikInfo . getAuthenticationToken ( ) } ,
new int [ ] { Types . VARCHAR , Types . VARCHAR , Types . VARCHAR , Types . VARCHAR , Types . BOOLEAN , Types . VARCHAR , Types . VARCHAR , Types . VARCHAR } ) ;
return piwikInfo ;
}
@Override
public List < PiwikInfo > getPiwikSitesForRepos ( ) {
LOGGER . debug ( " Getting piwik sites for repos! " ) ;
2017-12-22 11:29:02 +01:00
try {
return new JdbcTemplate ( dataSource ) . query ( GET_PIWIK_SITES , piwikRowMapper ) ;
} catch ( EmptyResultDataAccessException e ) {
return null ;
}
2017-12-07 18:57:12 +01:00
}
@Override
public void approvePiwikSite ( @PathVariable ( " repositoryId " ) String repositoryId ) {
new JdbcTemplate ( dataSource ) . update ( APPROVE_PIWIK_SITE , new Object [ ] { repositoryId } , new int [ ] { Types . VARCHAR } ) ;
}
@Override
2018-02-21 16:05:02 +01:00
public String getOpenaireId ( @PathVariable ( " repositoryId " ) String repositoryId ) {
LOGGER . debug ( " Getting openaire id for repository: " + repositoryId ) ;
2017-12-07 18:57:12 +01:00
try {
2018-02-21 16:05:02 +01:00
if ( repositoryId ! = null & & repositoryId . contains ( " :: " ) ) {
return repositoryId . split ( " :: " ) [ 0 ] + " :: " + MD5 . encrypt2Hex ( repositoryId . split ( " :: " ) [ 1 ] ) ;
}
2017-12-07 18:57:12 +01:00
else
return null ;
} catch ( NoSuchAlgorithmException e ) {
2018-02-21 16:05:02 +01:00
LOGGER . debug ( e ) ;
2017-12-07 18:57:12 +01:00
}
return null ;
}
}