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.Collection;
20  import java.util.List;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.struts.action.ActionForm;
28  import org.apache.struts.action.ActionForward;
29  import org.apache.struts.action.ActionMapping;
30  import org.seasar.tuigwaa.model.ModelService;
31  import org.seasar.tuigwaa.model.common.DomainUtils;
32  import org.seasar.tuigwaa.model.common.EntityInfo;
33  import org.seasar.tuigwaa.model.core.TgwEntity;
34  import org.seasar.tuigwaa.system.Constants;
35  import org.seasar.tuigwaa.system.ServiceHelper;
36  import org.seasar.tuigwaa.system.SiteConfig;
37  import org.seasar.tuigwaa.system.TgwServiceException;
38  import org.seasar.tuigwaa.util.TgwContext;
39  import org.seasar.tuigwaa.util.TgwUtils;
40  import org.seasar.tuigwaa.util.ajax.AbstractRowSetAction;
41  import org.seasar.tuigwaa.util.ajax.RowSetForm;
42  
43  /***
44   * @author nishioka
45   */
46  public class EntityAction extends AbstractRowSetAction {
47  
48  	private Log log = LogFactory.getLog(getClass());
49  
50  	private ModelService model;
51  
52  	private ControllerService controller;
53  
54  	private ServiceHelper helper;
55  	
56  	public EntityAction(ModelService entityService, ControllerService controller, ServiceHelper helper) {
57  		this.model = entityService;
58  		this.controller = controller;
59  		this.helper = helper;
60  	}
61  
62  	public String getFormName() {
63  		return "entityForm";
64  	}
65  
66  	public String getEditPath() {
67  		return "tiles.sitemanage.createTable";
68  	}
69  
70  	public String getBasePath() {
71  		return "tiles.sitemanage.tablelist";
72  	}
73  
74  	public ActionForward viewEntity(ActionMapping mapping, ActionForm form,
75  			HttpServletRequest request, HttpServletResponse response)
76  			throws Exception {
77  		TgwEntity entity = TgwUtils.getEntity(request, getServlet());
78  		return doViewEntityDetail(mapping, form, request, response, entity);
79  	}
80  
81  	public RowSetForm getNewRowSet(ActionMapping mapping, ActionForm form,
82  			HttpServletRequest request, HttpServletResponse response)
83  			throws Exception {
84  		SiteConfig siteConfig = TgwContext.getSiteConfig();
85  		String schema = siteConfig.getName();
86  		boolean easyMode = siteConfig.isEasyMode();
87  
88  		bindEntityInfoList(schema, request);
89  		return new EntityForm(schema, easyMode, model);
90  	}
91  
92  	public RowSetForm getRowSet(ActionMapping mapping, ActionForm form,
93  			HttpServletRequest request, HttpServletResponse response)
94  			throws Exception {
95  		TgwEntity entity = TgwUtils.getEntity(request, getServlet());
96  		SiteConfig siteConfig = TgwContext.getSiteConfig();
97  		boolean easyMode = siteConfig.isEasyMode();
98  		bindEntityInfoList(siteConfig.getName(), request);
99  		return new EntityForm(entity, easyMode, model);
100 	}
101 
102 	public ActionForward list(ActionMapping mapping, ActionForm form,
103 			HttpServletRequest request, HttpServletResponse response)
104 			throws Exception {
105 		String siteName = TgwContext.getSiteName();
106 		bindEntityInfoList(siteName, request);
107 		return getBaseForward();
108 	}
109 
110 	public ActionForward drop(ActionMapping mapping, ActionForm form,
111 			HttpServletRequest request, HttpServletResponse response)
112 			throws Exception {
113 		TgwEntity entity = TgwUtils.getEntity(request, getServlet());
114 
115 		log.info("Dropping Entity ... " + entity);
116 
117 		String siteName = TgwContext.getSiteName();
118 		String entityName = entity.getName();
119 
120 		model.dropEntity(siteName, entityName);
121 		controller.removeEntityConfig(siteName, entityName);
122 
123 		return list(mapping, form, request, response);
124 	}
125 
126 	public ActionForward doSave(ActionMapping mapping, ActionForm form,
127 			HttpServletRequest request, HttpServletResponse response)
128 			throws Exception {
129 
130 		EntityForm entityForm = (EntityForm) form;
131 		TgwEntity entity = entityForm.createEntity();
132 		List additionalEntities = entityForm.getAdditionalEntity();
133 
134 		log.info("Creating Entity ... " + entity.getName() + ","
135 				+ entity.getFieldIterator());
136 
137 		if (entityForm.isSaved()) {
138 			if(additionalEntities != null && additionalEntities.size()>0){
139 				model.createEntities(entity.getDomainName(), additionalEntities);
140 				controller.addEntityConfigs(additionalEntities);
141 			}
142 			boolean changed = entityForm.isChangedRowElement();
143 			Collection entities = model.alterEntity(entity, changed);
144 			controller.alterEntityConfigs(entities);
145 		} else {
146 			if (additionalEntities != null) {
147 				List list = new ArrayList();
148 				list.add(entity);
149 				list.addAll(additionalEntities);
150 				model.createEntities(entity.getDomainName(), list);
151 				controller.addEntityConfigs(list);
152 			} else {
153 				model.createEntity(entity);
154 				controller.addEntityConfig(entity);
155 			}
156 		}
157 		helper.setupFileDataTable(entity);
158 		return list(mapping, form, request, response);
159 	}
160 
161 	public ActionForward editOrder(ActionMapping mapping, ActionForm form,
162 			HttpServletRequest request, HttpServletResponse response)
163 			throws Exception {
164 
165 		TgwEntity entity = TgwUtils.getEntity(request, getServlet());
166 		String index = request.getParameter(Constants.PARAM_ORDER_IDX);
167 
168 		try {
169 			int idx = Integer.parseInt(index);
170 			entity.swapChild(idx - 1, idx);
171 			model.updateEntity(entity);
172 		} catch (NumberFormatException nfe) {
173 			log.error(nfe.getMessage());
174 		} catch (TgwServiceException tse) {
175 			log.error(tse.getMessage());
176 			tse.printStackTrace();
177 		}
178 		return doViewEntityDetail(mapping, form, request, response, entity);
179 	}
180 
181 	private ActionForward doViewEntityDetail(ActionMapping mapping,
182 			ActionForm form, HttpServletRequest request,
183 			HttpServletResponse response, TgwEntity entity) {
184 
185 		Collection entitySet = DomainUtils.getReferencedEntities(entity);
186 		request.setAttribute(Constants.RATTR_ENTITY_REFLIST, entitySet);
187 
188 		EntityInfo info = model.createEntityInfo(entity.getDomainName(), entity
189 				.getName());
190 		request.setAttribute(Constants.RATTR_ENTITYINFO, info);
191 
192 		bindEntityInfoList(entity.getDomainName(), request);
193 		return mapping.findForward("success");
194 	}
195 
196 	private void bindEntityInfoList(String siteName, HttpServletRequest request) {
197 		List infoList = model.createEntityInfoList(siteName);
198 		request.setAttribute(Constants.RATTR_ENTITYINFOLIST, infoList);
199 	}
200 }