View Javadoc

1   /*
2    * Created on Apr 27, 2005, Copyright UC Regents
3    */
4   package org.telscenter.pas.service;
5   
6   import java.beans.beancontext.BeanContextChildSupport;
7   import java.util.ArrayList;
8   import java.util.Iterator;
9   import java.util.List;
10  
11  import net.sf.sail.core.entity.ISock;
12  
13  import org.telscenter.pas.beans.PasActivity;
14  import org.telscenter.pas.beans.PasProject;
15  import org.telscenter.pas.beans.PasStep;
16  import org.telscenter.pas.navigation.events.INavigationListener;
17  
18  public class NavigationService implements INavigationService {
19  
20  	PasStep currentStep;
21  
22  	private PasStep previousStep;
23  	private PasActivity currentActivity;
24  	private PasActivity previousActivity;
25  	private PasProject pasProject;
26  	private ISock<String> navigationLogSock;
27  	private ISock<String> sessionStateSock;
28  	private List<String> sessionState;
29  
30  	List<INavigationListener> navigationListeners = new ArrayList<INavigationListener>();
31  
32  	/***
33  	 * @param pasProject
34  	 */
35  	public NavigationService(PasProject project) {
36  		this.pasProject = project;
37  		sessionState = new ArrayList<String>();
38  	}
39  
40  	public void addNavigationListener(INavigationListener nl) {
41  		navigationListeners.add(nl);
42  	}
43  
44  	// FIXME this just returns null
45  	protected PasActivity getActivity(PasStep step) {
46  		return null;
47  	}
48  
49  	public PasStep getCurrentStep() {
50  		return currentStep;
51  	}
52  
53  	private void updateSessionState() {
54  		String currentStepID = getItemUUID(currentStep);
55  		String previousStepID = getItemUUID(previousStep);
56  		if (sessionState.contains(previousStepID))
57  			sessionState.remove(previousStepID);
58  		if (!sessionState.contains(currentStepID))
59  			sessionState.add(currentStepID);
60  		String result = "";
61  		for (int i = 0; i < sessionState.size(); i++) {
62  			String id = sessionState.get(i);
63  			if (i > 0)
64  				result += " ";
65  			result += id;
66  		}
67  		sessionStateSock = pasProject.getSessionStateSock();
68  		if (sessionStateSock != null)
69  			sessionStateSock.add(result);
70  	}
71  
72  	/***
73  	 * @param step
74  	 * @return
75  	 */
76  	private String getItemLog(String name, BeanContextChildSupport item) {
77  		return pasProject.getItemLog(name, item);
78  	}
79  
80  	private String getItemUUID(BeanContextChildSupport item) {
81  		return pasProject.getItemUUID(item);
82  	}
83  
84  	public void setCurrentStep(PasStep step) {
85  		this.currentStep = step;
86  		this.currentActivity = getActivity(currentStep);
87  		navigationLogSock = pasProject.getNavigationLogSock();
88  		if (navigationLogSock != null) {
89  			if (currentStep != previousStep) {
90  				navigationLogSock.add(getItemLog("step_close", previousStep));
91  				navigationLogSock.add(getItemLog("step_open", currentStep));
92  			}
93  			if (currentActivity != previousActivity) {
94  				navigationLogSock.add(getItemLog("activity_close",
95  						previousActivity));
96  				navigationLogSock.add(getItemLog("activity_open",
97  						currentActivity));
98  			}
99  		}
100 
101 		for (INavigationListener listener : navigationListeners) {
102 			listener.newCurrentStep(currentStep);
103 		}
104 		
105 		updateSessionState();
106 	}
107 
108 	public PasStep getPreviousStep() {
109 		return previousStep;
110 	}
111 
112 	public void setPreviousStep(PasStep previousStep) {
113 		this.previousActivity = getActivity(previousStep);
114 		this.previousStep = previousStep;
115 	}
116 }