001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.sys.businessobject.lookup;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.Map;
021    import java.util.regex.Pattern;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.kuali.kfs.sys.KFSConstants;
025    import org.kuali.kfs.sys.batch.BatchJobStatus;
026    import org.kuali.kfs.sys.batch.service.SchedulerService;
027    import org.kuali.kfs.sys.context.SpringContext;
028    import org.kuali.kfs.sys.service.impl.KfsModuleServiceImpl;
029    import org.kuali.kfs.sys.service.impl.KfsParameterConstants;
030    import org.kuali.rice.kim.bo.impl.KimAttributes;
031    import org.kuali.rice.kim.bo.types.dto.AttributeSet;
032    import org.kuali.rice.kim.service.IdentityManagementService;
033    import org.kuali.rice.kim.service.KIMServiceLocator;
034    import org.kuali.rice.kns.authorization.BusinessObjectRestrictions;
035    import org.kuali.rice.kns.bo.BusinessObject;
036    import org.kuali.rice.kns.lookup.HtmlData;
037    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
038    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
039    import org.kuali.rice.kns.service.KNSServiceLocator;
040    import org.kuali.rice.kns.service.KualiConfigurationService;
041    import org.kuali.rice.kns.service.KualiModuleService;
042    import org.kuali.rice.kns.service.ParameterService;
043    import org.kuali.rice.kns.util.GlobalVariables;
044    import org.kuali.rice.kns.util.KNSConstants;
045    import org.kuali.rice.kns.util.UrlFactory;
046    
047    public class BatchJobStatusLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
048    
049        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BatchJobStatusLookupableHelperServiceImpl.class);
050    
051        private SchedulerService schedulerService;
052        private KualiConfigurationService configurationService;
053        private ParameterService parameterService;
054        private KualiModuleService kualiModuleService;
055        private IdentityManagementService identityManagementService;
056    
057        @Override
058        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
059            super.setBackLocation((String) fieldValues.get(KFSConstants.BACK_LOCATION));
060            super.setDocFormKey((String) fieldValues.get(KFSConstants.DOC_FORM_KEY));
061            List<BatchJobStatus> allJobs = schedulerService.getJobs();
062            List<BatchJobStatus> jobs = new ArrayList<BatchJobStatus>();
063    
064            String namespaceCode = fieldValues.get("namespaceCode");
065            String nameValue = fieldValues.get("name");
066            Pattern namePattern = null;
067            if (!StringUtils.isEmpty(nameValue)) {
068                namePattern = Pattern.compile(nameValue.replace("*", ".*"), Pattern.CASE_INSENSITIVE);
069            }
070            String schedulerGroup = fieldValues.get("group");
071            String jobStatus = fieldValues.get("status");
072            for (BatchJobStatus job : allJobs) {
073                if (!StringUtils.isEmpty(namespaceCode) &&
074                        (!namespaceCode.equalsIgnoreCase(job.getNamespaceCode()) && job.getNamespaceCode()!=null)) {
075                    continue;
076                }
077                if (namePattern != null && !namePattern.matcher(job.getName()).matches()) {
078                    continue; // match failed, skip this entry
079                }
080                if (!StringUtils.isEmpty(schedulerGroup) && !schedulerGroup.equalsIgnoreCase(job.getGroup())) {
081                    continue;
082                }
083                if (!StringUtils.isEmpty(jobStatus) && !jobStatus.equalsIgnoreCase(job.getStatus())) {
084                    continue;
085                }
086                jobs.add(job);
087            }
088    
089            return jobs;
090        }
091    
092        public boolean doesModuleServiceHaveJobStatus(BatchJobStatus job){
093            if(job!=null) {
094                KfsModuleServiceImpl moduleService = (KfsModuleServiceImpl)getKualiModuleService().getResponsibleModuleServiceForJob(job.getName());
095                //This means this job is externalized and we do not want to show any action urls for it.
096                return (moduleService!=null && moduleService.isExternalJob(job.getName()));
097            }
098            return false;
099        }
100        
101        /***
102         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.kns.bo.BusinessObject, java.util.List)
103         */
104        @Override
105        public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
106            if (businessObject instanceof BatchJobStatus) {
107                BatchJobStatus job = (BatchJobStatus) businessObject;
108                if(doesModuleServiceHaveJobStatus(job)) {
109                    return getEmptyActionUrls();
110                }
111                String linkText = "Modify";
112                AttributeSet permissionDetails = new AttributeSet(1);
113                permissionDetails.put(KimAttributes.NAMESPACE_CODE, job.getNamespaceCode() );
114                
115                if ( !SpringContext.getBean(IdentityManagementService.class).hasPermissionByTemplateName(
116                        GlobalVariables.getUserSession().getPerson().getPrincipalId(), 
117                        KNSConstants.KNS_NAMESPACE, 
118                        KFSConstants.PermissionTemplate.MODIFY_BATCH_JOB.name, 
119                        permissionDetails ) ) {
120                    linkText = "View";
121                }
122                String href = getKualiConfigurationService().getPropertyString(KFSConstants.APPLICATION_URL_KEY) + "/batchModify.do?methodToCall=start&name="+(UrlFactory.encode(job.getName()))+("&group=")+(UrlFactory.encode(job.getGroup()));
123                List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
124                AnchorHtmlData anchorHtmlData = new AnchorHtmlData(href, KFSConstants.START_METHOD, linkText);
125                anchorHtmlDataList.add(anchorHtmlData);
126                return anchorHtmlDataList;
127            }
128            return getEmptyActionUrls();
129        }
130    
131        /***
132         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getActionUrlTitleText(org.kuali.rice.kns.bo.BusinessObject, java.lang.String, java.util.List)
133         */
134        @Override
135        protected String getActionUrlTitleText(BusinessObject businessObject, String displayText, List pkNames, BusinessObjectRestrictions businessObjectRestrictions){
136            BatchJobStatus job = (BatchJobStatus) businessObject;
137            String titleText = displayText+" "
138                +getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(getBusinessObjectClass().getName()).getObjectLabel()
139                +" "
140                +getKualiConfigurationService().getPropertyString(TITLE_ACTION_URL_PREPENDTEXT_PROPERTY);
141            titleText += "Name="+job.getName()+" Group="+job.getGroup();
142            return titleText;
143        }
144        
145        public void setSchedulerService(SchedulerService schedulerService) {
146            this.schedulerService = schedulerService;
147        }
148    
149        public void setParameterService(ParameterService parameterService) {
150            this.parameterService = parameterService;
151        }
152    
153        public void setConfigurationService(KualiConfigurationService configurationService) {
154            this.configurationService = configurationService;
155        }
156    
157        public KualiModuleService getKualiModuleService() {
158            if ( kualiModuleService == null ) {
159                kualiModuleService = SpringContext.getBean(KualiModuleService.class);
160            }
161            return kualiModuleService;
162        }
163    
164        public IdentityManagementService getIdentityManagementService() {
165            if ( identityManagementService == null ) {
166                identityManagementService = SpringContext.getBean(IdentityManagementService.class);
167            }
168            return identityManagementService;
169        }
170    
171    }
172