View Javadoc
1   /*
2    * Copyright 2007 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.ole.sys.businessobject.lookup;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.regex.Pattern;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.ole.sys.OLEConstants;
26  import org.kuali.ole.sys.batch.BatchJobStatus;
27  import org.kuali.ole.sys.batch.service.SchedulerService;
28  import org.kuali.ole.sys.context.SpringContext;
29  import org.kuali.ole.sys.service.impl.OleModuleServiceImpl;
30  import org.kuali.rice.core.api.config.property.ConfigurationService;
31  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
32  import org.kuali.rice.kim.api.KimConstants;
33  import org.kuali.rice.kim.api.services.IdentityManagementService;
34  import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions;
35  import org.kuali.rice.kns.lookup.HtmlData;
36  import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
37  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
38  import org.kuali.rice.krad.bo.BusinessObject;
39  import org.kuali.rice.krad.service.KualiModuleService;
40  import org.kuali.rice.krad.util.GlobalVariables;
41  import org.kuali.rice.krad.util.KRADConstants;
42  import org.kuali.rice.krad.util.UrlFactory;
43  
44  public class BatchJobStatusLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
45  
46      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BatchJobStatusLookupableHelperServiceImpl.class);
47  
48      private SchedulerService schedulerService;
49      private ConfigurationService configurationService;
50      private ParameterService parameterService;
51      private KualiModuleService kualiModuleService;
52      private IdentityManagementService identityManagementService;
53  
54      @Override
55      public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
56          super.setBackLocation(fieldValues.get(OLEConstants.BACK_LOCATION));
57          super.setDocFormKey(fieldValues.get(OLEConstants.DOC_FORM_KEY));
58          List<BatchJobStatus> allJobs = schedulerService.getJobs();
59          List<BatchJobStatus> jobs = new ArrayList<BatchJobStatus>();
60  
61          String namespaceCode = fieldValues.get("namespaceCode");
62          String nameValue = fieldValues.get("name");
63          Pattern namePattern = null;
64          if (!StringUtils.isEmpty(nameValue)) {
65              namePattern = Pattern.compile(nameValue.replace("*", ".*"), Pattern.CASE_INSENSITIVE);
66          }
67          String schedulerGroup = fieldValues.get("group");
68          String jobStatus = fieldValues.get("status");
69          for (BatchJobStatus job : allJobs) {
70              if (!StringUtils.isEmpty(namespaceCode) &&
71                      (!namespaceCode.equalsIgnoreCase(job.getNamespaceCode()) && job.getNamespaceCode()!=null)) {
72                  continue;
73              }
74              if (namePattern != null && !namePattern.matcher(job.getName()).matches()) {
75                  continue; // match failed, skip this entry
76              }
77              if (!StringUtils.isEmpty(schedulerGroup) && !schedulerGroup.equalsIgnoreCase(job.getGroup())) {
78                  continue;
79              }
80              if (!StringUtils.isEmpty(jobStatus) && !jobStatus.equalsIgnoreCase(job.getStatus())) {
81                  continue;
82              }
83              jobs.add(job);
84          }
85  
86          return jobs;
87      }
88  
89      public boolean doesModuleServiceHaveJobStatus(BatchJobStatus job){
90          if(job!=null) {
91              OleModuleServiceImpl moduleService = (OleModuleServiceImpl)getKualiModuleService().getResponsibleModuleServiceForJob(job.getName());
92              //This means this job is externalized and we do not want to show any action urls for it.
93              return (moduleService!=null && moduleService.isExternalJob(job.getName()));
94          }
95          return false;
96      }
97  
98      /***
99       * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.krad.bo.BusinessObject, java.util.List)
100      */
101     @Override
102     public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
103         if (businessObject instanceof BatchJobStatus) {
104             BatchJobStatus job = (BatchJobStatus) businessObject;
105             if(doesModuleServiceHaveJobStatus(job)) {
106                 return getEmptyActionUrls();
107             }
108             String linkText = "Modify";
109             Map<String,String> permissionDetails = new HashMap<String,String>(1);
110             permissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, job.getNamespaceCode() );
111 
112             if ( !SpringContext.getBean(IdentityManagementService.class).hasPermissionByTemplateName(
113                     GlobalVariables.getUserSession().getPerson().getPrincipalId(),
114                     KRADConstants.KNS_NAMESPACE,
115                     OLEConstants.PermissionTemplate.MODIFY_BATCH_JOB.name,
116                     permissionDetails ) ) {
117                 linkText = "View";
118             }
119             String href = configurationService.getPropertyValueAsString(OLEConstants.APPLICATION_URL_KEY) + "/batchModify.do?methodToCall=start&name="+(UrlFactory.encode(job.getName()))+("&group=")+(UrlFactory.encode(job.getGroup()));
120             List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
121             AnchorHtmlData anchorHtmlData = new AnchorHtmlData(href, OLEConstants.START_METHOD, linkText);
122             anchorHtmlDataList.add(anchorHtmlData);
123             return anchorHtmlDataList;
124         }
125         return getEmptyActionUrls();
126     }
127 
128     /***
129      * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getActionUrlTitleText(org.kuali.rice.krad.bo.BusinessObject, java.lang.String, java.util.List)
130      */
131     @Override
132     protected String getActionUrlTitleText(BusinessObject businessObject, String displayText, List pkNames, BusinessObjectRestrictions businessObjectRestrictions){
133         BatchJobStatus job = (BatchJobStatus) businessObject;
134         String titleText = displayText+" "
135             +getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(getBusinessObjectClass().getName()).getObjectLabel()
136             +" "
137             +configurationService.getPropertyValueAsString(TITLE_ACTION_URL_PREPENDTEXT_PROPERTY);
138         titleText += "Name="+job.getName()+" Group="+job.getGroup();
139         return titleText;
140     }
141 
142     public void setSchedulerService(SchedulerService schedulerService) {
143         this.schedulerService = schedulerService;
144     }
145 
146     @Override
147     public void setParameterService(ParameterService parameterService) {
148         this.parameterService = parameterService;
149     }
150 
151     public void setConfigurationService(ConfigurationService configurationService) {
152         this.configurationService = configurationService;
153     }
154 
155     public KualiModuleService getKualiModuleService() {
156         if ( kualiModuleService == null ) {
157             kualiModuleService = SpringContext.getBean(KualiModuleService.class);
158         }
159         return kualiModuleService;
160     }
161 
162     public IdentityManagementService getIdentityManagementService() {
163         if ( identityManagementService == null ) {
164             identityManagementService = SpringContext.getBean(IdentityManagementService.class);
165         }
166         return identityManagementService;
167     }
168 
169 }
170