View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  }