Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: d0e7ce203583319aa4bff6a80261020e9654924f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*******************************************************************************
 * Copyright (c) 2003, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.j2ee.internal.ejb.provider;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationWrapper;
import org.eclipse.emf.common.notify.impl.NotificationImpl;
import org.eclipse.jst.j2ee.ejb.EJBJar;
import org.eclipse.jst.j2ee.ejb.EjbPackage;
import org.eclipse.jst.j2ee.ejb.EnterpriseBean;
import org.eclipse.jst.j2ee.ejb.Entity;
import org.eclipse.jst.j2ee.ejb.MessageDriven;
import org.eclipse.jst.j2ee.ejb.Session;
import org.eclipse.jst.j2ee.internal.J2EEVersionConstants;
import org.eclipse.jst.j2ee.internal.plugin.J2EEPlugin;
import org.eclipse.jst.j2ee.internal.provider.J2EEItemProvider;


/**
 * @author Sachin P Patel
 * 
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates. To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class GroupedEJBJarItemProvider extends EJBJarItemProvider {

	private GroupedSessionItemProvider sessionProvider = null;
	private GroupedEntityItemProvider entityProvider = null;
	private GroupedMessageItemProvider messageProvider = null;

	private static Hashtable sessionTable = new Hashtable();
	private static Hashtable entityTable = new Hashtable();
	private static Hashtable messageTable = new Hashtable();

	private static final String SESSION = EJBUIMessages.GroupedEJBJarItemProvider_UI_0;
	private static final String ENTITY = EJBUIMessages.GroupedEJBJarItemProvider_UI_1;
	private static final String MESSAGE = EJBUIMessages.GroupedEJBJarItemProvider_UI_2;

	//	Normally there is one instance of an item provider for all instances of the objecct
	//	in the tree. The item provider would be stateless. However, because we are doing tricks
	//	here, we are keeping track of all items this provider manages. The key in the map is the
	//	object (EJBJar), and the value is the children nodes that we inserted

	protected Map children = new HashMap();
	private boolean showAssemblyDescriptor;
	private boolean isDisposing;

	public GroupedEJBJarItemProvider(AdapterFactory adapterFactory, boolean showAssemblyDescriptor) {
		super(adapterFactory);
		this.showAssemblyDescriptor = showAssemblyDescriptor;
	}

	public Collection getChildren(Object object) {
		List result = initChildren(object);
		if (showAssemblyDescriptor) {
			if (((EJBJar) object).getAssemblyDescriptor() != null)
				result.add(((EJBJar) object).getAssemblyDescriptor());
		}
		return result;
	}

	protected List initChildren(Object object) {
		EJBJar ejbJar = (EJBJar) object;
		List allRootBeans = getAllRootBeans(ejbJar);

		boolean is20Jar = is20Jar(ejbJar);

		List localChildren = new ArrayList(5);

		List entityBeans = new ArrayList();
		List sessionBeans = new ArrayList();
		List messageBeans = new ArrayList();

		catagorizeBeans(allRootBeans, entityBeans, sessionBeans, messageBeans);
		orderBeans(entityBeans);
		orderBeans(sessionBeans);
		orderBeans(messageBeans);
		
		//ENABLE FOR: NOT TO SHOW EMPTY GROUPS
		//if (sessionBeans.size() > 0) {
		if (sessionTable.get(ejbJar) == null) {
			//create new item provider instance
			sessionProvider = new GroupedSessionItemProvider(adapterFactory, null, getImage(SESSION), object, sessionBeans);
			sessionTable.put(ejbJar, sessionProvider);
			localChildren.add(sessionProvider);
		} else {
			//use existing instance from table
			localChildren.add(sessionTable.get(ejbJar));
		}
		//}

		//ENABLE FOR: NOT TO SHOW EMPTY GROUPS
		//if (entityBeans.size() > 0) {
		if (entityTable.get(ejbJar) == null) {
			//create new item provider instance
			entityProvider = new GroupedEntityItemProvider(adapterFactory, null, getImage(ENTITY), object, entityBeans);
			entityTable.put(ejbJar, entityProvider);
			localChildren.add(entityProvider);
		} else {
			//use existing instance from table
			localChildren.add(entityTable.get(ejbJar));
		}
		//}

		//ENABLE FOR: NOT TO SHOW EMPTY GROUPS
		//if (is20Jar && messageBeans.size() > 0) {
		if (is20Jar) {
			if (messageTable.get(ejbJar) == null) {
				//create new item provider instance
				messageProvider = new GroupedMessageItemProvider(adapterFactory, null, getImage(MESSAGE), object, messageBeans);
				messageTable.put(ejbJar, messageProvider);
				localChildren.add(messageProvider);
			} else {
				//use existing instance from table
				localChildren.add(messageTable.get(ejbJar));
			}
		}
		//}

		children.put(object, localChildren);

		return localChildren;
	}

	protected void orderBeans(List beans) {
		Object[] beansArray = beans.toArray();
		Arrays.sort(beansArray, EJBNameComparator.singleton());
		beans.clear();
		beans.addAll(Arrays.asList(beansArray));
	}
	
	protected static List getAllRootBeans(EJBJar ejbJar) {
		return ejbJar.getEnterpriseBeans();
	}

	protected void catagorizeBeans(List allRootBeans, List entityBeans, List sessionBeans, List messageBeans) {
		for (int i = 0; i < allRootBeans.size(); i++) {
			if (((EnterpriseBean) allRootBeans.get(i)).isSession()) {
				sessionBeans.add(allRootBeans.get(i));
			} else if (((EnterpriseBean) allRootBeans.get(i)).isEntity()) {
				entityBeans.add(allRootBeans.get(i));
			} else if (((EnterpriseBean) allRootBeans.get(i)).isMessageDriven()) {
				messageBeans.add(allRootBeans.get(i));
			}
		}
	}

	static protected GroupedSessionItemProvider getSessionNode(Object object) {
		return (GroupedSessionItemProvider) sessionTable.get(object);
	}

	static protected GroupedEntityItemProvider getEntityNode(Object object) {
		return (GroupedEntityItemProvider) entityTable.get(object);
	}

	static protected GroupedMessageItemProvider getMessageNode(Object object) {
		return (GroupedMessageItemProvider) messageTable.get(object);
	}

	public Object getImage(String type) {
		if (type.equals(SESSION))
			return J2EEPlugin.getPlugin().getImage("sessionBean_obj"); //$NON-NLS-1$
		else if (type.equals(MESSAGE))
			return J2EEPlugin.getPlugin().getImage("message_bean_obj"); //$NON-NLS-1$
		else if (type.equals(ENTITY))
			return J2EEPlugin.getPlugin().getImage("entitybean_obj"); //$NON-NLS-1$
		else
			return null;
	}

	private boolean is20Jar(EJBJar ejbJar) {
		switch (ejbJar.getVersionID()) {
			case J2EEVersionConstants.EJB_1_0_ID :
			case J2EEVersionConstants.EJB_1_1_ID :
				return false;
			case J2EEVersionConstants.EJB_2_0_ID :
			case J2EEVersionConstants.EJB_2_1_ID :
			default :
				return true;
		}
	}

	public void notifyChanged(Notification notification) {
		if (notification.getEventType() == Notification.REMOVING_ADAPTER && notification.getOldValue() == this && !isDisposing) {
			removeTarget(notification);
			return;
		}
		if (notification.getFeature() == EjbPackage.eINSTANCE.getEJBJar_EnterpriseBeans()) {
			J2EEItemProvider provider = beansChanged((EJBJar) notification.getNotifier(), notification.getEventType(), notification.getOldValue(), notification.getNewValue(), notification.getPosition());

			//EJB's group has not been added yet, need to add group to tree before EJB can be added
			if (provider == null) {
				Notification msg = new NotificationImpl(Notification.ADD, null, getEJBItemProvider((EnterpriseBean) notification.getNewValue()), 1);
				NotificationWrapper notificationWrapper = new NotificationWrapper(notification.getNotifier(), msg);
				fireNotifyChanged(notificationWrapper);
				provider = beansChanged((EJBJar) notification.getNotifier(), notification.getEventType(), notification.getOldValue(), notification.getNewValue(), notification.getPosition());
			}

			//Fire notification for EJB add or remove
			NotificationWrapper notificationWrapper = new NotificationWrapper(provider, notification);
			fireNotifyChanged(notificationWrapper);

			//ENABLE FOR: NOT TO SHOW EMPTY GROUPS
			//If Group is empty remove the group
			/*
			 * if (provider != null && provider.getChildren().size() == 0) { Notification msg = new
			 * NotificationImpl(Notification.REMOVE, provider, null, 1); notificationWrapper = new
			 * NotificationWrapper(notification.getNotifier(), msg);
			 * fireNotifyChanged(notificationWrapper);
			 * 
			 * //Group is removed so flush out table entry if (provider instanceof
			 * GroupedSessionItemProvider) { sessionTable.remove(notification.getNotifier());
			 * provider = null; } else if (provider instanceof GroupedEntityItemProvider) {
			 * entityTable.remove(notification.getNotifier()); provider = null; } else if (provider
			 * instanceof GroupedMessageItemProvider) {
			 * messageTable.remove(notification.getNotifier()); provider = null; }
			 * 
			 * //If all groups are removed remove the extended children List allChildren = new
			 * ArrayList(this.getChildren((EJBJar) notification.getNotifier()));
			 * if(sessionTable.get(notification.getNotifier()) == null &&
			 * entityTable.get(notification.getNotifier()) == null &&
			 * messageTable.get(notification.getNotifier()) == null) { for(int i = 0; i <
			 * allChildren.size(); i++) { Notification message = new
			 * NotificationImpl(Notification.REMOVE, allChildren.get(i), null, 1);
			 * notificationWrapper = new NotificationWrapper(notification.getNotifier(), message);
			 * fireNotifyChanged(notificationWrapper); } this.getChildren((EJBJar)
			 * notification.getNotifier()).clear(); } }
			 */
		} else {
			super.notifyChanged(notification);
		}
	}

	protected J2EEItemProvider beansChanged(EJBJar ejbJar, int eventType, Object oldValue, Object newValue, int pos) {
		J2EEItemProvider provider = getItemProvider(ejbJar, oldValue, newValue);

		if (provider != null) {
			Collection grandChildren = provider.getChildren();

			switch (eventType) {
				case Notification.ADD : {
					if (!grandChildren.contains(newValue))
						grandChildren.add(newValue);

					break;
				}
				case Notification.ADD_MANY : {
					grandChildren.addAll((Collection) newValue);
					break;
				}
				case Notification.REMOVE : {
					grandChildren.remove(oldValue);
					break;
				}
				case Notification.REMOVE_MANY : {
					grandChildren.removeAll((Collection) oldValue);
					break;
				}
			}
		} else {
			//GroupedProvider for new bean does not exist, create one.
			List allRootBeans = getAllRootBeans(ejbJar);

			List entityBeans = new ArrayList();
			List sessionBeans = new ArrayList();
			List messageBeans = new ArrayList();

			catagorizeBeans(allRootBeans, entityBeans, sessionBeans, messageBeans);

			if (newValue instanceof Session) {
				sessionProvider = new GroupedSessionItemProvider(adapterFactory, null, getImage(SESSION), ejbJar, sessionBeans);
				sessionTable.put(ejbJar, sessionProvider);
			} else if (newValue instanceof Entity) {
				entityProvider = new GroupedEntityItemProvider(adapterFactory, null, getImage(ENTITY), ejbJar, entityBeans);
				entityTable.put(ejbJar, entityProvider);
			} else if (newValue instanceof MessageDriven) {
				messageProvider = new GroupedMessageItemProvider(adapterFactory, null, getImage(MESSAGE), ejbJar, messageBeans);
				messageTable.put(ejbJar, messageProvider);
			}
		}
		return provider;
	}

	static public J2EEItemProvider getEJBJarItemProvider(EJBJar ejbJar, Object bean) {
		J2EEItemProvider provider = null;
		if (ejbJar != null && bean != null) {
			if (bean instanceof Session) {
				provider = getSessionNode(ejbJar);
			} else if (bean instanceof Entity) {
				provider = getEntityNode(ejbJar);
			} else if (bean instanceof MessageDriven) {
				provider = getMessageNode(ejbJar);
			}
		}
		return provider;
	}

	static public J2EEItemProvider getEJBItemProvider(EnterpriseBean bean) {
		if (bean != null) {
			EJBJar ejbJar = bean.getEjbJar();
			return getEJBJarItemProvider(ejbJar, bean);
		}
		return null;
	}

	protected J2EEItemProvider getItemProvider(EJBJar ejbJar, Object oldValue, Object newValue) {
		if (newValue != null)
			return getEJBJarItemProvider(ejbJar, newValue);
		else if (oldValue != null)
			return getEJBJarItemProvider(ejbJar, oldValue);
		else
			return null;
	}

	public static boolean isRootBean(EnterpriseBean bean) {
		List allRootBeans = getAllRootBeans(bean.getEjbJar());
		if (allRootBeans != null && allRootBeans.contains(bean)) {
			return true;
		}
		return false;
	}

	//	Utility method for garbage collection - if EJBJar removed, remove
	//	all entires in table for EJBJar
	static public void flushOutTableEntriesForEJBJar(EJBJar ejbJar) {
		sessionTable.remove(ejbJar);
		entityTable.remove(ejbJar);
		messageTable.remove(ejbJar);
	}

	public boolean hasChildren(Object parent) {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.j2ee.internal.internal.ejb.provider.EJBJarItemProvider#removeTarget(org.eclipse.emf.common.notify.Notification)
	 */
	protected void removeTarget(Notification not) {
		if (not.getNotifier() instanceof EJBJar)
			flushOutTableEntriesForEJBJar((EJBJar) not.getNotifier());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.edit.provider.ItemProviderAdapter#dispose()
	 */
	public void dispose() {
		try {
			isDisposing = true;
			super.dispose();
		} finally {
			isDisposing = false;
		}
	}
}

Back to the top