001/* 002 * Copyright 2016 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.userdirectory.page; 017 018import org.apache.avalon.framework.component.Component; 019import org.apache.avalon.framework.logger.AbstractLogEnabled; 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.avalon.framework.service.Serviceable; 023import org.apache.commons.lang.StringUtils; 024 025import org.ametys.cms.repository.Content; 026import org.ametys.plugins.repository.AmetysObjectResolver; 027import org.ametys.plugins.repository.UnknownAmetysObjectException; 028import org.ametys.plugins.repository.metadata.CompositeMetadata; 029import org.ametys.plugins.repository.metadata.UnknownMetadataException; 030import org.ametys.plugins.userdirectory.UserDirectoryPageHandler; 031import org.ametys.web.repository.page.Page; 032 033/** 034 * Resolves an user directory page path from the associated user content. 035 */ 036public class UserDirectoryPageResolver extends AbstractLogEnabled implements Component, Serviceable 037{ 038 /** The avalon role. */ 039 public static final String ROLE = UserDirectoryPageResolver.class.getName(); 040 041 /** The ametys object resolver. */ 042 protected AmetysObjectResolver _ametysResolver; 043 /** The user directory page handler */ 044 protected UserDirectoryPageHandler _pageHandler; 045 046 047 @Override 048 public void service(ServiceManager serviceManager) throws ServiceException 049 { 050 _ametysResolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 051 _pageHandler = (UserDirectoryPageHandler) serviceManager.lookup(UserDirectoryPageHandler.ROLE); 052 } 053 054 /** 055 * Return the user page 056 * @param userContent the user page content 057 * @param siteName The current site name. 058 * @param contentTypeId the content type id 059 * @return the user page or null 060 */ 061 public UserPage getUserPage(Content userContent, String siteName, String contentTypeId) 062 { 063 Page userDirectoryRootPage = _pageHandler.getUserDirectoryRootPage(siteName, userContent.getLanguage(), contentTypeId); 064 065 if (userDirectoryRootPage == null) 066 { 067 return null; 068 } 069 070 return getUserPage(userDirectoryRootPage, userContent); 071 } 072 073 /** 074 * Return the user page 075 * @param userDirectoryRootPage the user directory root page 076 * @param userContent the user content 077 * @return the user page or null 078 */ 079 public UserPage getUserPage(Page userDirectoryRootPage, Content userContent) 080 { 081 try 082 { 083 // E.g: uduser://path?rootId=...&contentId=... 084 String userPath = _getUserPath(userDirectoryRootPage, userContent); 085 String pageId = "uduser://" + userPath + "?rootId=" + userDirectoryRootPage.getId() + "&contentId=" + userContent.getId(); 086 return _ametysResolver.resolveById(pageId); 087 } 088 catch (UnknownAmetysObjectException e) 089 { 090 return null; 091 } 092 } 093 094 /** 095 * Get the path of a user content 096 * @param userDirectoryRootPage The user directory root page where to look into 097 * @param userContent The user content 098 * @return The path 099 * @throws UnknownMetadataException if user page does not exist 100 */ 101 private String _getUserPath(Page userDirectoryRootPage, Content userContent) throws UnknownMetadataException 102 { 103 int depth = _pageHandler.getDepth(userDirectoryRootPage); 104 String metadataPath = _pageHandler.getClassificationMetadata(userDirectoryRootPage); 105 106 String[] pathSegments = metadataPath.split("/"); 107 CompositeMetadata metadataHolder = userContent.getMetadataHolder(); 108 109 for (int i = 0; i < pathSegments.length - 1; i++) 110 { 111 metadataHolder = metadataHolder.getCompositeMetadata(pathSegments[i]); 112 } 113 114 String metadataName = pathSegments[pathSegments.length - 1]; 115 116 String title = metadataHolder.getString(metadataName); 117 if (title.length() <= depth) 118 { 119 depth = title.length() - 1; 120 } 121 122 String path = ""; 123 for (int i = 0; i < depth; i++) 124 { 125 String pageName = StringUtils.lowerCase(title.substring(i, i + 1)); 126 path += StringUtils.isNumeric(pageName) ? "page-" + pageName : pageName; 127 if (i != depth - 1) 128 { 129 path += "/"; 130 } 131 } 132 133 return path; 134 } 135}