Solved

populate records into VO

Posted on 2011-02-24
17
302 Views
Last Modified: 2012-05-11
SQL query returns records as ..

project1,subproject1
project1,subproject2
project2,subproject1
project2,subproject2
project2,subproject3


I wish to populate this data into my VO class.

My VO class looks like this ..

class Project
{
private String name
// getter
//setter

List Subproject  // this is subproject
// getter
//setter
}

Whats the best way to populate this VO ?


Here is where I'm stuck


                 while (rs.next()) {
            
                  project = rs.getString(1);
                  subproject = rs.getString(2);
                       
                    // How do I populate the VO now ?
                    // Whats the best way to populate the VO ?

                  }
0
Comment
Question by:cofactor
  • 8
  • 8
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 34970504
Keep a record of the current value of the first column:
String colOneCurrent = "";

while(rs.next()) {
    String colOne = rs.getString(1);
    String colTwo = rs.getString(2);
    if (!colOne.equals(colOneCurrent)) {
        // Make new VO
        colOneCurrent = colOne;// Set new
    }
    else {
        // Same VO - add colTwo to list
    }
}

Open in new window

0
 
LVL 9

Expert Comment

by:ioanton
ID: 34970509
Try this:

Map<String, Project> map = new HashMap<String, Project>();

while (rs.next()) {
           
   project = rs.getString(1);
   subproject = rs.getString(2);

   Project p = map.get(project);
   if (p == null) {
      p = new Project();
      p.setName(project);
      map.put(project, p);
   }              
   p.getSubprojects().add(subproject);        
}
 
List<Project> projects = new ArrayList<Project>(map.values());
0
 

Author Comment

by:cofactor
ID: 34971952
>>   p.getSubprojects().add(subproject);  

I think, this will  throw NullPointerException.   Where do you initialize the List of Subproject   ?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 9

Expert Comment

by:ioanton
ID: 34972031
In the Project class:

List subprojects = new ArrayList();
0
 

Author Comment

by:cofactor
ID: 34976241
>>In the Project class:

No. I can not do there. I'm re-using the class. This is been used elsewhere. I have to do that in my logic.Where is the appropriate place ?
0
 

Author Comment

by:cofactor
ID: 34977081
CEHJ,

Could you please modify your code . I'm having difficulty populating the Subprojects in the else part.

something is missing in your code.


At the end  I would like to get List<Project> projects as similar to ioanton .  Could  you please modify your code so that it looks a better picture now.  I'm not sure how you are populating Project and Subproject List and so I'm in confusion.

0
 
LVL 9

Expert Comment

by:ioanton
ID: 34977625
Well, here is a workaround:

Map<String, Project> map = new HashMap<String, Project>();

while (rs.next()) {
           
   project = rs.getString(1);
   subproject = rs.getString(2);

   Project p = map.get(project);
   if (p == null) {
      p = new Project();
      p.setName(project);
      map.put(project, p);
   }              

   List subprojects = p.getSubprojects();

   if(subprojects == null) {
      subprojects == new ArrayList();
      p.setSubprojects(subprojects);
   }

   subprojects.add(subproject);
}
 
List<Project> projects = new ArrayList<Project>(map.values());
0
 

Author Comment

by:cofactor
ID: 34979372
That looks brilliant.

I have a  snippet of mockup  HTML  table  [code attached]

I wish to populate this mockup with data from my  Project VO class. Where do I put the loop so that it could generate the display properly.

In jsp , I get  List Projects = session.getAttribute("Projects")  // Projects is the list of Project VO's

I'm stuck with the looping .


..............
<tr>
                                                            <td rowspan="3" valign="middle" class="table_base_bg">Project1</td>
                                                            <td valign="middle" class="table_base_bg">1. Subproject1</td>
                                                          </tr>
                                                          <tr>
                                                            <td valign="middle" class="table_base_bg">2. Subproject2</td>
                                                          </tr>
                                                          <tr>
                                                            <td valign="middle" class="table_base_bg">3. Subproject3</td>
                                                          </tr>



<tr>
                                                            <td rowspan="3" valign="middle" class="table_base_bg">Project2</td>
                                                            <td valign="middle" class="table_base_bg">1. Subproject1</td>
                                                          </tr>
                                                          <tr>
                                                            <td valign="middle" class="table_base_bg">2. Subproject2</td>
                                                          </tr>
                                                          <tr>
                                                            <td valign="middle" class="table_base_bg">3. Subproject3</td>
                                                          </tr>
................

Open in new window

0
 
LVL 9

Expert Comment

by:ioanton
ID: 34979685
Are you using JSTL or just scriptlets?
0
 

Author Comment

by:cofactor
ID: 34985918
>>Are you using JSTL or just scriptlets?

Neither. I'll be using Struts 2 tag    i.e   <s:iterator>  tag.

If you are aware of Struts 2 tags , then its excellent. But , if you  dont know Struts 2 tags  ,  could you just show me using scriptlets ?  I'm stuck with the looping . see the <tr>,<td>'s carefully . Where do you put the loop ? this is the tricky part.


0
 
LVL 9

Expert Comment

by:ioanton
ID: 34986273
Here is a JSTL approach, whose output is the same as your html:

<c:forEach var="project" items=${projects}>
      <tr>
            <td valign="middle" class="table_base_bg">${project.name}</td>
            <td>
                  <table width="100%">
                        <c:forEach var="subproject" items="${project.subprojects}" varStatus="index">
                              <tr>
                                    <td>${index + 1}. ${subproject}</td>
                              </tr>
                        </c:forEach>                        
                  </table>
            </td>
      </tr>
</c:forEach>
0
 

Author Comment

by:cofactor
ID: 34987013
No.   This may not work.  It seems to me  this would crash the GUI layout. because you are not putting Projects and Subprojects in proper places.

For your convenience , I'm attaching  the GUI  and its relevant background HTML . Please  see the GUI layout  and the  relevant background html attached . I wish to populate this layout with my Project VO's


GUI
 subproject.png

 
<tr>
	<td colspan="2" class="table_right_border">
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
			<td width="72%" valign="middle" class="table_header_bg">Name of
			Project</td>
			<td width="24%" valign="middle" class="table_header_bg">Subproject</td>
		</tr>
		<tr>
			<td rowspan="3" valign="middle" class="table_base_bg">Project1</td>
			<td valign="middle" class="table_base_bg">1. Subproject1</td>
		</tr>
		<tr>
			<td valign="middle" class="table_base_bg">2. Subproject2</td>
		</tr>
		<tr>
			<td valign="middle" class="table_base_bg">3. Subproject3</td>
		</tr>
		<tr>
			<td rowspan="3" valign="middle" class="table_base_bg">Project2</td>
			<td valign="middle" class="table_base_bg">1. Subproject1</td>
		</tr>
		<tr>
			<td valign="middle" class="table_base_bg">2. Subproject2</td>
		</tr>
		<tr>
			<td valign="middle" class="table_base_bg">3. Subproject3</td>
		</tr>
	</table>
	</td>
</tr>

Open in new window

0
 
LVL 9

Expert Comment

by:ioanton
ID: 34988654
>>>It seems to me  this would crash the GUI layout

I can't figure out why do you say that. The logic is good, you only have to replace the JSTL tags with the appropriate Struts2 tags. At least, did you give it a go?
 
<tr>
      <td colspan="2" class="table_right_border">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <c:forEach var="project" items=${projects}>
                    <tr>
                             <td valign="middle" class="table_base_bg">${project.name}</td>
                             <td>
                                     <table width="100%">
                                             <c:forEach var="subproject" items="${project.subprojects}" varStatus="index">
                                                       <tr>
                                                                <td>${index + 1}. ${subproject}</td>
                                                       </tr>
                                             </c:forEach>                        
                                     </table>
                             </td>
                    </tr>
                </c:forEach>
      </table>
      </td>
</tr>
0
 

Author Comment

by:cofactor
ID: 34988718
Where is the header "Name of the Project"  and  "Subproject"  in your code  ?  In the HTML I had these headers.
0
 
LVL 9

Expert Comment

by:ioanton
ID: 34988737
Oh, is that a problem? Here they are:

<tr>
      <td colspan="2" class="table_right_border">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                       <th>Name of the Project</th>
                       <th>Subproject</th>
                <tr>
                <c:forEach var="project" items=${projects}>
                    <tr>
                             <td valign="middle" class="table_base_bg">${project.name}</td>
                             <td>
                                     <table width="100%">
                                             <c:forEach var="subproject" items="${project.subprojects}" varStatus="index">
                                                       <tr>
                                                                <td>${index + 1}. ${subproject}</td>
                                                       </tr>
                                             </c:forEach>                        
                                     </table>
                             </td>
                    </tr>
                </c:forEach>
      </table>
      </td>
</tr>
0
 
LVL 9

Accepted Solution

by:
ioanton earned 200 total points
ID: 34988741
Sorry, there is a typo on the line 7... please put </tr> instead of <tr>
0
 

Author Comment

by:cofactor
ID: 34991455
This works. Thanks.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question