001/*
002 * Copyright 2007 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 */
016package org.kuali.ole.sys.businessobject.lookup;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.regex.Pattern;
023
024import org.apache.commons.lang.StringUtils;
025import org.kuali.ole.sys.OLEConstants;
026import org.kuali.ole.sys.batch.BatchJobStatus;
027import org.kuali.ole.sys.batch.service.SchedulerService;
028import org.kuali.ole.sys.context.SpringContext;
029import org.kuali.ole.sys.service.impl.OleModuleServiceImpl;
030import org.kuali.rice.core.api.config.property.ConfigurationService;
031import org.kuali.rice.coreservice.framework.parameter.ParameterService;
032import org.kuali.rice.kim.api.KimConstants;
033import org.kuali.rice.kim.api.services.IdentityManagementService;
034import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions;
035import org.kuali.rice.kns.lookup.HtmlData;
036import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
037import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
038import org.kuali.rice.krad.bo.BusinessObject;
039import org.kuali.rice.krad.service.KualiModuleService;
040import org.kuali.rice.krad.util.GlobalVariables;
041import org.kuali.rice.krad.util.KRADConstants;
042import org.kuali.rice.krad.util.UrlFactory;
043
044public class BatchJobStatusLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
045
046    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BatchJobStatusLookupableHelperServiceImpl.class);
047
048    private SchedulerService schedulerService;
049    private ConfigurationService configurationService;
050    private ParameterService parameterService;
051    private KualiModuleService kualiModuleService;
052    private IdentityManagementService identityManagementService;
053
054    @Override
055    public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
056        super.setBackLocation(fieldValues.get(OLEConstants.BACK_LOCATION));
057        super.setDocFormKey(fieldValues.get(OLEConstants.DOC_FORM_KEY));
058        List<BatchJobStatus> allJobs = schedulerService.getJobs();
059        List<BatchJobStatus> jobs = new ArrayList<BatchJobStatus>();
060
061        String namespaceCode = fieldValues.get("namespaceCode");
062        String nameValue = fieldValues.get("name");
063        Pattern namePattern = null;
064        if (!StringUtils.isEmpty(nameValue)) {
065            namePattern = Pattern.compile(nameValue.replace("*", ".*"), Pattern.CASE_INSENSITIVE);
066        }
067        String schedulerGroup = fieldValues.get("group");
068        String jobStatus = fieldValues.get("status");
069        for (BatchJobStatus job : allJobs) {
070            if (!StringUtils.isEmpty(namespaceCode) &&
071                    (!namespaceCode.equalsIgnoreCase(job.getNamespaceCode()) && job.getNamespaceCode()!=null)) {
072                continue;
073            }
074            if (namePattern != null && !namePattern.matcher(job.getName()).matches()) {
075                continue; // match failed, skip this entry
076            }
077            if (!StringUtils.isEmpty(schedulerGroup) && !schedulerGroup.equalsIgnoreCase(job.getGroup())) {
078                continue;
079            }
080            if (!StringUtils.isEmpty(jobStatus) && !jobStatus.equalsIgnoreCase(job.getStatus())) {
081                continue;
082            }
083            jobs.add(job);
084        }
085
086        return jobs;
087    }
088
089    public boolean doesModuleServiceHaveJobStatus(BatchJobStatus job){
090        if(job!=null) {
091            OleModuleServiceImpl moduleService = (OleModuleServiceImpl)getKualiModuleService().getResponsibleModuleServiceForJob(job.getName());
092            //This means this job is externalized and we do not want to show any action urls for it.
093            return (moduleService!=null && moduleService.isExternalJob(job.getName()));
094        }
095        return false;
096    }
097
098    /***
099     * @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