1 16 package org.apache.cocoon.forms.samples.dreamteam; 17 18 import java.util.ArrayList ; 19 import java.util.Hashtable ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.w3c.dom.Document ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.Node ; 26 import org.w3c.dom.NodeList ; 27 28 29 32 public class Manager { 33 private static Manager manager = new Manager(); 34 private List allPersons = null; 35 private Team dreamTeam = null; 36 37 private Manager() { 38 super(); 39 } 40 41 public static Manager getManager() { 42 if (manager == null) { 43 manager = new Manager(); 44 } 45 return manager; 46 } 47 public Hashtable getAllPersonsByPosition() { 48 Hashtable list = new Hashtable (); 49 Team team = null; 50 TeamMember teamMember = null; 51 String position = null; 52 for (int i = 0; i < allPersons.size(); i++) { 53 teamMember = (TeamMember)allPersons.get(i); 54 position = teamMember.getPosition(); 55 if (list.containsKey(position)) { 56 team = (Team)list.get(position); 57 } 58 else { 59 team = new Team(); 60 list.put(position, team); 61 } 62 team.addMember(teamMember); 63 } 64 return list; 65 66 } 67 public TeamMember getPerson(String memberID) { 68 TeamMember member = null; 69 if (allPersons == null) { 70 return null; 71 } 72 for (Iterator iter = allPersons.iterator(); iter.hasNext();) { 73 member = (TeamMember) iter.next(); 74 if (member.getMemberId().equals(memberID)) { 75 return member; 76 } 77 } 78 return null; 79 } 80 81 public void buildDreamTeam(Team dreamlist) { 82 if (dreamTeam != null) { 83 dreamTeam.getTeam().clear(); 84 } 85 dreamTeam = new Team(); 86 TeamMember listMember = null; 87 TeamMember dreamMember = null; 88 for (Iterator iter = dreamlist.getTeam().iterator(); iter.hasNext();) { 89 listMember = (TeamMember) iter.next(); 90 dreamMember = getPerson(listMember.getMemberId()); 91 dreamTeam.addMember(dreamMember); 92 } 93 } 94 95 public Team getDreamTeam() { 96 if (dreamTeam == null) { 97 dreamTeam = new Team(); 98 dreamTeam.setTeam(allPersons); 99 } 100 return dreamTeam; 101 } 102 103 public void readPlayers(Document doc) { 104 NodeList players = doc.getElementsByTagName("player"); 105 Node player = null; 106 int size = players.getLength(); 107 for (int i = 0; i < size; i++) { 108 player = players.item(i); 109 addPlayer((Element )player); 110 } 111 } 112 113 private void addPlayer(Element player) { 114 if (allPersons == null) { 116 allPersons = new ArrayList (); 117 } 118 TeamMember member = null; 119 member = new TeamMember(); 120 member.setMemberId("" + (allPersons.size() + 1)); 121 member.setName(getElementValue(player, "name")); 122 member.setPosition(getElementValue(player, "position")); 123 member.setCountry(getElementValue(player, "country")); 124 allPersons.add(member); 126 } 127 128 private String getElementValue(Element element, String tag) { 129 String result = null; 130 NodeList nodes = element.getElementsByTagName(tag); 131 result = nodes.item(0).getFirstChild().getNodeValue(); 132 return result; 134 } 135 136 } 137 | Popular Tags |