-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReceptionService.java
245 lines (210 loc) · 9.43 KB
/
ReceptionService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.github.adminfaces.starter.service;
import com.github.adminfaces.starter.bean.AccTypesFormMB;
import com.github.adminfaces.starter.infra.model.Filter;
import com.github.adminfaces.starter.infra.model.SortOrder;
import com.github.adminfaces.starter.infra.security.LogonMB;
//import com.github.adminfaces.starter.infra.security.LogonMB;
import com.github.adminfaces.starter.model.Reception;
import com.github.adminfaces.starter.util.Db;
import com.github.adminfaces.template.config.AdminConfig;
import com.github.adminfaces.template.exception.BusinessException;
import javax.ejb.Stateless;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import java.io.Serializable;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static com.github.adminfaces.template.util.Assert.has;
/**
* @author rmpestano
* Reception Business logic
*/
@Stateless
public class ReceptionService implements Serializable {
@Inject
List<Reception> allPatients;
@Inject
private LogonMB logonMB;
protected PreparedStatement ps;
protected Statement stmt;
protected ResultSet rs;
private static Logger logger = Logger.getLogger(ReceptionService.class);
public String CurrentTime() {
return new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
}
public List<Reception> listByModel(String names) {
return allPatients.stream()
.filter(c -> c.getNames().equalsIgnoreCase(names))
.collect(Collectors.toList());
}
public List<Reception> paginate(Filter<Reception> filter) {
List<Reception> pagedPatients = new ArrayList<>();
if(has(filter.getSortOrder()) && !SortOrder.UNSORTED.equals(filter.getSortOrder())) {
pagedPatients = allPatients.stream().
sorted((c1, c2) -> {
if (filter.getSortOrder().isAscending()) {
return c1.getOpno().compareTo(c2.getOpno());
} else {
return c2.getOpno().compareTo(c1.getOpno());
}
})
.collect(Collectors.toList());
}
int page = filter.getFirst() + filter.getPageSize();
if (filter.getParams().isEmpty()) {
pagedPatients = pagedPatients.subList(filter.getFirst(), page > allPatients.size() ? allPatients.size() : page);
return pagedPatients;
}
List<Predicate<Reception>> predicates = configFilter(filter);
List<Reception> pagedList = allPatients.stream().filter(predicates
.stream().reduce(Predicate::or).orElse(t -> true))
.collect(Collectors.toList());
if (page < pagedList.size()) {
pagedList = pagedList.subList(filter.getFirst(), page);
}
if (has(filter.getSortField())) {
pagedList = pagedList.stream().
sorted((c1, c2) -> {
boolean asc = SortOrder.ASCENDING.equals(filter.getSortOrder());
if (asc) {
return c1.getOpno().compareTo(c2.getOpno());
} else {
return c2.getOpno().compareTo(c1.getOpno());
}
})
.collect(Collectors.toList());
}
return pagedList;
}
private List<Predicate<Reception>> configFilter(Filter<Reception> filter) {
List<Predicate<Reception>> predicates = new ArrayList<>();
if (filter.hasParam("id")) {
Predicate<Reception> idPredicate = (Reception c) -> c.getOpno().equals(filter.getParam("id"));
predicates.add(idPredicate);
}
if (has(filter.getEntity())) {
Reception filterEntity = filter.getEntity();
if (has(filterEntity.getOpno())) {
Predicate<Reception> opnoPredicate = (Reception c) -> c.getOpno().equals(filterEntity.getOpno());
predicates.add(opnoPredicate);
}
if (has(filterEntity.getNames())) {
Predicate<Reception> namesPredicate = (Reception c) -> c.getNames().toLowerCase().contains(filterEntity.getNames().toLowerCase());
predicates.add(namesPredicate);
}
}
return predicates;
}
public List<String> getOpno(String query) {
return allPatients.stream().filter(c -> c.getOpno()
.toLowerCase().contains(query.toLowerCase()))
.map(Reception::getOpno)
.collect(Collectors.toList());
}
public List<String> getNames(String query) {
return allPatients.stream().filter(c -> c.getNames()
.toLowerCase().contains(query.toLowerCase()))
.map(Reception::getNames)
.collect(Collectors.toList());
}
public void insert(Reception reception) {
validate(reception);
Db.connect();
try {
String IP = Inet4Address.getLocalHost().getHostAddress();
String acctypename = reception.getNames();
int userid= logonMB.getCurrentUserId();
logger.info(CurrentTime() + "|" + "Create New User Account Type Process" + "|" + "INFO" + "|" + "Processing - Create New User Account Type " +
"|" + acctypename + " |" + userid + "|" + IP + "|" + "jdbc:mysql://localhost/temis" + "|" + "Begin Creating User Account Type");
String sql = "INSERT INTO `tbl_temis_account_types`(name,date_created,created_by) VALUES (?,?,?)";
ps= Db.conn.prepareStatement(sql);
ps.setString(1, acctypename);//names
ps.setTimestamp(2, Db.getCurrentTimeStamp());
ps.setInt(3, userid);//received_by
int i = ps.executeUpdate();
//logger.error("reception.getNames()"+reception.getNames()+"reception.getNames()");
allPatients.add(reception);
logger.info(CurrentTime() + "|" + "Create New User Account Type Process" + "|" + "INFO" + "|" + "Finished - Create New User Account Type " +
"|" + acctypename + " |" + userid + "|" + IP + "|" + "jdbc:mysql://localhost/temis" + "|" + "Finished Creating User Account Type");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(e.getMessage());
}
Db.disconnect();
}
public void validate(Reception reception) {
BusinessException be = new BusinessException();
if (!has(reception.getNames())) {
be.addException(new BusinessException("Patient Name Cannot be empty"));
}
if (allPatients.stream().filter(c -> c.getNames().equalsIgnoreCase(reception.getNames())
&& c.getNames() != c.getNames()).count() > 0) {
be.addException(new BusinessException("Patient name must be unique"));
}
if(has(be.getExceptionList())) {
throw be;
}
}
public int deletePatient(Reception reception)
{
Db.connect();
int ret = 0;
try {
String IP = Inet4Address.getLocalHost().getHostAddress();
String acctypename = reception.getNames();
int tid = Integer.valueOf(reception.getOpno());
int userid= logonMB.getCurrentUserId();
logger.info(CurrentTime() + "|" + "Delete User Account Type Process" + "|" + "INFO" + "|" + "Processing - Delete User Account Type " +
"|" + acctypename + " |" + userid + "|" + IP + "|" + "jdbc:mysql://localhost/temis" + "|" + "Begin Deleting User Account Type");
String sqldelete = "DELETE FROM tbl_temis_account_types WHERE tid= "+tid;
logger.error("sqldelete"+sqldelete);
stmt = Db.conn.createStatement();
stmt.executeUpdate(sqldelete);
logger.info(CurrentTime() + "|" + "Delete User Account Type Process" + "|" + "INFO" + "|" + "Finished - Delete User Account Type " +
"|" + acctypename + " |" + userid + "|" + IP + "|" + "jdbc:mysql://localhost/temis" + "|" + "Finished Delete User Account Type");
} catch (Exception e) {
// TODO Auto-generated catch block
ret = 1;
e.printStackTrace();
logger.error(e.getStackTrace());
}
Db.disconnect();
return ret;
}
public void remove(Reception reception) {
deletePatient(reception);
allPatients.remove(reception);
}
public long count(Filter<Reception> filter) {
return allPatients.stream()
.filter(configFilter(filter).stream()
.reduce(Predicate::or).orElse(t -> true))
.count();
}
public Reception findByOpno(String opno) {
return allPatients.stream()
.filter(c -> c.getOpno().equals(opno))
.findFirst()
.orElseThrow(() -> new BusinessException("Patient not found with patient code# " + opno));
}
public void update(Reception reception) {
validate(reception);
allPatients.remove(allPatients.indexOf(reception));
allPatients.add(reception);
}
}