1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.core.batch;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.joda.time.DateTime;
24 import org.joda.time.format.DateTimeFormat;
25 import org.joda.time.format.DateTimeFormatter;
26 import org.kuali.kpme.core.api.KPMEConstants;
27 import org.kuali.rice.core.api.config.property.ConfigContext;
28 import org.kuali.rice.kim.api.identity.principal.Principal;
29 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30
31 public class BatchJobUtil {
32
33 public static final DateTimeFormatter FORMAT = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
34
35 public static String getJobGroupName(Class<?> jobClass, Map<String, String> jobGroupDataMap) {
36 return jobClass.getSimpleName() + "-JobGroup-" + getDataMapString(jobGroupDataMap);
37 }
38
39 public static String getJobName(Class<?> jobClass, Map<String, String> jobDataMap) {
40 return jobClass.getSimpleName() + "-Job-" + getDataMapString(jobDataMap);
41 }
42
43 public static String getTriggerGroupName(Class<?> jobClass, Map<String, String> triggerDataMap) {
44 return jobClass.getSimpleName() + "-TriggerGroup-" + getDataMapString(triggerDataMap);
45 }
46
47 public static String getTriggerName(Class<?> jobClass, DateTime jobDate) {
48 return jobClass.getSimpleName() + "-Trigger-" + "date=" + FORMAT.print(jobDate);
49 }
50
51 private static String getDataMapString(Map<String, String> dataMap) {
52 List<String> dataMapPairs = new ArrayList<String>();
53 for (Map.Entry<String, String> dataMapEntry : dataMap.entrySet()) {
54 dataMapPairs.add(dataMapEntry.getKey() + "=" + dataMapEntry.getValue());
55 }
56 return StringUtils.join(dataMapPairs, "&");
57 }
58
59 public static String getBatchUserPrincipalName() {
60 return ConfigContext.getCurrentContextConfig().getProperty(KPMEConstants.BATCH_USER_PRINCIPAL_NAME);
61 }
62
63 public static String getBatchUserPrincipalId() {
64 String principalName = ConfigContext.getCurrentContextConfig().getProperty(KPMEConstants.BATCH_USER_PRINCIPAL_NAME);
65 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
66 return principal == null ? null : principal.getPrincipalId();
67 }
68
69 }