2019-01-03 13:19:45 +01:00
package eu.dnetlib.repo.manager.service ;
2017-12-07 18:57:12 +01:00
2018-03-26 23:39:39 +02:00
import com.fasterxml.jackson.databind.ObjectMapper ;
2017-12-07 18:57:12 +01:00
import eu.dnetlib.domain.data.PiwikInfo ;
2018-03-26 23:39:39 +02:00
import eu.dnetlib.repo.manager.shared.RepositoryServiceException ;
2018-02-27 14:41:12 +01:00
import org.apache.commons.codec.digest.DigestUtils ;
2018-07-21 17:07:29 +02:00
import org.apache.log4j.Logger ;
2017-12-07 18:57:12 +01:00
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Qualifier ;
2018-03-26 23:39:39 +02:00
import org.springframework.beans.factory.annotation.Value ;
2017-12-22 11:29:02 +01:00
import org.springframework.dao.EmptyResultDataAccessException ;
2018-07-21 17:07:29 +02:00
import org.springframework.http.HttpStatus ;
import org.springframework.http.ResponseEntity ;
2017-12-07 18:57:12 +01:00
import org.springframework.jdbc.core.JdbcTemplate ;
import org.springframework.jdbc.core.RowMapper ;
2018-04-04 12:28:38 +02:00
import org.springframework.security.access.prepost.PreAuthorize ;
2019-01-03 13:19:45 +01:00
import org.springframework.stereotype.Service ;
2017-12-07 18:57:12 +01:00
import org.springframework.web.bind.annotation.PathVariable ;
2018-02-22 11:55:56 +01:00
import org.springframework.web.bind.annotation.RequestBody ;
2018-04-04 12:28:38 +02:00
import org.springframework.web.bind.annotation.RequestParam ;
2017-12-07 18:57:12 +01:00
import javax.sql.DataSource ;
2018-03-26 23:39:39 +02:00
import java.io.IOException ;
import java.io.UnsupportedEncodingException ;
import java.net.URL ;
import java.net.URLEncoder ;
2017-12-07 18:57:12 +01:00
import java.sql.Types ;
import java.util.List ;
2018-03-26 23:39:39 +02:00
import java.util.Map ;
2017-12-07 18:57:12 +01:00
2019-01-03 13:19:45 +01:00
@Service ( " piwikService " )
2019-02-04 13:47:04 +01:00
public class PiWikServiceImpl implements PiWikService {
2017-12-07 18:57:12 +01:00
@Qualifier ( " repomanager.dataSource " )
2018-07-21 17:07:29 +02:00
@Autowired
2017-12-07 18:57:12 +01:00
private DataSource dataSource ;
2018-03-26 23:39:39 +02:00
@Value ( " ${services.repomanager.analyticsURL} " )
private String analyticsURL ;
@Autowired
@Qualifier ( " emailUtils " )
EmailUtils emailUtils ;
2018-07-21 17:07:29 +02:00
private static final Logger LOGGER = Logger
2019-02-04 13:47:04 +01:00
. getLogger ( PiWikServiceImpl . class ) ;
2017-12-07 18:57:12 +01:00
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 = ?; " ;
2018-03-26 23:39:39 +02:00
2017-12-07 18:57:12 +01:00
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
2019-07-17 13:09:28 +02:00
@PreAuthorize ( " hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (hasRole('ROLE_USER') and #piwikInfo.requestorEmail == authentication.userInfo.email) " )
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
2019-07-17 13:09:28 +02:00
@PreAuthorize ( " hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') " )
2018-07-21 17:07:29 +02:00
public ResponseEntity < Object > approvePiwikSite ( @PathVariable ( " repositoryId " ) String repositoryId ) {
2017-12-07 18:57:12 +01:00
new JdbcTemplate ( dataSource ) . update ( APPROVE_PIWIK_SITE , new Object [ ] { repositoryId } , new int [ ] { Types . VARCHAR } ) ;
2018-07-21 17:07:29 +02:00
return new ResponseEntity < > ( " OK " , HttpStatus . OK ) ;
2017-12-07 18:57:12 +01:00
}
@Override
2018-02-21 16:05:02 +01:00
public String getOpenaireId ( @PathVariable ( " repositoryId " ) String repositoryId ) {
2018-02-27 14:41:12 +01:00
if ( repositoryId ! = null & & repositoryId . contains ( " :: " ) )
return repositoryId . split ( " :: " ) [ 0 ] + " :: " + DigestUtils . md5Hex ( repositoryId . split ( " :: " ) [ 1 ] ) ;
2017-12-07 18:57:12 +01:00
return null ;
}
2018-03-26 23:39:39 +02:00
@Override
2019-07-17 13:09:28 +02:00
@PreAuthorize ( " hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') " )
2018-07-21 17:07:29 +02:00
public ResponseEntity < Object > markPiwikSiteAsValidated ( @PathVariable ( " repositoryId " ) String repositoryId ) throws RepositoryServiceException {
2018-03-26 23:39:39 +02:00
try {
approvePiwikSite ( repositoryId ) ;
PiwikInfo piwikInfo = getPiwikSiteForRepo ( repositoryId ) ;
emailUtils . sendAdministratorMetricsEnabled ( piwikInfo ) ;
emailUtils . sendUserMetricsEnabled ( piwikInfo ) ;
} catch ( EmptyResultDataAccessException e ) {
LOGGER . error ( " Error while approving piwik site: " , e ) ;
emailUtils . reportException ( e ) ;
throw new RepositoryServiceException ( " General error " , RepositoryServiceException . ErrorCode . GENERAL_ERROR ) ;
} catch ( Exception e ) {
LOGGER . error ( " Error while sending email to administrator or user about the enabling of metrics " , e ) ;
emailUtils . reportException ( e ) ;
}
2018-07-21 17:07:29 +02:00
return new ResponseEntity < > ( " OK " , HttpStatus . OK ) ;
2018-03-26 23:39:39 +02:00
}
@Override
2019-07-17 13:09:28 +02:00
@PreAuthorize ( " hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (hasRole('ROLE_USER') and #piwikInfo.requestorEmail == authentication.userInfo.email) " )
2018-07-21 17:07:29 +02:00
public PiwikInfo enableMetricsForRepository ( @RequestParam ( " officialName " ) String officialName ,
@RequestParam ( " repoWebsite " ) String repoWebsite ,
@RequestBody PiwikInfo piwikInfo ) throws RepositoryServiceException {
2018-03-26 23:39:39 +02:00
try {
2018-04-04 12:28:38 +02:00
String URL = analyticsURL + " siteName= " + URLEncoder . encode ( officialName , " UTF-8 " ) + " &url= "
+ URLEncoder . encode ( repoWebsite , " UTF-8 " ) ;
Map map = new ObjectMapper ( ) . readValue ( new URL ( URL ) , Map . class ) ;
String siteId = null ;
if ( map . get ( " value " ) ! = null ) {
siteId = map . get ( " value " ) . toString ( ) ;
}
piwikInfo . setSiteId ( siteId ) ;
2018-03-26 23:39:39 +02:00
savePiwikInfo ( piwikInfo ) ;
emailUtils . sendAdministratorRequestToEnableMetrics ( piwikInfo ) ;
emailUtils . sendUserRequestToEnableMetrics ( piwikInfo ) ;
} catch ( UnsupportedEncodingException uee ) {
LOGGER . error ( " Error while creating piwikScript URL " , uee ) ;
emailUtils . reportException ( uee ) ;
throw new RepositoryServiceException ( " login.generalError " , RepositoryServiceException . ErrorCode . GENERAL_ERROR ) ;
} catch ( IOException ioe ) {
LOGGER . error ( " Error while creating piwik site " , ioe ) ;
emailUtils . reportException ( ioe ) ;
throw new RepositoryServiceException ( " login.generalError " , RepositoryServiceException . ErrorCode . GENERAL_ERROR ) ;
} catch ( Exception e ) {
LOGGER . error ( " Error while sending email to administrator or user about the request to enable metrics " , e ) ;
emailUtils . reportException ( e ) ;
}
2018-07-21 17:07:29 +02:00
return piwikInfo ;
2018-03-26 23:39:39 +02:00
}
2017-12-07 18:57:12 +01:00
}