KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > list > ui > ListServiceImpl


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.list.ui;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import javax.servlet.http.HttpSession JavaDoc;
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 /**
54  * @author Felix Gnass [fgnass at neteye dot de]
55  * @since 6.4
56  */

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 JavaDoc sessions = new ArrayList JavaDoc();
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 JavaDoc editorId, String JavaDoc parentId,
98             String JavaDoc choose, HttpServletRequest JavaDoc request) {
99
100         String JavaDoc 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 JavaDoc 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 JavaDoc key, HttpServletRequest JavaDoc request)
139             throws ListSessionExpiredException {
140
141         ListSession session = (ListSession) request.getSession().getAttribute(key);
142         if (session != null) {
143             //Trigger a modification check:
144
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 JavaDoc 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 JavaDoc key,HttpServletRequest JavaDoc request)
164             throws ListSessionExpiredException {
165
166         return getListSession(key, request).getModel(request);
167     }
168
169     public String JavaDoc getFilterFormHtml(String JavaDoc key, HttpServletRequest JavaDoc request)
170             throws ListSessionExpiredException {
171
172         return getListSession(key, request).getFilterFormHtml();
173     }
174
175     public List JavaDoc getListCommands(String JavaDoc key, HttpServletRequest JavaDoc request)
176             throws ListSessionExpiredException {
177
178         return getListSession(key, request).getListCommands(request);
179     }
180
181     public List JavaDoc getFormCommands(String JavaDoc key, String JavaDoc objectId,
182             HttpServletRequest JavaDoc request) throws ListSessionExpiredException {
183
184         return getListSession(key, request).getFormCommands(objectId, request);
185     }
186
187     public CommandResult execCommand(String JavaDoc key, ListItem item,
188             String JavaDoc commandId, boolean confirmed,
189             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
190             throws ListSessionExpiredException {
191
192         return getListSession(key, request).execCommand(
193                 item, commandId, confirmed, request, response);
194     }
195
196     public ListModel filter(String JavaDoc key, Map JavaDoc filter, HttpServletRequest JavaDoc request)
197             throws ListSessionExpiredException {
198         return getListSession(key, request).filter(filter, request);
199     }
200
201     public ListModel gotoPage(String JavaDoc key, int page, HttpServletRequest JavaDoc request)
202             throws ListSessionExpiredException {
203
204         return getListSession(key, request).gotoPage(page, request);
205     }
206
207     public ListModel sort(String JavaDoc key, String JavaDoc property,
208             HttpServletRequest JavaDoc request) throws ListSessionExpiredException {
209
210         return getListSession(key, request).sort(property, request);
211     }
212 }
213
Popular Tags