From 251228f44970a9d166626cafa8ebfdfb7af61a1c Mon Sep 17 00:00:00 2001
From: uhensler <urs.hensler@frentix.com>
Date: Tue, 19 Feb 2019 10:41:07 +0100
Subject: [PATCH] OO-3305: Refactoring to better use in other modules

---
 .../IdentityRelationshipService.java          |  3 ++
 .../basesecurity/RelationRightProvider.java   | 36 ++++++++++++++
 .../IdentityRelationshipServiceImpl.java      | 31 +++++++++++-
 .../manager/RelationRightDAO.java             | 40 +++------------
 .../java/org/olat/course/CourseModule.java    | 13 -----
 .../ViewCourseCalendarRightProvider.java      | 49 +++++++++++++++++++
 .../ViewEfficiencyStatementRightProvider.java | 49 +++++++++++++++++++
 .../_i18n/LocalStrings_de.properties          | 26 ++++------
 .../_i18n/LocalStrings_en.properties          | 14 ++----
 .../user/_i18n/LocalStrings_de.properties     |  2 -
 .../user/_i18n/LocalStrings_en.properties     |  2 -
 .../ui/role/EditRelationRoleController.java   |  2 +-
 .../role/RelationRolesAndRightsUIFactory.java |  1 -
 .../manager/RelationRightDAOTest.java         | 15 ++----
 14 files changed, 193 insertions(+), 90 deletions(-)
 create mode 100644 src/main/java/org/olat/basesecurity/RelationRightProvider.java
 create mode 100644 src/main/java/org/olat/course/groupsandrights/ViewCourseCalendarRightProvider.java
 create mode 100644 src/main/java/org/olat/course/groupsandrights/ViewEfficiencyStatementRightProvider.java

diff --git a/src/main/java/org/olat/basesecurity/IdentityRelationshipService.java b/src/main/java/org/olat/basesecurity/IdentityRelationshipService.java
index 7d6a660f994..420bb11324c 100644
--- a/src/main/java/org/olat/basesecurity/IdentityRelationshipService.java
+++ b/src/main/java/org/olat/basesecurity/IdentityRelationshipService.java
@@ -20,6 +20,7 @@
 package org.olat.basesecurity;
 
 import java.util.List;
+import java.util.Locale;
 
 import org.olat.core.id.Identity;
 
@@ -46,6 +47,8 @@ public interface IdentityRelationshipService {
 	
 	public List<RelationRight> getAvailableRights();
 	
+	public String getTranslatedName(RelationRight right, Locale locale);
+	
 	public boolean isInUse(RelationRole relationRole);
 	
 	public void deleteRole(RelationRole role);
diff --git a/src/main/java/org/olat/basesecurity/RelationRightProvider.java b/src/main/java/org/olat/basesecurity/RelationRightProvider.java
new file mode 100644
index 00000000000..9fb698cb51f
--- /dev/null
+++ b/src/main/java/org/olat/basesecurity/RelationRightProvider.java
@@ -0,0 +1,36 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+package org.olat.basesecurity;
+
+import java.util.Locale;
+
+/**
+ * 
+ * Initial date: 19 Feb 2019<br>
+ * @author uhensler, urs.hensler@frentix.com, http://www.frentix.com
+ *
+ */
+public interface RelationRightProvider {
+
+	public String getRight();
+	
+	public String getTranslatedName(Locale locale);
+	
+}
diff --git a/src/main/java/org/olat/basesecurity/manager/IdentityRelationshipServiceImpl.java b/src/main/java/org/olat/basesecurity/manager/IdentityRelationshipServiceImpl.java
index 53de2bb028c..1609b7f0f55 100644
--- a/src/main/java/org/olat/basesecurity/manager/IdentityRelationshipServiceImpl.java
+++ b/src/main/java/org/olat/basesecurity/manager/IdentityRelationshipServiceImpl.java
@@ -21,12 +21,16 @@ package org.olat.basesecurity.manager;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Locale;
+
+import javax.annotation.PostConstruct;
 
 import org.olat.basesecurity.IdentityRef;
 import org.olat.basesecurity.IdentityRelationshipService;
 import org.olat.basesecurity.IdentityToIdentityRelation;
 import org.olat.basesecurity.IdentityToIdentityRelationManagedFlag;
 import org.olat.basesecurity.RelationRight;
+import org.olat.basesecurity.RelationRightProvider;
 import org.olat.basesecurity.RelationRole;
 import org.olat.basesecurity.RelationRoleManagedFlag;
 import org.olat.core.id.Identity;
@@ -49,6 +53,16 @@ public class IdentityRelationshipServiceImpl implements IdentityRelationshipServ
 	@Autowired
 	private IdentityToIdentityRelationDAO identityRelationshipDao;
 	
+	@Autowired
+	private List<RelationRightProvider> relationRightProviders;
+	
+	@PostConstruct
+	void ensureRightsExists() {
+		for (RelationRightProvider relationRightProvider : relationRightProviders) {
+			relationRightDao.ensureRightExists(relationRightProvider.getRight());
+		}
+	}
+	
 	@Override
 	public RelationRole createRole(String role, List<RelationRight> rights) {
 		RelationRole relationRole = relationRoleDao.createRelationRole(role, null, null, null);
@@ -68,8 +82,6 @@ public class IdentityRelationshipServiceImpl implements IdentityRelationshipServ
 		return relationRole;
 	}
 
-
-
 	@Override
 	public RelationRole updateRole(RelationRole relationRole, List<RelationRight> rights) {
 		if(rights == null) {
@@ -93,6 +105,21 @@ public class IdentityRelationshipServiceImpl implements IdentityRelationshipServ
 		return relationRightDao.loadRelationRights();
 	}
 
+	@Override
+	public String getTranslatedName(RelationRight right, Locale locale) {
+		RelationRightProvider provider = getRelationRightProvider(right);
+		return provider != null? provider.getTranslatedName(locale): "???";
+	}
+
+	private RelationRightProvider getRelationRightProvider(RelationRight right) {
+		for (RelationRightProvider provider : relationRightProviders) {
+			if (provider.getRight().equals(right.getRight())) {
+				return provider;
+			}
+		}
+		return null;
+	}
+
 	@Override
 	public boolean isInUse(RelationRole relationRole) {
 		return identityRelationshipDao.isUsed(relationRole);
diff --git a/src/main/java/org/olat/basesecurity/manager/RelationRightDAO.java b/src/main/java/org/olat/basesecurity/manager/RelationRightDAO.java
index 23c1f5483f6..3736d0fb5d5 100644
--- a/src/main/java/org/olat/basesecurity/manager/RelationRightDAO.java
+++ b/src/main/java/org/olat/basesecurity/manager/RelationRightDAO.java
@@ -21,8 +21,6 @@ package org.olat.basesecurity.manager;
 
 import java.util.Date;
 import java.util.List;
-import java.util.Set;
-import java.util.stream.Collectors;
 
 import org.olat.basesecurity.RelationRight;
 import org.olat.basesecurity.model.RelationRightImpl;
@@ -79,40 +77,16 @@ public class RelationRightDAO {
 	/**
 	 * This method commits and closes the hibernate session.
 	 * 
-	 * @param rights The rights to create if they not exists
+	 * @param right The right to create if it not exists
 	 */
-	public void ensureRightsExists(Class<? extends Enum<?>> rightsEnum) {
-		Enum<?>[] rights = rightsEnum.getEnumConstants();
+	public void ensureRightExists(String right) {
+		if(!StringHelper.containsNonWhitespace(right)) return;
 		
-		List<RelationRight> relationRights = loadRelationRights();
-		Set<String> rightNames = relationRights.stream()
-				.map(RelationRight::getRight).collect(Collectors.toSet());
-		
-		for(Enum<?> right:rights) {
-			if(!rightNames.contains(right.name())) {
-				createRelationRight(right.name());
-			}
-		}
-		dbInstance.commitAndCloseSession();
-	}
-	
-	/**
-	 * This method commits and closes the hibernate session.
-	 * 
-	 * @param rights The rights to create if they not exists
-	 */
-	public void ensureRightsExists(String...  rights) {
-		if(rights == null || rights.length == 0 || rights[0] == null) return;
-		
-		List<RelationRight> relationRights = loadRelationRights();
-		Set<String> rightNames = relationRights.stream()
-				.map(RelationRight::getRight).collect(Collectors.toSet());
-		
-		for(String right:rights) {
-			if(StringHelper.containsNonWhitespace(right) && !rightNames.contains(right)) {
-				createRelationRight(right);
-			}
+		RelationRight relationRight = loadRelationRightByRight(right);
+		if (relationRight == null) {
+			createRelationRight(right);
 		}
+
 		dbInstance.commitAndCloseSession();
 	}
 
diff --git a/src/main/java/org/olat/course/CourseModule.java b/src/main/java/org/olat/course/CourseModule.java
index 7d6e3676826..42e817f7bb5 100644
--- a/src/main/java/org/olat/course/CourseModule.java
+++ b/src/main/java/org/olat/course/CourseModule.java
@@ -27,7 +27,6 @@ package org.olat.course;
 
 import java.util.HashMap;
 
-import org.olat.basesecurity.manager.RelationRightDAO;
 import org.olat.core.commons.services.notifications.SubscriptionContext;
 import org.olat.core.configuration.AbstractSpringModule;
 import org.olat.core.id.Identity;
@@ -37,7 +36,6 @@ import org.olat.core.util.coordinate.CoordinatorManager;
 import org.olat.core.util.event.GenericEventListener;
 import org.olat.core.util.resource.OresHelper;
 import org.olat.course.assessment.AssessmentManager;
-import org.olat.course.groupsandrights.CourseRightsEnum;
 import org.olat.course.nodes.CourseNode;
 import org.olat.course.run.environment.CourseEnvironment;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -75,9 +73,6 @@ public class CourseModule extends AbstractSpringModule {
 	public static final OLATResourceable ORESOURCEABLE_TYPE_COURSE = OresHelper.lookupType(CourseModule.class);
 	public static final String ORES_COURSE_ASSESSMENT = OresHelper.calculateTypeName(AssessmentManager.class);
 	
-	@Autowired
-	private RelationRightDAO relationRightDao;
-	
 	private static CoordinatorManager coordinatorManager;
 
 	@Autowired
@@ -102,14 +97,6 @@ public class CourseModule extends AbstractSpringModule {
 	@Override
 	public void init() {
 		initFromChangedProperties();
-		initCourseRights();
-	}
-	
-	/**
-	 * Initialize the course rights for user to user relations.
-	 */
-	private void initCourseRights() {
-		relationRightDao.ensureRightsExists(CourseRightsEnum.class);
 	}
 	
 	/**
diff --git a/src/main/java/org/olat/course/groupsandrights/ViewCourseCalendarRightProvider.java b/src/main/java/org/olat/course/groupsandrights/ViewCourseCalendarRightProvider.java
new file mode 100644
index 00000000000..4b2db9bd1d0
--- /dev/null
+++ b/src/main/java/org/olat/course/groupsandrights/ViewCourseCalendarRightProvider.java
@@ -0,0 +1,49 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+package org.olat.course.groupsandrights;
+
+import java.util.Locale;
+
+import org.olat.basesecurity.RelationRightProvider;
+import org.olat.core.gui.translator.Translator;
+import org.olat.core.util.Util;
+import org.springframework.stereotype.Component;
+
+/**
+ * 
+ * Initial date: 19 Feb 2019<br>
+ * @author uhensler, urs.hensler@frentix.com, http://www.frentix.com
+ *
+ */
+@Component
+public class ViewCourseCalendarRightProvider implements RelationRightProvider {
+
+	@Override
+	public String getRight() {
+		return CourseRightsEnum.viewCourseCalendar.name();
+	}
+
+	@Override
+	public String getTranslatedName(Locale locale) {
+		Translator translator = Util.createPackageTranslator(GroupsAndRightsController.class, locale);
+		return translator.translate("relation.right.viewCourseCalendar");
+	}
+
+}
diff --git a/src/main/java/org/olat/course/groupsandrights/ViewEfficiencyStatementRightProvider.java b/src/main/java/org/olat/course/groupsandrights/ViewEfficiencyStatementRightProvider.java
new file mode 100644
index 00000000000..51657e2a127
--- /dev/null
+++ b/src/main/java/org/olat/course/groupsandrights/ViewEfficiencyStatementRightProvider.java
@@ -0,0 +1,49 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+package org.olat.course.groupsandrights;
+
+import java.util.Locale;
+
+import org.olat.basesecurity.RelationRightProvider;
+import org.olat.core.gui.translator.Translator;
+import org.olat.core.util.Util;
+import org.springframework.stereotype.Component;
+
+/**
+ * 
+ * Initial date: 19 Feb 2019<br>
+ * @author uhensler, urs.hensler@frentix.com, http://www.frentix.com
+ *
+ */
+@Component
+public class ViewEfficiencyStatementRightProvider implements RelationRightProvider {
+
+	@Override
+	public String getRight() {
+		return CourseRightsEnum.viewEfficiencyStatement.name();
+	}
+
+	@Override
+	public String getTranslatedName(Locale locale) {
+		Translator translator = Util.createPackageTranslator(GroupsAndRightsController.class, locale);
+		return translator.translate("relation.right.viewCourseCalendar");
+	}
+
+}
diff --git a/src/main/java/org/olat/course/groupsandrights/_i18n/LocalStrings_de.properties b/src/main/java/org/olat/course/groupsandrights/_i18n/LocalStrings_de.properties
index 4659e760b64..8c6f2c4595d 100644
--- a/src/main/java/org/olat/course/groupsandrights/_i18n/LocalStrings_de.properties
+++ b/src/main/java/org/olat/course/groupsandrights/_i18n/LocalStrings_de.properties
@@ -1,29 +1,23 @@
 #Mon Mar 02 09:54:04 CET 2009
+add.all=Alle Rechte hinzuf\u00FCgen
 bgr.archive=Datenarchivierung
 bgr.assess=Bewertungs-Werkzeug
 bgr.assessmode=Pr\u00FCfungsmodus
+bgr.dbs=Kurs Datenbank
 bgr.editor=Kurseditor
 bgr.glossary=Glossar-Werkzeug
 bgr.groupmngt=Gruppen-Verwaltung
 bgr.membermngt=Mitglieder-Verwaltung
 bgr.statistics=Statistiken
+menu.rights=Rechte
 noRestriction=Keine Einschr\u00E4nkung
-bgr.dbs=Kurs Datenbank
+participant=Teilnehmer
+relation.right.viewCourseCalendar=Kurskalender ansehen
+relation.right.viewEfficiencyStatement=Leistungnachweise ansehen
+remove.all=Alle Rechte Entfernen
+repo.participant=Kursteilnehmer
+repo.tutor=Kursbetreuer
 table.header.groups=Gruppe
-table.header.role=Rolle
 table.header.remove=Entfernen
-menu.rights=Rechte
-remove.all=Alle Rechte Entfernen
-add.all=Alle Rechte hinzuf\u00fcgen
+table.header.role=Rolle
 tutor=Betreuer
-participant=Teilnehmer
-repo.tutor=Kursbetreuer
-repo.participant=Kursteilnehmer
-
-
-
-
-
-
-
-
diff --git a/src/main/java/org/olat/course/groupsandrights/_i18n/LocalStrings_en.properties b/src/main/java/org/olat/course/groupsandrights/_i18n/LocalStrings_en.properties
index 18600a5756c..48011728ce0 100644
--- a/src/main/java/org/olat/course/groupsandrights/_i18n/LocalStrings_en.properties
+++ b/src/main/java/org/olat/course/groupsandrights/_i18n/LocalStrings_en.properties
@@ -12,18 +12,12 @@ bgr.statistics=Statistics
 menu.rights=Rights
 noRestriction=No restriction
 participant=Participant
+relation.right.viewCourseCalendar=View course calenders
+relation.right.viewEfficiencyStatement=View efficiency statements
 remove.all=Remove all permissions
+repo.participant=Course participant
+repo.tutor=Course coach
 table.header.groups=Groups
 table.header.remove=Remove
 table.header.role=Role
 tutor=Coach
-repo.tutor=Course coach
-repo.participant=Course participant
-
-
-
-
-
-
-
-
diff --git a/src/main/java/org/olat/user/_i18n/LocalStrings_de.properties b/src/main/java/org/olat/user/_i18n/LocalStrings_de.properties
index 1bad84460c9..e390a00bc6a 100644
--- a/src/main/java/org/olat/user/_i18n/LocalStrings_de.properties
+++ b/src/main/java/org/olat/user/_i18n/LocalStrings_de.properties
@@ -103,8 +103,6 @@ pwdav.password.set=******* (Passwort verschl\u00FCsselt)
 pwdav.password.successful=Ihr neues WebDAV Passwort wurde erfolgreich gespeichert. Es ist ab sofort aktiv.
 pwdav.title=WebDAV Zugang
 pwdav.username=WebDAV Benutzername
-relation.right.viewEfficiencyStatement=Leistungnachweise ansehen
-relation.right.viewCourseCalendar=Kurskalender ansehen
 replayurl.active=Load Performance URL eingeschaltet
 reset.desc=Hier k\u00F6nnen Sie die personalisierten Systemkonfiguration wieder auf die Standardeinstellung zur\u00FCcksetzen. W\u00E4hlen Sie unten die Einstellungen aus, die zur\u00FCckgesetzt werden sollen. Um die Einstellungen zu aktivieren werden Sie automatisch aus dem System ausgeloggt\!
 reset.elements=Einstellungen
diff --git a/src/main/java/org/olat/user/_i18n/LocalStrings_en.properties b/src/main/java/org/olat/user/_i18n/LocalStrings_en.properties
index 10dc88f14f5..0160f5d573a 100644
--- a/src/main/java/org/olat/user/_i18n/LocalStrings_en.properties
+++ b/src/main/java/org/olat/user/_i18n/LocalStrings_en.properties
@@ -104,8 +104,6 @@ pwdav.password.successful=Your new WebDAV password has been saved successfully.
 pwdav.title=WebDAV access
 pwdav.username=WebDav user name
 replayurl.active=Load performance URL active
-relation.role.viewEfficiencyStatement=View efficiency statements
-relation.role.viewCourseCalendar=View course calenders
 reset.desc=You can reset your personalized system configuration to the default values using this form. Choose below the settings you want reset. To activate the changes the system will automatically trigger a logout. 
 reset.elements=Configurations
 reset.elements.guiprefs=Personalized interface components (menu, tool boxes, tables, portal, calendar etc.)
diff --git a/src/main/java/org/olat/user/ui/role/EditRelationRoleController.java b/src/main/java/org/olat/user/ui/role/EditRelationRoleController.java
index bbf92d6c876..d7e90134115 100644
--- a/src/main/java/org/olat/user/ui/role/EditRelationRoleController.java
+++ b/src/main/java/org/olat/user/ui/role/EditRelationRoleController.java
@@ -81,7 +81,7 @@ public class EditRelationRoleController extends FormBasicController {
 		String[] rightValues = new String[rights.size()];
 		for(int i=rights.size(); i-->0; ) {
 			rightKeys[i] = rights.get(i).getRight();
-			rightValues[i] = translate(RelationRolesAndRightsUIFactory.TRANS_RIGHT_PREFIX.concat(rights.get(i).getRight()));
+			rightValues[i] = identityRelationsService.getTranslatedName(rights.get(i), getLocale());
 		}
 		rightsEl = uifactory.addCheckboxesVertical("role.rights", formLayout, rightKeys, rightValues, 2);
 		rightsEl.setEnabled(!RelationRoleManagedFlag.isManaged(relationRole, RelationRoleManagedFlag.rights));
diff --git a/src/main/java/org/olat/user/ui/role/RelationRolesAndRightsUIFactory.java b/src/main/java/org/olat/user/ui/role/RelationRolesAndRightsUIFactory.java
index 07bbd0245bc..1f84ed46ccb 100644
--- a/src/main/java/org/olat/user/ui/role/RelationRolesAndRightsUIFactory.java
+++ b/src/main/java/org/olat/user/ui/role/RelationRolesAndRightsUIFactory.java
@@ -28,6 +28,5 @@ package org.olat.user.ui.role;
 public class RelationRolesAndRightsUIFactory {
 	
 	public static final String TRANS_ROLE_PREFIX = "relation.role.";
-	public static final String TRANS_RIGHT_PREFIX = "relation.right.";
 
 }
diff --git a/src/test/java/org/olat/basesecurity/manager/RelationRightDAOTest.java b/src/test/java/org/olat/basesecurity/manager/RelationRightDAOTest.java
index 966e62ab4af..d5318ffc6cc 100644
--- a/src/test/java/org/olat/basesecurity/manager/RelationRightDAOTest.java
+++ b/src/test/java/org/olat/basesecurity/manager/RelationRightDAOTest.java
@@ -92,18 +92,13 @@ public class RelationRightDAOTest extends OlatTestCase {
 	}
 	
 	@Test
-	public void ensureRightsExists_enum() {
-		relationRightDao.ensureRightsExists(UnitTestRights.class);
+	public void ensureRightsExists() {
+		String right = "unitTestRight";
+		relationRightDao.ensureRightExists(right);
 		dbInstance.commitAndCloseSession();
 		
-		RelationRight firstRight = relationRightDao.loadRelationRightByRight(UnitTestRights.iWantToTest.name());
-		Assert.assertNotNull(firstRight);
-		RelationRight secondRight = relationRightDao.loadRelationRightByRight(UnitTestRights.moreUnitTest.name());
-		Assert.assertNotNull(secondRight);
+		RelationRight loadedRight = relationRightDao.loadRelationRightByRight(right);
+		Assert.assertNotNull(loadedRight);
 	}
 	
-	public enum UnitTestRights {
-		iWantToTest,
-		moreUnitTest	
-	}
 }
-- 
GitLab