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.plugin;
17  
18  import java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  import javassist.CannotCompileException;
23  
24  import org.seasar.framework.container.S2Container;
25  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
26  import org.seasar.tuigwaa.cms.ContentsService;
27  import org.seasar.tuigwaa.cms.ContentsStoreService;
28  import org.seasar.tuigwaa.cms.core.CmsConstants;
29  import org.seasar.tuigwaa.cms.core.CmsRequest;
30  import org.seasar.tuigwaa.cms.core.Page;
31  import org.seasar.tuigwaa.controller.ControllerService;
32  import org.seasar.tuigwaa.database.DataTable;
33  import org.seasar.tuigwaa.database.function.CriteriaExeFunction;
34  import org.seasar.tuigwaa.database.function.DaoMethod;
35  import org.seasar.tuigwaa.database.function.criteria.CriteriaFunction;
36  import org.seasar.tuigwaa.database.function.criteria.CriteriaListFunction;
37  import org.seasar.tuigwaa.database.function.criteria.EqCriteriaFunction;
38  import org.seasar.tuigwaa.database.function.criteria.FirstResultCriteriaFunction;
39  import org.seasar.tuigwaa.database.function.criteria.MaxResultCriteriaFunction;
40  import org.seasar.tuigwaa.model.DAOService;
41  import org.seasar.tuigwaa.model.DataService;
42  import org.seasar.tuigwaa.model.ModelService;
43  import org.seasar.tuigwaa.model.common.EntityDAO;
44  import org.seasar.tuigwaa.model.core.TgwAttribute;
45  import org.seasar.tuigwaa.model.core.TgwEntity;
46  import org.seasar.tuigwaa.model.core.impl.FkAttribute;
47  import org.seasar.tuigwaa.model.core.impl.LinkAttribute;
48  import org.seasar.tuigwaa.model.core.impl.SelfAttribute;
49  import org.seasar.tuigwaa.plugin.database.TablePlugin;
50  import org.seasar.tuigwaa.plugin.database.component.ResultDto;
51  import org.seasar.tuigwaa.system.Constants;
52  import org.seasar.tuigwaa.system.ServiceHelper;
53  import org.seasar.tuigwaa.system.TgwRuntimeException;
54  import org.seasar.tuigwaa.system.TgwSecurityException;
55  import org.seasar.tuigwaa.util.TgwContext;
56  
57  import com.isenshi.util.CharUtil;
58  import com.isenshi.util.HtmlBuffer;
59  
60  public abstract class AbstractTgwPlugin extends AbstractPlugin {
61  
62  	private S2Container container;
63  
64  	protected Object getService(Object key) {
65  		if (container == null) {
66  			container = SingletonS2ContainerFactory.getContainer();
67  		}
68  		return container.getComponent(key);
69  	}
70  
71  	protected TgwEntity getEntity(String domainName, String entityDisplayName) {
72  		ModelService entityService = (ModelService) getService(ModelService.class);
73  		TgwEntity entity = entityService.getEntityByDisplayName(domainName,
74  				entityDisplayName);
75  		if (entity == null) {
76  			entity = entityService.getEntity(domainName, entityDisplayName);
77  		}
78  		return entity;
79  	}
80  
81  	protected void createEntity(TgwEntity entity) throws PluginException {
82  		ModelService entityService = (ModelService) getService(ModelService.class);
83  		ControllerService controller = (ControllerService) getService(ControllerService.class);
84  		try {
85  			entityService.createEntity(entity);
86  			controller.addEntityConfig(entity);
87  		} catch (CannotCompileException cce) {
88  			throw new PluginException(cce);
89  		} catch (ClassNotFoundException cnfe) {
90  			throw new PluginException(cnfe);
91  		}
92  	}
93  
94  	protected EntityDAO getEntityDAO(TgwEntity entity) {
95  		DAOService service = (DAOService) getService(DAOService.class);
96  		return service.getDAO(entity);
97  	}
98  
99  	protected void bindEntityId(Map params, String entityName, String id) {
100 		params.put(TgwPluginUtils.createEntityBindingName(entityName), id);
101 	}
102 
103 	protected Map searchBindingFkData(CmsRequest req, TgwEntity entity) {
104 		Map map = new HashMap();
105 		if(entity == null){
106 			throw new TgwRuntimeException("ITGW3002");
107 		}
108 		Iterator itr = entity.getFieldIterator();
109 		while (itr.hasNext()) {
110 			TgwAttribute field = (TgwAttribute) itr.next();
111 			if (field instanceof FkAttribute || field instanceof SelfAttribute) {
112 				TgwEntity refEntity = ((LinkAttribute) field).getRefEntity();
113 				Object refBean = getBean(req, refEntity);
114 				map.put(field.getName(), refBean);
115 			}
116 		}
117 		return map;
118 	}
119 
120 	protected CriteriaFunction createBindingCriteria(CmsRequest req,
121 			TgwEntity entity) {
122 		Map bindingFkObjMap = searchBindingFkData(req, entity);
123 		CriteriaListFunction extraCriteria = new CriteriaListFunction();
124 
125 		for (Iterator itr = bindingFkObjMap.keySet().iterator(); itr.hasNext();) {
126 			String attrName = (String) itr.next();
127 			extraCriteria.addFunction(new EqCriteriaFunction(attrName,
128 					bindingFkObjMap.get(attrName)));
129 		}
130 		return extraCriteria;
131 	}
132 
133 	protected Object getBean(CmsRequest req, TgwEntity entity) {
134 		Object bean = TgwPluginUtils.getEntityObject(req, entity);
135 		if (bean == null) {
136 			Long id = TgwPluginUtils.getEntityId(req, entity);
137 			if (id == null) {
138 				return null;
139 			}
140 			bean = getEntityDAO(entity).load(id);
141 			TgwPluginUtils.bindEntityObject(req, entity, bean);
142 		}
143 		return bean;
144 	}
145 
146 	protected Object getBeanByUserName(CmsRequest req, TgwEntity entity) {
147 		Object bean = TgwPluginUtils.getEntityObject(req, entity);
148 		if (bean == null) {
149 			String userName = TgwContext.getPrincipal().getName();
150 			bean = getEntityDAO(entity).loadByValue(
151 					Constants.ENTITY_BUILTIN_CREATIONUSER, userName);
152 		}
153 		return bean;
154 	}
155 
156 	protected void assertIncludePage(CmsRequest request, String includePagepath)
157 			throws PluginException {
158 		if (includePagepath.trim().equals(
159 				request.getPage().getResource().getPath()))
160 			throw new PluginException("same page cannot be included.");
161 
162 		if (request.hasParentWikiRequest())
163 			throw new PluginException("multiple include is not be allowed.");
164 	}
165 
166 	protected Page getPage(CmsRequest request, String pagepath) {
167 		ContentsService service_ = (ContentsService) getService(ContentsService.class);
168 		String domain = request.getSiteName();
169 
170 		Page page = null;
171 
172 		try {
173 			page = service_.getPage(domain, pagepath,
174 					CmsConstants.OUTPUTTYPE_HTML);
175 		} catch (TgwSecurityException e) {
176 			// do nothing
177 		}
178 		return page;
179 	}
180 
181 	protected Page getPageData(CmsRequest request, String pagepath) {
182 		ContentsStoreService storeService = (ContentsStoreService) getService(ContentsStoreService.class);
183 		Page page = null;
184 		try {
185 			page = storeService.getPage(request.getSiteName(), pagepath);
186 		} catch (TgwSecurityException e) {
187 			// do nothing
188 		}
189 		return page;
190 	}
191 
192 	protected boolean existPageOrFolder(CmsRequest request, String pagepath) {
193 		ContentsStoreService service = (ContentsStoreService) getService(ContentsStoreService.class);
194 		try {
195 			return service.isExistResource(request.getSiteName(), pagepath,
196 					false);
197 		} catch (TgwSecurityException e) {
198 			return false;
199 		}
200 	}
201 
202 	protected boolean existPage(CmsRequest request, String pagepath) {
203 		ContentsStoreService service = (ContentsStoreService) getService(ContentsStoreService.class);
204 		try {
205 			return service.isExistResource(request.getSiteName(), pagepath,
206 					true);
207 		} catch (TgwSecurityException e) {
208 			return false;
209 		}
210 	}
211 
212 	protected String getNoPageMessage(CmsRequest request, String pagepath) {
213 		return getNoPageMessage(request, pagepath, null, null);
214 	}
215 
216 	protected String getNoPageMessage(CmsRequest request, String pagepath,
217 			String entityName, String id) {
218 
219 		HtmlBuffer buf = new HtmlBuffer();
220 		buf.appendStartTag("blockquote");
221 
222 		if (existPageOrFolder(request, pagepath)) { // Maybe Folder exist
223 			String message = getMessage("message.existfolder");
224 			message = CharUtil.replace(message, pagepath);
225 			buf.appendBody(message);
226 		} else {
227 			Map map = new HashMap();
228 			map.put(Constants.PARAM_PAGENAME, pagepath);
229 			if (id != null) {
230 				bindEntityId(map, entityName, id);
231 			}
232 			String message = getMessage("message.nopage");
233 			message = CharUtil.replace(message, pagepath);
234 			buf.appendBody(message);
235 //			buf.appendAnchor("./createNamedPage.do", "Yes", map);
236 			buf.appendAnchor(request.getContextPath() + "/" + request.getSiteName() + "/createNamedPage.do", "Yes", map);
237 		}
238 		buf.endTag();
239 		return buf.toString();
240 	}
241 
242 	protected DataTable getDataTable(String siteName, Class clazz,
243 			CriteriaListFunction function) {
244 		return getDataTable(siteName, TgwPluginUtils.toPluginEntityName(clazz),
245 				function);
246 	}
247 
248 	protected DataTable getDataTable(String siteName, String entityName,
249 			CriteriaListFunction function) {
250 
251 		DataTable dataTable = null;
252 		TgwEntity entity = getEntity(siteName, entityName);
253 
254 		if (entity != null) {
255 			EntityDAO dao = getEntityDAO(entity);
256 			DaoMethod method = dao.getMethod(EntityDAO.INJECT_CRITERIA);
257 			dataTable = (DataTable) method.evaluate(function);
258 		}
259 
260 		return dataTable;
261 	}
262 
263 	/***
264 	 * should not be called, if you use newer type of plugin framework where it
265 	 * uses plugin name to build name of entity not class name, in that case use
266 	 * AbstractPlugin#setPojoClass
267 	 */
268 	protected final TgwEntity requireEntity(String siteName, Class clazz)
269 			throws PluginException {
270 		String entityName = TgwPluginUtils.toPluginEntityName(clazz);
271 		TgwEntity entity = getEntity(siteName, entityName);
272 
273 		ModelService model = (ModelService) getService(ModelService.class);
274 		ControllerService controller = (ControllerService) getService(ControllerService.class);
275 		
276 		ServiceHelper helper = (ServiceHelper) getService(ServiceHelper.class);
277 
278 		if (entity == null) {
279 			entity = model.readJavaClass(siteName, entityName, clazz);
280 			try {
281 				model.createEntity(entity);
282 				controller.addEntityConfig(entity);
283 				// for TUIGWAA-113
284 				helper.loadFunctionConfigs(entity);
285 			} catch (Exception e) {
286 				throw new PluginException(e);
287 			}
288 		}
289 		return entity;
290 	}
291 
292 	protected final boolean isAfterAction(CmsRequest request) {
293 		Object obj = request
294 				.getParameter("afteraction:" + getClass().getName());
295 		return obj != null;
296 	}
297 
298 	private static final String SEARCH_RESULT_FILTER = "_SRESULT_";
299 
300 	protected ResultDto getResultDto(CmsRequest req, TgwEntity entity,
301 			String filter) {
302 		return getResultDto(req, entity, filter, 0);
303 	}
304 
305 	protected ResultDto getResultDto(CmsRequest req, TgwEntity entity,
306 			String filter, int firstResult) {
307 		return getResultDto(req, entity, filter, firstResult, 0);
308 	}
309 
310 	protected ResultDto getResultDto(CmsRequest req, TgwEntity entity,
311 			String filter, int firstResult, int max) {
312 
313 		if (SEARCH_RESULT_FILTER.equals(filter)) {
314 			ResultDto dto = TablePlugin.getResult(entity);
315 			return dto;
316 		} else {
317 			ResultDto dto = createResultDto(req, entity, filter, firstResult,
318 					max);
319 			dto.setPath(TgwContext.getContextPath() + "/"
320 					+ TgwContext.getSiteName() + "/"
321 					+ CharUtil.urlEncode(req.getMainPagePath()) + "?"
322 					+ Constants.PARAM_PREFIX_ENTITY_ESCAPE + Constants.PARAM_ENTITY_NAME + "=" + entity.getName() 
323 					+ req.getURLEncodedQuery());
324 			return dto;
325 		}
326 	}
327 
328 	private int getMax(TgwEntity entity, String filterName) {
329 		if (filterName == null || filterName.equals("")) {
330 			return 0;
331 		}
332 		CriteriaExeFunction exe = (CriteriaExeFunction) getEntityDAO(entity)
333 				.getMethod(filterName);
334 		if (exe == null) {
335 			return 0;
336 		}
337 		CriteriaListFunction clist = (CriteriaListFunction) exe
338 				.getUnaryCriteriaFunction();
339 		Iterator itr = clist.getFunctionList().iterator();
340 		while (itr.hasNext()) {
341 			Object obj = itr.next();
342 			if (obj instanceof MaxResultCriteriaFunction) {
343 				Integer i = (Integer) ((MaxResultCriteriaFunction) obj)
344 						.getValue();
345 				return i.intValue();
346 			}
347 		}
348 		return 0;
349 	}
350 
351 	private ResultDto createResultDto(CmsRequest req, TgwEntity entity,
352 			String filter, int firstResult, int max) {
353 		CriteriaListFunction criteriaList = new CriteriaListFunction();
354 		criteriaList.addFunction(new FirstResultCriteriaFunction(firstResult));
355 		criteriaList.addFunction(createBindingCriteria(req, entity));
356 
357 		DataService dataService = (DataService) getService(DataService.class);
358 		DataTable dataTable = (DataTable) dataService.find(entity, filter,
359 				criteriaList);
360 		ResultDto dto = new ResultDto(entity, dataTable);
361 
362 		if (max == 0) {
363 			max = getMax(entity, filter);
364 		}
365 		if (max > 0) {
366 			int rows = (filter == null || "".equals(filter)) ? dataService
367 					.rowCount(entity) : dataService.rowCountByFilter(entity,
368 					filter, null, null);
369 			dto.setRows(rows);
370 		}
371 		dto.setMax(max);
372 		dto.setFirst(firstResult);
373 		return dto;
374 	}
375 }