RulesProfile profileToActivate = null;
boolean oneProfileIsActivated = false;
if (profiles.isEmpty()) {
- profileToActivate = RulesProfile.create("Default " + language.getName(), language.getKey());
- profileToActivate.setDefaultProfile(true);
- profileToActivate.setProvided(false);
+ profileToActivate = new RulesProfile("Default " + language.getName(), language.getKey(), true, false);
+
} else if (profiles.size() == 1) {
profileToActivate = profiles.get(0);
- } else if (noProfileAlreadyActivated(profiles)) {
+
+ } else {
Iterator<RulesProfile> iterator = profiles.iterator();
while (iterator.hasNext() && !oneProfileIsActivated) {
RulesProfile profile = iterator.next();
profileToActivate = profile;
}
}
- if (!oneProfileIsActivated && profileToActivate == null) {
- profileToActivate = profiles.get(0);
+ if (!oneProfileIsActivated) {
+ if (profileToActivate == null) {
+ profileToActivate = profiles.get(0);
+ }
}
}
if (!oneProfileIsActivated && profileToActivate != null) {
session.saveWithoutFlush(profileToActivate);
}
}
-
- private boolean noProfileAlreadyActivated(List<RulesProfile> profiles) {
- for (RulesProfile rulesProfile : profiles) {
- if (rulesProfile.getDefaultProfile()) {
- return true;
- }
- }
- return false;
- }
}
void saveProvidedProfiles(List<RulesProfile> profiles, DatabaseSession session) {
for (RulesProfile profile : profiles) {
TimeProfiler profiler = new TimeProfiler().start("Save profile " + profile);
- RulesProfile persistedProfile = findOrCreate(profile, session);
+ RulesProfile persistedProfile = findOrCreate(profile.getName(), profile.getLanguage(), session);
for (ActiveRule activeRule : profile.getActiveRules()) {
Rule rule = getPersistedRule(activeRule);
return rule;
}
- private RulesProfile findOrCreate(RulesProfile profile, DatabaseSession session) {
- RulesProfile persistedProfile = session.getSingleResult(RulesProfile.class, "name", profile.getName(), "language", profile.getLanguage());
- if (persistedProfile == null) {
- persistedProfile = RulesProfile.create(profile.getName(), profile.getLanguage());
- persistedProfile.setDefaultProfile(profile.getDefaultProfile());
- persistedProfile.setProvided(true);
+ private RulesProfile findOrCreate(String name, String language, DatabaseSession session) {
+ RulesProfile profile = session.getSingleResult(RulesProfile.class, "name", name, "language", language);
+ if (profile == null) {
+ profile = RulesProfile.create(name, language);
+ profile.setProvided(true);
+ profile.setDefaultProfile(false);
}
- return persistedProfile;
+ return profile;
}
}