2019-03-18 15:30:07 +01:00
package eu.dnetlib.data.mdstore.manager.controller ;
2019-03-20 15:36:23 +01:00
import java.util.Date ;
2019-03-18 15:30:07 +01:00
import java.util.List ;
import java.util.stream.Collectors ;
2019-03-20 15:36:23 +01:00
import javax.transaction.Transactional ;
2019-03-18 15:30:07 +01:00
import org.springframework.beans.factory.annotation.Autowired ;
2019-03-20 15:36:23 +01:00
import org.springframework.jdbc.core.JdbcTemplate ;
2019-03-18 15:30:07 +01:00
import org.springframework.web.bind.annotation.PathVariable ;
2019-03-20 15:36:23 +01:00
import org.springframework.web.bind.annotation.RequestBody ;
2019-03-18 15:30:07 +01:00
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RequestMethod ;
2019-03-19 13:34:40 +01:00
import org.springframework.web.bind.annotation.RequestParam ;
2019-03-18 15:30:07 +01:00
import org.springframework.web.bind.annotation.RestController ;
2019-03-20 15:36:23 +01:00
import eu.dnetlib.data.mdstore.manager.exceptions.MDStoreManagerException ;
import eu.dnetlib.data.mdstore.manager.exceptions.NoContentException ;
2019-03-18 15:30:07 +01:00
import eu.dnetlib.data.mdstore.manager.model.MDStore ;
2019-03-20 15:36:23 +01:00
import eu.dnetlib.data.mdstore.manager.model.MDStoreCurrentVersion ;
import eu.dnetlib.data.mdstore.manager.model.MDStoreVersion ;
2019-03-19 13:34:40 +01:00
import eu.dnetlib.data.mdstore.manager.model.MDStoreWithInfo ;
2019-03-20 15:36:23 +01:00
import eu.dnetlib.data.mdstore.manager.repository.MDStoreCurrentVersionRepository ;
2019-03-18 15:30:07 +01:00
import eu.dnetlib.data.mdstore.manager.repository.MDStoreRepository ;
2019-03-20 15:36:23 +01:00
import eu.dnetlib.data.mdstore.manager.repository.MDStoreVersionRepository ;
2019-03-19 13:34:40 +01:00
import eu.dnetlib.data.mdstore.manager.repository.MDStoreWithInfoRepository ;
2019-03-18 15:30:07 +01:00
@RestController
2019-03-20 15:36:23 +01:00
@RequestMapping ( " /mdstores " )
public class MDStoreController {
2019-03-18 15:30:07 +01:00
@Autowired
2019-03-19 13:34:40 +01:00
private MDStoreRepository mdstoreRepository ;
@Autowired
2019-03-20 15:36:23 +01:00
private MDStoreVersionRepository mdstoreVersionRepository ;
@Autowired
private MDStoreCurrentVersionRepository mdstoreCurrentVersionRepository ;
2019-03-19 13:34:40 +01:00
@Autowired
private MDStoreWithInfoRepository mdstoreWithInfoRepository ;
2019-03-20 15:36:23 +01:00
@Autowired
private JdbcTemplate jdbcTemplate ;
2019-03-18 15:30:07 +01:00
@RequestMapping ( value = " / " , method = RequestMethod . GET )
2019-03-20 15:36:23 +01:00
public Iterable < MDStoreWithInfo > find ( ) {
2019-03-19 13:34:40 +01:00
return mdstoreWithInfoRepository . findAll ( ) ;
2019-03-18 15:30:07 +01:00
}
2019-03-20 15:36:23 +01:00
@RequestMapping ( value = " /ids " , method = RequestMethod . GET )
public List < String > findIdentifiers ( ) {
2019-03-19 13:34:40 +01:00
return mdstoreRepository . findAll ( ) . stream ( ) . map ( MDStore : : getId ) . collect ( Collectors . toList ( ) ) ;
2019-03-18 15:30:07 +01:00
}
2019-03-20 15:36:23 +01:00
@RequestMapping ( value = " /mdstore/{mdId} " , method = RequestMethod . GET )
public MDStoreWithInfo get ( @PathVariable final String mdId ) throws NoContentException {
return mdstoreWithInfoRepository . findById ( mdId ) . orElseThrow ( ( ) - > new NoContentException ( " Missing mdstore: " + mdId ) ) ;
2019-03-18 15:30:07 +01:00
}
@RequestMapping ( value = " /count " , method = RequestMethod . GET )
2019-03-20 15:36:23 +01:00
public long count ( ) {
2019-03-19 13:34:40 +01:00
return mdstoreRepository . count ( ) ;
2019-03-18 15:30:07 +01:00
}
2019-03-19 13:34:40 +01:00
@RequestMapping ( value = " /new/{format}/{layout}/{interpretation} " , method = RequestMethod . PUT )
2019-03-20 15:36:23 +01:00
public MDStoreWithInfo createMDStore (
@PathVariable final String format ,
@PathVariable final String layout ,
@PathVariable final String interpretation ,
@RequestParam ( required = false ) final String dsId ,
@RequestParam ( required = false ) final String apiId ) throws MDStoreManagerException {
final String id = _createMDStore ( format , layout , interpretation , dsId , apiId ) ;
return mdstoreWithInfoRepository . findById ( id ) . orElseThrow ( ( ) - > new MDStoreManagerException ( " MDStore not found " ) ) ;
}
@Transactional
private String _createMDStore ( final String format , final String layout , final String interpretation , final String dsId , final String apiId ) {
2019-03-19 13:34:40 +01:00
final MDStore md = MDStore . newInstance ( dsId , apiId , format , layout , interpretation ) ;
mdstoreRepository . save ( md ) ;
2019-03-18 15:30:07 +01:00
2019-03-20 15:36:23 +01:00
final MDStoreVersion v = MDStoreVersion . newInstance ( md . getId ( ) , false ) ;
v . setLastUpdate ( new Date ( ) ) ;
mdstoreVersionRepository . save ( v ) ;
mdstoreCurrentVersionRepository . save ( MDStoreCurrentVersion . newInstance ( v ) ) ;
return md . getId ( ) ;
}
@Transactional
@RequestMapping ( value = " /mdstore/{mdId} " , method = RequestMethod . DELETE )
public void delete ( @PathVariable final String mdId ) throws MDStoreManagerException {
if ( ! mdstoreRepository . existsById ( mdId ) ) { throw new NoContentException ( " On delete MDStore not found " + " [ " + mdId + " ] " ) ; }
if ( mdstoreVersionRepository . countByMdstoreAndReadCountGreaterThan ( mdId ,
0 ) > 0 ) { throw new MDStoreManagerException ( " Read transactions found on mdstore : " + mdId ) ; }
if ( mdstoreVersionRepository . countByMdstoreAndWriting ( mdId ,
true ) > 0 ) { throw new MDStoreManagerException ( " Write transactions found on mdstore : " + mdId ) ; }
mdstoreCurrentVersionRepository . deleteById ( mdId ) ;
mdstoreVersionRepository . deleteByMdstore ( mdId ) ;
mdstoreRepository . deleteById ( mdId ) ;
}
@Transactional
@RequestMapping ( value = " /mdstore/{mdId}/newVersion " , method = RequestMethod . GET )
public MDStoreVersion prepareNewVersion1 ( @PathVariable final String mdId ) {
final MDStoreVersion v = MDStoreVersion . newInstance ( mdId , true ) ;
mdstoreVersionRepository . save ( v ) ;
return v ;
}
2019-03-19 13:34:40 +01:00
2019-03-20 15:36:23 +01:00
@Transactional
@RequestMapping ( value = " /version/{versionId}/commit/{size} " , method = RequestMethod . GET )
public void commitVersion ( @PathVariable final String versionId , @PathVariable final int size ) throws NoContentException {
final MDStoreVersion v = mdstoreVersionRepository . findById ( versionId ) . orElseThrow ( ( ) - > new NoContentException ( " Invalid version: " + versionId ) ) ;
mdstoreCurrentVersionRepository . save ( MDStoreCurrentVersion . newInstance ( v ) ) ;
v . setWriting ( false ) ;
v . setSize ( size ) ;
v . setLastUpdate ( new Date ( ) ) ;
mdstoreVersionRepository . save ( v ) ;
2019-03-18 15:30:07 +01:00
}
2019-03-20 15:36:23 +01:00
@RequestMapping ( value = " /versions/expired " , method = RequestMethod . GET )
public List < String > listExpiredVersions ( ) {
return jdbcTemplate . queryForList (
" select v.id from mdstore_versions v left outer join mdstore_current_versions cv on (v.id = cv.current_version) where v.writing = false and v.readcount = 0 and cv.mdstore is null; " ,
String . class ) ;
}
@Transactional
@RequestMapping ( value = " /version/{versionId} " , method = RequestMethod . DELETE )
public void deleteVersion ( @PathVariable final String versionId ) throws MDStoreManagerException {
_deleteVersion ( versionId ) ;
}
@Transactional
@RequestMapping ( value = " /versions/deleteList " , method = RequestMethod . POST )
public void deleteVersions ( @RequestBody final List < String > versions ) throws MDStoreManagerException {
for ( final String v : versions ) {
_deleteVersion ( v ) ;
2019-03-18 15:30:07 +01:00
}
}
2019-03-20 15:36:23 +01:00
private void _deleteVersion ( final String versionId ) throws MDStoreManagerException {
final MDStoreVersion v = mdstoreVersionRepository . findById ( versionId ) . orElseThrow ( ( ) - > new MDStoreManagerException ( " Version not found " ) ) ;
if ( v . isWriting ( ) ) { throw new MDStoreManagerException ( " I cannot delete this version because it is in write mode " ) ; }
if ( v . getReadCount ( ) > 0 ) { throw new MDStoreManagerException ( " I cannot delete this version because it is in read mode " ) ; }
if ( mdstoreCurrentVersionRepository
. countByCurrentVersion ( versionId ) > 0 ) { throw new MDStoreManagerException ( " I cannot delete this version because it is the current version " ) ; }
mdstoreVersionRepository . delete ( v ) ;
}
2019-03-18 15:30:07 +01:00
}