Source Code Cross Referenced for AbstractAction.java in  » J2EE » Mdarad » org » mdarad » framework » util » struts » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Mdarad » org.mdarad.framework.util.struts 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:            Mdarad-Toolobox is a collection of tools for Architected RAD
003:            (Rapid Application Development) based on an MDA approach.
004:            The toolbox contains frameworks and generators for many environments
005:            (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
006:            applications from a design Model
007:        	Copyright (C) 2004-2005  Elapse Technologies Inc.
008:
009:        	This library is free software; you can redistribute it and/or
010:        	modify it under the terms of the GNU General Public
011:        	License as published by the Free Software Foundation; either
012:        	version 2.1 of the License, or (at your option) any later version.
013:
014:        	This library is distributed in the hope that it will be useful,
015:        	but WITHOUT ANY WARRANTY; without even the implied warranty of
016:        	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
017:        	General Public License for more details.
018:
019:        	You should have received a copy of the GNU General Public
020:        	License along with this library; if not, write to the Free Software
021:        	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022:         */
023:        package org.mdarad.framework.util.struts;
024:
025:        import java.lang.reflect.InvocationTargetException;
026:        import java.lang.reflect.Method;
027:        import java.util.Collection;
028:        import java.util.Enumeration;
029:        import java.util.Iterator;
030:
031:        import javax.servlet.http.HttpServletRequest;
032:        import javax.servlet.http.HttpServletResponse;
033:
034:        import org.apache.commons.beanutils.ConvertUtils;
035:        import org.apache.commons.lang.StringUtils;
036:        import org.apache.struts.Globals;
037:        import org.apache.struts.action.ActionForm;
038:        import org.apache.struts.action.ActionForward;
039:        import org.apache.struts.action.ActionMapping;
040:        import org.apache.struts.action.ActionMessage;
041:        import org.apache.struts.action.ActionMessages;
042:        import org.apache.struts.tiles.actions.TilesAction;
043:        import org.dataisland.primitives.bean.Entity;
044:        import org.dataisland.primitives.bean.EntityPrimaryKey;
045:        import org.mdarad.framework.delegates.BusinessDelegate;
046:        import org.mdarad.framework.exception.ConcurrencyException;
047:        import org.mdarad.framework.exception.ServiceLocatorException;
048:        import org.mdarad.framework.exception.SystemException;
049:        import org.mdarad.framework.util.GUIDGenerationException;
050:        import org.mdarad.framework.util.struts.contextstack.ContextStack;
051:        import org.mdarad.framework.util.struts.contextstack.ContextStackElement;
052:        import org.mdarad.framework.util.struts.contextstack.ContextUpdateException;
053:        import org.mdarad.framework.util.struts.contextstack.OrphanChildException;
054:
055:        public abstract class AbstractAction extends TilesAction {
056:            public static final String WEB_CONTEXT_LOCALIZATION_CONTEXT_KEY = "org.mdarad.framework.util.struts.AbstractAction.webLocalizationContext";
057:            public static final String AGGREGATION_PARENT_FORWARD_PREFIX = "parent";
058:            public static final String AGGREGATION_PARENT_FORWARD_SEPARATOR = "-";
059:            public static final String SUCCESS_FORWARD_KEY = "success";
060:            public static final String CANCEL_FORWARD_KEY = "cancel";
061:            public static final String DISPLAY_FORWARD_KEY = "display";
062:            public static final String FAILURE_FORWARD_KEY = "failure";
063:            public static final String CHILD_AGGREGATION_FORWARD_PREFIX = "child";
064:            public static final String CHILD_AGGREGATION_FORWARD_SEPARATOR = "-";
065:            public final static String FORM_CHILD_ACTION_SUFFIX_PARAMETER_KEY = "form";
066:            public final static String DELETE_CHILD_ACTION_SUFFIX_PARAMETER_KEY = "delete";
067:            public final static String CONTEXT_STACK_USE_PARAMETER_KEY = "org.mdarad.framework.util.struts.AbstractAction.useContextStack";
068:            public final static String ID_IN_REQUEST_ATTRIBUTE_PARAMETER_KEY = "org.mdarad.framework.util.struts.AbstractAction.idInRequestAttributes";
069:
070:            protected ActionForward forwardCancel(ActionMapping actionMapping,
071:                    ActionForm actionForm,
072:                    HttpServletRequest httpServletRequest,
073:                    HttpServletResponse httpServletResponse) {
074:                ActionMessages actionMessages = new ActionMessages();
075:                actionMessages.add(Globals.MESSAGE_KEY, new ActionMessage(
076:                        "org.mdarad.framework.global.forward.cancel.message"));
077:                saveMessages(httpServletRequest, actionMessages);
078:                return actionMapping.findForward(CANCEL_FORWARD_KEY);
079:            }
080:
081:            protected ActionForward forwardSuccess(ActionMapping actionMapping,
082:                    ActionForm actionForm,
083:                    HttpServletRequest httpServletRequest,
084:                    HttpServletResponse httpServletResponse) {
085:                ActionMessages actionMessages = new ActionMessages();
086:                actionMessages.add(Globals.MESSAGE_KEY, new ActionMessage(
087:                        "org.mdarad.framework.global.forward.success.message"));
088:                saveMessages(httpServletRequest, actionMessages);
089:                return actionMapping.findForward(SUCCESS_FORWARD_KEY);
090:            }
091:
092:            protected ActionForward forwardDisplay(ActionMapping actionMapping,
093:                    ActionForm actionForm,
094:                    HttpServletRequest httpServletRequest,
095:                    HttpServletResponse httpServletResponse) {
096:                return actionMapping.findForward(DISPLAY_FORWARD_KEY);
097:            }
098:
099:            protected ActionForward forwardFailure(ActionMapping actionMapping,
100:                    ActionForm actionForm,
101:                    HttpServletRequest httpServletRequest,
102:                    HttpServletResponse httpServletResponse) {
103:                ActionMessages actionMessages = new ActionMessages();
104:                actionMessages.add(Globals.ERROR_KEY, new ActionMessage(
105:                        "org.mdarad.framework.global.forward.failure.message"));
106:                saveMessages(httpServletRequest, actionMessages);
107:                return actionMapping.findForward(FAILURE_FORWARD_KEY);
108:            }
109:
110:            protected ActionForward forwardDisplay(ActionMapping actionMapping,
111:                    ActionForm actionForm,
112:                    HttpServletRequest httpServletRequest,
113:                    HttpServletResponse httpServletResponse, String forward) {
114:                return actionMapping.findForward(forward);
115:            }
116:
117:            protected ActionForward forwardChildAggregation(
118:                    ActionMapping actionMapping, ActionForm actionForm,
119:                    HttpServletRequest httpServletRequest,
120:                    HttpServletResponse httpServletResponse,
121:                    String childAggregationName,
122:                    String childAggregationActionName) {
123:                return actionMapping
124:                        .findForward(getForwardAggregationChildName(
125:                                childAggregationName,
126:                                childAggregationActionName));
127:            }
128:
129:            protected String getForwardAggregationChildName(
130:                    String childAggregationName,
131:                    String childAggregationActionName) {
132:                StringBuffer forwardNameStringBuffer = new StringBuffer(
133:                        CHILD_AGGREGATION_FORWARD_PREFIX);
134:                forwardNameStringBuffer
135:                        .append(CHILD_AGGREGATION_FORWARD_SEPARATOR);
136:                forwardNameStringBuffer.append(childAggregationName);
137:                forwardNameStringBuffer
138:                        .append(CHILD_AGGREGATION_FORWARD_SEPARATOR);
139:                forwardNameStringBuffer.append(childAggregationActionName);
140:                return forwardNameStringBuffer.toString();
141:            }
142:
143:            protected ActionForward forwardAggregationParent(
144:                    ActionMapping actionMapping, ActionForm actionForm,
145:                    HttpServletRequest httpServletRequest,
146:                    HttpServletResponse httpServletResponse,
147:                    String aggregationParentName,
148:                    String parentChildAggregationName,
149:                    String aggregationParentForwardName) {
150:                return actionMapping
151:                        .findForward(getForwardAggregationParentName(
152:                                aggregationParentName,
153:                                parentChildAggregationName,
154:                                aggregationParentForwardName));
155:            }
156:
157:            protected String getForwardAggregationParentName(
158:                    String aggregationParentName,
159:                    String parentChildAggregationName,
160:                    String aggregationParentForwardName) {
161:                StringBuffer forwardNameStringBuffer = new StringBuffer(
162:                        AGGREGATION_PARENT_FORWARD_PREFIX);
163:                forwardNameStringBuffer
164:                        .append(AGGREGATION_PARENT_FORWARD_SEPARATOR);
165:                forwardNameStringBuffer.append(aggregationParentName);
166:                forwardNameStringBuffer
167:                        .append(AGGREGATION_PARENT_FORWARD_SEPARATOR);
168:                forwardNameStringBuffer.append(parentChildAggregationName);
169:                forwardNameStringBuffer
170:                        .append(AGGREGATION_PARENT_FORWARD_SEPARATOR);
171:                forwardNameStringBuffer.append(aggregationParentForwardName);
172:
173:                return forwardNameStringBuffer.toString();
174:            }
175:
176:            public void deleteContextObject(String contextStackKey,
177:                    Entity entityBean, HttpServletRequest httpServletRequest)
178:                    throws OrphanChildException, SystemException,
179:                    ConcurrencyException {
180:                ContextStack contextStack = getContextStack(contextStackKey,
181:                        httpServletRequest);
182:
183:                if (contextStack == null) {
184:                    throw new OrphanChildException(
185:                            "The child is being saved out of context. The context stack cannot be null.");
186:                }
187:
188:                try {
189:                    if (contextStack.size() > 1) {
190:                        // Get "current" context stack element
191:                        ContextStackElement currentContextStackElement = (ContextStackElement) contextStack
192:                                .peek();
193:
194:                        // Look for parent context stack element
195:                        ContextStackElement parentContextStackElement = (ContextStackElement) contextStack
196:                                .get(contextStack.size() - 2);
197:                        Entity parentObject = (Entity) parentContextStackElement
198:                                .getEntityBeanInstance();
199:                        Class parentClass = parentContextStackElement
200:                                .getEntityBeanInstance().getClass();
201:
202:                        // For multiple aggregation must delete element if exists and then add it to the existing list
203:                        if (currentContextStackElement
204:                                .isParentMultipleAggregation()) {
205:                            // Due to an hibernate bug (?) you first have to nullify a children to update it.
206:                            EntityPrimaryKey entityPrimaryKey = entityBean
207:                                    .getPrimaryKey();
208:
209:                            // Get the existing children
210:                            String getAggregationCollectionMethodName = "get"
211:                                    + StringUtils
212:                                            .capitalise(currentContextStackElement
213:                                                    .getParentAggregationName());
214:                            Method getAggregationCollectionMethod = parentClass
215:                                    .getMethod(
216:                                            getAggregationCollectionMethodName,
217:                                            null);
218:                            Collection aggregationCollectionMethod = (Collection) getAggregationCollectionMethod
219:                                    .invoke(parentObject, null);
220:
221:                            // Iterate through the collection to delete entity.
222:                            Iterator aggregationCollectionIterator = aggregationCollectionMethod
223:                                    .iterator();
224:                            while (aggregationCollectionIterator.hasNext()) {
225:                                Entity childAggregationEntity = (Entity) aggregationCollectionIterator
226:                                        .next();
227:                                EntityPrimaryKey childAggregationentityPrimaryKey = childAggregationEntity
228:                                        .getPrimaryKey();
229:                                if (entityPrimaryKey
230:                                        .equals(childAggregationentityPrimaryKey)) {
231:                                    aggregationCollectionMethod
232:                                            .remove(childAggregationEntity);
233:                                    break;
234:                                }
235:                            }
236:                        } else {
237:                            // Nullify
238:                            String setAggregation = "set"
239:                                    + StringUtils
240:                                            .capitalise(currentContextStackElement
241:                                                    .getParentAggregationName());
242:                            Class[] parameterTypes = new Class[] { entityBean
243:                                    .getClass() };
244:                            Method setAggregationMethod = parentClass
245:                                    .getMethod(setAggregation, parameterTypes);
246:                            Object[] parameters = new Object[1];
247:                            setAggregationMethod.invoke(parentObject,
248:                                    parameters);
249:                        }
250:
251:                        // delete
252:                        // Get the persistence facade instance
253:                        BusinessDelegate delegateInstance = currentContextStackElement
254:                                .getDelegate();
255:                        Class facadeClass = delegateInstance.getClass();
256:
257:                        //Invoke the save method from the persistence facade instance
258:                        String fullyQualifiedName = entityBean.getClass()
259:                                .getName();
260:                        Class[] parameterTypes = new Class[] { entityBean
261:                                .getClass() };
262:                        String className = fullyQualifiedName.substring(
263:                                fullyQualifiedName.lastIndexOf('.') + 1,
264:                                fullyQualifiedName.length());
265:                        Method saveMethod = facadeClass.getMethod("save"
266:                                + className, parameterTypes);
267:                        Object[] parameters = new Object[] { entityBean };
268:                        saveMethod.invoke(delegateInstance, parameters);
269:
270:                    }
271:                } catch (NoSuchMethodException e) {
272:                    throw new ContextUpdateException(
273:                            "The aggregated objects could not be saved", e);
274:                } catch (IllegalAccessException e) {
275:                    throw new ContextUpdateException(
276:                            "The aggregated objects could not be saved", e);
277:                } catch (InvocationTargetException e) {
278:                    Throwable nestedException = ((InvocationTargetException) e)
279:                            .getTargetException();
280:
281:                    if (nestedException instanceof  SystemException) {
282:                        throw (SystemException) nestedException;
283:                    } else {
284:                        throw new ContextUpdateException(
285:                                "The aggregated objects could not be saved", e);
286:                    }
287:                }
288:
289:                saveContextObjects(contextStack);
290:
291:            }
292:
293:            /**
294:             * Save the context objects by updating the parent elements and saving the root entity.
295:             *
296:             * @param contextStackKey    The key in the request to get access to the context stack
297:             * @param httpServletRequest
298:             * @throws OrphanChildException
299:             * @throws ConcurrencyException 
300:             * @throws SystemException 
301:             */
302:            public void saveContextObjectsByParent(String contextStackKey,
303:                    Entity entityBean, HttpServletRequest httpServletRequest,
304:                    boolean isNewInstance) throws OrphanChildException,
305:                    SystemException, ConcurrencyException {
306:                ContextStack contextStack = getContextStack(contextStackKey,
307:                        httpServletRequest);
308:
309:                //Must add the entity 
310:                ContextStackElement contextStackElement = (ContextStackElement) contextStack
311:                        .peek();
312:                contextStackElement.setNewInstance(isNewInstance);
313:                contextStackElement.setEntityBeanInstance(entityBean);
314:
315:                if (contextStack == null) {
316:                    throw new OrphanChildException(
317:                            "The child is being saved out of context. The context stack cannot be null.");
318:                }
319:
320:                saveContextObjectsByParent(contextStack);
321:
322:            }
323:
324:            /**
325:             * Save the context objects by updating the parent elements and saving the root entity.
326:             *
327:             * @param contextStack The context stack which contains the entities to the root
328:             * @throws SystemException 
329:             * @throws ConcurrencyException 
330:             */
331:            public void saveContextObjectsByParent(ContextStack contextStack)
332:                    throws SystemException, ConcurrencyException {
333:                updateContextObjects(contextStack);
334:                saveContextObjects(contextStack);
335:            }
336:
337:            /**
338:             * Save only the highest ranked parent (for the others will be saved by him)
339:             * 
340:             * @param contextStack
341:             * @param entityBean
342:             * @throws SystemException
343:             * @throws ConcurrencyException
344:             */
345:            protected void saveContextObjects(ContextStack contextStack)
346:                    throws SystemException, ConcurrencyException {
347:                try {
348:                    //Only save the parent of all entities
349:                    ContextStackElement parentContextStackElement = (ContextStackElement) contextStack
350:                            .get(0);
351:
352:                    Entity parentInstance = parentContextStackElement
353:                            .getEntityBeanInstance();
354:
355:                    // Get the persistence facade instance
356:                    BusinessDelegate parentDelegateInstance = parentContextStackElement
357:                            .getDelegate();
358:                    Class parentFacadeClass = parentDelegateInstance.getClass();
359:
360:                    //Invoke the save method from the persistence facade instance
361:                    String parentFullyQualifiedEntityName = parentContextStackElement
362:                            .getParentFullyQualifiedEntityName();
363:                    Class[] parameterTypes = new Class[] { Class
364:                            .forName(parentContextStackElement
365:                                    .getParentFullyQualifiedEntityName()) };
366:                    String parentClassName = parentFullyQualifiedEntityName
367:                            .substring(parentFullyQualifiedEntityName
368:                                    .lastIndexOf('.') + 1,
369:                                    parentFullyQualifiedEntityName.length());
370:                    Method saveParentMethod = parentFacadeClass.getMethod(
371:                            "save" + parentClassName, parameterTypes);
372:                    Object[] parameters = new Object[] { parentInstance };
373:                    saveParentMethod.invoke(parentDelegateInstance, parameters);
374:                } catch (NoSuchMethodException e) {
375:                    throw new ContextUpdateException(
376:                            "The aggregated objects could not be saved", e);
377:                } catch (IllegalAccessException e) {
378:                    throw new ContextUpdateException(
379:                            "The aggregated objects could not be saved", e);
380:                } catch (InvocationTargetException e) {
381:                    Throwable nestedException = ((InvocationTargetException) e)
382:                            .getTargetException();
383:                    if (nestedException instanceof  SystemException) {
384:                        throw (SystemException) nestedException;
385:                    } else if (nestedException instanceof  ConcurrencyException) {
386:                        throw (ConcurrencyException) nestedException;
387:                    } else {
388:                        throw new ContextUpdateException(
389:                                "The aggregated objects could not be saved", e);
390:                    }
391:                } catch (ClassNotFoundException e) {
392:                    throw new ContextUpdateException(
393:                            "The aggregated objects could not be saved", e);
394:                }
395:            }
396:
397:            /**
398:             * Get the context stack that is stored in the session
399:             *
400:             * @param contextStackKey
401:             * @param httpServletRequest
402:             * @return
403:             */
404:            static public ContextStack getContextStack(String contextStackKey,
405:                    HttpServletRequest httpServletRequest) {
406:                //Get Context Stack
407:                //The id can be as a parameter or as an attribute
408:                String contextStackIdString = httpServletRequest
409:                        .getParameter(contextStackKey);
410:                if (contextStackIdString == null) {
411:                    contextStackIdString = (String) httpServletRequest
412:                            .getAttribute(contextStackKey);
413:                }
414:
415:                String contextStackId = (String) ConvertUtils.convert(
416:                        contextStackIdString, String.class);
417:                return (ContextStack) httpServletRequest.getSession()
418:                        .getAttribute(contextStackId);
419:            }
420:
421:            static public void setContextStack(String contextStackKey,
422:                    ContextStack contextStack,
423:                    HttpServletRequest httpServletRequest) {
424:                //Get Context Stack
425:                String contextStackId = contextStack.getId();
426:                httpServletRequest.getSession().setAttribute(contextStackId,
427:                        contextStack);
428:                httpServletRequest
429:                        .setAttribute(contextStackKey, contextStackId);
430:            }
431:
432:            static public void nullifyContextStack(String contextStackKey,
433:                    ContextStack contextStack,
434:                    HttpServletRequest httpServletRequest) {
435:                //Get Context Stack
436:                String contextStackId = contextStack.getId();
437:                httpServletRequest.getSession().removeAttribute(contextStackId);
438:                httpServletRequest.removeAttribute(contextStackKey);
439:            }
440:
441:            static public ContextStack createNewContextStack(
442:                    String contextStackKey,
443:                    HttpServletRequest httpServletRequest) {
444:                ContextStack contextStack = null;
445:                try {
446:                    contextStack = new ContextStack();
447:                    // Set Context Stack
448:                    String contextStackId = contextStack.getId();
449:                    httpServletRequest.getSession().setAttribute(
450:                            contextStackId, contextStack);
451:                    httpServletRequest.setAttribute(contextStackKey,
452:                            contextStackId);
453:                } catch (GUIDGenerationException e) {
454:                    e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
455:                }
456:                return contextStack;
457:            }
458:
459:            public Entity getCurrentEntityFromContextStack(
460:                    String contextStackKey,
461:                    HttpServletRequest httpServletRequest) {
462:                ContextStack contextStack = getContextStack(contextStackKey,
463:                        httpServletRequest);
464:                Entity output = null;
465:                if (contextStack != null && contextStack.size() > 0) {
466:                    ContextStackElement contextStackElement = (ContextStackElement) contextStack
467:                            .get(contextStack.size() - 1);
468:                    if (contextStackElement != null) {
469:                        output = contextStackElement.getEntityBeanInstance();
470:                    }
471:                }
472:                return output;
473:            }
474:
475:            /**
476:             * Redirect the context stack key in the request
477:             *
478:             * @param contextStackKey
479:             * @param httpServletRequest
480:             */
481:            public void setContextStackIDInRequest(String contextStackKey,
482:                    HttpServletRequest httpServletRequest) {
483:                // Get Stack
484:                String contextStackId = (String) httpServletRequest
485:                        .getAttribute(contextStackKey);
486:
487:                if (contextStackId == null) {
488:                    String contextStackIdString = httpServletRequest
489:                            .getParameter(contextStackKey);
490:                    contextStackId = (String) ConvertUtils.convert(
491:                            contextStackIdString, String.class);
492:                    // Set context stack key in request
493:                    if (contextStackId != null) {
494:                        httpServletRequest.setAttribute(contextStackKey,
495:                                contextStackId);
496:                    }
497:                }
498:            }
499:
500:            protected void updateContextObjects(ContextStack contextStack)
501:                    throws SystemException, ConcurrencyException {
502:                try {
503:                    if (contextStack.size() > 1) {
504:                        for (int i = contextStack.size() - 1; i > 0; i--) {
505:                            // Get "current" context stack element
506:                            ContextStackElement currentContextStackElement = (ContextStackElement) contextStack
507:                                    .get(i);
508:                            boolean isNewInstance = currentContextStackElement
509:                                    .isNewInstance();
510:                            Entity entityBean = currentContextStackElement
511:                                    .getEntityBeanInstance();
512:
513:                            // Look for parent context stack element
514:                            ContextStackElement parentContextStackElement = (ContextStackElement) contextStack
515:                                    .get(i - 1);
516:                            Entity parentObject = (Entity) parentContextStackElement
517:                                    .getEntityBeanInstance();
518:                            Class parentClass = parentContextStackElement
519:                                    .getEntityBeanInstance().getClass();
520:
521:                            // For multiple aggregation must delete element if exists and then add it to the existing list
522:                            if (currentContextStackElement
523:                                    .isParentMultipleAggregation()) {
524:                                if (!isNewInstance) {
525:                                    // Due to an hibernate bug (?) you first have to nullify a children to update it.
526:                                    EntityPrimaryKey entityPrimaryKey = entityBean
527:                                            .getPrimaryKey();
528:
529:                                    // Get the existing children
530:                                    String getAggregationCollectionMethodName = "get"
531:                                            + StringUtils
532:                                                    .capitalise(currentContextStackElement
533:                                                            .getParentAggregationName());
534:                                    Method getAggregationCollectionMethod = parentClass
535:                                            .getMethod(
536:                                                    getAggregationCollectionMethodName,
537:                                                    null);
538:                                    Collection aggregationCollectionMethod = (Collection) getAggregationCollectionMethod
539:                                            .invoke(parentObject, null);
540:
541:                                    // Iterate through the collection to delete entity.
542:                                    Iterator aggregationCollectionIterator = aggregationCollectionMethod
543:                                            .iterator();
544:                                    while (aggregationCollectionIterator
545:                                            .hasNext()) {
546:                                        Entity childAggregationEntity = (Entity) aggregationCollectionIterator
547:                                                .next();
548:                                        EntityPrimaryKey childAggregationentityPrimaryKey = childAggregationEntity
549:                                                .getPrimaryKey();
550:                                        if (entityPrimaryKey
551:                                                .equals(childAggregationentityPrimaryKey)) {
552:                                            aggregationCollectionMethod
553:                                                    .remove(childAggregationEntity);
554:                                            break;
555:                                        }
556:                                    }
557:                                }
558:
559:                                String addAggregation = "add"
560:                                        + StringUtils
561:                                                .capitalise(currentContextStackElement
562:                                                        .getParentAggregationName());
563:                                Class[] parameterTypes = new Class[] { Object.class };
564:                                Method addAggregationMethod = parentClass
565:                                        .getMethod(addAggregation,
566:                                                parameterTypes);
567:                                Object[] parameters = new Object[] { entityBean };
568:                                addAggregationMethod.invoke(parentObject,
569:                                        parameters);
570:                            } else {
571:                                String setAggregation = "set"
572:                                        + StringUtils
573:                                                .capitalise(currentContextStackElement
574:                                                        .getParentAggregationName());
575:                                Class[] parameterTypes = new Class[] { entityBean
576:                                        .getClass() };
577:                                Method setAggregationMethod = parentClass
578:                                        .getMethod(setAggregation,
579:                                                parameterTypes);
580:                                Object[] parameters = new Object[1];
581:                                if (!isNewInstance) {
582:                                    // Due to an hibernate bug (?) you firs have to nullify a children to update it.
583:                                    setAggregationMethod.invoke(parentObject,
584:                                            parameters);
585:                                }
586:
587:                                //In the case of a single aggregation must simply set the element
588:                                parameters[0] = entityBean;
589:                                setAggregationMethod.invoke(parentObject,
590:                                        parameters);
591:                            }
592:                        }
593:                    }
594:                } catch (NoSuchMethodException e) {
595:                    throw new ContextUpdateException(
596:                            "The aggregated objects could not be saved", e);
597:                } catch (IllegalAccessException e) {
598:                    throw new ContextUpdateException(
599:                            "The aggregated objects could not be saved", e);
600:                } catch (InvocationTargetException e) {
601:                    Throwable nestedException = ((InvocationTargetException) e)
602:                            .getTargetException();
603:
604:                    if (nestedException instanceof  SystemException) {
605:                        throw (SystemException) nestedException;
606:                    } else if (nestedException instanceof  ConcurrencyException) {
607:                        throw (ConcurrencyException) nestedException;
608:                    } else {
609:                        throw new ContextUpdateException(
610:                                "The aggregated objects could not be saved", e);
611:                    }
612:                }
613:            }
614:
615:            public String getFirstParameterNameFromPrefix(
616:                    HttpServletRequest httpServletRequest, String prefix) {
617:                String output = null;
618:
619:                Enumeration enumeration = httpServletRequest
620:                        .getParameterNames();
621:
622:                while (enumeration.hasMoreElements()) {
623:                    String parameterName = (String) enumeration.nextElement();
624:                    if (parameterName != null
625:                            && parameterName.startsWith(prefix)) {
626:                        output = parameterName;
627:                        break;
628:                    }
629:                }
630:
631:                return output;
632:            }
633:
634:            /**
635:             * Get the aggregation parent name if found.  Otherwise return null
636:             * 
637:             * @param contextStack The context stack
638:             * @return The aggregation parent name, otherwise null
639:             */
640:            protected String getAggregationParentName(ContextStack contextStack) {
641:                //The element must have a parent
642:                if ((contextStack != null && contextStack.size() > 1)) {
643:                    ContextStackElement parentContextStackElement = (ContextStackElement) contextStack
644:                            .get(contextStack.size() - 2);
645:                    return parentContextStackElement
646:                            .getParentFullyQualifiedEntityName()
647:                            .substring(
648:                                    parentContextStackElement
649:                                            .getParentFullyQualifiedEntityName()
650:                                            .lastIndexOf('.') + 1);
651:                }
652:                return null;
653:            }
654:
655:            /**
656:             * Get the parent's child aggregation name if found.  Otherwise return null
657:             * 
658:             * @param contextStack The context stack
659:             * @return The parent child aggregation name, otherwise null
660:             */
661:            protected String getParentChildAggregationName(
662:                    ContextStack contextStack) {
663:                //The element must have a parent
664:                if (contextStack != null && contextStack.size() > 1) {
665:                    ContextStackElement currentContextStackElement = (ContextStackElement) contextStack
666:                            .peek();
667:                    return currentContextStackElement
668:                            .getParentAggregationName();
669:                }
670:                return null;
671:            }
672:
673:            /**
674:             * Determine the action forward for the aggregation we are currently navigating to 
675:             * 
676:             * @param childAggregationActionName
677:             * @param childAggregationName
678:             * @param actionMapping
679:             * @param actionForm
680:             * @param httpServletRequest
681:             * @param httpServletResponse
682:             * @return
683:             * @throws SystemException
684:             */
685:            protected ActionForward determineForwardAggregation(
686:                    String childAggregationActionName,
687:                    String childAggregationName, ActionMapping actionMapping,
688:                    ActionForm actionForm,
689:                    HttpServletRequest httpServletRequest,
690:                    HttpServletResponse httpServletResponse)
691:                    throws SystemException {
692:                ActionForward output = null;
693:                // Test for action name to be undertaken
694:                if (FORM_CHILD_ACTION_SUFFIX_PARAMETER_KEY
695:                        .equals(childAggregationActionName)) {
696:                    // We will redirect to the child aggregation form action
697:                    output = forwardChildAggregation(actionMapping, actionForm,
698:                            httpServletRequest, httpServletResponse,
699:                            childAggregationName, childAggregationActionName);
700:
701:                } else if (DELETE_CHILD_ACTION_SUFFIX_PARAMETER_KEY
702:                        .equals(childAggregationActionName)) {
703:                    // We will redirect to the child aggregation delete action
704:                    output = forwardChildAggregation(actionMapping, actionForm,
705:                            httpServletRequest, httpServletResponse,
706:                            childAggregationName, childAggregationActionName);
707:                }
708:
709:                // forward shouldn't be null but if it is, just return a user system failure.
710:                if (output == null)
711:                    throw new org.mdarad.framework.exception.SystemException(
712:                            "No forward of that name could be found: "
713:                                    + getForwardAggregationChildName(
714:                                            childAggregationName,
715:                                            childAggregationActionName));
716:                return output;
717:            }
718:
719:            /**
720:             * Update the contextStack with the necessary information
721:             * 
722:             * @param contextStack
723:             * @param entityBean
724:             * @param entityClass
725:             * @param businessDelegate
726:             * @param isChildMultipleAggregation
727:             * @param childAggregationName
728:             * @throws ServiceLocatorException
729:             */
730:            protected void updateContextStack(ContextStack contextStack,
731:                    Entity entityBean, Class entityClass,
732:                    BusinessDelegate businessDelegate,
733:                    boolean isChildMultipleAggregation, boolean isNewInstance,
734:                    String childAggregationName) throws ServiceLocatorException {
735:
736:                ContextStackElement currentContextStackElement;
737:                if (contextStack.size() > 0) {
738:                    currentContextStackElement = (ContextStackElement) contextStack
739:                            .peek();
740:                } else {
741:                    currentContextStackElement = new ContextStackElement();
742:                    contextStack.add(currentContextStackElement);
743:                }
744:
745:                // Fill information about current (parent) instance into last context stack element.
746:                currentContextStackElement.setDelegate(businessDelegate);
747:                currentContextStackElement
748:                        .setParentFullyQualifiedEntityName(entityClass
749:                                .getName());
750:                currentContextStackElement.setEntityBeanInstance(entityBean);
751:
752:                // Fill partial information about current (child) instance into context stack
753:                ContextStackElement childContextStackElement = new ContextStackElement();
754:                childContextStackElement
755:                        .setParentMultipleAggregation(isChildMultipleAggregation);
756:                childContextStackElement.setNewInstance(isNewInstance);
757:                childContextStackElement
758:                        .setParentAggregationName(childAggregationName);
759:                contextStack.push(childContextStackElement);
760:            }
761:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.