Announcement Announcement Module
Collapse
No announcement yet.
Another checkbox question... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Another checkbox question...

    Good day all,

    My scenario is as follows:

    I'm displaying a list of accounts, obtained from the database. I'm using the accountId as the value of my checkbox, in order for the user to select which accounts must be "unlinked".

    My problem - I'm not even sure where to begin! How will I get the value of the selected accounts in my controller? Or is there another way?

    Thanks

    Below my code:

    JSP:
    Code:
    <spring:bind path="linkedAccounts.lsaList"> 
                 
                  <c:forEach items="${status.value.dataItems}" var="linkedAccount" varStatus="loop">
                      <tr class="<c:out value='${linkedAccount.rowClass}' /> ">
                      	<td width="10">                
                      	  <input type="checkbox" name="selectedAccount" value="<c:out value="${linkedAccount.dataItem.id}"/>" />
                      	</td>
                        <td width="150">                   
                          <c:out value="${linkedAccount.dataItem.friendlyName}" />
                        </td>
                        <td width="120">
                          <c:out value="${linkedAccount.dataItem.account.accountKey}" />
                        </td>
                        <td width="50">
                          <c:out value="${linkedAccount.dataItem.account.accountOfType.description}" />
                        </td>
                        <td width="50">
                          <c:out value="${linkedAccount.dataItem.associatedDescription}" />
                        </td>
                      </tr>
    
                    </c:forEach>                              
    </spring:bind>
    FormBackingObject:
    Code:
    import za.co.telkom.cmss.web.DataList;
    
    public class UnlinkAccountsFormObject {
    	
    	private DataList lsaList;
    
    	public DataList getLsaList() {
    		return lsaList;
    	}
    
    	public void setLsaList(DataList lsaList) {
    		this.lsaList = lsaList;
    	}
    DataList:
    Code:
    package za.co.telkom.cmss.web;
    
    import java.util.List;
    import java.util.Iterator;
    import java.util.LinkedList;
    
    /**
     * Represents a list of data items to display in a table. This class
     * provides the capability to display alternate rows in different colours.
     */
    public class DataList {
    
        // A list of DataListItem objects
        private List dataItems;
    
        public DataList(List domainObjects) {
            this.dataItems = new LinkedList();
            for (int i = 0; i < domainObjects.size(); i++) {
                DataListItem item = new DataListItem();
                item.setDataItem( domainObjects.get(i));
                item.setIndex(i);
                getDataItems().add(item);
                item.setRowIsEven((i % 2) == 0);
            }
        }
    
        /**
         * Called by the &lt;c:forEach&gt; JSTL tag in the JSP.
         *
         * @return a List of domain objects
         */
        public List getDataItems() {
            return this.dataItems;
        }
    
        public int getSize() {
            return dataItems.size();
        }
    
        /**
         *
         * @return the List of domain objects wrapped in this DataList.
         */
        public List getDomainItems() {
            List domainObjects = new LinkedList();
            for (Iterator iter = dataItems.iterator(); iter.hasNext();) {
                DataListItem listItem = (DataListItem) iter.next();
                domainObjects.add(listItem.getDataItem());
            }
            return domainObjects;
        }
    }
    Controller:

    Code:
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
    		LOG.debug("############### In UnlinkAccountsController.formBackingObject");
        	
    		UnlinkAccountsFormObject unlinkAccountsFormObject = new UnlinkAccountsFormObject();	
        	
        	HttpSession session = request.getSession(false);
            if (session == null) {
            	System.out.println("############### Portal Session not set - user is not logged in");
            } else {       	
            	//Determine whether an account was selected.  If so, get the account details and store
            	//it in session.  Then flow to the account details view
            	String destination = request.getParameter("destination");
            	if (destination == null){
            		destination = "";
            	}else{
            		//Get account values
            		Long parId = new Long(request.getParameter("parId"));   
            		Long accountId = new Long(request.getParameter("accountId"));   
    	            String friendlyName = new String(request.getParameter("friendlyName"));   
    	            String accountKey = new String(request.getParameter("accountKey"));    
    	            
    	            //Store Account information in session variables	
    	        	session.setAttribute("parId", parId);	
    	        	session.setAttribute("accountId", accountId);	
    	        	session.setAttribute("friendlyName", friendlyName);	
    	        	session.setAttribute("accountKey", accountKey);
    	        	
    	        	//Flow to account details
    	        	RedirectView rv = new RedirectView("myaccounts-account1");
    	        	rv.setContextRelative(true);
    	        	rv.setHttp10Compatible(false);
    	        	LOG.debug("@@@@@@@@@@@@@@@@@@@@@@@@@ will return new Redirected ModelAndView");
    	        	return new ModelAndView(rv);
            	}
            	
            	// now get a list of accounts linked to the user logged in.
            	Person person = (Person) session.getAttribute("portal.person");
            	
            	DataList lsaList = new DataList(getLinkedServiceAccountDao().getLinkedServiceAccount(person.getId()));     	
            	
            	
            	LOG.debug("@@@@@@@@@@@@@@@@@@@@@@@@@ lsaLIST");
            	LOG.debug("@@@@@@@@@@@@@@@@@@@@@@@@@ lsaLIST:" + lsaList.getSize());   		
            	
            	unlinkAccountsFormObject.setLsaList(lsaList);
            }
            return unlinkAccountsFormObject;
        }
        
        public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,
                Object command, BindException errors) throws Exception {
        	LOG.debug("############### In UnlinkAccountsController.onSubmit");
        	
        	// Only do the unlink if "Unlink accounts" button was clicked
        	String submitButton = request.getParameter("submitDone");
    	  	if (submitButton == null) {
    	  	  	submitButton = "";
    	    }
    	  	
    	  	if(submitButton.equalsIgnoreCase("Unlink accounts")){
    	  		LOG.debug("############### In UnlinkAccountsController.onSubmit - Unlink accounts button clicked");
    	  		HttpSession session = request.getSession(false);
    	  	    if (session == null) {
    	  	    	LOG.debug("############### Portal Session not set - user is not logged in");	
    	        }
    	  	    else {   
    	  	    	UnlinkAccountsFormObject unlinkAccountsFormObject = (UnlinkAccountsFormObject) command;
    	  	    	for (Iterator iter = unlinkAccountsFormObject.getLsaList().getDataItems().iterator(); iter.hasNext();) {
    	  	    		DataListItem dataListItem = (DataListItem) iter.next();
    	  	    		PersonAccountRelation personAccountRelation = (PersonAccountRelation) dataListItem.getDataItem();
    	  	    		LOG.debug("@@@@@@@@@@@@@@@@@@ selected item: " + personAccountRelation.isUnlinkSelection());
    	  	    	}
    	  	    	
    //	  	    	for (Iterator iter = getLinkedServiceAccountDao().getAccountByRelation(parId).iterator(); iter.hasNext();) {			          
    //					PersonAccountRelation personAccountRelation = (PersonAccountRelation) iter.next();          	
    //			        linkedServiceAccountDao.deletePersonAccountRelation(personAccountRelation);
    //			        LOG.debug("@@@@@@@@@@@@@@ No of account relations for account (" 
    //			    			+ personAccountRelation.getAccount().getAccountKey() 
    //			    			+ "): " +  getLinkedServiceAccountDao().getPersonAccountRelationByAccountID(personAccountRelation.getAccount().getId()).size()  );
    //			        if (getLinkedServiceAccountDao().getPersonAccountRelationByAccountID(personAccountRelation.getAccount().getId()).size() == 0){					  
    //			        	getLinkedServiceAccountDao().deleteAccount(personAccountRelation.getAccount());
    //				    }
    //				}
    	  	    }
      	    	
      	    }
    	    
    	  	RedirectView rv = new RedirectView("linkedServiceAccount");
    		rv.setContextRelative(true);
    		rv.setHttp10Compatible(false);
    		LOG.debug("############### will return new Redirected ModelAndView");
    		return new ModelAndView(rv);
        }
Working...
X