1 package org.telscenter.pas.otrunk.skeleton.controllers;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import java.util.Arrays;
6 import java.util.List;
7
8 import org.apache.commons.collections.ListUtils;
9 import org.concord.framework.otrunk.DefaultOTController;
10 import org.concord.framework.otrunk.OTChangeEvent;
11 import org.concord.framework.otrunk.OTChangeListener;
12 import org.concord.framework.otrunk.OTObject;
13 import org.concord.framework.otrunk.OTObjectList;
14 import org.concord.framework.otrunk.OTObjectService;
15 import org.telscenter.pas.beans.PasActivity;
16 import org.telscenter.pas.beans.PasProject;
17 import org.telscenter.pas.beans.PasStep;
18 import org.telscenter.pas.otrunk.OTrunkStep;
19 import org.telscenter.pas.otrunk.skeleton.OTPasActivity;
20 import org.telscenter.pas.otrunk.skeleton.OTPasHint;
21 import org.telscenter.pas.otrunk.skeleton.OTPasProject;
22
23 public class OTPasProjectController extends DefaultOTController {
24
25 public static Class<?>[] realObjectClasses = { PasProject.class };
26 public static Class<? extends OTObject> otObjectClass = OTPasProject.class;
27 private OTChangeListener otListener;
28 private PropertyChangeListener pListener;
29
30 public void loadRealObject(Object realObject) {
31 PasProject project = (PasProject) realObject;
32 OTPasProject otProject = (OTPasProject) otObject;
33
34
35
36
37 project.setTitle(otProject.getTitle());
38
39
40 project.setAuthorNotes(otProject.getAuthorNotes());
41
42
43
44 OTObjectList activityList = otProject.getActivityList();
45 PasActivity[] activities = new PasActivity[activityList.size()];
46 for (int i = 0; i < activities.length; i++) {
47 OTPasActivity otActivity = (OTPasActivity) activityList.get(i);
48 activities[i] = (PasActivity) controllerService
49 .getRealObject(otActivity);
50 }
51 project.setActivities(activities);
52
53 }
54
55 public void registerRealObject(Object realObject) {
56 final PasProject project = (PasProject) realObject;
57 final OTPasProject otPasProject = (OTPasProject) otObject;
58
59 otListener = new OTChangeListener() {
60
61 public void stateChanged(OTChangeEvent e) {
62 String operation = e.getOperation();
63 System.out.println("PROJECT OPERATION: " + operation);
64
65 }
66 };
67
68 pListener = new PropertyChangeListener() {
69
70 public void propertyChange(PropertyChangeEvent evt) {
71
72
73
74
75
76
77 if( project.getActivities().length < otPasProject.getActivityList().size() ) {
78 Object deltaActivity = deltaArrays((Object[])evt.getOldValue(), (Object[])evt.getNewValue());
79 OTObject otActivity = controllerService.getOTObject(deltaActivity);
80 otPasProject.getActivityList().remove(otActivity);
81 } else if( project.getActivities().length > otPasProject.getActivityList().size() ) {
82 Object newActivity = deltaArrays((Object[])evt.getOldValue(), (Object[])evt.getNewValue());
83 OTObject otActivity = controllerService.getOTObject(newActivity);
84 otPasProject.getActivityList().add(otActivity);
85 }
86
87 }
88
89 };
90
91 project.addPropertyChangeListener(PasProject.ACTIVITIES,pListener);
92 otPasProject.addOTChangeListener(otListener);
93 }
94
95 /***
96 * deltas the two arrays and returns the delta
97 *
98 * @param a - an array
99 * @param b - an array
100 * @return
101 */
102 public static Object deltaArrays(Object[] a, Object[] b) {
103 List oldStepList = Arrays.asList(a);
104 List newStepList = Arrays.asList(b);
105 List subtractedList = null;
106
107
108
109 if(oldStepList.size() > newStepList.size()) {
110 subtractedList = ListUtils.subtract(oldStepList, newStepList);
111 } else if(newStepList.size() > oldStepList.size()) {
112 subtractedList = ListUtils.subtract(newStepList, oldStepList);
113 }
114
115 if(subtractedList != null && !subtractedList.isEmpty()) {
116 return subtractedList.get(0);
117 }
118 return null;
119 }
120
121 public void saveRealObject(Object realObject) {
122 PasProject project = (PasProject) realObject;
123 OTPasProject otProject = (OTPasProject) otObject;
124
125
126
127 otProject.setTitle(project.getTitle());
128
129
130 otProject.setAuthorNotes(project.getAuthorNotes());
131
132
133
134 PasActivity[] activities = project.getActivities();
135 OTObjectList activityList = otProject.getActivityList();
136 activityList.removeAll();
137
138 for (int i = 0; i < activities.length; i++) {
139 PasActivity activity = activities[i];
140 OTObject otActivity = controllerService.getOTObject(activity);
141 controllerService.saveRealObject(activity, otActivity);
142 activityList.add(otActivity);
143 }
144 project.setActivities(activities);
145 }
146
147 }