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.module.cam.document.web.struts;
017    
018    import static org.kuali.kfs.module.cam.CamsPropertyConstants.Asset.CAPITAL_ASSET_NUMBER;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletResponse;
022    
023    import org.apache.log4j.Logger;
024    import org.apache.struts.action.ActionForm;
025    import org.apache.struts.action.ActionForward;
026    import org.apache.struts.action.ActionMapping;
027    import org.kuali.kfs.module.cam.CamsKeyConstants;
028    import org.kuali.kfs.module.cam.CamsPropertyConstants;
029    import org.kuali.kfs.module.cam.businessobject.Asset;
030    import org.kuali.kfs.module.cam.businessobject.AssetPayment;
031    import org.kuali.kfs.module.cam.document.AssetTransferDocument;
032    import org.kuali.kfs.module.cam.document.service.AssetLocationService;
033    import org.kuali.kfs.module.cam.document.service.AssetPaymentService;
034    import org.kuali.kfs.module.cam.document.service.PaymentSummaryService;
035    import org.kuali.kfs.sys.context.SpringContext;
036    import org.kuali.kfs.sys.document.web.struts.FinancialSystemTransactionalDocumentActionBase;
037    import org.kuali.rice.kew.util.KEWConstants;
038    import org.kuali.rice.kim.bo.Person;
039    import org.kuali.rice.kns.util.ErrorMessage;
040    import org.kuali.rice.kns.util.GlobalVariables;
041    import org.kuali.rice.kns.util.MessageMap;
042    import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
043    
044    public class AssetTransferAction extends FinancialSystemTransactionalDocumentActionBase {
045        protected static final Logger LOG = Logger.getLogger(AssetTransferAction.class);
046    
047        /**
048         * This method had to override because asset information has to be refreshed before display
049         * 
050         * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#docHandler(org.apache.struts.action.ActionMapping,
051         *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
052         */
053        @Override
054        public ActionForward docHandler(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
055            ActionForward docHandlerForward = super.docHandler(mapping, form, request, response);
056    
057            // refresh asset information
058            AssetTransferForm assetTransferForm = (AssetTransferForm) form;
059            AssetTransferDocument assetTransferDocument = (AssetTransferDocument) assetTransferForm.getDocument();
060            handleRequestFromLookup(request, assetTransferForm, assetTransferDocument);
061            handleRequestFromWorkflow(assetTransferForm, assetTransferDocument);
062            Asset asset = assetTransferDocument.getAsset();
063            asset.refreshReferenceObject(CamsPropertyConstants.Asset.ASSET_LOCATIONS);
064            asset.refreshReferenceObject(CamsPropertyConstants.Asset.ASSET_PAYMENTS);
065            SpringContext.getBean(AssetLocationService.class).setOffCampusLocation(asset);
066            SpringContext.getBean(PaymentSummaryService.class).calculateAndSetPaymentSummary(asset);
067    
068            // populate old asset fields for historic retaining on document
069            String command = assetTransferForm.getCommand();
070            if (KEWConstants.INITIATE_COMMAND.equals(command)) {
071                assetTransferDocument.setOldOrganizationOwnerChartOfAccountsCode(asset.getOrganizationOwnerChartOfAccountsCode());
072                assetTransferDocument.setOldOrganizationOwnerAccountNumber(asset.getOrganizationOwnerAccountNumber());
073            }
074    
075            this.refresh(mapping, form, request, response);
076    
077            return docHandlerForward;
078        }
079    
080        /**
081         * This method handles when request is from a work flow document search
082         * 
083         * @param assetTransferForm Form
084         * @param assetTransferDocument Document
085         * @param service BusinessObjectService
086         * @return Asset
087         */
088        protected void handleRequestFromWorkflow(AssetTransferForm assetTransferForm, AssetTransferDocument assetTransferDocument) {
089            LOG.debug("Start- Handle request from workflow");
090            if (assetTransferForm.getDocId() != null) {
091                assetTransferDocument.refreshReferenceObject(CamsPropertyConstants.AssetTransferDocument.ASSET);
092                org.kuali.rice.kim.service.PersonService personService = SpringContext.getBean(org.kuali.rice.kim.service.PersonService.class);
093                Person person = personService.getPerson(assetTransferDocument.getRepresentativeUniversalIdentifier());
094                if (person != null) {
095                    assetTransferDocument.setAssetRepresentative(person);
096                }
097                else {
098                    LOG.error("org.kuali.rice.kim.service.PersonService returned null for uuid " + assetTransferDocument.getRepresentativeUniversalIdentifier());
099                }
100            }
101        }
102    
103        /**
104         * This method handles the request coming from asset lookup screen
105         * 
106         * @param request Request
107         * @param assetTransferForm Current form
108         * @param assetTransferDocument Document
109         * @param service Business Object Service
110         * @param asset Asset
111         * @return Asset
112         */
113        protected void handleRequestFromLookup(HttpServletRequest request, AssetTransferForm assetTransferForm, AssetTransferDocument assetTransferDocument) {
114            LOG.debug("Start - Handle request from asset lookup screen");
115            if (assetTransferForm.getDocId() == null) {
116                String capitalAssetNumber = request.getParameter(CAPITAL_ASSET_NUMBER);
117                assetTransferDocument.setCapitalAssetNumber(Long.valueOf(capitalAssetNumber));
118                assetTransferDocument.refreshReferenceObject(CamsPropertyConstants.AssetTransferDocument.ASSET);
119            }
120        }
121    
122    
123        /**
124         * Since the organization fields are view only we need to make sure they are in sync with the data entry fields.
125         * 
126         * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#refresh(org.apache.struts.action.ActionMapping,
127         *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
128         */
129        @Override
130        public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
131            ((KualiDocumentFormBase) form).setMessageMapFromPreviousRequest(new MessageMap());
132            ActionForward actionForward = super.refresh(mapping, form, request, response);
133    
134            AssetTransferDocument assetTransferDocument = ((AssetTransferForm) form).getAssetTransferDocument();
135    
136            assetTransferDocument.refreshReferenceObject(CamsPropertyConstants.AssetTransferDocument.ORGANIZATION_OWNER_ACCOUNT);
137            assetTransferDocument.refreshReferenceObject(CamsPropertyConstants.AssetTransferDocument.OLD_ORGANIZATION_OWNER_ACCOUNT);
138    
139            return actionForward;
140        }
141    
142    
143        /**
144         * Route the document
145         */
146        @Override
147        public ActionForward route(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
148            ActionForward actionForward = super.route(mapping, form, request, response);
149    
150            allPaymentsFederalOwnedMessage(form);
151    
152            return actionForward;
153        }
154    
155        protected void allPaymentsFederalOwnedMessage(ActionForm form) {
156            boolean allPaymentsFederalOwned = true;
157    
158            AssetTransferDocument assetTransferDocument = ((AssetTransferForm) form).getAssetTransferDocument();
159    
160            for (AssetPayment assetPayment : assetTransferDocument.getAsset().getAssetPayments()) {
161                if (!getAssetPaymentService().isPaymentFederalOwned(assetPayment)) {
162                    allPaymentsFederalOwned = false;
163                }
164            }
165    
166            // display a message for asset not generating ledger entries when it is federally owned
167            if (allPaymentsFederalOwned) {
168                GlobalVariables.getMessageList().add(0, new ErrorMessage(CamsKeyConstants.Transfer.MESSAGE_NO_LEDGER_ENTRY_REQUIRED_TRANSFER));
169            }
170        }
171    
172        protected AssetPaymentService getAssetPaymentService() {
173            return SpringContext.getBean(AssetPaymentService.class);
174        }
175    
176    }