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.system;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javassist.CannotCompileException;
24  
25  import org.seasar.tuigwaa.controller.ControllerService;
26  import org.seasar.tuigwaa.database.function.SearchExeFunction;
27  import org.seasar.tuigwaa.database.function.UpdateExeFunction;
28  import org.seasar.tuigwaa.database.util.FileData;
29  import org.seasar.tuigwaa.model.DAOService;
30  import org.seasar.tuigwaa.model.ModelService;
31  import org.seasar.tuigwaa.model.common.EntityUtils;
32  import org.seasar.tuigwaa.model.core.TgwEntity;
33  import org.seasar.tuigwaa.plugin.TgwPluginUtils;
34  import org.seasar.tuigwaa.view.CustomFormComponent;
35  import org.seasar.tuigwaa.view.HtmlService;
36  import org.seasar.tuigwaa.view.SearchFormComponent;
37  
38  public class ServiceHelperImpl implements ServiceHelper {
39  
40  	private HtmlService view;
41  
42  	private ModelService model;
43  
44  	private ControllerService controller;
45  
46  	private DAOService dao;
47  
48  	public void setView(HtmlService view) {
49  		this.view = view;
50  	}
51  
52  	public void setModel(ModelService model) {
53  		this.model = model;
54  	}
55  
56  	public void setController(ControllerService controller) {
57  		this.controller = controller;
58  	}
59  
60  	public void setDao(DAOService dao) {
61  		this.dao = dao;
62  	}
63  
64  	public SearchFormComponent getSearchForm(TgwEntity entity, String searchName) {
65  		SearchExeFunction exeFunction = getSearchExe(entity, searchName);
66  		return view.createSearchFormComponent(entity, exeFunction);
67  	}
68  
69  	public String getSearchActionName(TgwEntity entity, String sformName) {
70  		SearchExeFunction exeFunction = getSearchExe(entity, sformName);
71  		return controller.getSearchActionName(entity, exeFunction);
72  	}
73  
74  	public String getSearchFormName(TgwEntity entity, String sformName) {
75  		SearchExeFunction exeFunction = getSearchExe(entity, sformName);
76  		return controller.getSearchFormName(entity, exeFunction);
77  	}
78  
79  	public String getCustomFormName(TgwEntity entity, String cformName) {
80  		UpdateExeFunction exe = getUpdateExe(entity, cformName);
81  		return controller.getCustomFormName(entity, exe);
82  	}
83  
84  	public CustomFormComponent getCustomForm(TgwEntity entity,
85  			String cformName, Object bean) {
86  		UpdateExeFunction exeFunction = getUpdateExe(entity, cformName);
87  		return view.createCustomFormComponent(entity, bean, exeFunction);
88  	}
89  
90  	public String getCustomActionName(TgwEntity entity, String cformName) {
91  		UpdateExeFunction exe = getUpdateExe(entity, cformName);
92  		return controller.getCustomActionName(entity, exe);
93  	}
94  
95  	private UpdateExeFunction getUpdateExe(TgwEntity entity, String cformName) {
96  		return (UpdateExeFunction) dao.getCustomFormFunctionMap(entity).get(
97  				cformName);
98  	}
99  
100 	public SearchExeFunction getSearchExe(TgwEntity entity, String sformName) {
101 		Map map = dao.getSearchFunctionMap(entity);
102 		return (SearchExeFunction) map.get(sformName);
103 	}
104 
105 	public void setupControllerConfigs(String siteName) {
106 		List entities = model.getEntityList(siteName);
107 		controller.addEntityConfigs(entities);
108 		Iterator entityItr = model.getEntityIterator(siteName);
109 		while (entityItr.hasNext()) {
110 			TgwEntity entity = (TgwEntity) entityItr.next();
111 			loadFunctionConfigs(entity);
112 		}
113 	}
114 		
115 	public void loadFunctionConfigs(TgwEntity entity){
116 		dao.loadFunctions(entity);
117 		Map map = dao.getSearchFunctionMap(entity);
118 		if (map != null) {
119 			Iterator searchExeItr = map.keySet().iterator();
120 			while (searchExeItr.hasNext()) {
121 				SearchExeFunction exeFunction = (SearchExeFunction) map
122 						.get(searchExeItr.next());
123 				controller.addSearchConfig(entity, exeFunction);
124 			}
125 		}
126 		Map customFormMap = dao.getCustomFormFunctionMap(entity);
127 		if (customFormMap != null) {
128 			Iterator formItr = customFormMap.values().iterator();
129 			while (formItr.hasNext()) {
130 				UpdateExeFunction cform = (UpdateExeFunction) formItr
131 						.next();
132 				controller.addCustomFormConfig(entity, cform);
133 			}
134 		}		
135 	}	
136 
137 	public void setupFileDataTable(TgwEntity entity) {
138 		if (EntityUtils.isExistFileField(entity)) {
139 			TgwEntity fileEntity = getFileEntity(entity.getDomainName());
140 			if (fileEntity == null) {
141 				String entityName = TgwPluginUtils
142 						.toPluginEntityName(FileData.class);
143 				fileEntity = model.readJavaClass(entity.getDomainName(),
144 						entityName, FileData.class);
145 				try {
146 					model.createEntity(fileEntity);
147 				} catch (ClassNotFoundException e) {
148 					// do nothing
149 				} catch (CannotCompileException e) {
150 					// do nothing
151 				}
152 			}
153 		}
154 	}
155 
156 	public void setupFileDataTable(String siteName) {
157 		List list = new ArrayList();
158 		list.addAll(model.getEntityList(siteName));
159 		Iterator itr = list.iterator();
160 		while (itr.hasNext()) {
161 			TgwEntity entity = (TgwEntity) itr.next();
162 			setupFileDataTable(entity);
163 		}
164 	}
165 
166 	private TgwEntity getFileEntity(String domainName) {
167 		String entityName = TgwPluginUtils.toPluginEntityName(FileData.class);
168 		return model.getEntity(domainName, entityName);
169 	}
170 }