I don't actually iterate through the list. There is a JSF structure that takes care of the display. If you'd like to look at the code it's included below.
Some additional weird behavior is that if I use a select statement that selects on that third or forth column, I will find the record as it appears in the database. If I try to select all the records in the table, they come out corrupted in the manner pointed out in the original post. What's the deal?
Code:
package com.ibm.acin.beans;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
public class RelationshipManager implements java.io.Serializable {
private List logsjsf;
private Relationship[] relationship;
private String selection;
/**
* @return Returns the selection.
*/
public String getSelection() {
FacesContext facesContext = FacesContext.getCurrentInstance();
Map params = facesContext.getExternalContext().getRequestParameterMap();
String kk = (String) params.get("entryID");
System.out.println("getSelection entryID" + kk);
return kk;
}
/**
* @param selection
* The selection to set.
*/
public void setSelection(String selection) {
this.selection = selection;
}
public RelationshipManager() {
listWeight();
}
private static void isClosedOpen() {
if (HibernateUtil.getSessionFactory().isClosed()) {
HibernateUtil.getSessionFactory().openSession();
}
}
private static void open() {
HibernateUtil.getSessionFactory().openSession();
}
private static void close() {
HibernateUtil.getSessionFactory().close();
}
public static void deleteWeight(Relationship curBean) {
isClosedOpen();
HibernateUtil.getSessionFactory().getCurrentSession()
.beginTransaction();
HibernateUtil.getSessionFactory().getCurrentSession().delete(curBean);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
.commit();
}
public static void storeWeight(Relationship curBean) {
isClosedOpen();
HibernateUtil.getSessionFactory().getCurrentSession()
.beginTransaction();
HibernateUtil.getSessionFactory().getCurrentSession().save(curBean);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
.commit();
}
public static void updateWeight(Relationship curBean) {
isClosedOpen();
HibernateUtil.getSessionFactory().getCurrentSession()
.beginTransaction();
HibernateUtil.getSessionFactory().getCurrentSession().update(curBean);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
.commit();
}
private void listWeight() {
isClosedOpen();
HibernateUtil.getSessionFactory().getCurrentSession()
.beginTransaction();
List records = HibernateUtil.getSessionFactory().getCurrentSession()
.createQuery("from Relationship").list();
relationship = (Relationship[]) records
.toArray(new Relationship[records.size()]);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
.commit();
return;
}
/*
* This method gets all records with email of email and school id of
* schoolid. Then it searches through those records returned for roles
* passed in. For all those roles that exist in the records - false, for all
* those not found - true.
*
*/
private void findEntries(String schoolid, String email, boolean[] roles,
boolean[] foundroles, String[] ROLETYPE) {
isClosedOpen();
HibernateUtil.getSessionFactory().getCurrentSession()
.beginTransaction();
List records = HibernateUtil.getSessionFactory().getCurrentSession()
.createQuery(
"from Relationship as relationship where schoolid='"
+ schoolid + "' AND intranetid LIKE '" + email
+ "'").list();
relationship = null;
relationship = (Relationship[]) records
.toArray(new Relationship[records.size()]);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
.commit();
System.out.println(" OUTPUT ");
for(int i=0; i<relationship.length;i++){
System.out.println(" " + relationship[i].getRole() + " " + relationship[i].getSchoolid() + " " + relationship[i].getVolunteerid());
}
// User has not signed up before for this schoool, no entries
if (relationship.length == 0) {
// True means they haven't been found, so insert them
foundroles[0] = true;
foundroles[1] = true;
foundroles[2] = true;
foundroles[3] = true;
} else { // search for all of the roles
for (int i = 0; i < 4; i++) {
if (roles[i]) { // If the user signed up for this role
System.out.println(" ROLETYPE = " + ROLETYPE[i]);
if (searchLogsForRoleOf(ROLETYPE[i])) {
// we've found the thing, set false so we don't insert
// later
foundroles[i] = false;
} else {
// the role is not in the gathered records, set true to
// insert later
foundroles[i] = true;
}
} else{
// If the user has not attempted to sign up for this role, make sure to
// set to false to prevent insert later
foundroles[i] = false;
}
}
}
}
/**
*
* Returns true or false, depending on whether or not the roletype was found
* in the logs.
*
* @param string
* @return
*/
private boolean searchLogsForRoleOf(String role) {
for (int i = 0; i < relationship.length; i++) {
System.out.println(" " + relationship[i].getRole() + " " + role);
if (relationship[i].getRole().equalsIgnoreCase(role)) {
return true;
}
}
return false;
}
public static void addLogs(List newLogs) {
for (Iterator i = newLogs.iterator(); i.hasNext();) {
storeWeight((Relationship) i.next());
}
}
public static void deleteLogs(List delLogs) {
for (Iterator i = delLogs.iterator(); i.hasNext();) {
deleteWeight((Relationship) i.next());
}
}
public static void updateLogs(List delLogs) {
for (Iterator i = delLogs.iterator(); i.hasNext();) {
updateWeight((Relationship) i.next());
}
}
/**
* @return Returns the logs.
*/
public Relationship[] getLogs() {
//return (LogWeightBean[]) logs.toArray(new
// LogWeightBean[logs.size()]);
listWeight();
return relationship;
}
/**
* @param ivLogWeightBean
* The ivLogWeightBean to set.
*/
public void setLogs(Relationship[] beans) {
//logs = Arrays.asList(beans);
relationship = beans;
return;
}
/**
* @return Returns the logs.
*/
public List getLogsjsf() {
listWeight();
//return (LogWeightBean[]) logs.toArray(new
// LogWeightBean[logs.size()]);
if (logsjsf == null) {
logsjsf = Arrays.asList(relationship);
}
return logsjsf;
}
/**
* @param ivLogWeightBean
* The ivLogWeightBean to set.
*/
public void setLogsjsf(List logs) {
//Arrays.asList(logs);
logsjsf = logs;
return;
}
/**
* Returns the elements of List B that are not in List A
*
* @param A
* @param B
*/
private List compareLists(List A, List B) {
List newElems = new ArrayList();
for (Iterator i = B.iterator(); i.hasNext();) {
Relationship curBean = (Relationship) i.next();
if (!A.contains(curBean)) {
//elements that are not in List A
newElems.add(curBean);
}
}
return newElems;
}
private void createAndStoreRelationship(String schoolid, String userid,
String role, String verified) {
try {
HibernateUtil.getSessionFactory().getCurrentSession()
.beginTransaction();
Relationship theRelationship = new Relationship();
theRelationship.setSchoolid(schoolid);
theRelationship.setVolunteerid(userid);
theRelationship.setRole(role);
theRelationship.setOtherrole(verified);
HibernateUtil.getSessionFactory().getCurrentSession().save(
theRelationship);
HibernateUtil.getSessionFactory().getCurrentSession()
.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
}
public void addRelationship(String schoolid, String email, String role,
boolean verified) {
this.createAndStoreRelationship(schoolid, email, role, role+"X");
}
public String viewSelection() {
System.out.println("viewSelection - schoolid");
FacesContext facesContext = FacesContext.getCurrentInstance();
Map params = facesContext.getExternalContext().getRequestParameterMap();
String kk = (String) params.get("schoolid");
System.out.println(kk);
return kk;
}
/**
* @param string
*/
public Relationship getBeanByID(String schoolid) {
// TODO Auto-generated method stub
System.out
.println("getBeanByID - We are searching for the bean on school id = "
+ schoolid);
return null;
}
/**
* @param schoolid
* @param email
* @param string
* @return
*/
public void checkExistingRelationships(String schoolid, String email,
boolean[] roles, boolean[] foundroles, String[] ROLETYPE) {
this.findEntries(schoolid, email, roles, foundroles, ROLETYPE);
}
}