mardi 14 avril 2015

How to get data from other table in controller

I'm not very good in Java and Spring, and I need help on this. Thanks in advance.


I need to get data from other table(Customer) to be used in my printing routine.


Controller:



@RequestMapping( value = { FORM_URL }, method = RequestMethod.POST )
public ModelAndView processData( @ModelAttribute( "bean" ) PassBookIssuance formBean, BindingResult result,
HttpServletRequest request )
throws JournalException
{

ModelAndView mnv = null;

if ( request.getSession() == null )
{
mnv = new ModelAndView( "timeout.jsp" );
// display session timeout page
// add session timeout message error
mnv.addObject( DepositsControllerUtil.VAR_ERROR_MESSAGE,
ErrorCode.BDS_USER_SESSION_TIMEOUT.getDescription() );
return mnv;
}

// check if validation failed
if ( result.hasErrors() )
{
mnv = new ModelAndView( FORMVIEW );
// check if teller override boolean flag is to be enabled
DepositsControllerUtil.checkTellerOverrideError( mnv, result );
// add static values
loadDefault( mnv.getModelMap() );
// return to presentation layer with model that contains errors
return mnv;
}

/* ======== Validation if successful, continue processing then do a redirect======= */

// add required fields
fieldGenerator.populateConstantHeader( formBean, request );

// save transaction
JournalNonFinancial journalNonFT = journalNonFinancialManagerImpl.save(formBean);

// send to host
Map<String, Object> returnMap = hostConnectionDelegate.invoke( formBean, wsNonFTBean );

// get response from the returned map
HostRequest response = HostConnectionDelegateUtil.getHostResponse( returnMap );

// update transaction table based on host response
if ( !HostConnectionDelegateUtil.isApproved( response ) )
{
journalNonFT.setErrorCode( response.getErrorCode() );
journalNonFT.setStatus( response.getStatus() );
}
else
{
journalNonFT.setStatus( response.getStatus() );

}
// save journal
journalNonFinancialManagerImpl.saveJournalNonFinancial( journalNonFT );

if ( request.getSession() != null )
{
// add response to session for the presentation layer to display transaction results.
request.getSession().setAttribute( "bean", response );
}

/**
* Media validation - CDEP
*/
String acctNo = journalNonFT.getAccountNumber();
String name = "";
String unit = unitUtil.getUnit().getAddress();

String toPrint = acctNo + "\n" + name + "\n" + unit;

try {
SilentPrintMediaValidation print = new SilentPrintMediaValidation();
print.printPdf( toPrint, "Passbook Issuance" );
} catch ( Exception e ){
e.printStackTrace();
System.out.println( "Printing error" );
}

RedirectView redirectView = new RedirectView( "/general" + FORM_URL );
return new ModelAndView( redirectView );

}


Model:



package com.eteligent.core.form.nonfinancial;

import com.eteligent.core.form.HostNonFinancialRequest;
import com.eteligent.core.form.enums.PsBokIssueReasonCode;

public class PassBookIssuance extends HostNonFinancialRequest {

private String newSerialNo;

private String oldSerialNo;

private PsBokIssueReasonCode issueReason;


public String getNewSerialNo() {
return newSerialNo;
}

public void setNewSerialNo(String newSerialNo) {
this.newSerialNo = newSerialNo;
}

public String getOldSerialNo() {
return oldSerialNo;
}

public void setOldSerialNo(String oldSerialNo) {
this.oldSerialNo = oldSerialNo;
}

public PsBokIssueReasonCode getIssueReason() {
return issueReason;
}

public void setIssueReason(PsBokIssueReasonCode issueReason) {
this.issueReason = issueReason;
}

}


JSP:



<%@ taglib uri="http://ift.tt/QfKAz6" prefix="c"%>
<%@ taglib uri="http://ift.tt/1bRkrVx" prefix="fmt"%>
<%@ taglib uri="http://ift.tt/IED0jK" prefix="form"%>
<%@ taglib uri="http://ift.tt/1bjwgjt" prefix="tiles"%>

<%--Note to Dev: change only the form action value --%>

<div id="cash-deposit-form">

<c:if test="${override == true}">
<input type="hidden" name="override" value="y" />
</c:if>

<br />
<form:form name="formPage" action="/general/passbook-issuance-request" method="POST" modelAttribute="bean">

<%-- Hidden input for override verification --%>
<input type="hidden" name="override" value="${override}">


<!-- insert common attributes of financial transactions here -->
<tiles:insertTemplate template="/WEB-INF/layouts/common/tellering_common.jsp" />


<%-- Hidden input for transaction date holder for date extra handling --%>
<input type="hidden" id="transactionDateHolder" name="transactionDateHolder" value="${bean.transactionDate}">


<%-- Hidden transaction group name for collapsing the proper left menu navigation --%>
<input type="hidden" name="tranGroup" value="otherTrans" id="tranGroup">


<%-- Hidden page name to highlight the proper left menu navigation --%>
<input type="hidden" name="pageName" value="pbookIssue" id="pageName">


<div class="datagrid" style="display: inline-block;" >
<table width="100%" class="form basic-info" cellspacing="0" cellpadding="0">

<thead>
<tr>
<th colspan="2">Passbook Issuance Request</th>
</tr>
</thead>

<tr>
<td>Transaction Date :</td>
<td><form:input path="transactionDate" class="datepicker"
id="transactionDate" tabindex="1" />
<img alt="help" src="/images/question_blue.png"
class="datepickerHelp"> <form:errors path="transactionDate"
cssClass="error" />
</td>
</tr>
<tr>
<td>Account Number :</td>
<td><form:input path="accountNumber"
value="${sessionScope.bean.accountNumber}"
onkeypress="return isNumberKey(event)" tabindex="2" class="firstField"/>
<form:errors path="accountNumber" cssClass="error" />
</td>
</tr>
<tr>
<td>Old Passbook Serial Number :</td>
<td><form:input path="oldSerialNo"
onkeypress="return isNumberKey(event)" tabindex="4" value="${sessionScope.bean.oldSerialNo}" /> <form:errors
path="oldSerialNo" cssClass="error" /></td>
</tr>
<tr>
<td>New Passbook Serial Number :</td>
<td><form:input path="newSerialNo"
onkeypress="return isNumberKey(event)" tabindex="4" value="${sessionScope.bean.newSerialNo}" /> <form:errors
path="newSerialNo" cssClass="error" /></td>
</tr>
<tr>
<td>Reason For Passbook Issuance Request :</td>
<td> <form:select path="issueReason" items='${issueReasons}'
itemLabel="desc" itemValue="name" tabindex="4" /><form:errors
path="issueReason" cssClass="error" />
</td>
</tr>
<tr>
<td>Remarks :</td>
<td><form:input path="remarks" tabindex="4" value="${sessionScope.bean.remarks}"/> <form:errors
path="remarks" cssClass="error" /></td>
</tr>


<tr>
<td colspan="2"><hr color="grey" size="2"/></td>
</tr>


<tr>
<td>
<input name="btnSubmit" id="btnSubmit" type="submit" value="Submit" tabindex="8">
<!-- <input name="btnClear" id="btnClear" type="reset" value="Cancel" tabindex="9"> -->
<input name="btnBack" id="btnBack" type="button" value="Back" onclick="window.history.back()"
tabindex="10">
</td>
</tr>
</table>
</div>


<br />


<%--End of Account Details--%>
<%-- Start: Modal form --%>
<div id="overrideModalDialog" title="Override Transaction Prompt">
<fieldset>
<label for="officerId">Officer ID</label>
<input type="text" name="officerId" id="officerId" class="text ui-widget-content ui-corner-all" />
<label for="password">Password</label>
<input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
</fieldset>
</div>

<div id="amountModalDialog" title="Amount Entry"> </div>



</form:form>

<br/>

<tiles:insertTemplate template="/WEB-INF/layouts/common/tellering_status.jsp" />

<c:if test="${not empty bean && empty bean.errorCode.description && not empty bean.status }">
<c:remove var="bean" scope="session" />
</c:if>
</div>


So the problem lies in the controller, I don't know how to get the name field in Customer table. Since I did not declare Customer in that routine, how do I get the details in customer table? Moreover, I need to query the name using the account number.


I know this is kind of confusing, but I'm also confused too that's why I need your expertise.


Questions: Should I use DAO or service? There is one here but I don't know how to call them in the controller.


I've been exposed to UI programming too much, I'm forgetting the basics in backend so please help me. Thanks.


Aucun commentaire:

Enregistrer un commentaire