KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > samples > dreamteam > Manager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.samples.dreamteam;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Hashtable JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.w3c.dom.Document JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27
28
29 /**
30  * Manager
31  */

32 public class Manager {
33     private static Manager manager = new Manager();
34     private List JavaDoc 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 JavaDoc getAllPersonsByPosition() {
48         Hashtable JavaDoc list = new Hashtable JavaDoc();
49         Team team = null;
50         TeamMember teamMember = null;
51         String JavaDoc 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 JavaDoc memberID) {
68         TeamMember member = null;
69         if (allPersons == null) {
70             return null;
71         }
72         for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc doc) {
104         NodeList JavaDoc players = doc.getElementsByTagName("player");
105         Node JavaDoc player = null;
106         int size = players.getLength();
107         for (int i = 0; i < size; i++) {
108             player = players.item(i);
109             addPlayer((Element JavaDoc)player);
110         }
111     }
112
113     private void addPlayer(Element JavaDoc player) {
114         // convert the XML node to a TeamMember
115
if (allPersons == null) {
116             allPersons = new ArrayList JavaDoc();
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 // System.out.println(member.toString());
125
allPersons.add(member);
126     }
127
128     private String JavaDoc getElementValue(Element JavaDoc element, String JavaDoc tag) {
129         String JavaDoc result = null;
130         NodeList JavaDoc nodes = element.getElementsByTagName(tag);
131         result = nodes.item(0).getFirstChild().getNodeValue();
132 // System.out.println("element: " + tag + "=" + result);
133
return result;
134     }
135
136 }
137
Popular Tags