1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
44
45 @Service("ConfigurationManager")
46 @Transactional
47 final class ConfigurationManagerImpl implements ConfigurationManager {
48
49
50 private static final int EXPECTED_LOCALE_LENGTH = 2;
51
52
53 @Autowired
54 private AgencyDAO agencyDAO;
55
56
57 @Autowired
58 private LanguageDataDAO languageDataDAO;
59
60
61
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
84
85
86
87
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
129
130
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 }