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.controller;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.struts.validator.ValidatorForm;
25  import org.seasar.tuigwaa.cms.ContentsStoreService;
26  import org.seasar.tuigwaa.logic.TgwEvent;
27  import org.seasar.tuigwaa.logic.TgwFunction;
28  import org.seasar.tuigwaa.logic.TgwLogic;
29  import org.seasar.tuigwaa.logic.functor.AggregationValuePredicate;
30  import org.seasar.tuigwaa.logic.functor.EmailSendProcedure;
31  import org.seasar.tuigwaa.logic.functor.PageJumpProcedure;
32  import org.seasar.tuigwaa.logic.functor.RowDeleteProcedure;
33  import org.seasar.tuigwaa.logic.functor.RowUpdateProcedure;
34  import org.seasar.tuigwaa.model.DAOService;
35  import org.seasar.tuigwaa.model.ModelService;
36  import org.seasar.tuigwaa.model.common.EntityInfo;
37  import org.seasar.tuigwaa.model.common.EntityUtils;
38  import org.seasar.tuigwaa.model.core.TgwAttribute;
39  import org.seasar.tuigwaa.model.core.TgwEntity;
40  import org.seasar.tuigwaa.util.functor.TestProcedure;
41  import org.seasar.tuigwaa.util.functor.TruePredicate;
42  
43  import com.isenshi.util.functor.UnaryPredicate;
44  import com.isenshi.util.functor.UnaryProcedure;
45  
46  public class LogicForm extends ValidatorForm{
47  
48  	private static final long serialVersionUID = 5944162789716916L;
49  
50  	private static final String PROC_PAGEJUMP = "pageJump";
51  
52  	private static final String PROC_ROWUPDATE = "rowUpdate";
53  
54  	private static final String PROC_ROWDELETE = "rowDelete";
55  
56  	private static final String PROC_EMAILSEND = "mailSend";
57  
58  	private static final String PREDICATE_ANYTIME = "anytime";
59  
60  	private static final String PREDICATE_VALUEFUNC = "valueFunc";
61  
62  	private String siteName;
63  
64  	private ModelService entityService;
65  
66  	private ContentsStoreService slideService;
67  
68  	private DAOService daoService;
69  
70  	private String name;
71  
72  	private EventForm event = new EventForm();
73  
74  	private ProcedureForm procedure = new ProcedureForm();
75  
76  	private String predicateType = PREDICATE_ANYTIME;
77  
78  	private String procedureType = PROC_PAGEJUMP;
79  
80  	private ValueFuncPredicateForm valueFuncPredicate = new ValueFuncPredicateForm();
81  
82  	private boolean saved = false;
83  
84  	public LogicForm(String siteName, ModelService entityService,
85  			ContentsStoreService slideService, DAOService daoService) {
86  		this(siteName, null, entityService, slideService, daoService);
87  	}
88  
89  	public LogicForm(String siteName, TgwLogic logic,
90  			ModelService entityService, ContentsStoreService slideService,
91  			DAOService daoService) {
92  
93  		this.siteName = siteName;
94  		this.entityService = entityService;
95  		this.slideService = slideService;
96  		this.daoService = daoService;
97  
98  		if (logic != null) {
99  			init(logic);
100 		}
101 	}
102 
103 	public boolean isSaved() {
104 		return saved;
105 	}
106 
107 	private void init(TgwLogic logic) {
108 		this.saved = true;
109 		this.name = logic.getName();
110 
111 		TgwEvent tgwevent = logic.getEvent();
112 		TgwFunction function = logic.getFunction();
113 
114 		event.setEvent(tgwevent);
115 
116 		UnaryPredicate predi = function.getPredicate();
117 		if (predi instanceof TruePredicate) {
118 			predicateType = PREDICATE_ANYTIME;
119 		} else if (predi instanceof AggregationValuePredicate) {
120 			predicateType = PREDICATE_VALUEFUNC;
121 			valueFuncPredicate.setPredicate((AggregationValuePredicate) predi);
122 		}
123 
124 		UnaryProcedure proc = function.getProcedure();
125 		setProcedure(proc);
126 	}
127 
128 	public List getPageList() {
129 		try {
130 			return slideService.getRecursivePageList(siteName, "");
131 		} catch (Exception e) {
132 			e.printStackTrace();
133 			return null;
134 		}
135 	}
136 
137 	public Iterator getEntityList() {
138 		return entityService.getEntityIterator(siteName);
139 	}
140 
141 	public List getUpdateRuleList() {
142 		List infoList = entityService.createEntityInfoList(siteName);
143 		if (infoList == null) {
144 			return null;
145 		}
146 		List list = new ArrayList();
147 		for (Iterator i = infoList.iterator(); i.hasNext();) {
148 			EntityInfo info = (EntityInfo) i.next();
149 			list.addAll(info.getUpdateRuleList());
150 		}
151 		return list;
152 	}
153 
154 	public Map getCustomFormMap() {
155 		Iterator itr = entityService.getEntityIterator(siteName);
156 		if (itr == null) {
157 			return null;
158 		}
159 
160 		Map map = new HashMap();
161 		while (itr.hasNext()) {
162 			TgwEntity entity = (TgwEntity) itr.next();
163 			Map customFormMap = daoService.getCustomFormFunctionMap(entity);
164 			if (customFormMap != null && customFormMap.size() > 0) {
165 				map.put(entity, customFormMap.values());
166 			}
167 		}
168 		return map;
169 	}
170 
171 	public Map getAggregationValueMap() {
172 		Iterator itr = entityService.getEntityIterator(siteName);
173 		if (itr == null) {
174 			return null;
175 		}
176 		Map map = new HashMap();
177 		while (itr.hasNext()) {
178 			TgwEntity entity = (TgwEntity) itr.next();
179 			Map valueFuncMap = daoService.getValueFunctionMap(entity);
180 			if (valueFuncMap != null && valueFuncMap.size() > 0) {
181 				map.put(entity, valueFuncMap.values());
182 			}
183 		}
184 		return map;
185 	}
186 
187 	public void setName(String name) {
188 		this.name = name;
189 	}
190 
191 	public String getName() {
192 		return name;
193 	}
194 
195 	public EventForm getEvent() {
196 		return event;
197 	}
198 
199 	public void setEvent(EventForm event) {
200 		this.event = event;
201 	}
202 
203 	public String getPredicateType() {
204 		return predicateType;
205 	}
206 
207 	public void setPredicateType(String predicateType) {
208 		this.predicateType = predicateType;
209 	}
210 
211 	public TgwLogic getLogic() {
212 		String name = getName();
213 		TgwEvent tevent = getEvent().getEvent();
214 
215 		UnaryPredicate predicate = createPredicate();
216 		UnaryProcedure procedure = createProcedure(tevent);
217 		TgwFunction tfunction = new TgwFunction(predicate, procedure);
218 
219 		TgwLogic tlogic = new TgwLogic(name, tevent, tfunction);
220 		return tlogic;
221 	}
222 
223 	private UnaryPredicate createPredicate() {
224 		if (getPredicateType().equals(PREDICATE_ANYTIME)) {
225 			return TruePredicate.INSTANCE;
226 		} else if (getPredicateType().equals(PREDICATE_VALUEFUNC)) {
227 			return getValueFuncPredicate().getPredicate();
228 		} else {
229 			return null;
230 		}
231 	}
232 
233 	public void setProcedure(UnaryProcedure proc) {
234 		if (proc instanceof PageJumpProcedure) {
235 			PageJumpProcedure pageJumpProc = (PageJumpProcedure) proc;
236 			procedureType = PROC_PAGEJUMP;
237 			procedure.setPageName(pageJumpProc.getPageName());
238 		} else if (proc instanceof RowUpdateProcedure) {
239 			RowUpdateProcedure rowUpdateProc = (RowUpdateProcedure) proc;
240 			procedureType = PROC_ROWUPDATE;
241 			procedure.setUpdateRuleName(rowUpdateProc.getEntityName() + "."
242 					+ rowUpdateProc.getUpdateDaoName());
243 		} else if (proc instanceof RowDeleteProcedure) {
244 			RowDeleteProcedure rowDleteProc = (RowDeleteProcedure) proc;
245 			procedure.setDeleteEntityName(rowDleteProc.getEntityName());
246 			procedureType = PROC_ROWDELETE;
247 		} else if (proc instanceof EmailSendProcedure) {
248 			EmailSendProcedure emailSendProc = (EmailSendProcedure) proc;
249 			procedureType = PROC_EMAILSEND;
250 			procedure.setMailPageName(emailSendProc.getPageName());
251 			procedure.setMailType(emailSendProc.getType());
252 		}
253 	}
254 
255 	public UnaryProcedure createProcedure(TgwEvent tevent) {
256 		if (getProcedureType().equals(PROC_PAGEJUMP)) {
257 			return new PageJumpProcedure(getProcedure().getPageName());
258 		} else if (getProcedureType().equals(PROC_ROWDELETE)) {
259 			String entityName = getProcedure().getDeleteEntityName();
260 			return new RowDeleteProcedure(entityName);
261 		} else if (getProcedureType().equals(PROC_ROWUPDATE)) {
262 			String updateRuleName = getProcedure().getUpdateRuleName();
263 			String[] array = updateRuleName.split("//.");
264 			String entityName = array[0];
265 			String daoName = array[1];
266 			return new RowUpdateProcedure(entityName, daoName);
267 		} else if (getProcedureType().equals(PROC_EMAILSEND)) {
268 			String entityName = tevent.getOption();
269 			TgwEntity entity = entityService.getEntity(siteName, entityName);
270 			if (entity == null) {
271 				return null;
272 			}
273 			String emailField = null;
274 			for (Iterator i = entity.getFieldIterator(); i.hasNext();) {
275 				TgwAttribute field = (TgwAttribute) i.next();
276 				if (EntityUtils.isEmail(field)) {
277 					emailField = field.getName();
278 				}
279 			}
280 			return new EmailSendProcedure(emailField, getProcedure()
281 					.getMailPageName(), getProcedure().getMailType());
282 		} else {
283 			return new TestProcedure();
284 		}
285 	}
286 
287 	public String getProcedureType() {
288 		return procedureType;
289 	}
290 
291 	public void setProcedureType(String procedureType) {
292 		this.procedureType = procedureType;
293 	}
294 
295 	public ValueFuncPredicateForm getValueFuncPredicate() {
296 		return valueFuncPredicate;
297 	}
298 
299 	public void setValueFuncPredicate(ValueFuncPredicateForm valueFuncPredicate) {
300 		this.valueFuncPredicate = valueFuncPredicate;
301 	}
302 
303 	public ProcedureForm getProcedure() {
304 		return procedure;
305 	}
306 
307 	public void setProcedure(ProcedureForm procedure) {
308 		this.procedure = procedure;
309 	}
310 
311 	public class EventForm {
312 
313 		private String type = TgwEvent.PAGE_VIEW;
314 
315 		private String pageName;
316 
317 		private String afterEntityName;
318 
319 		private String beforeEntityName;
320 
321 		private String afterCustomFormName;
322 
323 		private String beforeCustomFormName;
324 
325 		public void setType(String type) {
326 			this.type = type;
327 		}
328 
329 		public String getType() {
330 			return type;
331 		}
332 
333 		public String getAfterEntityName() {
334 			return afterEntityName;
335 		}
336 
337 		public void setAfterEntityName(String afterEntityName) {
338 			this.afterEntityName = afterEntityName;
339 		}
340 
341 		public String getBeforeEntityName() {
342 			return beforeEntityName;
343 		}
344 
345 		public void setBeforeEntityName(String beforeEntityName) {
346 			this.beforeEntityName = beforeEntityName;
347 		}
348 
349 		public String getPageName() {
350 			return pageName;
351 		}
352 
353 		public void setPageName(String pageName) {
354 			this.pageName = pageName;
355 		}
356 
357 		public String getAfterCustomFormName() {
358 			return afterCustomFormName;
359 		}
360 
361 		public void setAfterCustomFormName(String afterCustomFormName) {
362 			this.afterCustomFormName = afterCustomFormName;
363 		}
364 
365 		public String getBeforeCustomFormName() {
366 			return beforeCustomFormName;
367 		}
368 
369 		public void setBeforeCustomFormName(String beforeCustomFormName) {
370 			this.beforeCustomFormName = beforeCustomFormName;
371 		}
372 
373 		public void setEvent(TgwEvent event) {
374 			setType(event.getType());
375 			String option = event.getOption();
376 			if (TgwEvent.PAGE_VIEW.equals(getType())) {
377 				setPageName(option);
378 			} else if (TgwEvent.BEFORE_DATA_SAVE.equals(getType())) {
379 				setBeforeEntityName(option);
380 			} else if (TgwEvent.AFTER_DATA_SAVE.equals(getType())) {
381 				setAfterEntityName(option);
382 			} else if (TgwEvent.BEFORE_CUSTOMFORM_SAVE.equals(getType())) {
383 				setBeforeCustomFormName(option);
384 			} else if (TgwEvent.AFTER_CUSTOMFORM_SAVE.equals(getType())) {
385 				setAfterCustomFormName(option);
386 			}
387 		}
388 
389 		public TgwEvent getEvent() {
390 			String option = null;
391 			if (TgwEvent.PAGE_VIEW.equals(getType())) {
392 				option = getPageName();
393 			} else if (TgwEvent.BEFORE_DATA_SAVE.equals(getType())) {
394 				option = getBeforeEntityName();
395 			} else if (TgwEvent.AFTER_DATA_SAVE.equals(getType())) {
396 				option = getAfterEntityName();
397 			} else if (TgwEvent.BEFORE_CUSTOMFORM_SAVE.equals(getType())) {
398 				option = getBeforeCustomFormName();
399 			} else if (TgwEvent.AFTER_CUSTOMFORM_SAVE.equals(getType())) {
400 				option = getAfterCustomFormName();
401 			}
402 			return new TgwEvent(getType(), option);
403 		}
404 	}
405 
406 	public class ValueFuncPredicateForm {
407 
408 		private String funcName;
409 
410 		private String condition;
411 
412 		private Double value;
413 
414 		public void setFuncName(String funcName) {
415 			this.funcName = funcName;
416 		}
417 
418 		public String getFuncName() {
419 			return funcName;
420 		}
421 
422 		public void setCondition(String condition) {
423 			this.condition = condition;
424 		}
425 
426 		public String getCondition() {
427 			return condition;
428 		}
429 
430 		public void setValue(Double value) {
431 			this.value = value;
432 		}
433 
434 		public Double getValue() {
435 			return value;
436 		}
437 
438 		public UnaryPredicate getPredicate() {
439 			String[] array = funcName.split("//.");
440 			String entityName = array[0];
441 			String aggFuncName = array[1];
442 
443 			return new AggregationValuePredicate(siteName, entityName,
444 					aggFuncName, value, condition);
445 		}
446 
447 		public void setPredicate(AggregationValuePredicate aggPredi) {
448 			this.funcName = aggPredi.getEntityName() + "."
449 					+ aggPredi.getAggregationFuncName();
450 			this.value = aggPredi.getValue();
451 			this.condition = aggPredi.getOpcode();
452 		}
453 	}
454 
455 	public class ProcedureForm {
456 
457 		private String pageName;
458 
459 		private String mailPageName;
460 
461 		private String mailType;
462 		
463 		private String updateRuleName;
464 
465 		private String deleteEntityName;
466 
467 		public String getPageName() {
468 			return pageName;
469 		}
470 
471 		public void setPageName(String pageName) {
472 			this.pageName = pageName;
473 		}
474 
475 		public void setMailPageName(String mailPageName) {
476 			this.mailPageName = mailPageName;
477 		}
478 
479 		public String getMailPageName() {
480 			return mailPageName;
481 		}
482 
483 		public void setUpdateRuleName(String updateRuleName) {
484 			this.updateRuleName = updateRuleName;
485 		}
486 
487 		public String getUpdateRuleName() {
488 			return updateRuleName;
489 		}
490 
491 		public String getDeleteEntityName() {
492 			return deleteEntityName;
493 		}
494 
495 		public void setDeleteEntityName(String deleteEntityName) {
496 			this.deleteEntityName = deleteEntityName;
497 		}
498 		
499 		public String getMailType() {
500 			return mailType;
501 		}
502 		
503 		public void setMailType(String mailType) {
504 			this.mailType = mailType;
505 		}
506 	}
507 }