View Javadoc

1   /*
2    * Copyright 2010 James Pether Sörling
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id: CitizenIntelligenceAgency.java 5582 2012-01-04 20:03:13Z pether $
17   *  $HeadURL: https://cia.svn.sourceforge.net/svnroot/cia/trunk/citizen-intelligence-agency/src/main/java/com/hack23/cia/web/impl/ui/application/CitizenIntelligenceAgency.java $
18   */
19  package com.hack23.cia.web.impl.ui.application;
20  
21  import java.util.Locale;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.springframework.beans.factory.annotation.Autowired;
26  import org.springframework.beans.factory.annotation.Qualifier;
27  import org.springframework.context.annotation.Scope;
28  import org.springframework.context.i18n.LocaleContextHolder;
29  import org.springframework.security.access.AccessDeniedException;
30  import org.springframework.security.core.context.SecurityContextHolder;
31  import org.springframework.stereotype.Component;
32  import org.vaadin.navigator7.NavigableApplication;
33  import org.vaadin.navigator7.window.NavigableAppLevelWindow;
34  
35  import com.google.gwt.language.client.translation.Language;
36  import com.hack23.cia.service.api.ApplicationManager;
37  import com.hack23.cia.service.api.ConfigurationManager;
38  import com.hack23.cia.web.impl.ui.common.ApplicationUserState;
39  import com.hack23.cia.web.impl.ui.common.ApplicationUserStateHolder;
40  import com.vaadin.Application;
41  import com.vaadin.service.ApplicationContext;
42  import com.vaadin.ui.Window.Notification;
43  
44  /**
45   * The Class CitizenIntelligenceAgency.
46   */
47  @Component(value = "citizenIntelligenceAgency")
48  @Scope(value = "prototype")
49  public final class CitizenIntelligenceAgency extends NavigableApplication
50  		implements ApplicationContext.TransactionListener, ApplicationUserState {
51  
52  	/** The Constant serialVersionUID. */
53  	private static final long serialVersionUID = 1L;
54  
55  	/** The Constant LOGGER. */
56  	private final static Logger LOGGER = LoggerFactory
57  			.getLogger(CitizenIntelligenceAgency.class);
58  
59  	/** The application manager. */
60  	@Autowired
61  	@Qualifier("ApplicationManager")
62  	private transient ApplicationManager applicationManager;
63  
64  	/** The configuration manager. */
65  	@Autowired
66  	@Qualifier("ConfigurationManager")
67  	private transient ConfigurationManager configurationManager;
68  
69  	/** The language. */
70  	private Language language;
71  
72  	/** The google api language. */
73  	private com.google.api.translate.Language googleApiLanguage;
74  
75  	/*
76  	 * (non-Javadoc)
77  	 * 
78  	 * @see
79  	 * org.vaadin.navigator7.NavigableApplication#createNewNavigableAppLevelWindow
80  	 * ()
81  	 */
82  	@Override
83  	public NavigableAppLevelWindow createNewNavigableAppLevelWindow() {
84  		setLanguage(Language.valueOf(getLocale().getDisplayLanguage()
85  				.toUpperCase()));
86  		return new TopLevelWindowTemplate(getUser(), this);
87  	}
88  
89  	/*
90  	 * (non-Javadoc)
91  	 * 
92  	 * @see
93  	 * com.hack23.cia.web.impl.ui.common.ApplicationUserState#getApplicationManager
94  	 * ()
95  	 */
96  	@Override
97  	public ApplicationManager getApplicationManager() {
98  		return applicationManager;
99  	}
100 
101 	/*
102 	 * (non-Javadoc)
103 	 * 
104 	 * @see com.hack23.cia.web.impl.ui.common.ApplicationUserState#
105 	 * getConfigurationManager()
106 	 */
107 	@Override
108 	public ConfigurationManager getConfigurationManager() {
109 		return configurationManager;
110 	}
111 
112 	/*
113 	 * (non-Javadoc)
114 	 * 
115 	 * @see
116 	 * com.hack23.cia.web.impl.ui.common.ApplicationUserState#getGoogleApiLanguage
117 	 * ()
118 	 */
119 	@Override
120 	public com.google.api.translate.Language getGoogleApiLanguage() {
121 		if (googleApiLanguage == null) {
122 			googleApiLanguage = com.google.api.translate.Language
123 					.valueOf(getLanguage().name().toUpperCase());
124 		}
125 		return googleApiLanguage;
126 	}
127 
128 	/*
129 	 * (non-Javadoc)
130 	 * 
131 	 * @see com.hack23.cia.web.impl.ui.common.ApplicationUserState#getLanguage()
132 	 */
133 	@Override
134 	public Language getLanguage() {
135 		if (language == null) {
136 			language = Language.valueOf(getLocale().getDisplayLanguage()
137 					.toUpperCase());
138 		}
139 		return language;
140 	}
141 
142 	/*
143 	 * (non-Javadoc)
144 	 * 
145 	 * @see com.vaadin.Application#getLocale()
146 	 */
147 	@Override
148 	public Locale getLocale() {
149 		return LocaleContextHolder.getLocale();
150 	}
151 
152 	/*
153 	 * (non-Javadoc)
154 	 * 
155 	 * @see com.vaadin.Application#getUser()
156 	 */
157 	@Override
158 	public Object getUser() {
159 		return SecurityContextHolder.getContext().getAuthentication();
160 	}
161 
162 	/*
163 	 * (non-Javadoc)
164 	 * 
165 	 * @see
166 	 * com.hack23.cia.web.impl.ui.common.ApplicationUserState#setLanguage(com
167 	 * .google.gwt.language.client.translation.Language)
168 	 */
169 	@Override
170 	public void setLanguage(final Language language) {
171 		this.language = language;
172 		googleApiLanguage = com.google.api.translate.Language.valueOf(language
173 				.name().toUpperCase());
174 	}
175 
176 	/*
177 	 * (non-Javadoc)
178 	 * 
179 	 * @see com.vaadin.Application#setLocale(java.util.Locale)
180 	 */
181 	@Override
182 	public void setLocale(final Locale locale) {
183 		LocaleContextHolder.setLocale(locale);
184 	}
185 
186 	/*
187 	 * (non-Javadoc)
188 	 * 
189 	 * @see
190 	 * com.vaadin.Application#terminalError(com.vaadin.terminal.Terminal.ErrorEvent
191 	 * )
192 	 */
193 	@Override
194 	public void terminalError(
195 			final com.vaadin.terminal.Terminal.ErrorEvent event) {
196 		super.terminalError(event);
197 		if (event.getThrowable().getCause() instanceof AccessDeniedException) {
198 			getMainWindow().showNotification("accessdenied",
199 					Notification.TYPE_ERROR_MESSAGE);
200 		}
201 	}
202 
203 	/*
204 	 * (non-Javadoc)
205 	 * 
206 	 * @see
207 	 * com.vaadin.service.ApplicationContext.TransactionListener#transactionEnd
208 	 * (com.vaadin.Application, java.lang.Object)
209 	 */
210 	@Override
211 	public void transactionEnd(final Application application,
212 			final Object object) {
213 		if (this != application) {
214 			return;
215 		}
216 		super.transactionEnd(application, object);
217 		ApplicationUserStateHolder.removeCurrentApplicationUserState();
218 		LOGGER.info("transactionEnd:" + object);
219 	}
220 
221 	/*
222 	 * (non-Javadoc)
223 	 * 
224 	 * @see
225 	 * com.vaadin.service.ApplicationContext.TransactionListener#transactionStart
226 	 * (com.vaadin.Application, java.lang.Object)
227 	 */
228 	@Override
229 	public void transactionStart(final Application application,
230 			final Object object) {
231 		if (this != application) {
232 			return;
233 		}
234 		ApplicationUserStateHolder.addCurrentApplicationUserState(this);
235 		super.transactionStart(application, object);
236 		LOGGER.info("transactionStart:" + object);
237 	}
238 
239 	/*
240 	 * (non-Javadoc)
241 	 * 
242 	 * @see
243 	 * com.hack23.cia.web.impl.ui.common.ApplicationUserState#translateFromEnglish
244 	 * (java.lang.String)
245 	 */
246 	@Override
247 	public String translateFromEnglish(final String string) {
248 		return getApplicationManager().getTranslator()
249 				.translateFromStandardProtocol(string, googleApiLanguage);
250 	}
251 
252 }