KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mdarad > framework > util > struts > AbstractAction


1 /*
2     Mdarad-Toolobox is a collection of tools for Architected RAD
3     (Rapid Application Development) based on an MDA approach.
4     The toolbox contains frameworks and generators for many environments
5     (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
6     applications from a design Model
7     Copyright (C) 2004-2005 Elapse Technologies Inc.
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public
11     License as published by the Free Software Foundation; either
12     version 2.1 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17     General Public License for more details.
18
19     You should have received a copy of the GNU General Public
20     License along with this library; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */

23 package org.mdarad.framework.util.struts;
24
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.apache.commons.beanutils.ConvertUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.struts.Globals;
37 import org.apache.struts.action.ActionForm;
38 import org.apache.struts.action.ActionForward;
39 import org.apache.struts.action.ActionMapping;
40 import org.apache.struts.action.ActionMessage;
41 import org.apache.struts.action.ActionMessages;
42 import org.apache.struts.tiles.actions.TilesAction;
43 import org.dataisland.primitives.bean.Entity;
44 import org.dataisland.primitives.bean.EntityPrimaryKey;
45 import org.mdarad.framework.delegates.BusinessDelegate;
46 import org.mdarad.framework.exception.ConcurrencyException;
47 import org.mdarad.framework.exception.ServiceLocatorException;
48 import org.mdarad.framework.exception.SystemException;
49 import org.mdarad.framework.util.GUIDGenerationException;
50 import org.mdarad.framework.util.struts.contextstack.ContextStack;
51 import org.mdarad.framework.util.struts.contextstack.ContextStackElement;
52 import org.mdarad.framework.util.struts.contextstack.ContextUpdateException;
53 import org.mdarad.framework.util.struts.contextstack.OrphanChildException;
54
55 public abstract class AbstractAction extends TilesAction {
56     public static final String JavaDoc WEB_CONTEXT_LOCALIZATION_CONTEXT_KEY = "org.mdarad.framework.util.struts.AbstractAction.webLocalizationContext";
57     public static final String JavaDoc AGGREGATION_PARENT_FORWARD_PREFIX = "parent";
58     public static final String JavaDoc AGGREGATION_PARENT_FORWARD_SEPARATOR = "-";
59     public static final String JavaDoc SUCCESS_FORWARD_KEY = "success";
60     public static final String JavaDoc CANCEL_FORWARD_KEY = "cancel";
61     public static final String JavaDoc DISPLAY_FORWARD_KEY = "display";
62     public static final String JavaDoc FAILURE_FORWARD_KEY = "failure";
63     public static final String JavaDoc CHILD_AGGREGATION_FORWARD_PREFIX = "child";
64     public static final String JavaDoc CHILD_AGGREGATION_FORWARD_SEPARATOR = "-";
65     public final static String JavaDoc FORM_CHILD_ACTION_SUFFIX_PARAMETER_KEY = "form";
66     public final static String JavaDoc DELETE_CHILD_ACTION_SUFFIX_PARAMETER_KEY = "delete";
67     public final static String JavaDoc CONTEXT_STACK_USE_PARAMETER_KEY = "org.mdarad.framework.util.struts.AbstractAction.useContextStack";
68     public final static String JavaDoc ID_IN_REQUEST_ATTRIBUTE_PARAMETER_KEY = "org.mdarad.framework.util.struts.AbstractAction.idInRequestAttributes";
69
70
71     protected ActionForward forwardCancel(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse) {
72         ActionMessages actionMessages = new ActionMessages();
73         actionMessages.add(Globals.MESSAGE_KEY, new ActionMessage("org.mdarad.framework.global.forward.cancel.message"));
74         saveMessages(httpServletRequest, actionMessages);
75         return actionMapping.findForward(CANCEL_FORWARD_KEY);
76     }
77
78     protected ActionForward forwardSuccess(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse) {
79         ActionMessages actionMessages = new ActionMessages();
80         actionMessages.add(Globals.MESSAGE_KEY, new ActionMessage("org.mdarad.framework.global.forward.success.message"));
81         saveMessages(httpServletRequest, actionMessages);
82         return actionMapping.findForward(SUCCESS_FORWARD_KEY);
83     }
84
85     protected ActionForward forwardDisplay(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse) {
86         return actionMapping.findForward(DISPLAY_FORWARD_KEY);
87     }
88
89     protected ActionForward forwardFailure(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse) {
90         ActionMessages actionMessages = new ActionMessages();
91         actionMessages.add(Globals.ERROR_KEY, new ActionMessage("org.mdarad.framework.global.forward.failure.message"));
92         saveMessages(httpServletRequest, actionMessages);
93         return actionMapping.findForward(FAILURE_FORWARD_KEY);
94     }
95
96     protected ActionForward forwardDisplay(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, String JavaDoc forward) {
97         return actionMapping.findForward(forward);
98     }
99
100     protected ActionForward forwardChildAggregation(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, String JavaDoc childAggregationName, String JavaDoc childAggregationActionName) {
101         return actionMapping.findForward(getForwardAggregationChildName(childAggregationName, childAggregationActionName));
102     }
103
104     protected String JavaDoc getForwardAggregationChildName(String JavaDoc childAggregationName, String JavaDoc childAggregationActionName) {
105         StringBuffer JavaDoc forwardNameStringBuffer = new StringBuffer JavaDoc(CHILD_AGGREGATION_FORWARD_PREFIX);
106         forwardNameStringBuffer.append(CHILD_AGGREGATION_FORWARD_SEPARATOR);
107         forwardNameStringBuffer.append(childAggregationName);
108         forwardNameStringBuffer.append(CHILD_AGGREGATION_FORWARD_SEPARATOR);
109         forwardNameStringBuffer.append(childAggregationActionName);
110         return forwardNameStringBuffer.toString();
111     }
112     
113     protected ActionForward forwardAggregationParent(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, String JavaDoc aggregationParentName, String JavaDoc parentChildAggregationName, String JavaDoc aggregationParentForwardName) {
114         return actionMapping.findForward(getForwardAggregationParentName(aggregationParentName, parentChildAggregationName, aggregationParentForwardName));
115     }
116     
117     protected String JavaDoc getForwardAggregationParentName(String JavaDoc aggregationParentName, String JavaDoc parentChildAggregationName, String JavaDoc aggregationParentForwardName) {
118         StringBuffer JavaDoc forwardNameStringBuffer = new StringBuffer JavaDoc(AGGREGATION_PARENT_FORWARD_PREFIX);
119         forwardNameStringBuffer.append(AGGREGATION_PARENT_FORWARD_SEPARATOR);
120         forwardNameStringBuffer.append(aggregationParentName);
121         forwardNameStringBuffer.append(AGGREGATION_PARENT_FORWARD_SEPARATOR);
122         forwardNameStringBuffer.append(parentChildAggregationName);
123         forwardNameStringBuffer.append(AGGREGATION_PARENT_FORWARD_SEPARATOR);
124         forwardNameStringBuffer.append(aggregationParentForwardName);
125
126         return forwardNameStringBuffer.toString();
127     }
128
129     public void deleteContextObject(String JavaDoc contextStackKey, Entity entityBean, HttpServletRequest JavaDoc httpServletRequest) throws OrphanChildException, SystemException, ConcurrencyException {
130         ContextStack contextStack = getContextStack(contextStackKey, httpServletRequest);
131
132         if (contextStack == null) {
133             throw new OrphanChildException("The child is being saved out of context. The context stack cannot be null.");
134         }
135
136         try {
137             if(contextStack.size() > 1) {
138                 // Get "current" context stack element
139
ContextStackElement currentContextStackElement = (ContextStackElement) contextStack.peek();
140
141                 // Look for parent context stack element
142
ContextStackElement parentContextStackElement = (ContextStackElement) contextStack.get(contextStack.size() - 2);
143                 Entity parentObject = (Entity) parentContextStackElement.getEntityBeanInstance();
144                 Class JavaDoc parentClass = parentContextStackElement.getEntityBeanInstance().getClass();
145
146                 // For multiple aggregation must delete element if exists and then add it to the existing list
147
if(currentContextStackElement.isParentMultipleAggregation()) {
148                     // Due to an hibernate bug (?) you first have to nullify a children to update it.
149
EntityPrimaryKey entityPrimaryKey = entityBean.getPrimaryKey();
150
151                     // Get the existing children
152
String JavaDoc getAggregationCollectionMethodName = "get" + StringUtils.capitalise(currentContextStackElement.getParentAggregationName());
153                     Method JavaDoc getAggregationCollectionMethod = parentClass.getMethod(getAggregationCollectionMethodName, null);
154                     Collection JavaDoc aggregationCollectionMethod = (Collection JavaDoc) getAggregationCollectionMethod.invoke(parentObject, null);
155
156                     // Iterate through the collection to delete entity.
157
Iterator JavaDoc aggregationCollectionIterator = aggregationCollectionMethod.iterator();
158                     while(aggregationCollectionIterator.hasNext()) {
159                         Entity childAggregationEntity = (Entity) aggregationCollectionIterator.next();
160                         EntityPrimaryKey childAggregationentityPrimaryKey = childAggregationEntity.getPrimaryKey();
161                         if(entityPrimaryKey.equals(childAggregationentityPrimaryKey)) {
162                             aggregationCollectionMethod.remove(childAggregationEntity);
163                             break;
164                         }
165                     }
166                 } else {
167                     // Nullify
168
String JavaDoc setAggregation = "set" + StringUtils.capitalise(currentContextStackElement.getParentAggregationName());
169                     Class JavaDoc[] parameterTypes = new Class JavaDoc[] {entityBean.getClass()};
170                     Method JavaDoc setAggregationMethod = parentClass.getMethod(setAggregation, parameterTypes);
171                     Object JavaDoc[] parameters = new Object JavaDoc[1];
172                     setAggregationMethod.invoke(parentObject, parameters);
173                 }
174
175                 // delete
176
// Get the persistence facade instance
177
BusinessDelegate delegateInstance = currentContextStackElement.getDelegate();
178                 Class JavaDoc facadeClass = delegateInstance.getClass();
179
180                 //Invoke the save method from the persistence facade instance
181
String JavaDoc fullyQualifiedName = entityBean.getClass().getName();
182                 Class JavaDoc[] parameterTypes = new Class JavaDoc[]{entityBean.getClass()};
183                 String JavaDoc className = fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf('.') + 1, fullyQualifiedName.length());
184                 Method JavaDoc saveMethod = facadeClass.getMethod("save" + className, parameterTypes);
185                 Object JavaDoc[] parameters = new Object JavaDoc[]{entityBean};
186                 saveMethod.invoke(delegateInstance, parameters);
187
188             }
189         } catch (NoSuchMethodException JavaDoc e) {
190             throw new ContextUpdateException("The aggregated objects could not be saved", e);
191         } catch (IllegalAccessException JavaDoc e) {
192             throw new ContextUpdateException("The aggregated objects could not be saved", e);
193         } catch (InvocationTargetException JavaDoc e) {
194             Throwable JavaDoc nestedException = ((InvocationTargetException JavaDoc)e).getTargetException();
195             
196             if(nestedException instanceof SystemException) {
197                 throw (SystemException) nestedException;
198             } else {
199                 throw new ContextUpdateException("The aggregated objects could not be saved", e);
200             }
201         }
202
203         saveContextObjects(contextStack);
204
205     }
206
207     /**
208      * Save the context objects by updating the parent elements and saving the root entity.
209      *
210      * @param contextStackKey The key in the request to get access to the context stack
211      * @param httpServletRequest
212      * @throws OrphanChildException
213      * @throws ConcurrencyException
214      * @throws SystemException
215      */

216     public void saveContextObjectsByParent(String JavaDoc contextStackKey, Entity entityBean, HttpServletRequest JavaDoc httpServletRequest, boolean isNewInstance) throws OrphanChildException, SystemException, ConcurrencyException {
217         ContextStack contextStack = getContextStack(contextStackKey, httpServletRequest);
218
219         //Must add the entity
220
ContextStackElement contextStackElement = (ContextStackElement) contextStack.peek();
221         contextStackElement.setNewInstance(isNewInstance);
222         contextStackElement.setEntityBeanInstance(entityBean);
223         
224         if (contextStack == null) {
225             throw new OrphanChildException("The child is being saved out of context. The context stack cannot be null.");
226         }
227
228         saveContextObjectsByParent(contextStack);
229
230     }
231     
232     /**
233      * Save the context objects by updating the parent elements and saving the root entity.
234      *
235      * @param contextStack The context stack which contains the entities to the root
236      * @throws SystemException
237      * @throws ConcurrencyException
238      */

239     public void saveContextObjectsByParent(ContextStack contextStack) throws SystemException, ConcurrencyException {
240             updateContextObjects(contextStack);
241             saveContextObjects(contextStack);
242     }
243     
244     /**
245      * Save only the highest ranked parent (for the others will be saved by him)
246      *
247      * @param contextStack
248      * @param entityBean
249      * @throws SystemException
250      * @throws ConcurrencyException
251      */

252     protected void saveContextObjects(ContextStack contextStack) throws SystemException, ConcurrencyException {
253         try {
254             //Only save the parent of all entities
255
ContextStackElement parentContextStackElement = (ContextStackElement) contextStack.get(0);
256
257             Entity parentInstance = parentContextStackElement.getEntityBeanInstance();
258
259             // Get the persistence facade instance
260
BusinessDelegate parentDelegateInstance = parentContextStackElement.getDelegate();
261             Class JavaDoc parentFacadeClass = parentDelegateInstance.getClass();
262
263             //Invoke the save method from the persistence facade instance
264
String JavaDoc parentFullyQualifiedEntityName = parentContextStackElement.getParentFullyQualifiedEntityName();
265             Class JavaDoc[] parameterTypes = new Class JavaDoc[]{Class.forName(parentContextStackElement.getParentFullyQualifiedEntityName())};
266             String JavaDoc parentClassName = parentFullyQualifiedEntityName.substring(parentFullyQualifiedEntityName.lastIndexOf('.') + 1, parentFullyQualifiedEntityName.length());
267             Method JavaDoc saveParentMethod = parentFacadeClass.getMethod("save" + parentClassName, parameterTypes);
268             Object JavaDoc[] parameters = new Object JavaDoc[]{parentInstance};
269             saveParentMethod.invoke(parentDelegateInstance, parameters);
270         } catch (NoSuchMethodException JavaDoc e) {
271             throw new ContextUpdateException("The aggregated objects could not be saved", e);
272         } catch (IllegalAccessException JavaDoc e) {
273             throw new ContextUpdateException("The aggregated objects could not be saved", e);
274         } catch (InvocationTargetException JavaDoc e) {
275             Throwable JavaDoc nestedException = ((InvocationTargetException JavaDoc)e).getTargetException();
276             if(nestedException instanceof SystemException) {
277                 throw (SystemException) nestedException;
278             } else if(nestedException instanceof ConcurrencyException) {
279                 throw (ConcurrencyException) nestedException;
280             } else {
281                 throw new ContextUpdateException("The aggregated objects could not be saved", e);
282             }
283         } catch (ClassNotFoundException JavaDoc e) {
284             throw new ContextUpdateException("The aggregated objects could not be saved", e);
285         }
286     }
287
288     /**
289      * Get the context stack that is stored in the session
290      *
291      * @param contextStackKey
292      * @param httpServletRequest
293      * @return
294      */

295     static public ContextStack getContextStack(String JavaDoc contextStackKey, HttpServletRequest JavaDoc httpServletRequest) {
296         //Get Context Stack
297
//The id can be as a parameter or as an attribute
298
String JavaDoc contextStackIdString = httpServletRequest.getParameter(contextStackKey);
299         if(contextStackIdString == null ) {
300             contextStackIdString = (String JavaDoc) httpServletRequest.getAttribute(contextStackKey);
301         }
302         
303         String JavaDoc contextStackId = (String JavaDoc) ConvertUtils.convert(contextStackIdString, String JavaDoc.class);
304         return (ContextStack) httpServletRequest.getSession().getAttribute(contextStackId);
305     }
306     static public void setContextStack(String JavaDoc contextStackKey, ContextStack contextStack, HttpServletRequest JavaDoc httpServletRequest) {
307         //Get Context Stack
308
String JavaDoc contextStackId = contextStack.getId();
309         httpServletRequest.getSession().setAttribute(contextStackId, contextStack);
310         httpServletRequest.setAttribute(contextStackKey, contextStackId);
311     }
312     static public void nullifyContextStack(String JavaDoc contextStackKey, ContextStack contextStack, HttpServletRequest JavaDoc httpServletRequest) {
313         //Get Context Stack
314
String JavaDoc contextStackId = contextStack.getId();
315         httpServletRequest.getSession().removeAttribute(contextStackId);
316         httpServletRequest.removeAttribute(contextStackKey);
317     }
318     static public ContextStack createNewContextStack(String JavaDoc contextStackKey, HttpServletRequest JavaDoc httpServletRequest) {
319         ContextStack contextStack = null;
320         try {
321             contextStack = new ContextStack();
322             // Set Context Stack
323
String JavaDoc contextStackId = contextStack.getId();
324             httpServletRequest.getSession().setAttribute(contextStackId, contextStack);
325             httpServletRequest.setAttribute(contextStackKey, contextStackId);
326         } catch (GUIDGenerationException e) {
327             e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
328
}
329         return contextStack;
330     }
331
332     public Entity getCurrentEntityFromContextStack(String JavaDoc contextStackKey, HttpServletRequest JavaDoc httpServletRequest) {
333         ContextStack contextStack = getContextStack(contextStackKey, httpServletRequest);
334         Entity output = null;
335         if(contextStack != null && contextStack.size() > 0) {
336             ContextStackElement contextStackElement = (ContextStackElement) contextStack.get(contextStack.size() - 1);
337             if(contextStackElement != null) {
338                 output = contextStackElement.getEntityBeanInstance();
339             }
340         }
341         return output;
342     }
343
344     /**
345      * Redirect the context stack key in the request
346      *
347      * @param contextStackKey
348      * @param httpServletRequest
349      */

350     public void setContextStackIDInRequest(String JavaDoc contextStackKey, HttpServletRequest JavaDoc httpServletRequest) {
351         // Get Stack
352
String JavaDoc contextStackId = (String JavaDoc) httpServletRequest.getAttribute(contextStackKey);
353
354         if (contextStackId == null) {
355             String JavaDoc contextStackIdString = httpServletRequest.getParameter(contextStackKey);
356             contextStackId = (String JavaDoc) ConvertUtils.convert(contextStackIdString, String JavaDoc.class);
357             // Set context stack key in request
358
if(contextStackId != null) {
359                 httpServletRequest.setAttribute(contextStackKey, contextStackId);
360             }
361         }
362     }
363
364     protected void updateContextObjects(ContextStack contextStack) throws SystemException, ConcurrencyException {
365         try {
366             if(contextStack.size() > 1) {
367                 for(int i = contextStack.size()-1; i > 0; i--) {
368                     // Get "current" context stack element
369
ContextStackElement currentContextStackElement = (ContextStackElement) contextStack.get(i);
370                     boolean isNewInstance = currentContextStackElement.isNewInstance();
371                     Entity entityBean = currentContextStackElement.getEntityBeanInstance();
372     
373                     // Look for parent context stack element
374
ContextStackElement parentContextStackElement = (ContextStackElement) contextStack.get(i-1);
375                     Entity parentObject = (Entity) parentContextStackElement.getEntityBeanInstance();
376                     Class JavaDoc parentClass = parentContextStackElement.getEntityBeanInstance().getClass();
377     
378                     // For multiple aggregation must delete element if exists and then add it to the existing list
379
if(currentContextStackElement.isParentMultipleAggregation()) {
380                         if(!isNewInstance) {
381                             // Due to an hibernate bug (?) you first have to nullify a children to update it.
382
EntityPrimaryKey entityPrimaryKey = entityBean.getPrimaryKey();
383     
384                             // Get the existing children
385
String JavaDoc getAggregationCollectionMethodName = "get" + StringUtils.capitalise(currentContextStackElement.getParentAggregationName());
386                             Method JavaDoc getAggregationCollectionMethod = parentClass.getMethod(getAggregationCollectionMethodName, null);
387                             Collection JavaDoc aggregationCollectionMethod = (Collection JavaDoc) getAggregationCollectionMethod.invoke(parentObject, null);
388     
389                             // Iterate through the collection to delete entity.
390
Iterator JavaDoc aggregationCollectionIterator = aggregationCollectionMethod.iterator();
391                             while(aggregationCollectionIterator.hasNext()) {
392                                 Entity childAggregationEntity = (Entity) aggregationCollectionIterator.next();
393                                 EntityPrimaryKey childAggregationentityPrimaryKey = childAggregationEntity.getPrimaryKey();
394                                 if(entityPrimaryKey.equals(childAggregationentityPrimaryKey)) {
395                                     aggregationCollectionMethod.remove(childAggregationEntity);
396                                     break;
397                                 }
398                             }
399                         }
400     
401                         String JavaDoc addAggregation = "add" + StringUtils.capitalise(currentContextStackElement.getParentAggregationName());
402                         Class JavaDoc[] parameterTypes = new Class JavaDoc[] {Object JavaDoc.class};
403                         Method JavaDoc addAggregationMethod = parentClass.getMethod(addAggregation, parameterTypes);
404                         Object JavaDoc[] parameters = new Object JavaDoc[] {entityBean};
405                         addAggregationMethod.invoke(parentObject, parameters);
406                     } else {
407                         String JavaDoc setAggregation = "set" + StringUtils.capitalise(currentContextStackElement.getParentAggregationName());
408                         Class JavaDoc[] parameterTypes = new Class JavaDoc[] {entityBean.getClass()};
409                         Method JavaDoc setAggregationMethod = parentClass.getMethod(setAggregation, parameterTypes);
410                         Object JavaDoc[] parameters = new Object JavaDoc[1];
411                         if(!isNewInstance) {
412                             // Due to an hibernate bug (?) you firs have to nullify a children to update it.
413
setAggregationMethod.invoke(parentObject, parameters);
414                         }
415     
416     
417                         //In the case of a single aggregation must simply set the element
418
parameters[0] = entityBean;
419                         setAggregationMethod.invoke(parentObject, parameters);
420                     }
421                 }
422             }
423         } catch (NoSuchMethodException JavaDoc e) {
424             throw new ContextUpdateException("The aggregated objects could not be saved", e);
425         } catch (IllegalAccessException JavaDoc e) {
426             throw new ContextUpdateException("The aggregated objects could not be saved", e);
427         } catch (InvocationTargetException JavaDoc e) {
428             Throwable JavaDoc nestedException = ((InvocationTargetException JavaDoc)e).getTargetException();
429             
430             if(nestedException instanceof SystemException) {
431                 throw (SystemException) nestedException;
432             } else if(nestedException instanceof ConcurrencyException) {
433                 throw (ConcurrencyException) nestedException;
434             } else {
435                 throw new ContextUpdateException("The aggregated objects could not be saved", e);
436             }
437         }
438     }
439
440     public String JavaDoc getFirstParameterNameFromPrefix(HttpServletRequest JavaDoc httpServletRequest, String JavaDoc prefix) {
441         String JavaDoc output = null;
442
443         Enumeration JavaDoc enumeration = httpServletRequest.getParameterNames();
444
445         while(enumeration.hasMoreElements()) {
446             String JavaDoc parameterName = (String JavaDoc) enumeration.nextElement();
447             if(parameterName != null && parameterName.startsWith(prefix)) {
448                 output = parameterName;
449                 break;
450             }
451         }
452
453         return output;
454     }
455
456     /**
457      * Get the aggregation parent name if found. Otherwise return null
458      *
459      * @param contextStack The context stack
460      * @return The aggregation parent name, otherwise null
461      */

462     protected String JavaDoc getAggregationParentName(ContextStack contextStack) {
463         //The element must have a parent
464
if((contextStack != null && contextStack.size() > 1)) {
465             ContextStackElement parentContextStackElement = (ContextStackElement) contextStack.get(contextStack.size() - 2);
466             return parentContextStackElement.getParentFullyQualifiedEntityName().substring(parentContextStackElement.getParentFullyQualifiedEntityName().lastIndexOf('.') + 1);
467         }
468         return null;
469     }
470     
471     /**
472      * Get the parent's child aggregation name if found. Otherwise return null
473      *
474      * @param contextStack The context stack
475      * @return The parent child aggregation name, otherwise null
476      */

477     protected String JavaDoc getParentChildAggregationName(ContextStack contextStack) {
478         //The element must have a parent
479
if(contextStack != null && contextStack.size() > 1) {
480             ContextStackElement currentContextStackElement = (ContextStackElement) contextStack.peek();
481             return currentContextStackElement.getParentAggregationName();
482         }
483         return null;
484     }
485     
486     /**
487      * Determine the action forward for the aggregation we are currently navigating to
488      *
489      * @param childAggregationActionName
490      * @param childAggregationName
491      * @param actionMapping
492      * @param actionForm
493      * @param httpServletRequest
494      * @param httpServletResponse
495      * @return
496      * @throws SystemException
497      */

498     protected ActionForward determineForwardAggregation(String JavaDoc childAggregationActionName, String JavaDoc childAggregationName, ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse) throws SystemException {
499         ActionForward output = null;
500         // Test for action name to be undertaken
501
if(FORM_CHILD_ACTION_SUFFIX_PARAMETER_KEY.equals(childAggregationActionName)) {
502             // We will redirect to the child aggregation form action
503
output = forwardChildAggregation(actionMapping, actionForm, httpServletRequest, httpServletResponse, childAggregationName, childAggregationActionName);
504
505         } else if(DELETE_CHILD_ACTION_SUFFIX_PARAMETER_KEY.equals(childAggregationActionName)) {
506             // We will redirect to the child aggregation delete action
507
output = forwardChildAggregation(actionMapping, actionForm, httpServletRequest, httpServletResponse, childAggregationName, childAggregationActionName);
508         }
509
510         // forward shouldn't be null but if it is, just return a user system failure.
511
if(output == null) throw new org.mdarad.framework.exception.SystemException("No forward of that name could be found: " + getForwardAggregationChildName(childAggregationName, childAggregationActionName));
512         return output;
513     }
514     
515     /**
516      * Update the contextStack with the necessary information
517      *
518      * @param contextStack
519      * @param entityBean
520      * @param entityClass
521      * @param businessDelegate
522      * @param isChildMultipleAggregation
523      * @param childAggregationName
524      * @throws ServiceLocatorException
525      */

526     protected void updateContextStack( ContextStack contextStack, Entity entityBean,
527                                         Class JavaDoc entityClass, BusinessDelegate businessDelegate,
528                                         boolean isChildMultipleAggregation, boolean isNewInstance,
529                                         String JavaDoc childAggregationName)
530             throws ServiceLocatorException {
531         
532         ContextStackElement currentContextStackElement;
533         if(contextStack.size() > 0) {
534             currentContextStackElement = (ContextStackElement) contextStack.peek();
535         } else {
536             currentContextStackElement = new ContextStackElement();
537             contextStack.add(currentContextStackElement);
538         }
539
540         // Fill information about current (parent) instance into last context stack element.
541
currentContextStackElement.setDelegate(businessDelegate);
542         currentContextStackElement.setParentFullyQualifiedEntityName(entityClass.getName());
543         currentContextStackElement.setEntityBeanInstance(entityBean);
544
545         // Fill partial information about current (child) instance into context stack
546
ContextStackElement childContextStackElement = new ContextStackElement();
547         childContextStackElement.setParentMultipleAggregation(isChildMultipleAggregation);
548         childContextStackElement.setNewInstance(isNewInstance);
549         childContextStackElement.setParentAggregationName(childAggregationName);
550         contextStack.push(childContextStackElement);
551     }
552 }
Popular Tags