KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > model > handler > XmlModelHandler


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.model.handler;
17
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19
20 import com.jdon.bussinessproxy.meta.MethodMetaArgs;
21 import com.jdon.controller.events.Event;
22 import com.jdon.controller.events.EventModel;
23 import com.jdon.controller.model.ModelIF;
24 import com.jdon.controller.service.Service;
25 import com.jdon.controller.service.ServiceFacade;
26 import com.jdon.model.ModelForm;
27 import com.jdon.model.ModelHandler;
28 import com.jdon.util.Debug;
29
30
31 /**
32  * it is a meta ModelHandler, all methods are finished by
33  * the definition in jdonframework.xml
34  *
35  * XmlModelHandler is loaded by XmlHandlerClassFactory from container.xml
36  *
37  * XmlModelHandler is the client of container, XmlModelHandler will be made
38  * many instance by HandlerObjectFactory.
39  *
40  * @author banq
41  */

42 public class XmlModelHandler extends ModelHandler {
43   private final static String JavaDoc module = XmlModelHandler.class.getName();
44   
45   private HandlerMethodMetaArgsFactory maFactory;;
46   
47   private ServiceFacade serviceFacade;
48
49    
50     /**
51      * @param service
52      */

53    public XmlModelHandler() {
54         this.maFactory = new HandlerMethodMetaArgsFactory();
55         this.serviceFacade = new ServiceFacade();
56    }
57   /**
58    * if your application need initialize the ModelForm,
59    * this method is a option.
60    * extends thie method.
61    */

62   public ModelIF initModelIF(EventModel em, ModelForm form, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
63     ModelIF result = null;
64     try {
65         HandlerMetaDef hm = this.modelMapping.getHandlerMetaDef();
66         String JavaDoc serviceName = hm.getServiceRef();
67         Debug.logVerbose("[JdonFramework] construct the CRUD method for the service:" + serviceName , module);
68         MethodMetaArgs methodMetaArgs = maFactory.createinitMethod(hm, em);
69         
70         Service service = serviceFacade.getService(request);
71         if (methodMetaArgs != null)
72         result = (ModelIF)service.execute(serviceName,
73                                         methodMetaArgs,
74                                         request);
75     } catch (Exception JavaDoc e) {
76        Debug.logError("[JdonFramework] initModel error: " + e , module);
77        throw new Exception JavaDoc(e);
78     }
79     return result;
80   }
81   
82   
83
84   public ModelIF findModelIF(Object JavaDoc keyValue, HttpServletRequest JavaDoc request) throws
85       java.lang.Exception JavaDoc {
86     Object JavaDoc result = null;
87     try {
88         HandlerMetaDef hm = this.modelMapping.getHandlerMetaDef();
89         String JavaDoc serviceName = hm.getServiceRef();
90         Debug.logVerbose("[JdonFramework] construct the CRUD method for the service:" + serviceName , module);
91         MethodMetaArgs methodMetaArgs = maFactory.createGetMethod(hm, keyValue);
92         if (methodMetaArgs.getMethodName() == null)
93             throw new Exception JavaDoc("no configure findMethod value, but now you call it: ");
94         Service service = serviceFacade.getService(request);
95         result = service.execute(serviceName,
96                                  methodMetaArgs,
97                                  request);
98     } catch (Exception JavaDoc e) {
99         Debug.logError("[JdonFramework] findModelByKey error: " + e + " maybe not configure getMethod", module);
100         throw new Exception JavaDoc(e);
101     }
102     Debug.logVerbose("[JdonFramework] result type:" + result.getClass().getName(), module);
103     return (ModelIF) result;
104   }
105
106   public void serviceAction(EventModel em, HttpServletRequest JavaDoc request) throws
107       java.lang.Exception JavaDoc {
108       Debug.logVerbose("[JdonFramework] enter the serviceAction " , module);
109     try {
110       HandlerMetaDef hm = this.modelMapping.getHandlerMetaDef();
111       String JavaDoc serviceName = hm.getServiceRef();
112       MethodMetaArgs methodMetaArgs = null;
113       switch (em.getActionType()) {
114         case Event.CREATE:
115           Debug.logVerbose("[JdonFramework] construct the CRUD method for the service:" + serviceName , module);
116           methodMetaArgs = maFactory.createCreateMethod(hm, em);
117           break;
118         case Event.EDIT:
119           Debug.logVerbose("[JdonFramework] construct the CRUD method for the service:" + serviceName , module);
120           methodMetaArgs = maFactory.createUpdateMethod(hm, em);
121           break;
122         case Event.DELETE:
123           Debug.logVerbose("[JdonFramework] construct the CRUD method for the service:" + serviceName , module);
124           methodMetaArgs = maFactory.createDeleteMethod(hm, em);
125           break;
126         default:
127           Debug.logVerbose("[JdonFramework] construct the command method for the service:" + serviceName , module);
128           methodMetaArgs = maFactory.createDirectMethod(em.getActionName(), new Object JavaDoc[]{em});
129       }
130       Debug.logVerbose( " execute the method: " + methodMetaArgs.getMethodName() + " for the service: " + serviceName , module);
131       Service service = serviceFacade.getService(request);
132       service.execute(serviceName, methodMetaArgs, request);
133     } catch (Exception JavaDoc ex) {
134       Debug.logError("[JdonFramework] serviceAction Error: " + ex , module);
135       throw new Exception JavaDoc(" serviceAction Error:" + ex);
136     }
137
138   }
139   
140  
141 }
142
Popular Tags