1
|
package eu.dnetlib.goldoa.service.eligibility;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.Eligibility;
|
4
|
import eu.dnetlib.goldoa.domain.Project;
|
5
|
import eu.dnetlib.goldoa.domain.Request;
|
6
|
import eu.dnetlib.goldoa.service.EligibilityManager;
|
7
|
import eu.dnetlib.goldoa.service.ProjectManager;
|
8
|
import eu.dnetlib.goldoa.service.RequestManager;
|
9
|
import org.apache.commons.logging.Log;
|
10
|
import org.apache.commons.logging.LogFactory;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
import org.springframework.beans.factory.annotation.Value;
|
13
|
import org.springframework.stereotype.Component;
|
14
|
|
15
|
import java.util.Date;
|
16
|
import java.util.List;
|
17
|
|
18
|
/*
|
19
|
* Created by antleb on 3/9/15.
|
20
|
*/
|
21
|
@Component(value = "projectEligibilityManager")
|
22
|
public class ProjectEligibiliyManager implements EligibilityManager {
|
23
|
|
24
|
@Autowired
|
25
|
private ProjectManager projectManager;
|
26
|
@Autowired
|
27
|
private RequestManager requestManager;
|
28
|
|
29
|
@Value("${goldoa.eligibility.project.expiration.limit}")
|
30
|
private long expirationOk;
|
31
|
@Value("${goldoa.eligibility.project.expiration.threshold}")
|
32
|
private float expirationThreshold;
|
33
|
@Value("${goldoa.eligibility.project.expiration.iffy.message}")
|
34
|
private String expirationIffy;
|
35
|
@Value("${goldoa.eligibility.project.expiration.nono.message}")
|
36
|
private String expirationNono;
|
37
|
@Value("${goldoa.eligibility.project.stillRunning.nono.message}")
|
38
|
private String stillRunningNono;
|
39
|
@Value("${goldoa.eligibility.project.request.count}")
|
40
|
private int maxRequests;
|
41
|
@Value("${goldoa.eligibility.project.request.nono.message}")
|
42
|
private String maxRequestsNono;
|
43
|
|
44
|
private Log log = LogFactory.getLog(ProjectEligibiliyManager.class);
|
45
|
|
46
|
@Override
|
47
|
public Eligibility validate(Request request) {
|
48
|
Eligibility eligibility = new Eligibility();
|
49
|
|
50
|
if (request.getProject() != null) {
|
51
|
Project project = request.getProject();
|
52
|
|
53
|
eligibility.merge(validateExpirationDate(project));
|
54
|
eligibility.merge(validateNumberOfRequests(project));
|
55
|
|
56
|
}
|
57
|
return eligibility;
|
58
|
}
|
59
|
|
60
|
private Eligibility validateNumberOfRequests(Project project) {
|
61
|
List<Request> requests = requestManager.getForProject(project.getId());
|
62
|
int completed = 0;
|
63
|
|
64
|
for (Request request : requests) {
|
65
|
switch (request.getStatus()) {
|
66
|
case CONDITIONALLY_APPROVED:
|
67
|
case APPROVED:
|
68
|
case ACCOUNTING_PROCESSING:
|
69
|
case ACCOUNTING_ON_HOLD:
|
70
|
case ACCOUNTING_PAID:
|
71
|
completed++;
|
72
|
break;
|
73
|
}
|
74
|
}
|
75
|
|
76
|
if (completed >= maxRequests)
|
77
|
return new Eligibility(Eligibility.Status.NONO, maxRequestsNono);
|
78
|
|
79
|
return Eligibility.OK;
|
80
|
}
|
81
|
|
82
|
private Eligibility validateExpirationDate(Project project) {
|
83
|
|
84
|
Date enddate = new Date(project.getEnddate().getTime());
|
85
|
|
86
|
if (!enddate.before(new Date())) {
|
87
|
return new Eligibility(Eligibility.Status.NONO, stillRunningNono);
|
88
|
} else if (enddate.after(new Date(System.currentTimeMillis() - expirationOk * 1000L * 60 * 60 * 24 * 30))) {
|
89
|
return Eligibility.OK;
|
90
|
} else if (enddate.after(new Date((long) (System.currentTimeMillis() - (1.0f + expirationThreshold) * expirationOk)))) {
|
91
|
return new Eligibility(Eligibility.Status.IFFY, expirationIffy);
|
92
|
} else {
|
93
|
return new Eligibility(Eligibility.Status.NONO, expirationNono);
|
94
|
}
|
95
|
|
96
|
// try {
|
97
|
// Date date = new SimpleDateFormat("dd/MM/yyyy").parse("01/01/2013");
|
98
|
//
|
99
|
// if (project.getEndDate().after(date)) {
|
100
|
// if (project.getEndDate().before(new Date()))
|
101
|
// return new Eligibility(Eligibility.Status.OK);
|
102
|
// else
|
103
|
// return new Eligibility(Eligibility.Status.NONO, stillRunningNono);
|
104
|
// } else
|
105
|
// return new Eligibility(Eligibility.Status.NONO, expirationNono);
|
106
|
// } catch (ParseException e) {
|
107
|
// e.printStackTrace();
|
108
|
//
|
109
|
// return Eligibility.OK;
|
110
|
// }
|
111
|
}
|
112
|
|
113
|
public ProjectManager getProjectManager() {
|
114
|
return projectManager;
|
115
|
}
|
116
|
|
117
|
public void setProjectManager(ProjectManager projectManager) {
|
118
|
this.projectManager = projectManager;
|
119
|
}
|
120
|
|
121
|
public RequestManager getRequestManager() {
|
122
|
return requestManager;
|
123
|
}
|
124
|
|
125
|
public void setRequestManager(RequestManager requestManager) {
|
126
|
this.requestManager = requestManager;
|
127
|
}
|
128
|
|
129
|
public long getExpirationOk() {
|
130
|
return expirationOk;
|
131
|
}
|
132
|
|
133
|
public void setExpirationOk(Long expirationOk) {
|
134
|
this.expirationOk = (expirationOk * 1000L * 60 * 60 * 24 * 30);
|
135
|
}
|
136
|
|
137
|
public float getExpirationThreshold() {
|
138
|
return expirationThreshold;
|
139
|
}
|
140
|
|
141
|
public void setExpirationThreshold(float expirationThreshold) {
|
142
|
this.expirationThreshold = expirationThreshold;
|
143
|
}
|
144
|
|
145
|
public String getExpirationIffy() {
|
146
|
return expirationIffy;
|
147
|
}
|
148
|
|
149
|
public void setExpirationIffy(String expirationIffy) {
|
150
|
this.expirationIffy = expirationIffy;
|
151
|
}
|
152
|
|
153
|
public String getExpirationNono() {
|
154
|
return expirationNono;
|
155
|
}
|
156
|
|
157
|
public void setExpirationNono(String expirationNono) {
|
158
|
this.expirationNono = expirationNono;
|
159
|
}
|
160
|
|
161
|
public int getMaxRequests() {
|
162
|
return maxRequests;
|
163
|
}
|
164
|
|
165
|
public void setMaxRequests(int maxRequests) {
|
166
|
this.maxRequests = maxRequests;
|
167
|
}
|
168
|
|
169
|
public String getMaxRequestsNono() {
|
170
|
return maxRequestsNono;
|
171
|
}
|
172
|
|
173
|
public void setMaxRequestsNono(String maxRequestsNono) {
|
174
|
this.maxRequestsNono = maxRequestsNono;
|
175
|
}
|
176
|
|
177
|
public String getStillRunningNono() {
|
178
|
return stillRunningNono;
|
179
|
}
|
180
|
|
181
|
public void setStillRunningNono(String stillRunningNono) {
|
182
|
this.stillRunningNono = stillRunningNono;
|
183
|
}
|
184
|
}
|