1 package org.kuali.kpme.edo.admin.web;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.StringReader;
6 import java.lang.reflect.Type;
7 import java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.LinkedList;
10 import java.util.List;
11 import java.util.Map;
12
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16 import org.apache.commons.collections.CollectionUtils;
17 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
18 import org.apache.commons.fileupload.servlet.ServletFileUpload;
19 import org.apache.cxf.common.util.StringUtils;
20 import org.apache.log4j.Logger;
21 import org.apache.struts.action.ActionForm;
22 import org.apache.struts.action.ActionForward;
23 import org.apache.struts.action.ActionMapping;
24 import org.apache.struts.upload.FormFile;
25 import org.kuali.kpme.edo.api.group.EdoGroupTracking;
26 import org.kuali.kpme.edo.api.reviewlayerdef.EdoReviewLayerDefinition;
27 import org.kuali.kpme.edo.base.web.EdoAction;
28 import org.kuali.kpme.edo.group.EdoGroup;
29 import org.kuali.kpme.edo.service.EdoServiceLocator;
30 import org.kuali.kpme.edo.util.EdoConstants;
31 import org.kuali.kpme.edo.util.EdoRule;
32 import org.kuali.rice.kim.api.group.Group;
33 import org.kuali.rice.kim.api.identity.principal.Principal;
34 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
35 import org.kuali.rice.krad.util.GlobalVariables;
36 import org.kuali.rice.krad.util.MessageMap;
37 import org.kuali.rice.krad.util.ObjectUtils;
38
39 import com.google.gson.Gson;
40 import com.google.gson.reflect.TypeToken;
41
42
43
44
45
46
47
48
49
50
51 public class EdoAdminGroupsAction extends EdoAction {
52 private static final Logger LOG = Logger.getLogger(EdoGenAdminAction.class);
53
54 @Override
55 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
56
57 EdoAdminGroupsForm groupsForm = (EdoAdminGroupsForm) form;
58
59
60 return super.execute(mapping, form, request, response);
61 }
62
63 public ActionForward uploadDeptCSV(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
64
65 EdoAdminGroupsForm groupsForm = (EdoAdminGroupsForm) form;
66 MessageMap msgmap = GlobalVariables.getMessageMap();
67
68 FormFile uploadFile = groupsForm.getUploadGrpFile();
69
70 if (ServletFileUpload.isMultipartContent(request)) {
71 DiskFileItemFactory factory = new DiskFileItemFactory();
72 factory.setSizeThreshold(EdoConstants.FILE_UPLOAD_PARAMETERS.THRESHHOLD_SIZE);
73 factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
74
75 ServletFileUpload upload = new ServletFileUpload(factory);
76 upload.setFileSizeMax(EdoConstants.FILE_UPLOAD_PARAMETERS.MAX_FILE_SIZE);
77 upload.setSizeMax(EdoConstants.FILE_UPLOAD_PARAMETERS.REQUEST_SIZE);
78
79 int uploadSize = uploadFile.getFileSize();
80
81
82 if (uploadSize < 1) {
83 LOG.error("File size is zero.");
84 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.fileUpload.empty");
85 ActionForward fwd = mapping.findForward("basic");
86 return fwd;
87 }
88
89 String CSVfile = new String(uploadFile.getFileData(), "UTF-8");
90 ((EdoAdminGroupsForm) form).setUnitList(CSVfile);
91 }
92 ActionForward fwd = mapping.findForward("basic");
93
94 return fwd;
95 }
96
97 public ActionForward uploadMbrCSV(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
98
99 EdoAdminGroupsForm groupsForm = (EdoAdminGroupsForm) form;
100 MessageMap msgmap = GlobalVariables.getMessageMap();
101
102 FormFile uploadFile = groupsForm.getUploadMbrFile();
103
104 if (ServletFileUpload.isMultipartContent(request)) {
105 DiskFileItemFactory factory = new DiskFileItemFactory();
106 factory.setSizeThreshold(EdoConstants.FILE_UPLOAD_PARAMETERS.THRESHHOLD_SIZE);
107 factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
108
109 ServletFileUpload upload = new ServletFileUpload(factory);
110 upload.setFileSizeMax(EdoConstants.FILE_UPLOAD_PARAMETERS.MAX_FILE_SIZE);
111 upload.setSizeMax(EdoConstants.FILE_UPLOAD_PARAMETERS.REQUEST_SIZE);
112
113 int uploadSize = uploadFile.getFileSize();
114
115
116 if (uploadSize < 1) {
117 LOG.error("File size is zero.");
118 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.fileUpload.empty");
119 ActionForward fwd = mapping.findForward("basic");
120 return fwd;
121 }
122
123 String CSVfile = new String(uploadFile.getFileData(), "UTF-8");
124 ((EdoAdminGroupsForm) form).setGroupMembers(CSVfile);
125
126 request.setAttribute("groupNameTitle", ((EdoAdminGroupsForm) form).getAddToGroupName());
127 request.setAttribute("groupJSON", ((EdoAdminGroupsForm) form).getGroupJSON());
128 }
129 ActionForward fwd = mapping.findForward("basic");
130
131 return fwd;
132 }
133
134 public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
135
136 MessageMap msgmap = GlobalVariables.getMessageMap();
137 Map<String,String> unitListHash = new HashMap<String, String>();
138
139 request.setAttribute("foundEntries", false);
140 String unitList = "";
141 String groupList = "";
142 String groupJSON = "";
143
144 String deptCode = ((EdoAdminGroupsForm) form).getDepartmentCode();
145 String schoolCode = ((EdoAdminGroupsForm) form).getSchoolCode();
146 String campusCode = ((EdoAdminGroupsForm) form).getCampusCode();
147 String workflowId = EdoConstants.EDO_DEFAULT_WORKFLOW_ID;
148
149 if (StringUtils.isEmpty(deptCode)) {
150 deptCode = "%";
151 }
152 if (StringUtils.isEmpty(schoolCode)) {
153 schoolCode = "%";
154 }
155 if (StringUtils.isEmpty(campusCode)) {
156 campusCode = "%";
157 }
158 List<EdoGroupTracking> foundEntries = EdoServiceLocator.getEdoGroupTrackingService().findEdoGroupTrackingEntries(deptCode, schoolCode, campusCode);
159 if (CollectionUtils.isNotEmpty(foundEntries)) {
160 request.setAttribute("foundEntries", true);
161 for (EdoGroupTracking groupTracking : foundEntries) {
162 String unitString = "";
163 String tmpDeptId = "";
164 String tmpSchoolId = "";
165 String tmpCampusId = "";
166 if (!StringUtils.isEmpty(groupTracking.getDepartmentId()) ) {
167 tmpDeptId = groupTracking.getDepartmentId();
168 }
169 unitString = unitString.concat(tmpDeptId);
170 unitString = unitString.concat(",");
171
172 if (!StringUtils.isEmpty(groupTracking.getOrganizationCode())) {
173 tmpSchoolId = groupTracking.getOrganizationCode();
174 }
175 unitString = unitString.concat(tmpSchoolId);
176 unitString = unitString.concat(",");
177
178 if (!StringUtils.isEmpty(groupTracking.getGroupKey().getCampusCode())) {
179 tmpCampusId = groupTracking.getGroupKey().getCampusCode();
180 }
181 unitString = unitString.concat(tmpCampusId);
182 unitString = unitString.concat(String.format("%n"));
183
184 unitListHash.put(unitString,"1");
185
186 String revLevelDescription = "";
187 EdoReviewLayerDefinition rld = EdoServiceLocator.getEdoReviewLayerDefinitionService().getReviewLayerDefinition(workflowId, groupTracking.getReviewLevelName());
188 if (ObjectUtils.isNotNull(rld)) {
189 revLevelDescription = rld.getDescription();
190 }
191 groupList = groupList.concat(groupTracking.getGroupName());
192 groupList = groupList.concat(",");
193 groupList = groupList.concat(revLevelDescription);
194 groupList = groupList.concat(String.format("%n"));
195 List<String> gList = new LinkedList<String>();
196 gList.add(groupTracking.getGroupName());
197 gList.add(revLevelDescription);
198 groupJSON = groupJSON.concat(getStringListJSONString(gList) + ",");
199 }
200 for (String key : unitListHash.keySet()) {
201 unitList = unitList.concat(key);
202 }
203
204 ((EdoAdminGroupsForm) form).setUnitList(unitList);
205 ((EdoAdminGroupsForm) form).setGroupList(groupList);
206 ((EdoAdminGroupsForm) form).setGroupJSON(groupJSON);
207
208 request.setAttribute("groupJSON", groupJSON);
209 } else {
210 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.search.empty");
211 }
212
213 return mapping.findForward("basic");
214 }
215
216 public ActionForward addToList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
217
218 MessageMap msgmap = GlobalVariables.getMessageMap();
219 String groupString = "";
220 String currentUnitList = ((EdoAdminGroupsForm) form).getUnitList();
221 String deptCode = ((EdoAdminGroupsForm) form).getDepartmentCode();
222 String schoolCode = ((EdoAdminGroupsForm) form).getSchoolCode();
223 String campusCode = ((EdoAdminGroupsForm) form).getCampusCode();
224 String workflowId = EdoConstants.EDO_DEFAULT_WORKFLOW_ID;
225
226 if (StringUtils.isEmpty(currentUnitList)) {
227 currentUnitList = "";
228 }
229
230 if (StringUtils.isEmpty(deptCode)) {
231 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.search.empty");
232 return mapping.findForward("basic");
233 }
234 if (StringUtils.isEmpty(schoolCode)) {
235 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.search.empty");
236 return mapping.findForward("basic");
237 }
238 if (StringUtils.isEmpty(campusCode)) {
239 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.search.empty");
240 return mapping.findForward("basic");
241 }
242
243 if (deptCode.contains("%") || deptCode.contains("*")) {
244 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.add.nowildcard");
245 return mapping.findForward("basic");
246 }
247 if (schoolCode.contains("%") || schoolCode.contains("*")) {
248 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.add.nowildcard");
249 return mapping.findForward("basic");
250 }
251 if (campusCode.contains("%") || campusCode.contains("*")) {
252 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.add.nowildcard");
253 return mapping.findForward("basic");
254 }
255
256 groupString = groupString.concat(deptCode + ",");
257 groupString = groupString.concat(schoolCode + ",");
258 groupString = groupString.concat(campusCode + String.format("%n"));
259
260
261
262 ((EdoAdminGroupsForm) form).setUnitList(currentUnitList + String.format("%n") + groupString);
263
264 return mapping.findForward("basic");
265 }
266
267 public ActionForward buildList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
268
269 MessageMap msgmap = GlobalVariables.getMessageMap();
270 EdoAdminGroupsForm groupsForm = (EdoAdminGroupsForm) form;
271 List<EdoGroup> grpList = new LinkedList<EdoGroup>();
272 String groupListing = "";
273 String groupJSON = "";
274
275 String instCode = groupsForm.getInstitutionCode();
276 String workflowId = EdoConstants.EDO_DEFAULT_WORKFLOW_ID;
277
278 BufferedReader rdr = new BufferedReader(new StringReader(groupsForm.getUnitList()));
279 List<String> lines = new ArrayList<String>();
280 for (String line = rdr.readLine(); line != null; line = rdr.readLine()) {
281 lines.add(line);
282 }
283 rdr.close();
284
285
286 if (lines.get(0).equals("DEPARTMENTCODE")) {
287 lines.remove(0);
288 }
289
290 for (String line : lines) {
291 boolean isValid = true;
292 String[] codes = line.split(",");
293 if (codes.length != 3) {
294 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.import.line", line);
295 LOG.info("Import line does not match expected values: " + line);
296 continue;
297 }
298 if (!EdoRule.validateDepartmentId(codes[0]) ) {
299 isValid = false;
300 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.validate.code", "Department ID", line);
301 LOG.info("Invalid department ID: " + codes[0]);
302 }
303 if (!EdoRule.validateSchoolId(codes[1]) ) {
304 isValid = false;
305 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.validate.code", "School ID", line);
306 LOG.info("Invalid school ID: " + codes[1]);
307 }
308 if (!EdoRule.validateCampusId(codes[2]) ) {
309 isValid = false;
310 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.validate.code", "Campus ID", line);
311 LOG.info("Invalid campus ID: " + codes[2]);
312 }
313 if (isValid) {
314 grpList.addAll(EdoServiceLocator.getEdoGroupService().createKimGroups(workflowId, codes[0], codes[1], codes[2], instCode));
315
316 }
317 }
318
319 for (EdoGroup grp : grpList) {
320 EdoGroupTracking grpTracking = EdoServiceLocator.getEdoGroupTrackingService().getEdoGroupTrackingByGroupName(grp.getGroupName());
321 EdoReviewLayerDefinition rld = EdoServiceLocator.getEdoReviewLayerDefinitionService().getReviewLayerDefinition(workflowId, grpTracking.getReviewLevelName());
322
323 groupListing = groupListing.concat(grp.getGroupName() + "," + rld.getDescription() + String.format("%n"));
324 List<String> gList = new LinkedList<String>();
325 gList.add(grp.getGroupName());
326 gList.add(rld.getDescription());
327
328 groupJSON = groupJSON.concat(getStringListJSONString(gList) + ",");
329 EdoServiceLocator.getEdoGroupService().addGroupToRole(grp);
330
331 }
332 groupsForm.setGroupList(groupListing);
333 request.setAttribute("groupJSON", groupJSON);
334
335 return mapping.findForward("basic");
336 }
337
338 public ActionForward getGroupMembers(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
339 EdoAdminGroupsForm cliForm = (EdoAdminGroupsForm) form;
340
341 String list = "";
342 Group grp = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(EdoConstants.EDO_NAME_SPACE, request.getParameter("name"));
343 List<String> members = EdoServiceLocator.getEdoGroupService().findGroupMembers(grp.getId());
344 List<String> usernames = new LinkedList<String>();
345
346 for (String mbr : members) {
347 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(mbr);
348 usernames.add(principal.getPrincipalName());
349 list = list.concat(principal.getPrincipalName() + String.format("%n"));
350 }
351
352 cliForm.setGroupMembersJSON(list);
353
354 return mapping.findForward("ws");
355 }
356
357 public ActionForward addMembersToGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
358 MessageMap msgmap = GlobalVariables.getMessageMap();
359 EdoAdminGroupsForm groupsForm = (EdoAdminGroupsForm) form;
360 List<String> successful = new LinkedList<String>();
361
362 String groupJSON = groupsForm.getGroupJSON();
363 String groupName = groupsForm.getAddToGroupName();
364
365 BufferedReader rdr = new BufferedReader(new StringReader(groupsForm.getGroupMembers()));
366 List<String> lines = new ArrayList<String>();
367 for (String line = rdr.readLine(); line != null; line = rdr.readLine()) {
368 lines.add(line);
369 }
370 rdr.close();
371
372 for (String line : lines) {
373 if (line.matches(".*[\\W].*")) {
374 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.import.invalid.char", line);
375 LOG.info("Import line has non-valid characters: " + line);
376 continue;
377 }
378 String[] user = line.split(",");
379 if (user.length != 1) {
380 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.import.line", line);
381 LOG.info("Import line does not match expected values: " + line);
382 continue;
383 }
384
385 if (!StringUtils.isEmpty(user[0])) {
386 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(user[0]);
387
388 if (ObjectUtils.isNull(principal)) {
389 LOG.info("No such principal: " + user[0]);
390 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.addmember.noprincipal", user[0]);
391 continue;
392 }
393 Group grp = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(EdoConstants.EDO_NAME_SPACE, groupName);
394 if (ObjectUtils.isNull(grp)) {
395 LOG.info("No such group " + groupName);
396 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.addmember.nogroup", groupName);
397 continue;
398 }
399 boolean isMember = EdoServiceLocator.getEdoGroupService().isMemberOfGroup(principal.getPrincipalName(), groupName);
400 if (!isMember) {
401 boolean success = EdoServiceLocator.getEdoGroupService().addMemberToGroup(principal.getPrincipalId(), grp.getId());
402 if (success) {
403 successful.add(principal.getPrincipalName());
404 } else {
405 msgmap.putError(EdoConstants.ErrorKeys.ERROR_KEYS, "error.group.search.empty", user[0], groupsForm.getAddToGroupName());
406 }
407 }
408 }
409 }
410
411 request.setAttribute("groupJSON", groupJSON);
412 request.setAttribute("groupNameTitle", groupName);
413 request.setAttribute("addsuccess", successful.toString());
414
415 return mapping.findForward("basic");
416 }
417
418 private String getGroupJSONString(String groupName) {
419 ArrayList<String> tmp = new ArrayList<String>();
420 Type tmpType = new TypeToken<List<String>>() {}.getType();
421 Gson gson = new Gson();
422
423 tmp.add(groupName);
424
425 return gson.toJson(tmp, tmpType).toString();
426 }
427
428 private String getStringListJSONString(List<String> stringList) {
429 Gson gson = new Gson();
430
431 return gson.toJson(stringList).toString();
432 }
433
434 }