001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: /*
017: * Created on Jul 16, 2004
018: *
019: * To change the template for this generated file go to
020: * Window>Preferences>Java>Code Generation>Code and Comments
021: */
022: package org.kuali.module.pdp.action.customerprofile;
023:
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.commons.lang.StringUtils;
031: import org.apache.struts.action.ActionForm;
032: import org.apache.struts.action.ActionForward;
033: import org.apache.struts.action.ActionMapping;
034: import org.apache.struts.action.ActionMessage;
035: import org.apache.struts.action.ActionMessages;
036: import org.kuali.kfs.context.SpringContext;
037: import org.kuali.module.chart.bo.Account;
038: import org.kuali.module.chart.bo.ObjectCode;
039: import org.kuali.module.chart.bo.SubAccount;
040: import org.kuali.module.chart.bo.SubObjCd;
041: import org.kuali.module.chart.service.AccountService;
042: import org.kuali.module.chart.service.ObjectCodeService;
043: import org.kuali.module.chart.service.ProjectCodeService;
044: import org.kuali.module.chart.service.SubAccountService;
045: import org.kuali.module.chart.service.SubObjectCodeService;
046: import org.kuali.module.pdp.action.BaseAction;
047: import org.kuali.module.pdp.bo.Bank;
048: import org.kuali.module.pdp.bo.CustomerBank;
049: import org.kuali.module.pdp.bo.CustomerProfile;
050: import org.kuali.module.pdp.bo.DisbursementType;
051: import org.kuali.module.pdp.form.customerprofile.CustomerBankForm;
052: import org.kuali.module.pdp.form.customerprofile.CustomerProfileForm;
053: import org.kuali.module.pdp.service.BankService;
054: import org.kuali.module.pdp.service.CustomerProfileService;
055: import org.kuali.module.pdp.service.ReferenceService;
056: import org.kuali.module.pdp.service.SecurityRecord;
057: import org.kuali.module.pdp.utilities.GeneralUtilities;
058:
059: /**
060: * @author delyea
061: */
062: public class CustomerProfileSaveAction extends BaseAction {
063: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
064: .getLogger(CustomerProfileSaveAction.class);
065: private CustomerProfileService customerProfileService;
066: private ReferenceService referenceService;
067: private BankService bankService;
068: private AccountService accountService;
069: private SubAccountService subAccountService;
070: private ObjectCodeService objectCodeService;
071: private SubObjectCodeService subObjectCodeService;
072: private ProjectCodeService projectCodeService;
073:
074: public CustomerProfileSaveAction() {
075: super ();
076: setCustomerProfileService(SpringContext
077: .getBean(CustomerProfileService.class));
078: setReferenceService(SpringContext
079: .getBean(ReferenceService.class));
080: setBankService(SpringContext.getBean(BankService.class));
081: setAccountService(SpringContext.getBean(AccountService.class));
082: setSubAccountService(SpringContext
083: .getBean(SubAccountService.class));
084: setObjectCodeService(SpringContext
085: .getBean(ObjectCodeService.class));
086: setSubObjectCodeService(SpringContext
087: .getBean(SubObjectCodeService.class));
088: setProjectCodeService(SpringContext
089: .getBean(ProjectCodeService.class));
090: }
091:
092: public void setCustomerProfileService(CustomerProfileService c) {
093: customerProfileService = c;
094: }
095:
096: public void setReferenceService(ReferenceService r) {
097: referenceService = r;
098: }
099:
100: public void setBankService(BankService b) {
101: bankService = b;
102: }
103:
104: public void setAccountService(AccountService accountService) {
105: this .accountService = accountService;
106: }
107:
108: public void setObjectCodeService(ObjectCodeService objectCodeService) {
109: this .objectCodeService = objectCodeService;
110: }
111:
112: public void setProjectCodeService(
113: ProjectCodeService projectCodeService) {
114: this .projectCodeService = projectCodeService;
115: }
116:
117: public void setSubAccountService(SubAccountService subAccountService) {
118: this .subAccountService = subAccountService;
119: }
120:
121: public void setSubObjectCodeService(
122: SubObjectCodeService subObjectCodeService) {
123: this .subObjectCodeService = subObjectCodeService;
124: }
125:
126: protected boolean isAuthorized(ActionMapping mapping,
127: ActionForm form, HttpServletRequest request,
128: HttpServletResponse response) {
129: SecurityRecord sr = getSecurityRecord(request);
130: return sr.isSysAdminRole();
131: }
132:
133: protected ActionForward executeLogic(ActionMapping mapping,
134: ActionForm form, HttpServletRequest request,
135: HttpServletResponse response) throws Exception {
136: LOG.info("executeLogic() starting");
137: CustomerProfileForm customerProfileForm = (CustomerProfileForm) form;
138: CustomerProfile cp = customerProfileForm.getCustomerProfile();
139: String forward = "list";
140:
141: ActionMessages actionErrors = new ActionMessages();
142: String buttonPressed = GeneralUtilities
143: .whichButtonWasPressed(request);
144:
145: if (buttonPressed != null && actionErrors.isEmpty()) {
146: LOG.debug("executeLogic() buttonPressed is "
147: + buttonPressed);
148: if (buttonPressed.startsWith("btnClear")) {
149: actionErrors.clear();
150: CustomerProfileForm newProfile = (CustomerProfileForm) form;
151:
152: CustomerBankForm[] dtl = newProfile
153: .getCustomerBankForms();
154: for (int i = 0; i < dtl.length; i++) {
155: CustomerBankForm cbf = dtl[i];
156: cbf.setBankId(null);
157: newProfile.setCustomerBankForms(i, cbf);
158: }
159: newProfile.clearForm();
160: request.setAttribute("CustomerProfileForm", newProfile);
161: forward = "edit";
162: } else if (buttonPressed.startsWith("btnCancel")) {
163: actionErrors.clear();
164: forward = "list";
165: } else {
166: // first we need to validate that this customer doesn't exist already
167: String chartCode = cp.getChartCode();
168: String orgCode = cp.getOrgCode();
169: String subUnitCode = cp.getSubUnitCode();
170: if (customerAlreadyExists(chartCode, orgCode,
171: subUnitCode)
172: && StringUtils.isEmpty(customerProfileForm
173: .getId())) {
174: actionErrors.add("errors", new ActionMessage(
175: "customerProfileForm.profileExists",
176: chartCode + "-" + orgCode + "-"
177: + subUnitCode));
178: }
179: // Validate the accounting information
180: LOG
181: .debug("executeLogic() Validating accounting fields");
182: Account acct = accountService.getByPrimaryId(
183: customerProfileForm.getDefaultChartCode(),
184: customerProfileForm.getDefaultAccountNumber());
185: if (acct == null) {
186: actionErrors
187: .add(
188: "defaultAccountNumber",
189: new ActionMessage(
190: "customerProfileForm.invalidAccountNumber"));
191: } else {
192: if (!"-----".equals(customerProfileForm
193: .getDefaultSubAccountNumber())) {
194: SubAccount subAcct = subAccountService
195: .getByPrimaryId(
196: customerProfileForm
197: .getDefaultChartCode(),
198: customerProfileForm
199: .getDefaultAccountNumber(),
200: customerProfileForm
201: .getDefaultSubAccountNumber());
202: if (subAcct == null) {
203: actionErrors
204: .add(
205: "defaultSubAccountNumber",
206: new ActionMessage(
207: "customerProfileForm.invalidSubAccountNumber"));
208: }
209: }
210: ObjectCode oc = objectCodeService
211: .getByPrimaryIdForCurrentYear(
212: customerProfileForm
213: .getDefaultChartCode(),
214: customerProfileForm
215: .getDefaultObjectCode());
216: if (oc == null) {
217: actionErrors
218: .add(
219: "defaultObjectCode",
220: new ActionMessage(
221: "customerProfileForm.invalidObjectCode"));
222: } else {
223: if (!"---".equals(customerProfileForm
224: .getDefaultSubObjectCode())) {
225: SubObjCd soc = subObjectCodeService
226: .getByPrimaryIdForCurrentYear(
227: customerProfileForm
228: .getDefaultChartCode(),
229: customerProfileForm
230: .getDefaultAccountNumber(),
231: customerProfileForm
232: .getDefaultObjectCode(),
233: customerProfileForm
234: .getDefaultSubObjectCode());
235: if (soc == null) {
236: actionErrors
237: .add(
238: "defaultSubObjectCode",
239: new ActionMessage(
240: "customerProfileForm.invalidSubObjectCode"));
241: }
242: }
243: }
244: }
245: if (!actionErrors.isEmpty()) { // If we had errors, save them.
246: saveErrors(request, actionErrors);
247: return mapping.getInputForward();
248: }
249:
250: LOG.debug("executeLogic() Saving customer profile");
251:
252: CustomerBankForm[] dtl = customerProfileForm
253: .getCustomerBankForms();
254: CustomerProfile storedProfile = customerProfileService
255: .get(GeneralUtilities
256: .convertStringToInteger(customerProfileForm
257: .getId()));
258:
259: cp.setLastUpdateUser(getUser(request));
260:
261: List storedCustomerBanks = null;
262: if (!(storedProfile == null)) {
263: storedCustomerBanks = storedProfile
264: .getCustomerBanks();
265: }
266: customerProfileService.save(cp);
267: LOG.debug("executeLogic() storedCustomerBanks = "
268: + storedCustomerBanks);
269:
270: for (int i = 0; i < dtl.length; i++) {
271: CustomerBankForm element = dtl[i];
272: LOG
273: .debug("executeLogic() Checking CustomerBanks for CustomerProfil ID "
274: + cp.getId()
275: + " with Disbursement Type of "
276: + element
277: .getDisbursementDescription());
278: LOG.debug("executeLogic() DisbTypeCode = "
279: + element.getDisbursementTypeCode());
280: LOG
281: .debug("executeLogic() The profile from the DB is "
282: + storedProfile);
283:
284: if (!(storedProfile == null)) {
285: LOG.debug("executeLogic() 1");
286: }
287:
288: if ((!(storedProfile == null))
289: && (!(storedCustomerBanks.equals(null)))) {
290: // Profile exists and at least one CustomerBank Exists
291: LOG
292: .debug("executeLogic() At least one CustomerBank and profile stored");
293: LOG
294: .debug("executeLogic() The Bank ID entered is "
295: + element.getBankId());
296: if (!(element.getBankId()
297: .equals(new Integer(0)))) {
298: // User has stored a bankId
299: CustomerBank cb = new CustomerBank();
300: DisbursementType dt = (DisbursementType) referenceService
301: .getCode(
302: "DisbursementType",
303: element
304: .getDisbursementTypeCode());
305: cb.setDisbursementType(dt);
306: cb.setCustomerProfile(cp);
307: cb.setLastUpdateUser(getUser(request));
308:
309: for (Iterator iter = storedCustomerBanks
310: .iterator(); iter.hasNext();) {
311: CustomerBank storedCustomerBank = (CustomerBank) iter
312: .next();
313: if (element
314: .getDisbursementTypeCode()
315: .equals(
316: storedCustomerBank
317: .getDisbursementType()
318: .getCode())) {
319: // UPDATE CUSTOMERBANK storedCustomerBank by setting Version and ID
320: LOG
321: .debug("executeLogic() The next CustomerBank stored will be an UPDATE from existing");
322: cb
323: .setId(storedCustomerBank
324: .getId());
325: cb.setVersion(storedCustomerBank
326: .getVersion());
327: }
328: }
329:
330: Bank b = bankService.get(element
331: .getBankId());
332: cb.setBank(b);
333: LOG
334: .debug("executeLogic() Now storing CustomerBank with bankId "
335: + element.getBankId()
336: + " and DisbursementType of "
337: + dt.getDescription());
338: customerProfileService.saveCustomerBank(cb);
339: } else {
340: // User has stored null as the bankId
341: for (Iterator iter = storedCustomerBanks
342: .iterator(); iter.hasNext();) {
343: CustomerBank storedCustomerBank = (CustomerBank) iter
344: .next();
345: if (element
346: .getDisbursementTypeCode()
347: .equals(
348: storedCustomerBank
349: .getDisbursementType()
350: .getCode())) {
351: // DELETE CUSTOMERBANK storedCustomerBank
352: LOG
353: .debug("executeLogic() Now deleting CustomerBank with id of "
354: + storedCustomerBank
355: .getId());
356: storedCustomerBank
357: .setLastUpdateUser(getUser(request));
358: customerProfileService
359: .deleteCustomerBank(storedCustomerBank);
360: }
361: }
362: }
363: } else {
364: // There are no stored CustomerBanks or no profile (therefore no CustomerBanks
365: if (!(element.getBankId()
366: .equals(new Integer(0)))) {
367: // Array for this ACH type contains a bankId
368: LOG
369: .debug("executeLogic() No CustomerBanks or profile stored... so now storing with bankId "
370: + element.getBankId());
371: DisbursementType dt = (DisbursementType) referenceService
372: .getCode(
373: "DisbursementType",
374: element
375: .getDisbursementTypeCode());
376: Bank b = bankService.get(element
377: .getBankId());
378:
379: CustomerBank cb = new CustomerBank();
380: cb.setDisbursementType(dt);
381: cb.setCustomerProfile(cp);
382: cb.setBank(b);
383: cb.setLastUpdateUser(getUser(request));
384: customerProfileService.saveCustomerBank(cb);
385: }
386: }
387: }
388:
389: if (storedProfile == null) {
390: LOG.debug("executeLogic() 1");
391: customerProfileService.save(cp);
392: }
393: LOG.debug("executeLogic() custId is: " + cp.getId());
394: actionErrors.add("success", new ActionMessage(
395: "success.profile.saved"));
396: LOG
397: .debug("executeLogic() Save or Update complete; exit method.");
398: forward = "list";
399: }
400: }
401:
402: // If we had errors, save them.
403: if (!actionErrors.isEmpty()) {
404: saveErrors(request, actionErrors);
405: }
406:
407: return mapping.findForward(forward);
408: }
409:
410: private boolean customerAlreadyExists(String chartCode,
411: String orgCode, String subUnitCode) {
412: // attempt to retrieve this through the CustomerService
413:
414: if (!StringUtils.isEmpty(chartCode)
415: && !StringUtils.isEmpty(orgCode)
416: && !StringUtils.isEmpty(subUnitCode)) {
417: CustomerProfile existingProfile = customerProfileService
418: .get(chartCode, orgCode, subUnitCode);
419: if (existingProfile != null) {
420: return true;
421: }
422: }
423: return false;
424: }
425: }
|