001: package hero.client.manager;
002:
003: /*
004: *
005: * MListener.java -
006: * Copyright (C) 2003 Ecoo Team
007: * valdes@loria.fr
008: *
009: *
010: * This program is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU Lesser General Public License
012: * as published by the Free Software Foundation; either version 2
013: * of the License, or (at your option) any later version.
014: *
015: * This program is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: * GNU Lesser General Public License for more details.
019: *
020: * You should have received a copy of the GNU Lesser General Public License
021: * along with this program; if not, write to the Free Software
022: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
023: */
024:
025: import hero.interfaces.Constants;
026: import hero.util.EventConstants;
027: import hero.client.events.JMSServicesClient;
028: import hero.util.BonitaClient;
029:
030: import java.util.ArrayList;
031: import java.util.Collection;
032: import java.util.Hashtable;
033:
034: import javax.jms.Message;
035: import javax.jms.MessageListener;
036:
037: public class MListener implements MessageListener, Constants.Nd,
038: EventConstants {
039:
040: ArrayList projectListener = new ArrayList();
041: ArrayList todoListener = new ArrayList();
042: ArrayList activityListener = new ArrayList();
043:
044: Collection projectele;
045: Collection todoele;
046: Collection activityele;
047:
048: BonitaClient soapclient;
049: String actualproject = "nullproject";
050: String userName = null;
051:
052: private static MListener mjms = null; // Singleton pattern
053:
054: public MListener(String userName, String userPassword, String host,
055: int port) {
056: try {
057: soapclient = new BonitaClient(host, port,
058: "/bonita_ws/services/");
059: soapclient.login(userName, userPassword);
060: this .userName = userName;
061: } catch (Exception e) {
062: e.printStackTrace();
063: }
064: }
065:
066: public static MListener getInstance(String userName,
067: String userPassword, String host, int port) {
068: if (mjms == null) {
069: mjms = new MListener(userName, userPassword, host, port);
070: }
071: return mjms;
072: }
073:
074: public void subscription(String subs) throws Exception {
075: //JMSServicesClient jms = JMSServicesClient.getInstance();
076: JMSServicesClient jms = new JMSServicesClient();
077: jms.createSubscription(this , subs);
078: }
079:
080: public void addEventProjectListener(JProjectList list) {
081: projectListener.add(list);
082: }
083:
084: public void removeEventProjectListener(JProjectList list) {
085: int index = projectListener.indexOf(list);
086: projectListener.remove(index);
087: }
088:
089: public void addEventTodoListener(JTodoList list) {
090: todoListener.add(list);
091: }
092:
093: public void removeEventTodoListener(JTodoList list) {
094: int index = todoListener.indexOf(list);
095: todoListener.remove(index);
096: }
097:
098: public void addEventActivityListener(JActivityList list) {
099: activityListener.add(list);
100: }
101:
102: public void removeEventActivityListener(JActivityList list) {
103: int index = activityListener.indexOf(list);
104: activityListener.remove(index);
105: }
106:
107: public void onMessage(Message message) {
108: try {
109: Hashtable evt = JMSServicesClient.getEvt(message);
110:
111: String type = (String) evt.get(EVENT);
112: if (type.equals(EventConstants.START)
113: || type.equals(EventConstants.TERMINATED)
114: || type.equals(EventConstants.CANCELED)
115: || type.equals(EventConstants.ANTICIPATING)
116: || type.equals(CHANGEROLE)) {
117:
118: Collection todoele = soapclient
119: .getToDoList((String) evt.get(PROJECTNAME));
120: Collection activityele = soapclient
121: .getActivityList((String) evt.get(PROJECTNAME));
122:
123: try {
124: int i = 0;
125: for (i = 0; i < todoListener.size(); i++)
126: ((JTodoList) todoListener.get(i)).changeEvent(
127: todoele.toArray(), (String) evt
128: .get(PROJECTNAME));
129:
130: for (i = 0; i < activityListener.size(); i++)
131: ((JActivityList) activityListener.get(i))
132: .changeEvent(activityele.toArray(),
133: (String) evt.get(PROJECTNAME));
134:
135: } catch (Exception e) {
136:
137: }
138: } else if ((type.equals(DELETENODE) && ((new Integer(
139: (String) evt.get(NODESTATE))).intValue() == READY || (new Integer(
140: (String) evt.get(NODESTATE))).intValue() == ANTICIPABLE))
141: || type.equals(READYINITIAL)
142: || type.equals(ADDEDGE)
143: || type.equals(DELETEEDGE)
144: || type.equals(SETUSERROLE)) {
145: Collection todoele = soapclient
146: .getToDoList((String) evt.get(PROJECTNAME));
147:
148: try {
149: int i = 0;
150: for (i = 0; i < todoListener.size(); i++) {
151: if (((JTodoList) todoListener.get(i))
152: .isChanged(todoele.toArray()))
153: ((JTodoList) todoListener.get(i))
154: .changeEvent(todoele.toArray(),
155: (String) evt
156: .get(PROJECTNAME));
157: }
158:
159: } catch (Exception exmu) {
160: exmu.printStackTrace();
161: }
162:
163: } else if (type.equals(ADDNODE)) {
164: Collection todoele = soapclient
165: .getToDoList((String) evt.get(PROJECTNAME));
166:
167: try {
168: int i = 0;
169: for (i = 0; i < todoListener.size(); i++) {
170: if (((JTodoList) todoListener.get(i))
171: .isChanged(todoele.toArray()))
172: ((JTodoList) todoListener.get(i))
173: .changeEvent(todoele.toArray(),
174: (String) evt
175: .get(PROJECTNAME));
176: }
177:
178: } catch (Exception exaddnode) {
179: exaddnode.printStackTrace();
180: }
181: } else if (type.equals(ADDUSERPROJECT)) {
182: try {
183: int i = 0;
184: for (i = 0; i < projectListener.size(); i++) {
185: if (((String) evt.get(USERNAME))
186: .equals(userName)) {
187: ((JProjectList) projectListener.get(i))
188: .addElement((String) evt
189: .get(PROJECTNAME));
190: this .subscription(" projectName = '"
191: + (String) evt.get(PROJECTNAME)
192: + "'");
193: }
194: }
195:
196: } catch (Exception exadduser) {
197: exadduser.printStackTrace();
198: }
199: } else if (type.equals(DELETEPROJECT)) {
200: try {
201: int i = 0;
202: // Remove project from the project list and dispose the frame if exists.
203: for (i = 0; i < projectListener.size(); i++) {
204: ((JProjectList) projectListener.get(i))
205: .changeEvent(new Object[] {},
206: (String) evt.get(PROJECTNAME));
207: hero.client.grapheditor.Frame projectFrame = (hero.client.grapheditor.Frame) ((JProjectList) projectListener
208: .get(i)).projects.get(evt
209: .get(PROJECTNAME));
210: if (projectFrame != null)
211: projectFrame.dispose();
212: ((JProjectList) projectListener.get(i)).projects
213: .remove(evt.get(PROJECTNAME));
214: ((JTodoList) todoListener.get(i)).changeEvent(
215: new Object[] {}, (String) evt
216: .get(PROJECTNAME));
217: ((JActivityList) activityListener.get(i))
218: .changeEvent(new Object[] {},
219: (String) evt.get(PROJECTNAME));
220: }
221: } catch (Exception exadduser) {
222: exadduser.printStackTrace();
223: }
224: }
225: } catch (Exception jmse) {
226: }
227:
228: }
229: }
|