1 package org.kuali.kpme.edo.admin.web;
2
3 import com.google.gson.Gson;
4 import com.google.gson.JsonArray;
5 import com.google.gson.JsonParser;
6
7 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
8 import org.apache.commons.fileupload.servlet.ServletFileUpload;
9 import org.apache.log4j.Logger;
10 import org.apache.struts.action.ActionForm;
11 import org.apache.struts.action.ActionForward;
12 import org.apache.struts.action.ActionMapping;
13 import org.apache.struts.upload.FormFile;
14 import org.kuali.kpme.edo.api.reviewlayerdef.EdoReviewLayerDefinition;
15 import org.kuali.kpme.edo.base.web.EdoAction;
16 import org.kuali.kpme.edo.group.EdoGroup;
17 import org.kuali.kpme.edo.reviewlayerdef.EdoReviewLayerDefinitionBo;
18 import org.kuali.kpme.edo.service.EdoServiceLocator;
19 import org.kuali.kpme.edo.util.EdoConstants;
20 import org.kuali.rice.kim.api.group.Group;
21 import org.kuali.rice.kim.api.identity.principal.Principal;
22 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
23 import org.kuali.rice.krad.util.GlobalVariables;
24 import org.kuali.rice.krad.util.MessageMap;
25 import org.kuali.rice.krad.util.ObjectUtils;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import java.io.BufferedReader;
31 import java.io.File;
32 import java.io.StringReader;
33 import java.math.BigDecimal;
34 import java.util.*;
35
36
37
38
39
40
41
42
43
44 public class EdoAdminGroupMembersAction extends EdoAction {
45 private static final Logger LOG = Logger.getLogger(EdoAdminGroupMembersAction.class);
46
47 @Override
48 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
49
50 EdoAdminGroupMembersForm adminForm = (EdoAdminGroupMembersForm)form;
51
52 adminForm.setDepartmentSelectList(EdoServiceLocator.getEdoGroupTrackingService().getDistinctDepartmentList());
53 adminForm.setSchoolSelectList(EdoServiceLocator.getEdoGroupTrackingService().getDistinctSchoolList());
54 adminForm.setCampusSelectList(EdoServiceLocator.getEdoGroupTrackingService().getDistinctCampusList());
55 adminForm.setReviewLevelSelectList(EdoServiceLocator.getEdoReviewLayerDefinitionService().getReviewLayerDefinitionDescriptionsByWorkflow(EdoConstants.EDO_DEFAULT_WORKFLOW_ID));
56
57 request.setAttribute("reviewLevelSelectList", EdoServiceLocator.getEdoReviewLayerDefinitionService().getReviewLayerDefinitionDescriptionsByWorkflow(EdoConstants.EDO_DEFAULT_WORKFLOW_ID));
58
59 return super.execute(mapping, form, request, response);
60 }
61
62 public ActionForward loadMemberData(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
63 EdoAdminGroupMembersForm adminForm = (EdoAdminGroupMembersForm)form;
64 MessageMap msgmap = GlobalVariables.getMessageMap();
65 List<List<String>> member_records = new ArrayList<List<String>>();
66 List<String> successfulMembers = new LinkedList<String>();
67
68 String memberData = adminForm.getMemberData();
69 Gson gson = new Gson();
70 JsonArray array = new JsonArray();
71 JsonParser parser = new JsonParser();
72
73 array = parser.parse(memberData).getAsJsonArray();
74 for (int i = 0; i < array.size(); i++) {
75 List<String> record = new ArrayList<String>();
76
77 JsonArray ar = array.get(i).getAsJsonArray();
78 if (ar.size() != 11 ) {
79 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.import.line", ar.getAsString());
80 continue;
81 }
82 for (int j = 0; j < ar.size(); j++) {
83 record.add(ar.get(j).getAsString());
84 }
85 member_records.add(record);
86 }
87
88 successfulMembers.addAll(processMemberRecords(member_records));
89
90
91 request.setAttribute("successfulMembers", successfulMembers);
92 return mapping.findForward("basic");
93 }
94
95 public ActionForward uploadMbrCSV(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
96
97 EdoAdminGroupMembersForm memberForm = (EdoAdminGroupMembersForm) form;
98 MessageMap msgmap = GlobalVariables.getMessageMap();
99
100 FormFile uploadFile = memberForm.getUploadMbrFile();
101
102 if (ServletFileUpload.isMultipartContent(request)) {
103 DiskFileItemFactory factory = new DiskFileItemFactory();
104 factory.setSizeThreshold(EdoConstants.FILE_UPLOAD_PARAMETERS.THRESHHOLD_SIZE);
105 factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
106
107 ServletFileUpload upload = new ServletFileUpload(factory);
108 upload.setFileSizeMax(EdoConstants.FILE_UPLOAD_PARAMETERS.MAX_FILE_SIZE);
109 upload.setSizeMax(EdoConstants.FILE_UPLOAD_PARAMETERS.REQUEST_SIZE);
110
111 int uploadSize = uploadFile.getFileSize();
112
113
114 if (uploadSize < 1) {
115 LOG.error("File size is zero.");
116 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.fileUpload.empty");
117 ActionForward fwd = mapping.findForward("basic");
118 return fwd;
119 }
120
121 String CSVfile = new String(uploadFile.getFileData(), "UTF-8");
122
123 BufferedReader rdr = new BufferedReader(new StringReader(CSVfile));
124 List<String> lines = new ArrayList<String>();
125 List<List<String>> member_records = new ArrayList<List<String>>();
126
127 for (String line = rdr.readLine(); line != null; line = rdr.readLine()) {
128 List<String> record = Arrays.asList(line.split(","));
129 member_records.add(record);
130 lines.add(line);
131 }
132 rdr.close();
133
134 member_records.remove(0);
135 List<List<String>> convertedMemberRecords = convertCSVRecordsToMemberRecords(member_records);
136 List<String> successfulMembers = processMemberRecords(convertedMemberRecords);
137
138 request.setAttribute("successfulMembers", successfulMembers);
139 }
140
141 ActionForward fwd = mapping.findForward("basic");
142
143 return fwd;
144 }
145
146 private List<List<String>> convertCSVRecordsToMemberRecords(List<List<String>> csv_records) {
147 List<List<String>> member_records = new ArrayList<List<String>>();
148 MessageMap msgmap = GlobalVariables.getMessageMap();
149
150 Collection<EdoReviewLayerDefinition> rlds = EdoServiceLocator.getEdoReviewLayerDefinitionService().getReviewLayerDefinitions();
151 Map<String,BigDecimal> rldMap = new HashMap<String, BigDecimal>();
152
153 for (EdoReviewLayerDefinition rld : rlds) {
154 if (rld.getVoteType().equals("None")) {
155 continue;
156 }
157 rldMap.put(rld.getDescription().toUpperCase(), new BigDecimal(rld.getEdoReviewLayerDefinitionId()));
158 }
159
160 for (List<String> csv_rec : csv_records) {
161 List<String> tmp_csv_rec = new ArrayList<String>();
162
163 String levelDescription = csv_rec.get(3).toUpperCase();
164 String tnpFlag = csv_rec.get(4).toUpperCase();
165 String chairFlag = csv_rec.get(5).toUpperCase();
166
167 String levelValue = rldMap.get(levelDescription).toString();
168 String tnpValue = null;
169 if (tnpFlag.equals("T") || tnpFlag.equals("TENURE")) {
170 tnpValue = "TENURE";
171 }
172 if (tnpFlag.equals("P") || tnpFlag.equals("PROMOTION")) {
173 tnpValue = "PROMOTION";
174 }
175 if (tnpValue.isEmpty()) {
176 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.import.invalid.char", csv_rec.toString());
177 continue;
178 }
179 String chairValue = "FYI";
180 if (chairFlag.equals("Y") || chairFlag.equals("YES")) {
181 chairValue = "APPROVE";
182 }
183 tmp_csv_rec.add(csv_rec.get(0));
184 tmp_csv_rec.add(csv_rec.get(1));
185 tmp_csv_rec.add(csv_rec.get(2));
186 tmp_csv_rec.add(levelValue);
187 tmp_csv_rec.add(csv_rec.get(3));
188 tmp_csv_rec.add(tnpValue);
189 tmp_csv_rec.add(csv_rec.get(4));
190 tmp_csv_rec.add(chairValue);
191 tmp_csv_rec.add(csv_rec.get(5));
192 tmp_csv_rec.add(csv_rec.get(6));
193 tmp_csv_rec.add("");
194 member_records.add(tmp_csv_rec);
195 }
196 return member_records;
197 }
198
199 private List<String> processMemberRecords(List<List<String>> member_records) {
200
201 List<String> successfulMembers = new LinkedList<String>();
202 MessageMap msgmap = GlobalVariables.getMessageMap();
203
204
205 for (List<String> rec : member_records) {
206
207 String username = rec.get(9);
208 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(username);
209
210 if (ObjectUtils.isNull(principal)) {
211 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.addmember.noprincipal", username);
212 continue;
213 }
214 EdoReviewLayerDefinition rld = EdoServiceLocator.getEdoReviewLayerDefinitionService().getReviewLayerDefinitionById(rec.get(3));
215 String nodeName = rld.getNodeName().toUpperCase();
216 BigDecimal reviewLevel = new BigDecimal(rld.getReviewLevel());
217 String dossierType = rec.get(5);
218 String workflowAction = rec.get(7);
219
220 String unitName = rec.get(0);
221
222
223 if (reviewLevel.compareTo(BigDecimal.valueOf(2)) == 1) {
224 unitName = rec.get(1);
225 }
226
227 if (reviewLevel.compareTo(BigDecimal.valueOf(4)) == 1) {
228 unitName = rec.get(2);
229 }
230 if (reviewLevel.compareTo(BigDecimal.valueOf(5)) == 1) {
231 dossierType = "";
232 }
233 String groupName = EdoServiceLocator.getEdoGroupService().buildGroupName(nodeName,workflowAction,dossierType,unitName);
234
235
236 List<EdoGroup> grpList = EdoServiceLocator.getEdoGroupService().createKimGroups(EdoConstants.EDO_DEFAULT_WORKFLOW_ID, rec.get(0), rec.get(1), rec.get(2), "IU");
237 Group grp = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(EdoConstants.EDO_NAME_SPACE,groupName);
238 if (ObjectUtils.isNotNull(grp)) {
239 if (!EdoServiceLocator.getEdoGroupService().addMemberToGroup(principal.getPrincipalId(),grp.getId())) {
240 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.addmember.error", principal.getPrincipalName(),grp.getName());
241 continue;
242 }
243 successfulMembers.add(principal.getPrincipalName());
244 } else {
245 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.addmember.nogroup", groupName);
246 }
247 }
248 return successfulMembers;
249 }
250 }