Display data from Custom Query(Joined tables) in liferay search container - mysql

I have followed this wiki and have successfully built a custom query.
It works fine. I have used a join between tables.
My question is how do I display it on a jsp using liferay search container since className in search container requires one model class.
EDIT:
What I have tried till now is this:
<%
getAttendanceData attName = new getAttendanceData();
List<Object[]> displayAttListName = AttendanceLocalServiceUtil.findAttendance();
ArrayList name = new ArrayList();
ArrayList title = new ArrayList();
ArrayList status = new ArrayList();
ArrayList remarks = new ArrayList();
for(Object[] att:displayAttListName) {
name.add(att[0]);
title.add(att[1]);
status.add(att[2]);
remarks.add(att[3]);
}
%>
<liferay-ui:search-container delta="20" emptyResultsMessage="No Results Found">
<liferay-ui:search-container-results
total="<%= displayAttListName.size() %>"
results="<%= ListUtil.subList(displayAttListName , searchContainer.getStart(), searchContainer.getEnd()) %>"
/>
<liferay-ui:search-container-row modelVar="search"
className="java.lang.Object">
<%
for(Object displayName:name) {
%>
<liferay-ui:search-container-column-text name='studName' value = '<%=String.valueOf(displayName)%>' href="">
</liferay-ui:search-container-column-text>
<%
}
%>
</liferay-ui:search-container-row>
<liferay-ui:search-iterator/>
</liferay-ui:search-container>
What I have done above displays each of the 10 names 10 times in a column.
I want the names to appear on each new row. How shoould I modify the above code?
EDIT 2
Considering the name array defines earlier I did the following:
<liferay-ui:search-container-column-text name="employee name" href = "">
<%=name.getClass().getDeclaredFields().toString() %>
</liferay-ui:search-container-column-text>
With the above, I am getting the result something like: [Ljava.lang.reflect.Field;#195f1af on each row.

I see that the name, title, status and remarks field are all String (as per your comment) so in the for loop you should cast the Object as a String and you don't need the four ArrayList for this.
Here is how the row tag would look like:
<liferay-ui:search-container-row className="java.lang.Object" modelVar="search">
<%--
Since an "Object[]" is nothing but an "Object", we first cast the "search"
instance to an "Object[]" and then to a "String"
--%>
<liferay-ui:search-container-column-text name='name' value='<%= (String) ((Object[])search)[0] %>' />
<liferay-ui:search-container-column-text name='title' value='<%= (String) ((Object[])search)[1] %>' />
<liferay-ui:search-container-column-text name='status' value='<%= (String) ((Object[])search)[2] %>' />
<liferay-ui:search-container-column-text name='remarks' value='<%= (String) ((Object[])search)[3] %>' />
</liferay-ui:search-container-row>
There you go, this should work.
A more cleaner way I think would be to have a POJO defined that would store these values and then the POJO's list can be returned. I have not tried the second approach though.
Another standard approach is to include extra fields in any one of the entity's *Impl and then returning the list of that entity, in your case I would assume you have Student and Attendance entities, so you can put the fields status & remarks in StudentImpl and then return a List<Student> or put fname in AttendanceImpl and return List<Attendance> from the finder method. (updated after this comment)

Related

How to avoid null values from insertion into the database?

This question is related to this question.
I have created many input boxes using a for loop. from this whatever user type value, is get into another JSP page. from the input box values are getting properly.
But at the time of insertion that values into the database inserted as a blank or null. only if user type first input boxes in sequential order(first input box,second input box, third input box) then only value is getting inserted properly as per the expected results.
Even i'm checking the conditions also that is it contain null values or not. but still It'll will get inserted null values.
The main expected result is if user type particular value into the textbox then the value get inserted properly not the blank values get inserted into database.
<!-----new.jsp---------------------------------------------->
<%
for(int i = 0; i<ar.size(); i++)
{
%><span class="left-check"><%=ar.get(i)%></span><%
%>
<!--name=abc will be used in jsp to get value selected in checkboxes-->
<input id ="<%=idcounter%>" type="checkbox" name = "abc" value="<%=ar.get(i)%>" />
<input class = "left-marg-input7 size" id ="<%=idcounter%>" type="text" name = "abc_val" /><br><br>
<%
idcounter++;
}
%>
</center><br><br>
<center><button type= "submit" name="action" >SIGN UP</button></center>
</form>
</body>
</html>
Then the Next jsp page is :
<!------insertdata.jsp---------------------------------------------->
<html>
<%
String check[]= request.getParameterValues("abc");
String checkval[] = request.getParameterValues("abc_val");
String check_str = "";
String checkval_str = "";
if(check != null && checkval != null)
{
// there might be more one checkbox selected so, using loop
for(int i=0; i<check.length;i++)
{
// printing values selected from
check_str = check[i].toString();
checkval_str = checkval[i].toString();
if(checkval_str != null)
{
%><script>alert(<%=checkval_str%>);</script><%
st.executeUpdate("insert into user_assign_leave(org_email,user_email,leave_name,assign_leave,balance_leave)values('"+org_email+"','"+email+"','"+check_str+"','"+checkval_str+"','"+checkval_str+"')");
}
st.executeUpdate("insert into user_assign_leave(org_email,user_email,leave_name,assign_leave,balance_leave)values('"+org_email+"','"+email+"','"+check_str+"','"+checkval_str+"','"+checkval_str+"')");
}
}
st.executeUpdate("insert into user(org_name,org_email,name,email,mobile,custom_ID,pass)values('"+org_name+"','"+org_email+"','"+username+"','"+email+"','"+contactno+"','"+customer_id+"','"+password+"')");
%>
</body>
</html>

if statement not executed in JSP scriptlet

i encountered the following problem. I have made a table in mysql named 'users' and i want to add users using JSP. The primary key is the Email and i want to check before adding another user if the email already exists. If the email already exists the users will not be added to the database and a message "email already exists" will appear.
I created ResultSet rs object and extracted the "Email" column into a String email using a while loop.Now, here comes the problem. Suppose i have a user with the "denis#yahoo.com" email. If i try to add an user with the exact same email an error will occur which means my if statement was ignored. I looked up in the debug mode for the variables email and Email and they had the exact same "denis#yahoo.com" and still the if statement was ignored.
<!-- file name: addUser.jsp -->
<%
String Email=request.getParameter("Email");
String Name = request.getParameter("Name");
String Adress = request.getParameter("Adress");
String Phone = request.getParameter("Phone");
if (Email != null)
{
jb.connect(); // connect to the database using JavaBean class
ResultSet rs;
rs=jb.seeTable("users");// rs gets first row of table
String email;
int a=1;
while(rs.next())
{
email=rs.getString("Email");
if(Email==email)
{
a=0;
jb.disconnect();
%>
<p> The email already exists</p>
<%
break;
}
}
if(a==1){
jb.addUser(Email,Name, Adress, Phone);
jb.disconnect();
%>
<p>Data has been added.</p>
<%
}
} else {
%>
<form action="addUser.jsp" >
Email: <input type="text" name="Email"><br>
Name: <input type="text" name="Name"><br>
Adress: <input type="text" name="Adress"><br>
Phone: <input type="text" name="Phone"><br>
<button type="submit" >Add User</button><br>
</form>
<%
}
%>
I use NetBeans IDE 8.2, and something that i think is odd appeared in the debug mode. One of the email i try to test appeared with bold characters meanwhile the other was not bold.
Why is my if statement ignored if i add an user with the exact same email of another? Please help me out!

While Loop issue when querying Database in JSP

I am having trouble understanding why my codes does not work as I had thought. I am having issue in having my input text value to be that of my resultset value. Let me apologise for my bad explanation. Before I continue, I would like to mention that I am using JSP to run my application.
Say in my database, I have the following names, "Peter", "John" , "Simon" and when I queried, I will store their names in a hidden input text, as the names will be used for other usage. But I am facing the issue where by only "PETER" name is registered in the input text, as Peter is the first entry in the database.
Below is an abstrace of my while loop. If anyone know whats the underlying cause please let me know. Thanks in advance guys.
<%while (rs.next()) { %>
<tr><td>
<img alt="logo" src= "<%= rs.getString("ItemImage")%>" width="600" height = "400"/><button type="button" onclick="initialize2()" width="600" >Location</button>
</td>
<td>
<input type = 'hidden' id = 'itemName' value = "<%=rs.getString("ItemName")%>" />
<input type = "hidden" id = "address" value = "<%= rs.getString("ItemLocation")%>" />
<div id="<%= rs.getString("ItemName")%>" style="width: 600px; height: 400px;"></div>
</td>
</tr>
<% } %>
You are repeating the same id over and over which registers only one input text.
Try, for example:
<% string name = "itemName" + counter; %>
<input type='hidden' name="<%=name%>" value="<%=rs.getString("ItemName")%>" />

returning data from checkboxes in mvc 2 view

I'm using checkboxes in the view of my MVC2 project to allow users to select multiple objects. Here is the code in my view(skipping unrelated lines:
<h2>Install New Equipment</h2>
//Html.BeginForm("CreateRequest1", "Home", FormMethod.Post);
<div>Employee's First Name: <%= Model.Employee.EmpFName%></div>
<div>Employee's Last Name: <%= Model.Employee.EmpLName%></div>
<div>Employee's Phone Number: <%= Model.Employee.Phone%> </div>
<br />
<div>Please select the equipment you would like to request:</div><br />
<div> <% foreach (var info in ViewData.Model.EquipDescription)
{ %>
<% = Html.CheckBox("Description", info.ID) %><%=info.Description%> <br />
<%} %>
</div><br />
<div>Please Select the Location for the Equipment to be Installed </div><br />
<div>Building <%= Html.DropDownList("NewBuildings", new SelectList((IEnumerable)ViewData["buildings"], "ID", "Buildings")) %>
Floor <%= Html.DropDownList("NewFloors", new SelectList((IEnumerable)ViewData["floors"], "ID", "FloorNumber")) %>
Office<%= Html.DropDownList("NewOffices", new SelectList((IEnumerable)ViewData["offices"], "ID", "OfficeNumber")) %>
</div>
<br />
<div>Comments: <%=Html.TextArea("Comments") %></div><br />
<%Html.EndForm(); %>
(I removed the <%%> around the begin form line so my whole post would show)
Everything display perfectly in the view. I recieve all the other data from the user. I just don't know how to recieve the data from the selected checkboxes
[HttpPost]
public ActionResult CreateRequest1(int NewBuildings, int NewFloors, int NewOffices, string comments, int[] Description)
What should I add here to get the selected values?
You'll need to add a parameter to your method like:
[HttpPost]
public ActionResult CreateRequest1(int NewBuildings, int NewFloors, int NewOffices, string comments, ICollection Description)
I assume your value is an int, but you can change it if required.
You can read more here:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

How to display the contents of an arraylist in a drop down box

I have a sql statement that pulls infomration about tagnum's for individual pidm's. Every pidm can have multiple tagnum's so I am dropping the information into an arraylist. I want to display the contents of this arraylist in a dropdown box on a html page.
Here is the code for the arraylist:
<table style="border:transparent" style="width:100%">
<tr>
<td style ="width: 300px;">
<select style="width:150px;"tabindex="5" name="Tag">
<option></option>
<%} rscheck.close();
ResultSet rsTagCheck = stmt.executeQuery("SELECT PARKING.XKRPRMT.XKRPRMT_PIDM, PARKING.XKRPRMT.XKRPRMT_STATUS, PARKING.XKRPRMT.XKRPRMT_EXPIRE_YR, PARKING.XKRPRMT.XKRPRMT_TAG FROM PARKING.XKRPRMT WHERE XKRPRMT_PIDM ='" + BannerID + "'");
while (rsTagCheck.next()){
ArrayList<String> myTag = new ArrayList<String>();
myTag.add(rsTagCheck.getString("XKRPRMT_TAG"));
%>
<option><%= myTag.get(0) %></option>
</select>
</td>
I can get the first element to show in the drop down box, but anything after that show an outofbounds exception. I want to know how to display ALL of the information in the arraylist.
#Pointy I did that and all I got was this:
It put the first one in there, but the rest would not populate!!
There's no reason to create the array list at all.
while (rsTagCheck.next()) {
%>
<option><%= rsTagCheck.getString("XKRPRMT_TAG") %></option>
<%
}
edit — of course in practice you should be careful about what those strings might contain. If the strings come from some sort of user input, you shouldn't be just dumping them unwashed into the HTML. That's a whole other subject however.
Don't use scriptlets, use jstl tags and in this case
<c:forEach var="myTag" items="${rsTagCheck}">
and
<c:out value="${myTag.getString('XKRPRMT_TAG')}" />
Actually looking again at your code, I would not put the db query in a scriptlet! DB access should not be done here, pass the resultsets to jsp from servlet, and loop through data using jstl.