@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.validation.Errors ;
import org.springframework.validation.Errors ;
import ru.ulstu.core.util.DateUtils ;
import ru.ulstu.deadline.model.Deadline ;
import ru.ulstu.deadline.model.Deadline ;
import ru.ulstu.deadline.service.DeadlineService ;
import ru.ulstu.deadline.service.DeadlineService ;
import ru.ulstu.file.model.FileDataDto ;
import ru.ulstu.file.model.FileDataDto ;
@ -17,7 +18,7 @@ import ru.ulstu.name.BaseService;
import ru.ulstu.paper.model.Paper ;
import ru.ulstu.paper.model.Paper ;
import ru.ulstu.paper.model.PaperDto ;
import ru.ulstu.paper.model.PaperDto ;
import ru.ulstu.paper.service.PaperService ;
import ru.ulstu.paper.service.PaperService ;
import ru.ulstu.p roject.model.Project ;
import ru.ulstu.p ing.service.PingService ;
import ru.ulstu.project.model.ProjectDto ;
import ru.ulstu.project.model.ProjectDto ;
import ru.ulstu.project.service.ProjectService ;
import ru.ulstu.project.service.ProjectService ;
import ru.ulstu.timeline.service.EventService ;
import ru.ulstu.timeline.service.EventService ;
@ -52,6 +53,7 @@ public class GrantService extends BaseService {
private final EventService eventService ;
private final EventService eventService ;
private final GrantNotificationService grantNotificationService ;
private final GrantNotificationService grantNotificationService ;
private final KiasService kiasService ;
private final KiasService kiasService ;
private final PingService pingService ;
public GrantService ( GrantRepository grantRepository ,
public GrantService ( GrantRepository grantRepository ,
FileService fileService ,
FileService fileService ,
@ -61,7 +63,8 @@ public class GrantService extends BaseService {
PaperService paperService ,
PaperService paperService ,
EventService eventService ,
EventService eventService ,
GrantNotificationService grantNotificationService ,
GrantNotificationService grantNotificationService ,
KiasService kiasService ) {
KiasService kiasService ,
PingService pingService ) {
this . grantRepository = grantRepository ;
this . grantRepository = grantRepository ;
this . kiasService = kiasService ;
this . kiasService = kiasService ;
this . baseRepository = grantRepository ;
this . baseRepository = grantRepository ;
@ -72,6 +75,11 @@ public class GrantService extends BaseService {
this . paperService = paperService ;
this . paperService = paperService ;
this . eventService = eventService ;
this . eventService = eventService ;
this . grantNotificationService = grantNotificationService ;
this . grantNotificationService = grantNotificationService ;
this . pingService = pingService ;
}
public GrantDto getExistGrantById ( Integer id ) {
return new GrantDto ( findById ( id ) ) ;
}
}
public List < Grant > findAll ( ) {
public List < Grant > findAll ( ) {
@ -82,17 +90,13 @@ public class GrantService extends BaseService {
return convert ( findAll ( ) , GrantDto : : new ) ;
return convert ( findAll ( ) , GrantDto : : new ) ;
}
}
public GrantDto findOneDto ( Integer id ) {
return new GrantDto ( grantRepository . findOne ( id ) ) ;
}
@Transactional
@Transactional
public Integer create ( GrantDto grantDto ) throws IOException {
public Grant create ( GrantDto grantDto ) throws IOException {
Grant newGrant = copyFromDto ( new Grant ( ) , grantDto ) ;
Grant newGrant = copyFromDto ( new Grant ( ) , grantDto ) ;
newGrant = grantRepository . save ( newGrant ) ;
newGrant = grantRepository . save ( newGrant ) ;
eventService . createFrom Grant( newGrant ) ;
eventService . createFrom Object( newGrant , Collections . emptyList ( ) , false , "гранта" ) ;
grantNotificationService . sendCreateNotification ( newGrant ) ;
grantNotificationService . sendCreateNotification ( newGrant ) ;
return newGrant .getId ( ) ;
return newGrant ;
}
}
private Grant copyFromDto ( Grant grant , GrantDto grantDto ) throws IOException {
private Grant copyFromDto ( Grant grant , GrantDto grantDto ) throws IOException {
@ -103,9 +107,11 @@ public class GrantService extends BaseService {
grant . setProject ( projectService . findById ( grantDto . getProject ( ) . getId ( ) ) ) ;
grant . setProject ( projectService . findById ( grantDto . getProject ( ) . getId ( ) ) ) ;
}
}
grant . setDeadlines ( deadlineService . saveOrCreate ( grantDto . getDeadlines ( ) ) ) ;
grant . setDeadlines ( deadlineService . saveOrCreate ( grantDto . getDeadlines ( ) ) ) ;
grant . setFiles ( fileService . saveOrCreate ( grantDto . getFiles ( ) . stream ( )
if ( ! grant . getFiles ( ) . isEmpty ( ) ) {
. filter ( f - > ! f . isDeleted ( ) )
grant . setFiles ( fileService . saveOrCreate ( grantDto . getFiles ( ) . stream ( )
. collect ( toList ( ) ) ) ) ;
. filter ( f - > ! f . isDeleted ( ) )
. collect ( toList ( ) ) ) ) ;
}
grant . getAuthors ( ) . clear ( ) ;
grant . getAuthors ( ) . clear ( ) ;
if ( grantDto . getAuthorIds ( ) ! = null & & ! grantDto . getAuthorIds ( ) . isEmpty ( ) ) {
if ( grantDto . getAuthorIds ( ) ! = null & & ! grantDto . getAuthorIds ( ) . isEmpty ( ) ) {
grantDto . getAuthorIds ( ) . forEach ( authorIds - > grant . getAuthors ( ) . add ( userService . findById ( authorIds ) ) ) ;
grantDto . getAuthorIds ( ) . forEach ( authorIds - > grant . getAuthors ( ) . add ( userService . findById ( authorIds ) ) ) ;
@ -120,14 +126,15 @@ public class GrantService extends BaseService {
return grant ;
return grant ;
}
}
public void createProject ( GrantDto grantDto ) throws IOException {
public void createProject ( GrantDto grantDto ) throws IOException {
grantDto . setProject (
grantDto . setProject (
new ProjectDto ( projectService . save ( new ProjectDto ( grantDto . getTitle ( ) ) ) ) ) ;
new ProjectDto ( projectService . save ( new ProjectDto ( grantDto . getTitle ( ) ) ) ) ) ;
}
}
@Transactional
@Transactional
p ublic Integer update ( GrantDto grantDto ) throws IOException {
p rivate Integer update ( GrantDto grantDto ) throws IOException {
Grant grant = grantRepository. findOne ( grantDto . getId ( ) ) ;
Grant grant = findById ( grantDto . getId ( ) ) ;
Set < User > oldAuthors = new HashSet < > ( grant . getAuthors ( ) ) ;
Set < User > oldAuthors = new HashSet < > ( grant . getAuthors ( ) ) ;
User oldLeader = grant . getLeader ( ) ;
User oldLeader = grant . getLeader ( ) ;
for ( FileDataDto file : grantDto . getFiles ( ) . stream ( )
for ( FileDataDto file : grantDto . getFiles ( ) . stream ( )
@ -156,40 +163,25 @@ public class GrantService extends BaseService {
}
}
@Transactional
@Transactional
public void delete ( Integer grantId ) throws IOException {
public boolean delete ( Integer grantId ) throws IOException {
Grant grant = grantRepository . findOne ( grantId ) ;
Grant grant = findById ( grantId ) ;
grantRepository . delete ( grant ) ;
if ( grant ! = null ) {
grantRepository . delete ( grant ) ;
return true ;
}
return false ;
}
}
public List < Grant . GrantStatus > getGrantStatuses ( ) {
public List < Grant . GrantStatus > getGrantStatuses ( ) {
return Arrays . asList ( Grant . GrantStatus . values ( ) ) ;
return Arrays . asList ( Grant . GrantStatus . values ( ) ) ;
}
}
@Transactional
public Grant create ( String title , Project projectId , Date deadlineDate , User user , Paper paper ) {
Grant grant = new Grant ( ) ;
grant . setTitle ( title ) ;
grant . setComment ( "Комментарий к гранту 1" ) ;
grant . setProject ( projectId ) ;
grant . setStatus ( APPLICATION ) ;
grant . getDeadlines ( ) . add ( new Deadline ( deadlineDate , "первый дедлайн" ) ) ;
grant . getAuthors ( ) . add ( user ) ;
grant . setLeader ( user ) ;
grant . getPapers ( ) . add ( paper ) ;
grant = grantRepository . save ( grant ) ;
eventService . createFromGrant ( grant ) ;
grantNotificationService . sendCreateNotification ( grant ) ;
return grant ;
}
public boolean save ( GrantDto grantDto , Errors errors ) throws IOException {
public boolean save ( GrantDto grantDto , Errors errors ) throws IOException {
grantDto . setName ( grantDto . getTitle ( ) ) ;
grantDto . setName ( grantDto . getTitle ( ) ) ;
filterEmptyDeadlines ( grantDto ) ;
filterEmptyDeadlines ( grantDto ) ;
checkEmptyDeadlines ( grantDto , errors ) ;
checkEmptyDeadlines ( grantDto , errors ) ;
checkEmptyLeader ( grantDto , errors ) ;
checkEmptyLeader ( grantDto , errors ) ;
checkUniqueName ( grantDto , errors , grantDto . getId ( ) , " title", " Грант с таким именем уже существует") ;
checkUniqueName ( grantDto , errors , grantDto . getId ( ) , "Грант с таким именем уже существует" ) ;
if ( errors . hasErrors ( ) ) {
if ( errors . hasErrors ( ) ) {
return false ;
return false ;
}
}
@ -201,7 +193,7 @@ public class GrantService extends BaseService {
return true ;
return true ;
}
}
p ublic boolean saveFromKias ( GrantDto grantDto ) throws IOException {
p rivate boolean saveFromKias ( GrantDto grantDto ) throws IOException {
grantDto . setName ( grantDto . getTitle ( ) ) ;
grantDto . setName ( grantDto . getTitle ( ) ) ;
String title = checkUniqueName ( grantDto , grantDto . getId ( ) ) ; //проверка уникальности имени
String title = checkUniqueName ( grantDto , grantDto . getId ( ) ) ; //проверка уникальности имени
if ( title ! = null ) {
if ( title ! = null ) {
@ -231,11 +223,9 @@ public class GrantService extends BaseService {
}
}
private boolean checkSameDeadline ( GrantDto grantDto , Integer id ) {
private boolean checkSameDeadline ( GrantDto grantDto , Integer id ) {
Date date = grantDto . getDeadlines ( ) . get ( 0 ) . getDate ( ) ; //дата с сайта киас
Date date = DateUtils . clearTime ( grantDto . getDeadlines ( ) . get ( 0 ) . getDate ( ) ) ; //дата с сайта киас
if ( deadlineService . findByGrantIdAndDate ( id , date ) . compareTo ( date ) = = 0 ) { //если есть такая строка с датой
Date foundGrantDate = DateUtils . clearTime ( deadlineService . findByGrantIdAndDate ( id , date ) ) ;
return true ;
return foundGrantDate ! = null & & foundGrantDate . compareTo ( date ) = = 0 ;
}
return false ;
}
}
public List < User > getGrantAuthors ( GrantDto grantDto ) {
public List < User > getGrantAuthors ( GrantDto grantDto ) {
@ -273,20 +263,22 @@ public class GrantService extends BaseService {
return paperService . findAllNotCompleted ( ) ;
return paperService . findAllNotCompleted ( ) ;
}
}
public void attachPaper ( GrantDto grantDto ) {
public List < PaperDto > attachPaper ( GrantDto grantDto ) {
if ( ! grantDto . getPaperIds ( ) . isEmpty ( ) ) {
if ( ! grantDto . getPaperIds ( ) . isEmpty ( ) ) {
grantDto . getPapers ( ) . clear ( ) ;
grantDto . getPapers ( ) . clear ( ) ;
grantDto . setPapers ( getGrantPapers ( grantDto . getPaperIds ( ) ) ) ;
grantDto . setPapers ( getGrantPapers ( grantDto . getPaperIds ( ) ) ) ;
} else {
} else {
grantDto . getPapers ( ) . clear ( ) ;
grantDto . getPapers ( ) . clear ( ) ;
}
}
return grantDto . getPapers ( ) ;
}
}
public void removeDeadline ( GrantDto grantDto , Integer deadlineId ) {
public GrantDto removeDeadline ( GrantDto grantDto , Integer deadlineId ) {
if ( grantDto . getDeadlines ( ) . get ( deadlineId ) . getId ( ) ! = null ) {
if ( grantDto . getDeadlines ( ) . get ( deadlineId ) . getId ( ) ! = null ) {
grantDto . getRemovedDeadlineIds ( ) . add ( grantDto . getDeadlines ( ) . get ( deadlineId ) . getId ( ) ) ;
grantDto . getRemovedDeadlineIds ( ) . add ( grantDto . getDeadlines ( ) . get ( deadlineId ) . getId ( ) ) ;
}
}
grantDto . getDeadlines ( ) . remove ( ( int ) deadlineId ) ;
grantDto . getDeadlines ( ) . remove ( ( int ) deadlineId ) ;
return grantDto ;
}
}
private List < User > getCompletedPapersAuthors ( Paper . PaperType type ) {
private List < User > getCompletedPapersAuthors ( Paper . PaperType type ) {
@ -312,10 +304,11 @@ public class GrantService extends BaseService {
. collect ( toList ( ) ) ;
. collect ( toList ( ) ) ;
}
}
public void filterEmptyDeadlines ( GrantDto grantDto ) {
public List < Deadline > filterEmptyDeadlines ( GrantDto grantDto ) {
grantDto . setDeadlines ( grantDto . getDeadlines ( ) . stream ( )
grantDto . setDeadlines ( grantDto . getDeadlines ( ) . stream ( )
. filter ( dto - > dto . getDate ( ) ! = null | | ! org. springframework . util . StringUtils. isEmpty ( dto . getDescription ( ) ) )
. filter ( dto - > dto . getDate ( ) ! = null | | ! StringUtils. isEmpty ( dto . getDescription ( ) ) )
. collect ( Collectors . toList ( ) ) ) ;
. collect ( Collectors . toList ( ) ) ) ;
return grantDto . getDeadlines ( ) ;
}
}
@Transactional
@Transactional
@ -333,7 +326,16 @@ public class GrantService extends BaseService {
return convert ( findAllActive ( ) , GrantDto : : new ) ;
return convert ( findAllActive ( ) , GrantDto : : new ) ;
}
}
p rivate List < Grant > findAllActive ( ) {
p ublic List < Grant > findAllActive ( ) {
return grantRepository . findAllActive ( ) ;
return grantRepository . findAllActive ( ) ;
}
}
public Grant findById ( Integer id ) {
return grantRepository . getOne ( id ) ;
}
@Transactional
public void ping ( int grantId ) throws IOException {
pingService . addPing ( findById ( grantId ) ) ;
}
}
}