+ /*
+ * DIRECTORY
+ */
+
+ public static Content roleToContent(CmsUserManager userManager, ContentSession contentSession, Role role) {
+ UserDirectory userDirectory = userManager.getDirectory(role);
+ String path = directoryPath(userDirectory) + userDirectory.getRolePath(role);
+ Content content = contentSession.get(path);
+ return content;
+ }
+
+ public static Content hierarchyUnitToContent(ContentSession contentSession, HierarchyUnit hierarchyUnit) {
+ Directory directory = hierarchyUnit.getDirectory();
+ StringJoiner relativePath = new StringJoiner(SLASH_STRING);
+ buildHierarchyUnitPath(hierarchyUnit, relativePath);
+ String path = directoryPath(directory) + relativePath.toString();
+ Content content = contentSession.get(path);
+ return content;
+ }
+
+ /** The path to this {@link Directory}. Ends with a /. */
+ private static String directoryPath(Directory directory) {
+ return CmsContentRepository.DIRECTORY_BASE + SLASH + directory.getName() + SLASH;
+ }
+
+ /** Recursively build a relative path of a {@link HierarchyUnit}. */
+ private static void buildHierarchyUnitPath(HierarchyUnit current, StringJoiner relativePath) {
+ if (current.getParent() == null) // directory
+ return;
+ buildHierarchyUnitPath(current.getParent(), relativePath);
+ relativePath.add(current.getHierarchyUnitName());
+ }
+
+ /*
+ * CONSUMER UTILS
+ */
+
+ public static Content createCollections(ContentSession session, String path) {
+ if (session.exists(path)) {
+ Content content = session.get(path);
+ if (!content.isContentClass(DName.collection.qName())) {
+ throw new IllegalStateException("Content " + path + " already exists, but is not a collection");
+ } else {
+ return content;
+ }
+ } else {
+ String[] parentPath = getParentPath(path);
+ Content parent = createCollections(session, parentPath[0]);
+ Content content = parent.add(parentPath[1], DName.collection.qName());
+ return content;
+ }
+ }
+
+ public static ContentSession openDataAdminSession(ContentRepository repository) {
+ LoginContext loginContext;
+ try {
+ loginContext = CmsAuth.DATA_ADMIN.newLoginContext();
+ loginContext.login();
+ } catch (LoginException e1) {
+ throw new RuntimeException("Could not login as data admin", e1);
+ } finally {
+ }
+
+ ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
+ try {
+ Thread.currentThread().setContextClassLoader(ContentUtils.class.getClassLoader());
+ return CurrentSubject.callAs(loginContext.getSubject(), () -> repository.get());
+ } finally {
+ Thread.currentThread().setContextClassLoader(currentCl);
+ }
+ }
+