@ -6,7 +6,6 @@ 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 ;
@ -18,7 +17,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 ing.service.PingService ;
import ru.ulstu.p roject.model.Project ;
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 ;
@ -27,7 +26,6 @@ import ru.ulstu.user.service.UserService;
import java.io.IOException ;
import java.io.IOException ;
import java.text.ParseException ;
import java.text.ParseException ;
import java.time.temporal.ChronoUnit ;
import java.util.Arrays ;
import java.util.Arrays ;
import java.util.Collections ;
import java.util.Collections ;
import java.util.Date ;
import java.util.Date ;
@ -54,7 +52,6 @@ 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 ,
@ -64,8 +61,7 @@ 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 ;
@ -76,11 +72,6 @@ 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 ( ) {
@ -91,13 +82,17 @@ 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 Grant create ( GrantDto grantDto ) throws IOException {
public Integer 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 . createFromObject ( newGrant , Collections . emptyList ( ) , false , "гранта" ) ;
eventService . createFromObject ( newGrant , Collections . emptyList ( ) , false , "гранта" ) ;
grantNotificationService . sendCreateNotification ( newGrant ) ;
grantNotificationService . sendCreateNotification ( newGrant ) ;
return newGrant ;
return newGrant .getId ( ) ;
}
}
private Grant copyFromDto ( Grant grant , GrantDto grantDto ) throws IOException {
private Grant copyFromDto ( Grant grant , GrantDto grantDto ) throws IOException {
@ -108,11 +103,9 @@ 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 ( ) ) ) ;
if ( ! grant . getFiles ( ) . isEmpty ( ) ) {
grant . setFiles ( fileService . saveOrCreate ( grantDto . getFiles ( ) . stream ( )
grant . setFiles ( fileService . saveOrCreate ( grantDto . getFiles ( ) . stream ( )
. filter ( f - > ! f . isDeleted ( ) )
. filter ( f - > ! f . isDeleted ( ) )
. collect ( toList ( ) ) ) ) ;
. 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 ) ) ) ;
@ -127,15 +120,14 @@ 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 rivate Integer update ( GrantDto grantDto ) throws IOException {
p ublic Integer update ( GrantDto grantDto ) throws IOException {
Grant grant = findById ( grantDto . getId ( ) ) ;
Grant grant = grantRepository. findOne ( 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 ( )
@ -164,25 +156,40 @@ public class GrantService extends BaseService {
}
}
@Transactional
@Transactional
public boolean delete ( Integer grantId ) throws IOException {
public void delete ( Integer grantId ) throws IOException {
Grant grant = findById ( grantId ) ;
Grant grant = grantRepository . findOne ( grantId ) ;
if ( grant ! = null ) {
grantRepository . delete ( grant ) ;
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 . createFromObject ( grant , Collections . emptyList ( ) , false , "гранта" ) ;
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 ( ) , "Грант с таким именем уже существует" ) ;
checkUniqueName ( grantDto , errors , grantDto . getId ( ) , " title", " Грант с таким именем уже существует") ;
if ( errors . hasErrors ( ) ) {
if ( errors . hasErrors ( ) ) {
return false ;
return false ;
}
}
@ -194,11 +201,11 @@ public class GrantService extends BaseService {
return true ;
return true ;
}
}
p rivate boolean saveFromKias ( GrantDto grantDto ) throws IOException {
p ublic 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 ) {
Grant grantFromDB = grantRepository . find First ByTitle( title ) ; //грант с таким же названием из бд
Grant grantFromDB = grantRepository . find ByTitle( title ) ; //грант с таким же названием из бд
if ( checkSameDeadline ( grantDto , grantFromDB . getId ( ) ) ) { //если дедайны тоже совпадают
if ( checkSameDeadline ( grantDto , grantFromDB . getId ( ) ) ) { //если дедайны тоже совпадают
return false ;
return false ;
} else { //иначе грант уже был в системе, но в другом году, поэтому надо создать
} else { //иначе грант уже был в системе, но в другом году, поэтому надо создать
@ -224,8 +231,8 @@ public class GrantService extends BaseService {
}
}
private boolean checkSameDeadline ( GrantDto grantDto , Integer id ) {
private boolean checkSameDeadline ( GrantDto grantDto , Integer id ) {
Date date = DateUtils. clearTime ( grantDto. getDeadlines ( ) . get ( 0 ) . getDate ( ) ) ; //дата с сайта киас
Date date = grantDto. getDeadlines ( ) . get ( 0 ) . getDate ( ) ; //дата с сайта киас
Date foundGrantDate = DateUtils. clearTime ( deadlineService. findByGrantIdAndDate ( id , date ) ) ;
Date foundGrantDate = deadlineService. findByGrantIdAndDate ( id , date ) ;
return foundGrantDate ! = null & & foundGrantDate . compareTo ( date ) = = 0 ;
return foundGrantDate ! = null & & foundGrantDate . compareTo ( date ) = = 0 ;
}
}
@ -264,22 +271,20 @@ public class GrantService extends BaseService {
return paperService . findAllNotCompleted ( ) ;
return paperService . findAllNotCompleted ( ) ;
}
}
public List < PaperDto > attachPaper ( GrantDto grantDto ) {
public void 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 GrantDto removeDeadline ( GrantDto grantDto , Integer deadlineId ) {
public void 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 ) {
@ -305,11 +310,10 @@ public class GrantService extends BaseService {
. collect ( toList ( ) ) ;
. collect ( toList ( ) ) ;
}
}
public List < Deadline > filterEmptyDeadlines ( GrantDto grantDto ) {
public void filterEmptyDeadlines ( GrantDto grantDto ) {
grantDto . setDeadlines ( grantDto . getDeadlines ( ) . stream ( )
grantDto . setDeadlines ( grantDto . getDeadlines ( ) . stream ( )
. filter ( dto - > dto . getDate ( ) ! = null | | ! StringUtils. isEmpty ( dto . getDescription ( ) ) )
. filter ( dto - > dto . getDate ( ) ! = null | | ! org. springframework . util . StringUtils. isEmpty ( dto . getDescription ( ) ) )
. collect ( Collectors . toList ( ) ) ) ;
. collect ( Collectors . toList ( ) ) ) ;
return grantDto . getDeadlines ( ) ;
}
}
@Transactional
@Transactional
@ -327,77 +331,11 @@ public class GrantService extends BaseService {
return convert ( findAllActive ( ) , GrantDto : : new ) ;
return convert ( findAllActive ( ) , GrantDto : : new ) ;
}
}
p ublic List < Grant > findAllActive ( ) {
p rivate List < Grant > findAllActive ( ) {
return grantRepository . findAllActive ( ) ;
return grantRepository . findAllActive ( ) ;
}
}
public Grant findById ( Integer id ) {
public Grant findGrantById ( Integer grantId ) {
return grantRepository . getOne ( id ) ;
return grantRepository . findOne ( grantId ) ;
}
@Transactional
public void ping ( int grantId ) throws IOException {
pingService . addPing ( findById ( grantId ) ) ;
}
public void save ( Grant grant ) {
if ( isEmpty ( grant . getId ( ) ) ) {
create ( grant ) ;
} else {
update ( grant ) ;
}
}
@Transactional
public Grant create ( Grant grant ) {
Grant newGrant = grantRepository . save ( grant ) ;
grantNotificationService . sendCreateNotification ( newGrant ) ;
return newGrant ;
}
@Transactional
public Integer update ( Grant newGrant ) {
Grant oldGrant = grantRepository . getOne ( newGrant . getId ( ) ) ;
//Grant.GrantStatus oldStatus = oldGrant.getStatus();
Set < User > oldAuthors = new HashSet < > ( oldGrant . getAuthors ( ) ) ;
newGrant = grantRepository . save ( newGrant ) ;
for ( User author : newGrant . getAuthors ( ) ) {
if ( ! oldAuthors . contains ( author ) ) {
grantNotificationService . sendCreateNotification ( newGrant ) ;
}
}
// if (newGrant.getStatus() != oldStatus) {
// grantNotificationService.statusChangeNotification(newPaper, oldStatus);
// }
return newGrant . getId ( ) ;
}
public void createByTitle ( String newGrantTitle ) {
Grant grant = new Grant ( ) ;
grant . setTitle ( newGrantTitle ) ;
grant . setStatus ( APPLICATION ) ;
grant . getAuthors ( ) . add ( userService . getCurrentUser ( ) ) ;
grant . setLeader ( userService . getCurrentUser ( ) ) ;
grant . getDeadlines ( ) . add ( deadlineService . createWithOffset ( new Date ( ) , 1 , ChronoUnit . WEEKS ) ) ;
create ( grant ) ;
}
public List < Grant > findAllActiveByCurrentUser ( ) {
return findAllActive ( )
. stream ( )
. filter ( grant - > grant . getAuthors ( ) . contains ( userService . getCurrentUser ( ) ) | |
grant . getLeader ( ) . equals ( userService . getCurrentUser ( ) ) )
. collect ( toList ( ) ) ;
}
public void delete ( List < Grant > grants ) {
grants . forEach ( grant - > delete ( grant ) ) ;
}
public void delete ( Grant grant ) {
deadlineService . delete ( grant . getDeadlines ( ) ) ;
grantRepository . delete ( grant ) ;
}
}
}
}