1 24 package org.riotfamily.riot.list.ui; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 import javax.servlet.http.HttpSession ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.riotfamily.common.i18n.AdvancedMessageCodesResolver; 39 import org.riotfamily.common.i18n.MessageResolver; 40 import org.riotfamily.common.web.util.SessionReferenceRemover; 41 import org.riotfamily.common.xml.ConfigurableBean; 42 import org.riotfamily.common.xml.ConfigurationEventListener; 43 import org.riotfamily.forms.controller.FormContextFactory; 44 import org.riotfamily.riot.editor.EditorRepository; 45 import org.riotfamily.riot.editor.ListDefinition; 46 import org.riotfamily.riot.list.command.CommandResult; 47 import org.springframework.context.MessageSource; 48 import org.springframework.context.MessageSourceAware; 49 import org.springframework.transaction.PlatformTransactionManager; 50 import org.springframework.util.Assert; 51 import org.springframework.web.servlet.support.RequestContextUtils; 52 53 57 public class ListServiceImpl implements ListService, MessageSourceAware, 58 ConfigurationEventListener { 59 60 private static final Log log = LogFactory.getLog(ListServiceImpl.class); 61 62 private EditorRepository editorRepository; 63 64 private MessageSource messageSource; 65 66 private AdvancedMessageCodesResolver messageCodesResolver; 67 68 private FormContextFactory formContextFactory; 69 70 private PlatformTransactionManager transactionManager; 71 72 private Collection sessions = new ArrayList (); 73 74 public void setMessageCodesResolver(AdvancedMessageCodesResolver codesResolver) { 75 this.messageCodesResolver = codesResolver; 76 } 77 78 public void setEditorRepository(EditorRepository editorRepository) { 79 this.editorRepository = editorRepository; 80 editorRepository.addListener(this); 81 editorRepository.getListRepository().addListener(this); 82 editorRepository.getFormRepository().addListener(this); 83 } 84 85 public void setMessageSource(MessageSource messageSource) { 86 this.messageSource = messageSource; 87 } 88 89 public void setFormContextFactory(FormContextFactory formContextFactory) { 90 this.formContextFactory = formContextFactory; 91 } 92 93 public void setTransactionManager(PlatformTransactionManager transactionManager) { 94 this.transactionManager = transactionManager; 95 } 96 97 public ListSession getOrCreateListSession(String editorId, String parentId, 98 String choose, HttpServletRequest request) { 99 100 String key = "list-" + editorId + "#" + parentId; 101 if (choose != null) { 102 key += "-choose:" + choose; 103 } 104 105 ListSession listSession = null; 106 try { 107 listSession = getListSession(key, request); 108 } 109 catch (ListSessionExpiredException e) { 110 log.info("Session expired - creating a new one ..."); 111 } 112 if (listSession == null) { 113 ListDefinition listDef = editorRepository.getListDefinition(editorId); 114 Assert.notNull(listDef, "No such ListDefinition: " + editorId); 115 116 MessageResolver messageResolver = new MessageResolver(messageSource, 117 messageCodesResolver, RequestContextUtils.getLocale(request)); 118 119 listSession = new ListSession(key, listDef, parentId, messageResolver, 120 request.getContextPath(), editorRepository.getFormRepository(), 121 formContextFactory, transactionManager); 122 123 if (choose != null) { 124 listSession.setChooserTarget(editorRepository.getEditorDefinition(choose)); 125 } 126 127 HttpSession httpSession = request.getSession(); 128 httpSession.setAttribute(key, listSession); 129 sessions.add(listSession); 130 131 SessionReferenceRemover.removeFromCollectionOnInvalidation( 132 httpSession, sessions, listSession); 133 134 } 135 return listSession; 136 } 137 138 protected ListSession getListSession(String key, HttpServletRequest request) 139 throws ListSessionExpiredException { 140 141 ListSession session = (ListSession) request.getSession().getAttribute(key); 142 if (session != null) { 143 editorRepository.getListRepository().getListConfig(session.getListId()); 145 if (!session.isExpired()) { 146 return session; 147 } 148 } 149 throw new ListSessionExpiredException(); 150 151 } 152 153 public void beanReconfigured(ConfigurableBean bean) { 154 Iterator it = sessions.iterator(); 155 while (it.hasNext()) { 156 ListSession session = (ListSession) it.next(); 157 log.info("Invalidating session " + session.getKey()); 158 session.invalidate(); 159 it.remove(); 160 } 161 } 162 163 public ListModel getModel(String key,HttpServletRequest request) 164 throws ListSessionExpiredException { 165 166 return getListSession(key, request).getModel(request); 167 } 168 169 public String getFilterFormHtml(String key, HttpServletRequest request) 170 throws ListSessionExpiredException { 171 172 return getListSession(key, request).getFilterFormHtml(); 173 } 174 175 public List getListCommands(String key, HttpServletRequest request) 176 throws ListSessionExpiredException { 177 178 return getListSession(key, request).getListCommands(request); 179 } 180 181 public List getFormCommands(String key, String objectId, 182 HttpServletRequest request) throws ListSessionExpiredException { 183 184 return getListSession(key, request).getFormCommands(objectId, request); 185 } 186 187 public CommandResult execCommand(String key, ListItem item, 188 String commandId, boolean confirmed, 189 HttpServletRequest request, HttpServletResponse response) 190 throws ListSessionExpiredException { 191 192 return getListSession(key, request).execCommand( 193 item, commandId, confirmed, request, response); 194 } 195 196 public ListModel filter(String key, Map filter, HttpServletRequest request) 197 throws ListSessionExpiredException { 198 return getListSession(key, request).filter(filter, request); 199 } 200 201 public ListModel gotoPage(String key, int page, HttpServletRequest request) 202 throws ListSessionExpiredException { 203 204 return getListSession(key, request).gotoPage(page, request); 205 } 206 207 public ListModel sort(String key, String property, 208 HttpServletRequest request) throws ListSessionExpiredException { 209 210 return getListSession(key, request).sort(property, request); 211 } 212 } 213 | Popular Tags |