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.cab.document.web.struts;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    
027    import org.apache.struts.action.ActionForm;
028    import org.apache.struts.action.ActionForward;
029    import org.apache.struts.action.ActionMapping;
030    import org.kuali.kfs.fp.businessobject.CapitalAssetInformation;
031    import org.kuali.kfs.module.cab.CabConstants;
032    import org.kuali.kfs.module.cab.CabKeyConstants;
033    import org.kuali.kfs.module.cab.CabPropertyConstants;
034    import org.kuali.kfs.module.cab.businessobject.GeneralLedgerEntry;
035    import org.kuali.kfs.module.cab.document.service.GlAndPurApHelperService;
036    import org.kuali.kfs.module.cab.document.service.GlLineService;
037    import org.kuali.kfs.module.cam.CamsConstants.DocumentTypeName;
038    import org.kuali.kfs.sys.context.SpringContext;
039    import org.kuali.rice.core.util.RiceConstants;
040    import org.kuali.rice.kns.document.Document;
041    import org.kuali.rice.kns.service.BusinessObjectService;
042    import org.kuali.rice.kns.util.GlobalVariables;
043    import org.kuali.rice.kns.util.KNSConstants;
044    import org.kuali.rice.kns.util.ObjectUtils;
045    
046    /**
047     * Struts action class that handles GL Line Processing Screen actions
048     */
049    public class GlLineAction extends CabActionBase {
050    
051        /**
052         * Action "process" from CAB GL Lookup screen is processed by this method
053         * 
054         * @param mapping {@link ActionMapping}
055         * @param form {@link ActionForm}
056         * @param request {@link HttpServletRequest}
057         * @param response {@link HttpServletResponse}
058         * @return {@link ActionForward}
059         * @throws Exception
060         */
061        public ActionForward process(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
062            GlLineForm glLineForm = (GlLineForm) form;
063            GeneralLedgerEntry entry = findGeneralLedgerEntry(request);
064            if (ObjectUtils.isNotNull(entry)) {
065                prepareRecordsForDisplay(glLineForm, entry);
066            }
067            if (!entry.isActive()) {
068                GlobalVariables.getMessageList().add(CabKeyConstants.WARNING_GL_PROCESSED);
069            }
070            return mapping.findForward(RiceConstants.MAPPING_BASIC);
071        }
072    
073        private void prepareRecordsForDisplay(GlLineForm glLineForm, GeneralLedgerEntry entry) {
074            BusinessObjectService boService = SpringContext.getBean(BusinessObjectService.class);
075            GlLineService glLineService = SpringContext.getBean(GlLineService.class);
076            Map<String, Object> fieldValues = new HashMap<String, Object>();
077            fieldValues.put(CabPropertyConstants.DOCUMENT_NUMBER, entry.getDocumentNumber());
078            // fieldValues.put(CabPropertyConstants.ACTIVE, true);
079            Collection<GeneralLedgerEntry> entries = boService.findMatchingOrderBy(GeneralLedgerEntry.class, fieldValues, CabPropertyConstants.GeneralLedgerEntry.GENERAL_LEDGER_ACCOUNT_IDENTIFIER, true);
080            List<GeneralLedgerEntry> fullList = new ArrayList<GeneralLedgerEntry>();
081            // make the default one as the first one
082            entry.setSelected(true);
083            fullList.add(0, entry);
084    
085            for (GeneralLedgerEntry generalLedgerEntry : entries) {
086                if (!generalLedgerEntry.getGeneralLedgerAccountIdentifier().equals(entry.getGeneralLedgerAccountIdentifier())) {
087                    generalLedgerEntry.setSelected(false);
088                    fullList.add(generalLedgerEntry);
089                }
090            }
091            glLineForm.setRelatedGlEntries(fullList);
092            glLineForm.setPrimaryGlAccountId(entry.getGeneralLedgerAccountIdentifier());
093            CapitalAssetInformation capitalAssetInformation = glLineService.findCapitalAssetInformation(entry);
094            glLineForm.setCapitalAssetInformation(capitalAssetInformation);
095        }
096    
097        /**
098         * Finds GL entry using the key from request
099         * 
100         * @param request HttpServletRequest
101         * @return GeneralLedgerEntry
102         */
103        protected GeneralLedgerEntry findGeneralLedgerEntry(HttpServletRequest request) {
104            String glAcctId = request.getParameter(CabPropertyConstants.GeneralLedgerEntry.GENERAL_LEDGER_ACCOUNT_IDENTIFIER);
105            Long cabGlEntryId = Long.valueOf(glAcctId);
106            return findGeneralLedgerEntry(cabGlEntryId, false);
107        }
108    
109    
110        /**
111         * Action "Create Assets" from CAB GL Detail Selection screen is processed by this method. This will initiate an asset global
112         * document and redirect the user to document edit page.
113         * 
114         * @param mapping ActionMapping
115         * @param form ActionForm
116         * @param request HttpServletRequest
117         * @param response HttpServletResponse
118         * @return ActionForward
119         * @throws Exception
120         */
121        public ActionForward submitAssetGlobal(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
122            GlLineForm glLineForm = (GlLineForm) form;
123            GlLineService glLineService = SpringContext.getBean(GlLineService.class);
124            GeneralLedgerEntry defaultGeneralLedgerEntry = findGeneralLedgerEntry(glLineForm.getPrimaryGlAccountId(), true);
125            List<GeneralLedgerEntry> submitList = prepareSubmitList(glLineForm, defaultGeneralLedgerEntry);
126            if (submitList.isEmpty()) {
127                form.reset(mapping, request);
128                return mapping.findForward(RiceConstants.MAPPING_BASIC);
129            }
130    
131            // set the default as the first entry in the list if it's null
132            if (ObjectUtils.isNull(defaultGeneralLedgerEntry)) {
133                defaultGeneralLedgerEntry = submitList.get(0);
134            }
135    
136            Document maintDoc = glLineService.createAssetGlobalDocument(submitList, defaultGeneralLedgerEntry);
137            List<GeneralLedgerEntry> pendingList = new ArrayList<GeneralLedgerEntry>();
138            preparePendingForAction(mapping, request, glLineForm, maintDoc, pendingList);
139            if (!pendingList.isEmpty()) {
140                return mapping.findForward(RiceConstants.MAPPING_BASIC);
141            }
142            return new ActionForward(getGlAndPurApHelperService().getDocHandlerUrl(maintDoc.getDocumentNumber(), DocumentTypeName.ASSET_ADD_GLOBAL), true);
143        }
144    
145        private void preparePendingForAction(ActionMapping mapping, HttpServletRequest request, GlLineForm glLineForm, Document maintDoc, List<GeneralLedgerEntry> pendingList) {
146            List<GeneralLedgerEntry> relatedGlEntries = glLineForm.getRelatedGlEntries();
147    
148            for (GeneralLedgerEntry generalLedgerEntry : relatedGlEntries) {
149                if (!generalLedgerEntry.isSelected()) {
150                    GeneralLedgerEntry entry = findGeneralLedgerEntry(generalLedgerEntry.getGeneralLedgerAccountIdentifier(), true);
151                    if (entry != null && entry.isActive()) {
152                        pendingList.add(entry);
153                    }
154                }
155            }
156            if (!pendingList.isEmpty()) {
157                glLineForm.reset(mapping, request);
158                glLineForm.setPrimaryGlAccountId(pendingList.get(0).getGeneralLedgerAccountIdentifier());
159                glLineForm.setCurrDocNumber(maintDoc.getDocumentNumber());
160                GeneralLedgerEntry entry = findGeneralLedgerEntry(pendingList.get(0).getGeneralLedgerAccountIdentifier(), true);
161                prepareRecordsForDisplay(glLineForm, entry);
162            }
163        }
164    
165        /**
166         * Helper method to prepare the submit list
167         * 
168         * @param glLineForm ActionForm
169         * @param defaultGeneralLedgerEntry Default selected GL Entry
170         * @return List of submitted entries
171         * @throws Exception
172         */
173        protected List<GeneralLedgerEntry> prepareSubmitList(GlLineForm glLineForm, GeneralLedgerEntry defaultGeneralLedgerEntry) throws Exception {
174            BusinessObjectService boService = SpringContext.getBean(BusinessObjectService.class);
175            GlLineService glLineService = SpringContext.getBean(GlLineService.class);
176            List<GeneralLedgerEntry> submitList = new ArrayList<GeneralLedgerEntry>();
177            if (defaultGeneralLedgerEntry != null) {
178                defaultGeneralLedgerEntry.setSelected(true);
179            }
180    
181            List<GeneralLedgerEntry> relatedGlEntries = glLineForm.getRelatedGlEntries();
182            for (GeneralLedgerEntry generalLedgerEntry : relatedGlEntries) {
183                if (generalLedgerEntry.isSelected()) {
184                    GeneralLedgerEntry entry = findGeneralLedgerEntry(generalLedgerEntry.getGeneralLedgerAccountIdentifier(), true);
185                    if (entry != null && entry.isActive()) {
186                        submitList.add(entry);
187                    }
188                }
189            }
190            return submitList;
191        }
192    
193        /**
194         * Action "Create Payments" from CAB GL Detail Selection screen is processed by this method. This will initiate an asset payment
195         * global document and redirect the user to document edit page.
196         * 
197         * @param mapping ActionMapping
198         * @param form ActionForm
199         * @param request HttpServletRequest
200         * @param response HttpServletResponse
201         * @return ActionForward
202         * @throws Exception
203         */
204        public ActionForward submitPaymentGlobal(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
205            GlLineService glLineService = SpringContext.getBean(GlLineService.class);
206            GlLineForm glLineForm = (GlLineForm) form;
207            GeneralLedgerEntry defaultGeneralLedgerEntry = findGeneralLedgerEntry(glLineForm.getPrimaryGlAccountId(), true);
208    
209            List<GeneralLedgerEntry> submitList = prepareSubmitList(glLineForm, defaultGeneralLedgerEntry);
210            if (submitList.isEmpty()) {
211                form.reset(mapping, request);
212                return mapping.findForward(RiceConstants.MAPPING_BASIC);
213            }
214    
215            // set the default as the first entry in the list if it's null
216            if (ObjectUtils.isNull(defaultGeneralLedgerEntry)) {
217                defaultGeneralLedgerEntry = submitList.get(0);
218            }
219    
220            Document document = glLineService.createAssetPaymentDocument(submitList, defaultGeneralLedgerEntry);
221            List<GeneralLedgerEntry> pendingList = new ArrayList<GeneralLedgerEntry>();
222            preparePendingForAction(mapping, request, glLineForm, document, pendingList);
223            if (!pendingList.isEmpty()) {
224                return mapping.findForward(RiceConstants.MAPPING_BASIC);
225            }
226            return new ActionForward(getGlAndPurApHelperService().getDocHandlerUrl(document.getDocumentNumber(), DocumentTypeName.ASSET_PAYMENT), true);
227        }
228    
229        /**
230         * Retrieves the CAB General Ledger Entry from DB
231         * 
232         * @param generalLedgerEntryId Entry Id
233         * @return GeneralLedgerEntry
234         */
235        protected GeneralLedgerEntry findGeneralLedgerEntry(Long generalLedgerEntryId, boolean requireNew) {
236            BusinessObjectService boService = SpringContext.getBean(BusinessObjectService.class);
237            Map<String, Object> pkeys = new HashMap<String, Object>();
238            pkeys.put(CabPropertyConstants.GeneralLedgerEntry.GENERAL_LEDGER_ACCOUNT_IDENTIFIER, generalLedgerEntryId);
239            if (requireNew) {
240                pkeys.put(CabPropertyConstants.GeneralLedgerEntry.ACTIVITY_STATUS_CODE, CabConstants.ActivityStatusCode.NEW);
241            }
242            GeneralLedgerEntry entry = (GeneralLedgerEntry) boService.findByPrimaryKey(GeneralLedgerEntry.class, pkeys);
243            return entry;
244        }
245    
246        /**
247         * Cancels the action and returns to portal main page
248         * 
249         * @param mapping {@link ActionMapping}
250         * @param form {@link ActionForm}
251         * @param request {@link HttpServletRequest}
252         * @param response {@link HttpServletResponse}
253         * @return {@link ActionForward}
254         * @throws Exception
255         */
256        public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
257            return mapping.findForward(KNSConstants.MAPPING_PORTAL);
258        }
259    
260        /**
261         * @see org.kuali.rice.kns.web.struts.action.KualiAction#showAllTabs(org.apache.struts.action.ActionMapping,
262         *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
263         */
264        @Override
265        public ActionForward showAllTabs(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
266            GlLineForm glLineForm = (GlLineForm) form;
267            List<GeneralLedgerEntry> relatedGlEntries = glLineForm.getRelatedGlEntries();
268            for (GeneralLedgerEntry generalLedgerEntry : relatedGlEntries) {
269                if (generalLedgerEntry.getGeneralLedgerAccountIdentifier().equals(glLineForm.getPrimaryGlAccountId())) {
270                    generalLedgerEntry.setSelected(true);
271                }
272            }
273            return super.showAllTabs(mapping, form, request, response);
274        }
275    
276        public ActionForward reload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
277            GlLineForm glLineForm = (GlLineForm) form;
278            glLineForm.getRelatedGlEntries().clear();
279            GeneralLedgerEntry entry = findGeneralLedgerEntry(glLineForm.getPrimaryGlAccountId(), false);
280            if (entry != null) {
281                prepareRecordsForDisplay(glLineForm, entry);
282            }
283            return mapping.findForward(RiceConstants.MAPPING_BASIC);
284        }
285    
286        protected GlAndPurApHelperService getGlAndPurApHelperService() {
287            return SpringContext.getBean(GlAndPurApHelperService.class);
288        }
289    }