Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4ae32f131be925f84465c159893ed7e8fedf5608 (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
/*******************************************************************************
 * Copyright (c) 2001, 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.model.internal.validation;

import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.jem.java.Field;
import org.eclipse.jem.java.JavaClass;
import org.eclipse.jem.java.Method;
import org.eclipse.jst.j2ee.common.SecurityRole;
import org.eclipse.jst.j2ee.common.SecurityRoleRef;
import org.eclipse.jst.j2ee.ejb.AssemblyDescriptor;
import org.eclipse.jst.j2ee.ejb.CommonRelationshipRole;
import org.eclipse.jst.j2ee.ejb.EJBJar;
import org.eclipse.jst.j2ee.ejb.EJBRelation;
import org.eclipse.jst.j2ee.ejb.EJBRelationshipRole;
import org.eclipse.jst.j2ee.ejb.EnterpriseBean;
import org.eclipse.jst.j2ee.ejb.MethodElement;
import org.eclipse.jst.j2ee.ejb.MethodPermission;
import org.eclipse.jst.j2ee.ejb.MethodTransaction;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;



/**
 * @version 	1.0
 * @author
 */
public final class MessageUtility {
	public static final String NO_MESSAGE_ID = ""; //$NON-NLS-1$
	
	private static MessageUtility inst = null;

	private MessageUtility() {
		//Default
	}
	
	public static MessageUtility getUtility() {
		if(inst == null) {
			inst = new MessageUtility();
		}
		return inst;
	}
	
	public static String getGroupName(Object targetParent) {
		if(targetParent == null) {
			// No context to associate the message with
			return null;
		}
		
		// So far, the only target parent which is used as a group name is the EnterpriseBean
		if(targetParent instanceof EnterpriseBean) {
			EnterpriseBean bean = (EnterpriseBean)targetParent;
			if(bean == null) {
				return null;
			}
			
			if(bean.getName() != null) {
				return bean.getName();
			}
			return ((XMIResource)bean.eResource()).getID(bean);
		}
		
		return null;
	}
	
	protected String getMessageId(Object messageNumber, EnterpriseBean bean, IValidationRule rule) {
		if(messageNumber == null) {
			return NO_MESSAGE_ID;
		}
		
		Map ids = rule.getMessageIds();
		if((ids == null) || (ids.size() == 0)) {
			return messageNumber.toString();
		}
		
		int index = -1;

		String[] result = (String[])ids.get(messageNumber);
		if(result == null) {
			return messageNumber.toString();
		}
		else if(result.length == 1) {
			if(result[0].equals("")) { //$NON-NLS-1$
				return messageNumber.toString();
			}
			return result[0];
		}
		else {
			// By convention, this is the order of the entries in the String[]
			if(bean.isSession()) {
				index = 0;
			}
			else if(bean.isContainerManagedEntity()) {
				index = 1;
			}
			else if(bean.isBeanManagedEntity()) {
				index = 2;
			}
			else if(bean.isMessageDriven()) {
				index = 3;
			}
			else {
				// what the heck is it?
				return messageNumber.toString();
			}
	
			if((result.length <= index) || (result[index].equals(""))) { //$NON-NLS-1$
				return messageNumber.toString();
			}
			
			return result[index];
		}
	}
	
	/*
	 * Use this method when the message is not registered on a method.
	 */	
	protected String getMessageId(Object messageNumber, IValidationRule rule) {
		if(messageNumber == null) {
			return NO_MESSAGE_ID;
		}
		
		Map ids = rule.getMessageIds();
		if((ids == null) || (ids.size() == 0)) {
			return messageNumber.toString();
		}
		
		String[] result = (String[])ids.get(messageNumber);
		if((result == null) || (result.length == 0) || (result[0].equals(""))) { //$NON-NLS-1$
			return messageNumber.toString();
		}
		return result[0];
	}
	
	/*
	 * Use this method when the message is registered on a method.
	 */
	protected String getMessageId(Object messageNumber, IValidationRule rule, boolean isMethodOnClass) {
		if(messageNumber == null) {
			return NO_MESSAGE_ID;
		}
		
		Map ids = rule.getMessageIds();
		if((ids == null) || (ids.size() == 0)) {
			return messageNumber.toString();
		}
		
		String[] messages = (String[])ids.get(messageNumber);
		String result = null;
		if(messages != null) {
			if(isMethodOnClass && messages.length == 2) {
				result = messages[1];
			}
			else if (messages.length >= 1) {
				result = messages[0];
			}
		}
		
		if((result == null) || (result.equals(""))) { //$NON-NLS-1$
			return messageNumber.toString();
		}
		
		return result;
	}
	
	protected IMessage getMessage(IEJBValidationContext vc, int severity, String id, String[] parms, Object target, String groupName) {
		IMessage message = vc.getMessage();
		message.setSeverity(severity);
		message.setId(id);
		message.setParams(parms);
		message.setTargetObject(target);
		message.setGroupName(groupName);
		return message;
	}
	
	/** 
	 * For use only by the DD VRules.
	 */
	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EJBJar target, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, null, target, null);
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, SecurityRole target, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, null, target, null);
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, SecurityRoleRef target, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, null, target, null);
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, MethodElement target, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, null, target, null);
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, MethodTransaction target, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, null, target, null);
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, MethodPermission target, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, null, target, null);
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EJBRelationshipRole target, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, null, target, null);
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, CommonRelationshipRole target, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, null, target, null);
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EJBRelation target, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, null, target, null);
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, AssemblyDescriptor target, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, null, target, null);
	}

	/** 
	 * For use only by the DD VRules.
	 */
	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EJBJar target, String[] parms, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, parms, target, null);
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, MethodElement target, String[] parms, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, parms, target, null);
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EJBRelationshipRole target, String[] parms, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, parms, target, null);
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, SecurityRoleRef target, String[] parms, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, parms, target, null);
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EJBRelation target, String[] parms, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, parms, target, null);
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EnterpriseBean bean, Object target, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, null, target, getGroupName(bean));
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EnterpriseBean bean, Object target, String[] parms, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, parms, target, getGroupName(bean));
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EnterpriseBean bean, IValidationRule rule) {
		String id = getMessageId(messageNumber, bean, rule);
		return getMessage(vc, severity, id, null, bean, getGroupName(bean));
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EnterpriseBean bean, String[] parms, IValidationRule rule) {
		String id = getMessageId(messageNumber, bean, rule);
		return getMessage(vc, severity, id, parms, bean, getGroupName(bean));
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EnterpriseBean bean, JavaClass clazz, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, null, clazz, getGroupName(bean));
	}

	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EnterpriseBean bean, JavaClass clazz, String[] additionalParms, IValidationRule rule) {
		String id = getMessageId(messageNumber, rule);
		return getMessage(vc, severity, id, additionalParms, clazz, getGroupName(bean));
	}
	
	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EnterpriseBean bean, JavaClass clazz, Method method, IValidationRule rule) {
		if(method == null) {
			return getMessage(vc, messageNumber, severity, bean, clazz, rule);
		}
		
		return getMessage(vc, messageNumber, severity, null, clazz, method, method.getMethodElementSignature(), ValidationRuleUtility.onClass(clazz, method), getGroupName(bean), rule);
	}
		
	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EnterpriseBean bean, JavaClass clazz, Method method, String[] additionalParms, IValidationRule rule) {
		if(method == null) {
			return getMessage(vc, messageNumber, severity, bean, clazz, additionalParms, rule);
		}
		
		return getMessage(vc, messageNumber, severity, additionalParms, clazz, method, method.getMethodElementSignature(), ValidationRuleUtility.onClass(clazz, method), getGroupName(bean), rule);
	}
		
	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EnterpriseBean bean, JavaClass clazz, Field field, IValidationRule rule) {
		if(field == null) {
			return getMessage(vc, messageNumber, severity, bean, clazz, rule);
		}
		
		return getMessage(vc, messageNumber, severity, null, clazz, field, field.getName(), ValidationRuleUtility.onClass(clazz, field), getGroupName(bean), rule);
	}
		
	public IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, EnterpriseBean bean, JavaClass clazz, Field field, String[] additionalParms, IValidationRule rule) {
		if(field == null) {
			return getMessage(vc, messageNumber, severity, bean, clazz, additionalParms, rule);
		}
		
		return getMessage(vc, messageNumber, severity, additionalParms, clazz, field, field.getName(), ValidationRuleUtility.onClass(clazz, field), getGroupName(bean), rule);
	}
	
	protected IMessage getMessage(IEJBValidationContext vc, Object messageNumber, int severity, String[] additionalParms, JavaClass clazz, EObject fieldOrMethod, String fieldOrMethodName, boolean isMethodOnClass, String groupName, IValidationRule rule) {
		if(fieldOrMethod == null) {
			String id = getMessageId(messageNumber, rule);
			return getMessage(vc, severity, id, additionalParms, clazz, groupName);
		}
		
		String[] parms = null;
		EObject target = null;
		if(isMethodOnClass) {
			// leave parms null since no parms are needed
			target = fieldOrMethod;
			parms = additionalParms;
		}
		else {
			if(additionalParms == null) {
				parms = new String[]{fieldOrMethodName};
			}
			else {
				parms = new String[additionalParms.length + 1];
				parms[0] = fieldOrMethodName;
				System.arraycopy(additionalParms, 0, parms, 1, additionalParms.length);
			}
			target = clazz;
		}

		String id = getMessageId(messageNumber, rule, isMethodOnClass);
		return getMessage(vc, severity, id, parms, target, groupName);
	}
}

Back to the top