RulesProfile profileToActivate = null;
boolean oneProfileIsActivated = false;
if (profiles.isEmpty()) {
- profileToActivate = new RulesProfile("Default " + language.getName(), language.getKey(), true, false);
-
+ profileToActivate = RulesProfile.create("Default " + language.getName(), language.getKey());
+ profileToActivate.setDefaultProfile(true);
+ profileToActivate.setProvided(false);
} else if (profiles.size() == 1) {
profileToActivate = profiles.get(0);
-
- } else {
+ } else if (!activeProfileFoundInDB(profiles)) {
Iterator<RulesProfile> iterator = profiles.iterator();
while (iterator.hasNext() && !oneProfileIsActivated) {
RulesProfile profile = iterator.next();
profileToActivate = profile;
}
}
- if (!oneProfileIsActivated) {
- if (profileToActivate == null) {
- profileToActivate = profiles.get(0);
- }
+ if (!oneProfileIsActivated && profileToActivate == null) {
+ profileToActivate = profiles.get(0);
}
}
if (!oneProfileIsActivated && profileToActivate != null) {
session.saveWithoutFlush(profileToActivate);
}
}
+
+ private boolean activeProfileFoundInDB(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.getName(), profile.getLanguage(), session);
+ RulesProfile persistedProfile = findOrCreate(profile, session);
for (ActiveRule activeRule : profile.getActiveRules()) {
Rule rule = getPersistedRule(activeRule);
return rule;
}
- 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);
+ 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);
}
- return profile;
+ return persistedProfile;
}
}