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 */ 016 017package org.ametys.plugins.userdirectory.page; 018 019import java.util.Map; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.avalon.framework.service.Serviceable; 024import org.apache.commons.lang.StringUtils; 025 026import org.ametys.cms.repository.Content; 027import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO; 028import org.ametys.plugins.repository.AmetysObjectFactory; 029import org.ametys.plugins.repository.AmetysObjectResolver; 030import org.ametys.plugins.repository.AmetysRepositoryException; 031import org.ametys.plugins.repository.UnknownAmetysObjectException; 032import org.ametys.plugins.userdirectory.UserDirectoryPageHandler; 033import org.ametys.web.repository.page.Page; 034import org.ametys.web.skin.SkinsManager; 035 036/** 037 * {@link AmetysObjectFactory} handling {@link UserPage}. 038 */ 039public class UserPageFactory implements AmetysObjectFactory<UserPage>, Serviceable 040{ 041 private AmetysObjectResolver _resolver; 042 private UserDirectoryPageHandler _userDirectoryPageHandler; 043 private SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO; 044 private SkinsManager _skinManager; 045 046 @Override 047 public void service(ServiceManager manager) throws ServiceException 048 { 049 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 050 _userDirectoryPageHandler = (UserDirectoryPageHandler) manager.lookup(UserDirectoryPageHandler.ROLE); 051 _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE); 052 _skinManager = (SkinsManager) manager.lookup(SkinsManager.ROLE); 053 } 054 055 public UserPage getAmetysObjectById(String id) throws AmetysRepositoryException 056 { 057 // E.g: uduser://path?rootId=...&contentId=... 058 String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "uduser://"), "?rootId="); 059 String rootId = StringUtils.substringBefore(StringUtils.substringAfter(id, "?rootId="), "&contentId="); 060 Page root = _resolver.resolveById(rootId); 061 062 String contentId = StringUtils.substringAfter(id, "&contentId="); 063 Map<String, String> userPagesContent = _userDirectoryPageHandler.getUserPagesContent(root, path); 064 if (userPagesContent.values().contains(contentId)) 065 { 066 Content syncContent = _resolver.resolveById(contentId); 067 return new UserPage(root, syncContent, path, _resolver, _userDirectoryPageHandler, _synchronizableContentsCollectionDAO, _skinManager); 068 } 069 else 070 { 071 throw new UnknownAmetysObjectException("No user content for id " + contentId); 072 } 073 } 074 075 public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException 076 { 077 // Id is like uduser://path?rootId=...&contentId=... 078 String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "uduser://"), "?rootId="); 079 String rootId = StringUtils.substringBefore(StringUtils.substringAfter(id, "?rootId="), "&contentId="); 080 if (!_resolver.hasAmetysObjectForId(rootId)) 081 { 082 return false; 083 } 084 085 Page root = _resolver.resolveById(rootId); 086 087 String contentId = StringUtils.substringAfter(id, "&contentId="); 088 Map<String, String> userPagesContent = _userDirectoryPageHandler.getUserPagesContent(root, path); 089 return userPagesContent.values().contains(contentId); 090 } 091 092 public String getScheme() 093 { 094 return "uduser"; 095 } 096 097 SynchronizableContentsCollectionDAO _getSynchronizableContentsCollectionDAO() 098 { 099 return _synchronizableContentsCollectionDAO; 100 } 101}