public class HaulAction extends CommonAction{
/** Log4j logging. */
private static Log logger = LogFactory.getLog(SwitchBackhaulAction.class);
public HaulAction() {
}
public ActionForward render(ActionMapping mapping,
ActionForm form, PortletConfig config,RenderRequest request, RenderResponse
response)
throws Exception{
{
logger.info("Render Action :" +IConstant.ENTERED);
String actionName=null;
String selectedTab=request.getParameter("selectedTab");
logger.info("Selected Tab :" + selectedTab);
String forward = "normalSwitchBackhaul";
//String forward=IConstant.FORWARD_SUCCESS;
SwitchBackhaulForm
switchBackhaulForm=(SwitchBackhaulForm)form;
try{
request.setAttribute("selectedTab",forward);
String
circuitName=(String)request.getParameter("hiddenLabel");
System.out.println("circuitName :"+circuitName);
logger.info("Label in Render Action :" +circuitName);
SwitchBackhaulHelper helper=new SwitchBackhaulHelper();
actionName=request.getParameter("Button");
String reset=request.getParameter("reset");
String userId=PortletUtil.getUsername(request);
System.out.println("userId : "+userId);
PortletSession session=request.getPortletSession();
boolean forceEnabled;
if(session.getAttribute("forceEnable",session.APPLICATION_SCOPE)==null){
forceEnabled=false;
}else{
forceEnabled=((Boolean)session.getAttribute("forceEnable",session.APPLICATION_SCOPE)).booleanValue();
}
/* Added for Display All Checkbox change */
//get the role of the Logged in User
Collection
roles=(Collection)session.getAttribute("roles",session.APPLICATION_SCOPE);
//chk if the logged in user is Super user or not
if(null != roles
&& roles.contains("PROVGUISUPER")){
//set the user as an Super User in the form
switchBackhaulForm.setIsSuperUser("true");
logger.debug(" SwitchBackhaulAction : Role of Logged in User is Super
User");
}else{
//set the user as not an Super User in the Form
switchBackhaulForm.setIsSuperUser("false");
logger.debug(" SwitchBackhaulAction : Role of Logged in User is not
Super User");
}
/* End of Addition */
List<CustomerInfo>
customerList=null;
//populate customer list
if(reset!=null && reset.equals("true"))
{
customerList=helper.getCustomers(userId);
Collections.sort(customerList);
switchBackhaulForm.setCustomerNames(customerList);
logger.info(" got customers " + customerList.size());
switchBackhaulForm.setClientServiceList(null);
//make a call to helper to get the Filter inputs
helper.getFilterInputs(userId,switchBackhaulForm);
forward="customerSuccess";
}
logger.info("Clicked on Action :" + actionName);
ArrayList<ServiceInfo> filterServices=null;
if(actionName==null)
{
logger.info("cleaning session ");
cleanSession(request);
}
while(actionName!=null){
if(actionName.equals("Fetch")){
logger.info("Fetch Circuits Action :" + IConstant.ENTERED);
/* Added
Change By Sukanta dey for SVC Layer*/
helper.getCircuits(switchBackhaulForm,userId);
/* Change End
*/
List ipProfiles=helper.getAllIpProfiles(userId);
logger.info("Fetch Circuits Action :" + IConstant.EXITED);
break;
}
if(actionName.equals("Get Services")){
String forceEnabledCheckbox=request.getParameter("forceEnabledCheckbox");
System.out.println("forceEnabledCheckbox : "+forceEnabledCheckbox);
if(forceEnabledCheckbox!=null){
forceEnabled=Boolean.parseBoolean(forceEnabledCheckbox);
session.setAttribute("forceEnable",forceEnabled,PortletSession.APPLICATION_SCOPE);
}
logger.info("Get Services Action :" + IConstant.ENTERED);
boolean
showDisconnectCheckbox=switchBackhaulForm.getShowDisconnectCheckbox();
String [] selectedFrom=request.getParameterValues("selectedFromCircuit");
System.out.println("selectedFrom : ");
for(int i=0;i<selectedFrom.length;i++){
System.out.print(selectedFrom[i]+",");
}
if(showDisconnectCheckbox){
if(selectedFrom !=
null &&
selectedFrom.length > 1){
logger.info("***Checkbox value is
True***" +
selectedFrom[0]);
switchBackhaulForm.setSelectedFromCircuit(selectedFrom[0]);
}
}
else{
if(selectedFrom !=
null &&
selectedFrom.length > 1){
logger.info("***Checkbox value is
False***" +
selectedFrom[1]);
switchBackhaulForm.setSelectedFromCircuit(selectedFrom[1]);
}
}
selectedFrom=null;
/* Added for
Display all checkbox change */
//get the
status of the displayAll checkbox
boolean displayAllCheckbox=switchBackhaulForm.isDisplayAllCheckbox();
//collect the
values of checkboxes
String [] selectedTo=request.getParameterValues("selectedToCircuit");
System.out.println("selectedTo : ");
for(int i=0;i<selectedTo.length;i++){
System.out.print(selectedTo[i]+",");
}
//chk the
vallue of checkbox
if(displayAllCheckbox){
if(null != selectedTo && selectedTo.length > 1){
logger.info("*** Display All Checkbox value
is True ***" +
selectedTo[0]);
//if checkbx
is checked get the circuit from total circuits
switchBackhaulForm.setSelectedToCircuit(selectedTo[0]);
}
}
else{
if(null != selectedTo && selectedTo.length > 1){
logger.info("*** Display All Checkbox value
is False ***" + selectedTo[1]);
//if checkbx
is not checked get the circuit from matching circuits
switchBackhaulForm.setSelectedToCircuit(selectedTo[1]);
}
}
//make the
string array null
selectedTo=null;
/* End of
Addition */
String circuitId=switchBackhaulForm.getSelectedFromCircuit();
logger.info("Circuit ID :" + circuitId);
PortletContext portletContext=request.getPortletSession().getPortletContext(); //to get CPE Encap HashMap.
filterServices=helper.getFilterServicesList(switchBackhaulForm,circuitId,portletContext,userId);
List<ClientServiceFormBean> serviceDetail=helper.getSidsByCircuit(switchBackhaulForm,filterServices,circuitName);
request.setAttribute("details",serviceDetail);
filterServices=null;
System.out.println("set all the records....");
logger.info("Get Services :" + IConstant.EXITED);
break;
}
}
}
catch(Exception e)
{
StringWriter
sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.error("Exception occured in Switch Backhaul Action :" + sw.toString());
ActionErrors actionErrors = new ActionErrors();
ActionMessage
searchEntryError = null;
searchEntryError
= new
ActionMessage(IConstant.ERROR_COMMON,e.getMessage());
actionErrors.add(IConstant.PROVISIONING_EXCEPTION_ERROR_KEY,searchEntryError);
saveErrors(actionErrors,
request);
}
logger.info("Render Action : " + IConstant.EXITED);
return mapping.findForward(forward);
}
}
}
This HaulAction gets called when we click on the button and the if
condition gets executed and return all the records to the browser.Now in order
to use reverse ajax I have refactored the code to remote class
public class
SwitchBackHaulService implements Runnable{
private static String FUNCTION_NAME="fillTable";
private ServerContext sctx;
private WebContext wctx;
public SwitchBackHaulService() {
wctx=WebContextFactory.get();
ServletContext
servletContext = wctx.getServletContext();
sctx = ServerContextFactory.get(servletContext);
System.out.println("SwitchBackHaulService created");
}
public void run() {
getData();
}
private void getData(){
List<ClientServiceFormBean>
servicesDetails=fillServices();
String
currentPage = "/html/portlet/provisioning-webapp/switchbackhaul.jsp";
Collection<ScriptSession>
sessions = sctx.getScriptSessionsByPage(currentPage);
Util
util = new Util(sessions);
ResourceBundle
resource=ResourceBundle.getBundle("provgui_menu_items");
String
displayRecords=resource.getString("totalRecords");
int batch=Integer.parseInt(displayRecords);
int fromIndex = 0;
int toIndex = 0;
int size = servicesDetails.size();
for (int i=0; i<size/batch; i++) {
List<ClientServiceFormBean>
records= getRecordsToDisplay(servicesDetails,fromIndex,toIndex);
fromIndex=toIndex;
toIndex+=batch;
util.addFunctionCall(FUNCTION_NAME, records);
}
}
/**
* Getting Data from provgui_backend
*
*/
private List<ClientServiceFormBean> fillServices(){
List<ClientServiceFormBean>
serviceDetails=new
ArrayList<ClientServiceFormBean>();
System.out.println("Inside fillServices");
// the attributes which we got from
RenderRequest now getting from HttpServletRequest
ServletContext
context= wctx.getServletContext();
HttpServletRequest
request=wctx.getHttpServletRequest();
// the attributes which we got from
PortletSession now getting from session
HttpSession
session=wctx.getSession();
SwicthBackHaulForm
switchBackHaulForm=( SwicthBackHaulForm)session.getAttribute(“swicthBackHaulForm”);
String
forceEnabledCheckbox=request.getParameter("forceEnabledCheckbox");
System.out.println("forceEnabledCheckbox : "+forceEnabledCheckbox);
if(forceEnabledCheckbox!=null){
boolean forceEnabled=Boolean.parseBoolean(forceEnabledCheckbox);
//session.setAttribute("forceEnable",forceEnabled,PortletSession.APPLICATION_SCOPE);
session.setAttribute("forceEnable",forceEnabled);
}
// Commenting formBean needs to
uncomment later
//boolean
showDisconnectCheckbox=switchBackhaulForm.getShowDisconnectCheckbox();
String [] selectedFrom=request.getParameterValues("selectedFromCircuit");
System.out.println("selectedFrom : ");
for(int i=0;i<selectedFrom.length;i++){
System.out.print(selectedFrom[i]+",");
}
// Commenting formBean needs to uncomment later
if(showDisconnectCheckbox){
if(selectedFrom
!= null && selectedFrom.length > 1){
switchBackhaulForm.setSelectedFromCircuit(selectedFrom[0]);
}
}
else{
if(selectedFrom
!= null && selectedFrom.length > 1){
switchBackhaulForm.setSelectedFromCircuit(selectedFrom[1]);
}
}
selectedFrom=null;
/* Added for Display all checkbox change */
//get the status of the displayAll checkbox
// Commenting formBean needs to uncomment later
//boolean
displayAllCheckbox=switchBackhaulForm.isDisplayAllCheckbox();
//collect the values of checkboxes
String []
selectedTo=request.getParameterValues("selectedToCircuit");
System.out.println("selectedTo : ");
for(int i=0;i<selectedTo.length;i++){
System.out.print(selectedTo[i]+",");
}
// Commenting formBean needs to uncomment later
//chk the vallue of checkbox
if(displayAllCheckbox){
if(null != selectedTo && selectedTo.length > 1){
//if checkbx is checked get the
circuit from total circuits
switchBackhaulForm.setSelectedToCircuit(selectedTo[0]);
}
}
else{
if(null != selectedTo && selectedTo.length > 1){
//if checkbx is not checked get the
circuit from matching circuits
switchBackhaulForm.setSelectedToCircuit(selectedTo[1]);
}
}
//make the string array null
selectedTo=null;
/* End of Addition */
// Commenting formBean needs to uncomment later
String
circuitId=switchBackhaulForm.getSelectedFromCircuit();
//Needs to verify whether usedId can be fetched from
HttpServletRequest
String userId=PortletUtil.getUsername(request);
System.out.println("userId : "+userId);
String circuitName=(String)request.getParameter("hiddenLabel");
System.out.println("circuitName :"+circuitName);
//Needs to look at the following code
try {
SwitchBackhaulHelper helper=new SwitchBackhaulHelper();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//PortletContext
portletContext=request.getPortletSession().getPortletContext(); //to get CPE
Encap HashMap.
//Assuming ServletContext will work
ServletContext portletContext=wctx.getServletContext();
HashMap
encapMap=(HashMap)portletContext.getAttribute("cpeEncapMap");
System.out.println("encapMap : "+encapMap);
// Commenting formBean needs to uncomment later
ArrayList<ServiceInfo>
filterServices=getFilterServicesList(switchBackhaulForm,circuitId,portletContext,userId);
List<ClientServiceFormBean>
serviceDetail=helper.getSidsByCircuit(switchBackhaulForm,filterServices,circuitName);
return serviceDetail;
}
private List<ClientServiceFormBean>
getRecordsToDisplay(List<ClientServiceFormBean> servicesDetails,int fromIndex,int toIndex){
return servicesDetails.subList(fromIndex,
toIndex);
}
public void getServices(){
new Thread(this).start();
}
private ArrayList<ServiceInfo>
getFilterServicesList(SwitchBackhaulForm form, String circuitId,ServletContext
portletContext,String userId) throws Exception {
ProvisioningServerBusinessDelegate
delegate=new
ProvisioningServerBusinessDelegate();;
HashMap encapMap=null;
if(portletContext.getAttribute("cpeEncapMap")==null){
//Make a call to SI to get CPE Encap
HashMap.
encapMap=delegate.getCPEEncapMap(userId);
System.out.println("encapMap : "+encapMap);
portletContext.setAttribute("cpeEncapMap",encapMap);
}else{
encapMap=(HashMap)portletContext.getAttribute("cpeEncapMap");
}
long switchId=Long.parseLong(circuitId.trim());
Long ipType=null;
Long noOfIp=null;
Long noOfSids=null;
String
ipProfile=form.getSelectedIpProfile();
if (!(ipProfile.equals("-1"))) {
ipType=new
Long(ipProfile);
}
String serviceCategory=form.getSelectedServiceCategory();
String
serviceClass=form.getSelectedServiceClass();
boolean isDisconnected = form.getIsDisconnected();
if (serviceCategory.equals("-1")) {
serviceCategory=null;
}
if(serviceClass.equals("-1")){
serviceClass=null;
}
String
numOfIp=form.getNumOfIp();
String
numOfSids=form.getNumOfSids();
if (numOfIp.equals("")) {
noOfIp=null;
} else {
noOfIp=new Long(numOfIp);
}
if (numOfSids.equals("")) {
noOfSids=null;
} else {
noOfSids=new
Long(numOfSids);
}
ArrayList<ServiceInfo> nonFilterList =
delegate.getSidsByCircuit(switchId, ipType, serviceCategory, serviceClass,
noOfIp, noOfSids, isDisconnected,userId);
ArrayList<ServiceInfo> filterServicesList=new ArrayList<ServiceInfo>();
for(ServiceInfo
serviceInfo :
nonFilterList){
String serviceType=serviceInfo.getServiceType()!=null ? serviceInfo.getServiceType().toString() : null;
if
(serviceInfo.getServiceClass().equals("GENERIC") || (serviceType!=null && (serviceType.equals("DATA") ||serviceType.equals("SECONDARY_DATA") || serviceType.equals("VOICE") || serviceType.equals("VPN")))) {
/* Adding encapsulation Type Name into
the dto to display in jsp */
String
cpeEncapType = (String) encapMap.get(serviceInfo.getCpeEncapId());
serviceInfo.setCpeEncapType(cpeEncapType);
filterServicesList.add(serviceInfo);
}
}
nonFilterList=null;
return
filterServicesList;
}
}
Now my questions are
1.
The code
needs to get SwitchBackHaulForm which contains data.I have used struts creator
and able to get the SwitchBackHaulForm but with empty data.The
SwitchBackHaulForm doesn’t get populated with request parameters.How can I
do it?
2.
The
refactored code or the code in remote class (SwitchBackHaulService.java) needs
to access the attributes in PortletRequest,PortletSession and PortletContext.I
have tried to get it fromHttpSession and ServletContext they are coming as
null.How can I access the attributes in PortletRequest,PortletSession and
PortletContext?
3.
We are
using tiles for UI(the mainLayout.jsp will have reference to
header.jsp,footer.jsp and body.jsp ) and if I hard code the body.jsp page where
I need to send the data the size of the session is coming as null.If I am
coding in this way String currentPage=webcontext.getCurrentPage(); the size of
the session is not null but the current page is not as expected this might be
because of tiles layout.How can I give the correct value of the jsp page in
case of tiles?
Please let me know.
Thanks,
Pavan T | Software Engineer | Persistent
Systems Limited
pavan_t@... | Tel: +91 (20) 3023 4150
Innovation in software
product design, development and delivery - www.persistentsys.com