X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2FContentUtils.java;h=ed27ce8e8366a24c5fcd2ce0e285026c3ff1afa9;hb=feddb4be70a8304dd4a533efee6e14c22691b500;hp=e1fbcb1a9f440157b818a5c12dc8cf264be75c11;hpb=975fb5e581d0650768afc68a0e839657f318e77a;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/acr/ContentUtils.java b/org.argeo.cms/src/org/argeo/cms/acr/ContentUtils.java index e1fbcb1a9..ed27ce8e8 100644 --- a/org.argeo.cms/src/org/argeo/cms/acr/ContentUtils.java +++ b/org.argeo.cms/src/org/argeo/cms/acr/ContentUtils.java @@ -13,11 +13,13 @@ import javax.xml.namespace.QName; import org.argeo.api.acr.Content; import org.argeo.api.acr.ContentRepository; import org.argeo.api.acr.ContentSession; -import org.argeo.api.acr.CrName; +import org.argeo.api.acr.DName; import org.argeo.api.cms.CmsAuth; +import org.argeo.api.cms.directory.CmsDirectory; +import org.argeo.api.cms.directory.HierarchyUnit; +import org.argeo.api.cms.directory.UserDirectory; import org.argeo.cms.CmsUserManager; -import org.argeo.osgi.useradmin.UserDirectory; -import org.argeo.util.CurrentSubject; +import org.argeo.cms.util.CurrentSubject; import org.osgi.service.useradmin.Role; /** Utilities and routines around {@link Content}. */ @@ -63,6 +65,7 @@ public class ContentUtils { // } public static final char SLASH = '/'; + public static final String SLASH_STRING = Character.toString(SLASH); public static final String ROOT_SLASH = "" + SLASH; public static final String EMPTY = ""; @@ -120,14 +123,39 @@ public class ContentUtils { throw new IllegalArgumentException("Path " + path + " contains //"); } + /* + * DIRECTORY + */ + public static Content roleToContent(CmsUserManager userManager, ContentSession contentSession, Role role) { UserDirectory userDirectory = userManager.getDirectory(role); - String path = CmsContentRepository.DIRECTORY_BASE + SLASH + userDirectory.getName() + SLASH - + userDirectory.getRolePath(role); + String path = directoryPath(userDirectory) + userDirectory.getRolePath(role); + Content content = contentSession.get(path); + return content; + } + + public static Content hierarchyUnitToContent(ContentSession contentSession, HierarchyUnit hierarchyUnit) { + CmsDirectory 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 CmsDirectory}. Ends with a /. */ + private static String directoryPath(CmsDirectory 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 */ @@ -135,7 +163,7 @@ public class ContentUtils { public static Content createCollections(ContentSession session, String path) { if (session.exists(path)) { Content content = session.get(path); - if (!content.isContentClass(CrName.collection.qName())) { + if (!content.isContentClass(DName.collection.qName())) { throw new IllegalStateException("Content " + path + " already exists, but is not a collection"); } else { return content; @@ -143,7 +171,7 @@ public class ContentUtils { } else { String[] parentPath = getParentPath(path); Content parent = createCollections(session, parentPath[0]); - Content content = parent.add(parentPath[1], CrName.collection.qName()); + Content content = parent.add(parentPath[1], DName.collection.qName()); return content; } }