001/*
002 * Copyright 2009 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.fp.businessobject.lookup;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.commons.lang.StringUtils;
022import org.kuali.ole.fp.businessobject.CashDrawer;
023import org.kuali.ole.fp.document.service.CashReceiptService;
024import org.kuali.ole.fp.service.CashDrawerService;
025import org.kuali.rice.kns.lookup.HtmlData;
026import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
027import org.kuali.rice.krad.bo.BusinessObject;
028import org.kuali.rice.krad.document.DocumentAuthorizer;
029import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
030import org.kuali.rice.krad.util.GlobalVariables;
031import org.kuali.rice.krad.util.KRADConstants;
032
033/**
034 * Override of KualiLookupableHelperServiceImpl to prevent the editing and copying of Cash Drawers.  Also to
035 * keep the hobbitses away from my precious.
036 */
037public class CashDrawerLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
038    protected CashReceiptService cashReceiptService;
039    protected CashDrawerService cashDrawerService;
040
041    /**
042     * Return an empty list - you can't edit or copy cash drawers.
043     * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.krad.bo.BusinessObject, java.util.List)
044     */
045    @Override
046    public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
047        List<HtmlData> htmlDataList = new ArrayList<HtmlData>();
048        if (StringUtils.isNotBlank(getMaintenanceDocumentTypeName()) && allowsMaintenanceEditAction(businessObject) && isEditOfCashDrawerAuthorized((CashDrawer)businessObject) && ((CashDrawer)businessObject).isClosed()) {
049            htmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_EDIT_METHOD_TO_CALL, pkNames));
050        }
051        return htmlDataList;
052    }
053
054    /**
055     *
056     * @param cashDrawer
057     * @return
058     */
059    protected boolean isEditOfCashDrawerAuthorized(CashDrawer cashDrawer) {
060        String cashDrawerDocType = KRADServiceLocatorWeb.getDocumentDictionaryService().getMaintenanceDocumentTypeName(CashDrawer.class);
061        DocumentAuthorizer documentAuthorizer = KRADServiceLocatorWeb.getDocumentDictionaryService().getDocumentAuthorizer(cashDrawerDocType);
062        return documentAuthorizer.canInitiate(cashDrawerDocType, GlobalVariables.getUserSession().getPerson());
063    }
064
065    /**
066     * Overridden to see if the current user already has a cash drawer created associated with their campus - if
067     * there is a cash drawer already, then no new or copy is allowed
068     * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#allowsMaintenanceNewOrCopyAction()
069     */
070    @Override
071    public boolean allowsMaintenanceNewOrCopyAction() {
072        final String currentUserCampus = cashReceiptService.getCashReceiptVerificationUnitForUser(GlobalVariables.getUserSession().getPerson());
073        final CashDrawer cashDrawer = cashDrawerService.getByCampusCode(currentUserCampus);
074
075        if (cashDrawer != null) {
076            return false;
077        }
078        return super.allowsMaintenanceNewOrCopyAction();
079    }
080
081    public void setCashDrawerService(CashDrawerService cashDrawerService) {
082        this.cashDrawerService = cashDrawerService;
083    }
084
085    public void setCashReceiptService(CashReceiptService cashReceiptService) {
086        this.cashReceiptService = cashReceiptService;
087    }
088
089}