forked from slifty/OpenHMIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoCManager.java
191 lines (136 loc) · 6.44 KB
/
CoCManager.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
package org.openhmis.manager;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.openhmis.dao.TmpProjectContinuumDAO;
import org.openhmis.domain.TmpProjectContinuum;
import org.openhmis.dto.ClientDTO;
import org.openhmis.dto.CoCDTO;
public class CoCManager {
private static final TmpProjectContinuumDAO tmpProjectContinuumDAO = new TmpProjectContinuumDAO();
public CoCManager() {}
public static CoCDTO getCoCByProjectCoCId(String projectCoCId) {
Integer projectCoCIdInt = Integer.parseInt(projectCoCId);
TmpProjectContinuum tmpProjectContinuum= tmpProjectContinuumDAO.getTmpProjectContinuumById(projectCoCIdInt);
CoCDTO CoCDTO = CoCManager.generateCoCDTO(tmpProjectContinuum);
return CoCDTO;
}
public static List<CoCDTO> getCoCs() {
List<CoCDTO> coCDTOs = new ArrayList<CoCDTO>();
// Collect the projects
List<TmpProjectContinuum> tmpProjectContinuums = tmpProjectContinuumDAO.getTmpProjectContinuums();
// For each project, collect and map the data
// TODO: this should be done in a single query
for (Iterator<TmpProjectContinuum> iterator = tmpProjectContinuums.iterator(); iterator.hasNext();) {
TmpProjectContinuum tmpProjectContinuum = iterator.next();
CoCDTO coCDTO = CoCManager.generateCoCDTO(tmpProjectContinuum);
coCDTOs.add(coCDTO);
}
return coCDTOs;
}
public static List<CoCDTO> getCoCs(Date updateDate) {
List<CoCDTO> coCDTOs = new ArrayList<CoCDTO>();
// Collect the projects
List<TmpProjectContinuum> tmpProjectContinuums = tmpProjectContinuumDAO.getTmpProjectContinuums(updateDate);
// For each project, collect and map the data
// TODO: this should be done in a single query
for (Iterator<TmpProjectContinuum> iterator = tmpProjectContinuums.iterator(); iterator.hasNext();) {
TmpProjectContinuum tmpProjectContinuum = iterator.next();
CoCDTO coCDTO = CoCManager.generateCoCDTO(tmpProjectContinuum);
coCDTOs.add(coCDTO);
}
return coCDTOs;
}
public static List<CoCDTO> getCoCsByProjectId(String projectId) {
List<CoCDTO> coCDTOs = new ArrayList<CoCDTO>();
// Collect the projects
List<TmpProjectContinuum> tmpProjectContinuums = tmpProjectContinuumDAO.getTmpProjectContinuumsByProjectId(Integer.parseInt(projectId));
// For each project, collect and map the data
// TODO: this should be done in a single query
for (Iterator<TmpProjectContinuum> iterator = tmpProjectContinuums.iterator(); iterator.hasNext();) {
TmpProjectContinuum tmpProjectContinuum = iterator.next();
CoCDTO coCDTO = CoCManager.generateCoCDTO(tmpProjectContinuum);
coCDTOs.add(coCDTO);
}
return coCDTOs;
}
public static List<CoCDTO> getCoCsByProjectId(String projectId, Date updateDate) {
List<CoCDTO> coCDTOs = new ArrayList<CoCDTO>();
// Collect the projects
List<TmpProjectContinuum> tmpProjectContinuums = tmpProjectContinuumDAO.getTmpProjectContinuumsByProjectId(Integer.parseInt(projectId), updateDate);
// For each project, collect and map the data
// TODO: this should be done in a single query
for (Iterator<TmpProjectContinuum> iterator = tmpProjectContinuums.iterator(); iterator.hasNext();) {
TmpProjectContinuum tmpProjectContinuum = iterator.next();
CoCDTO coCDTO = CoCManager.generateCoCDTO(tmpProjectContinuum);
coCDTOs.add(coCDTO);
}
return coCDTOs;
}
public static CoCDTO addCoC(CoCDTO inputDTO) {
// Validate the CoC
// TODO: this should return a list of errors that get wrapped appropriately
if(!validateCoC(inputDTO))
return null;
// Generate a CoC from the input
TmpProjectContinuum tmpProjectContinuum = CoCManager.generateTmpProjectContinuum(inputDTO);
// Set Export fields
tmpProjectContinuum.setDateCreated(new Date());
tmpProjectContinuum.setDateUpdated(new Date());
// Save the client to allow secondary object generation
tmpProjectContinuumDAO.save(tmpProjectContinuum);
inputDTO.setProjectCoCId(tmpProjectContinuum.getProjectCocId().toString());
// Return the resulting VO
return CoCManager.generateCoCDTO(tmpProjectContinuum);
}
public static CoCDTO updateCoC(CoCDTO inputDTO) {
// Generate a TmpProject from the input
TmpProjectContinuum tmpProjectContinuum = CoCManager.generateTmpProjectContinuum(inputDTO);
// Validate the CoC
// TODO: this should return a list of errors that get wrapped appropriately
if(!validateCoC(inputDTO))
return null;
tmpProjectContinuum.setProjectCocId(Integer.parseInt(inputDTO.getProjectCoCId()));
tmpProjectContinuum.setDateUpdated(new Date());
// Update the client
tmpProjectContinuumDAO.update(tmpProjectContinuum);
// Return the resulting VO
return CoCManager.generateCoCDTO(tmpProjectContinuum);
}
public static boolean deleteCoC(String projectCoCId) {
TmpProjectContinuum tmpProjectContinuum = tmpProjectContinuumDAO.getTmpProjectContinuumById(Integer.parseInt(projectCoCId));
tmpProjectContinuumDAO.delete(tmpProjectContinuum);
return true;
}
public static boolean validateCoC(CoCDTO inputDTO) {
// There really aren't fields to validate right now.
return true;
}
public static CoCDTO generateCoCDTO(TmpProjectContinuum tmpProjectContinuum) {
Integer projectCoCId = tmpProjectContinuum.getProjectCocId();
CoCDTO coCDTO = new CoCDTO();
coCDTO.setProjectCoCId(tmpProjectContinuum.getProjectCocId().toString());
coCDTO.setProjectId(tmpProjectContinuum.getProjectId().toString());
// Universal Data Standard: Project Identifiers (2014, 2.2)
coCDTO.setCoCCode(tmpProjectContinuum.getCocCode());
// Universal Data Standard: Inventories (2014, 2.7)
coCDTO.setInventories(InventoryManager.getInventoriesByProjectCoCId(projectCoCId.toString()));
// Universal Data Standard: Sites (2014, 2.8)
coCDTO.setSites(SiteManager.getSitesByProjectCoCId(projectCoCId.toString()));
// Export Standard Fields
coCDTO.setDateCreated(tmpProjectContinuum.getDateCreated());
coCDTO.setDateUpdated(tmpProjectContinuum.getDateUpdated());
return coCDTO;
}
public static TmpProjectContinuum generateTmpProjectContinuum(CoCDTO inputDTO) {
TmpProjectContinuum tmpProjectContinuum = new TmpProjectContinuum();
tmpProjectContinuum.setProjectId(Integer.parseInt(inputDTO.getProjectId()));
// Universal Data Standard: Project Identifiers (2014, 2.2)
tmpProjectContinuum.setCocCode(inputDTO.getCoCCode());
// Export Standard Fields
tmpProjectContinuum.setDateCreated(inputDTO.getDateCreated());
tmpProjectContinuum.setDateUpdated(inputDTO.getDateUpdated());
return tmpProjectContinuum;
}
}