1 24 package org.riotfamily.riot.form.ui; 25 26 import java.io.IOException ; 27 import java.util.Map ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.riotfamily.common.web.util.ServletUtils; 33 import org.riotfamily.forms.Form; 34 import org.riotfamily.forms.factory.FormRepository; 35 import org.riotfamily.riot.editor.ObjectEditorDefinition; 36 import org.riotfamily.riot.editor.EditorDefinitionUtils; 37 import org.riotfamily.riot.editor.EditorRepository; 38 import org.riotfamily.riot.editor.ListDefinition; 39 import org.riotfamily.riot.list.ui.ListService; 40 import org.riotfamily.riot.list.ui.ListSession; 41 import org.riotfamily.riot.security.AccessController; 42 import org.springframework.transaction.PlatformTransactionManager; 43 import org.springframework.web.servlet.ModelAndView; 44 import org.springframework.web.servlet.view.RedirectView; 45 46 49 public class FormController extends BaseFormController { 50 51 private static final String PARAM_SAVED = "saved"; 52 53 private ListService listService; 54 55 public FormController(EditorRepository editorRepository, 56 FormRepository formRepository, 57 PlatformTransactionManager transactionManager, 58 ListService listService) { 59 60 super(editorRepository, formRepository, transactionManager); 61 this.listService = listService; 62 } 63 64 protected Map createModel(Form form, ObjectEditorDefinition editorDefinition, 65 HttpServletRequest request, HttpServletResponse response) { 66 67 Map model = super.createModel(form, editorDefinition, request, response); 68 model.put(PARAM_SAVED, Boolean.valueOf(request.getParameter(PARAM_SAVED) != null)); 69 70 Object object = null; 71 if (!form.isNew()) { 72 object = form.getBackingObject(); 73 } 74 75 if (!AccessController.isGranted("view", object) 76 || !AccessController.isGranted("use-editor", editorDefinition)) { 77 78 try { 79 response.sendError(HttpServletResponse.SC_FORBIDDEN); 80 } 81 catch (IOException e) { 82 } 83 return null; 84 } 85 86 87 model.put("childLists", editorDefinition.getChildEditorReferences(object, 88 form.getFormContext().getMessageResolver())); 89 90 ListDefinition parentListDef = EditorDefinitionUtils 91 .getParentListDefinition(editorDefinition); 92 93 if (parentListDef != null) { 94 ListSession session = listService.getOrCreateListSession( 95 parentListDef.getId(), FormUtils.getParentId(form), 96 null, request); 97 98 model.put("listKey", session.getKey()); 99 } 100 101 return model; 102 } 103 104 protected ModelAndView afterSave(Form form, ObjectEditorDefinition editorDefinition, 105 HttpServletRequest request, HttpServletResponse response) { 106 107 if (!editorDefinition.getChildEditorDefinitions().isEmpty()) { 108 return reloadForm(form, editorDefinition); 109 } 110 else { 111 return showParentList(form, editorDefinition); 112 } 113 } 114 115 protected ModelAndView afterUpdate(Form form, ObjectEditorDefinition editorDefinition, 116 HttpServletRequest request, HttpServletResponse response) { 117 118 return showParentList(form, editorDefinition); 119 } 120 121 protected ModelAndView showParentList(Form form, 122 ObjectEditorDefinition editorDefinition) { 123 124 String listUrl = editorDefinition.createEditorPath( 125 form.getBackingObject(), 126 form.getFormContext().getMessageResolver()) 127 .getParent().getEditorUrl(); 128 129 return new ModelAndView(new RedirectView(listUrl, true)); 130 } 131 132 protected ModelAndView reloadForm(Form form, 133 ObjectEditorDefinition editorDefinition) { 134 135 String formUrl = editorDefinition.createEditorPath( 136 form.getBackingObject(), 137 form.getFormContext().getMessageResolver()) 138 .getEditorUrl(); 139 140 formUrl = ServletUtils.addParameter(formUrl, "saved", "true"); 141 return new ModelAndView(new RedirectView(formUrl, true)); 142 } 143 144 public static String getUrl(String editorId, String objectId, String parentId) { 145 StringBuffer sb = new StringBuffer (); 146 sb.append("/form/").append(editorId); 147 if (objectId != null) { 148 sb.append('/').append(objectId); 149 } 150 if (parentId != null) { 151 sb.append("?parentId=").append(parentId); 152 } 153 return sb.toString(); 154 } 155 } 156 | Popular Tags |