View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.logic.interceptor;
17  
18  import org.aopalliance.intercept.MethodInterceptor;
19  import org.aopalliance.intercept.MethodInvocation;
20  import org.seasar.tuigwaa.database.function.AbstractExeFunction;
21  import org.seasar.tuigwaa.logic.TgwFunction;
22  import org.seasar.tuigwaa.model.common.EntityUtils;
23  import org.seasar.tuigwaa.plugin.TgwPluginUtils;
24  import org.seasar.tuigwaa.util.TgwContext;
25  
26  import com.isenshi.util.TableMap;
27  
28  public class DataSaveInterceptorImpl implements DataSaveInterceptor,
29  		MethodInterceptor {
30  
31  	private TableMap afterActionMap = new TableMap();
32  
33  	private TableMap beforeActionMap = new TableMap();
34  
35  	private TableMap afterCustomFormMap = new TableMap();
36  
37  	private TableMap beforeCustomFormMap = new TableMap();
38  
39  	public void registerBeforeAction(String siteName, String entityName,
40  			TgwFunction logic) {
41  		beforeActionMap.put(logic, siteName, entityName);
42  	}
43  
44  	public void registerAfterAction(String siteName, String entityName,
45  			TgwFunction logic) {
46  		afterActionMap.put(logic, siteName, entityName);
47  	}
48  
49  	public void deregisterBeforeAction(String siteName, String entityName) {
50  		beforeActionMap.remove(siteName, entityName);
51  	}
52  
53  	public void deregisterAfterAction(String siteName, String entityName) {
54  		afterActionMap.remove(siteName, entityName);
55  	}
56  
57  	public void registerBeforeCustomFormAction(String siteName,
58  			String customFormName, TgwFunction function) {
59  		beforeCustomFormMap.put(function, siteName, customFormName);
60  	}
61  
62  	public void registerAfterCustomFormAction(String siteName,
63  			String customFormName, TgwFunction function) {
64  		afterCustomFormMap.put(function, siteName, customFormName);
65  	}
66  
67  	public void deregisterBeforeCustomFormAction(String siteName,
68  			String customFormName) {
69  		beforeCustomFormMap.remove(siteName, customFormName);
70  	}
71  
72  	public void deregisterAfterCustomFormAction(String siteName,
73  			String customFormName) {
74  		afterCustomFormMap.remove(siteName, customFormName);
75  	}
76  
77  	public Object invoke(MethodInvocation invocation) throws Throwable {
78  		AbstractExeFunction function = (AbstractExeFunction) invocation
79  				.getThis();
80  		String thisSchemaName = function.getSchema();
81  		String thisEntityName = function.getEntityName();
82  		String methodName = function.getName();
83  
84  		TgwFunction beforeTFunction = (TgwFunction) beforeActionMap.get(
85  				thisSchemaName, thisEntityName);
86  		TgwFunction afterTFunction = (TgwFunction) afterActionMap.get(
87  				thisSchemaName, thisEntityName);
88  		TgwFunction beforeCustomFormFunction = (TgwFunction) beforeCustomFormMap
89  				.get(thisSchemaName, thisEntityName + "." + methodName);
90  		TgwFunction afterCustomFormFunction = (TgwFunction) afterCustomFormMap
91  				.get(thisSchemaName, thisEntityName + "." + methodName);
92  
93  		Object[] args = invocation.getArguments();
94  		
95  		if(args != null && args.length == 0){
96  			return invocation.proceed();
97  		}
98  		
99  		
100 		Object obj = args[0];
101 		
102 		
103 		if (obj instanceof Long) {
104 			obj = null;
105 		}
106 
107 		boolean saveFlag = obj != null && !EntityUtils.hasId(obj);
108 
109 		String pageName = TgwContext.getPageName();
110 
111 		if (beforeTFunction != null && saveFlag) {
112 			beforeTFunction.run(obj);
113 		}
114 		if (beforeCustomFormFunction != null && saveFlag) {
115 			beforeCustomFormFunction.run(obj);
116 		}
117 
118 		boolean pageMovedFlag = (pageName != null && !pageName
119 				.equals(TgwContext.getPageName()));
120 
121 		if (pageMovedFlag) {
122 			return null;
123 		}
124 
125 		Object ret = invocation.proceed();
126 		
127 		Long id = EntityUtils.getId(obj);
128 		if (afterTFunction != null && saveFlag) {
129 			TgwPluginUtils.bindEntityId(function.getEntity(),id);			
130 			afterTFunction.run(obj);			
131 		}
132 		if (afterCustomFormFunction != null && saveFlag) {
133 			TgwPluginUtils.bindEntityId(function.getEntity(),id);			
134 			afterCustomFormFunction.run(obj);
135 		}
136 		return ret;
137 	}
138 }