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$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.service.impl;
20  
21  import java.text.SimpleDateFormat;
22  import java.util.ArrayList;
23  import java.util.Date;
24  import java.util.List;
25  import java.util.Locale;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.springframework.beans.factory.annotation.Autowired;
29  import org.springframework.security.access.annotation.Secured;
30  import org.springframework.stereotype.Service;
31  import org.springframework.transaction.annotation.Transactional;
32  
33  import com.hack23.cia.model.internal.application.system.impl.Agency;
34  import com.hack23.cia.model.internal.application.system.impl.LanguageData;
35  import com.hack23.cia.model.internal.application.system.impl.Portal;
36  import com.hack23.cia.model.internal.application.system.impl.PortalType;
37  import com.hack23.cia.service.api.ConfigurationManager;
38  import com.hack23.cia.service.api.UserConfiguration;
39  import com.hack23.cia.service.data.api.AgencyDAO;
40  import com.hack23.cia.service.data.api.LanguageDataDAO;
41  
42  /**
43   * The Class ConfigurationManagerImpl.
44   */
45  @Service("ConfigurationManager")
46  @Transactional
47  final class ConfigurationManagerImpl implements ConfigurationManager {
48  
49  	/** The Constant EXPECTED_LOCALE_LENGTH. */
50  	private static final int EXPECTED_LOCALE_LENGTH = 2;
51  
52  	/** The agency dao. */
53  	@Autowired
54  	private AgencyDAO agencyDAO;
55  
56  	/** The language data dao. */
57  	@Autowired
58  	private LanguageDataDAO languageDataDAO;
59  
60  	/**
61  	 * Instantiates a new configuration manager impl.
62  	 */
63  	public ConfigurationManagerImpl() {
64  		super();
65  	}
66  
67  	@Secured({"ROLE_ANONYMOUS","ROLE_USER", "ROLE_ADMIN" })
68  	@Override
69  	public UserConfiguration getUserConfiguration(final String url,final String locale) {
70  		final Agency agency = agencyDAO.getAll().get(0);
71  		Portal usePortal = null;
72  		final LanguageData languageData = findLanguage(locale);
73  		for (final Portal portal : agency.getPortals()) {
74  			if ((usePortal == null && PortalType.DEFAULT == portal.getPortalType()) || url.contains(portal.getPortalName())) {
75  				usePortal = portal;
76  			}
77  		}
78  
79  		return new UserConfigurationImpl(agency, usePortal,languageData);
80  	}
81  
82  	/**
83  	 * Find language.
84  	 *
85  	 * @param locale
86  	 *            the locale
87  	 * @return the language data
88  	 */
89  	private LanguageData findLanguage(final String locale) {
90  		for (final LanguageData languageData : languageDataDAO.getAll()) {
91  			if (languageData.getLanguageCode().equalsIgnoreCase(locale)) {
92  				return languageData;
93  			}
94  		}
95  		return null;
96  	}
97  
98  	@Secured({ "ROLE_ADMIN" })
99  	@Override
100 	public void createDefaultConfigIfEmpty() {
101 		if (agencyDAO.getAll().isEmpty()) {
102 			final List<Portal> portals = new ArrayList<>();
103 			final Portal defaulPortal = new Portal().withPortalType(PortalType.DEFAULT).withDescription("Global Portal")
104 					.withPortalName("Default");
105 			portals.add(defaulPortal);
106 
107 			final Portal domainPortal = new Portal().withPortalType(PortalType.DOMAIN).withDescription("Hack23.com")
108 					.withPortalName("www.hack23.com");
109 			portals.add(domainPortal);
110 
111 			final Agency agency = new Agency().withAgencyName("Citizen Intelligence Agency")
112 					.withDescription("Tracking politicians like bugs");
113 			agency.setPortals(portals);
114 			agencyDAO.persist(agency);
115 		}
116 	}
117 
118 	@Secured({ "ROLE_ADMIN" })
119 	@Override
120 	public void createDefaultLanguagesIfEmpty() {
121 		if (languageDataDAO.getAll().isEmpty()) {
122 			languageDataDAO.persist(getSupportedLocalesLanguageData());
123 
124 		}
125 	}
126 
127 	/**
128 	 * Gets the supported locales language data.
129 	 *
130 	 * @return the supported locales language data
131 	 */
132 	private static List<LanguageData> getSupportedLocalesLanguageData() {
133 		final List<LanguageData> languages = new ArrayList<>();
134 
135 		for (final Locale locale : SimpleDateFormat.getAvailableLocales()) {
136 			final String localeString = locale.toString().trim();
137 			if (locale.getDisplayCountry(Locale.ENGLISH).length() == 0 && !StringUtils.isEmpty(localeString)
138 					&& localeString.length() == EXPECTED_LOCALE_LENGTH) {
139 				languages.add(new LanguageData().withCreatedDate(new Date()).withLanguageCode(localeString)
140 						.withLanguageName(locale.getDisplayName(Locale.ENGLISH)).withLanguageEnabled(false));
141 
142 			}
143 		}
144 
145 		return languages;
146 	}
147 
148 }