001/* 002 * Copyright 2020 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.workspaces.members; 017 018import org.apache.avalon.framework.service.ServiceException; 019import org.apache.avalon.framework.service.ServiceManager; 020 021import org.ametys.cms.data.type.indexing.IndexableElementType; 022import org.ametys.cms.model.CMSDataContext; 023import org.ametys.cms.model.properties.AbstractProperty; 024import org.ametys.cms.model.properties.Property; 025import org.ametys.cms.repository.Content; 026import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition; 027import org.ametys.cms.search.model.CriterionDefinitionHelper; 028import org.ametys.cms.search.model.IndexationAwareElementDefinition; 029import org.ametys.cms.search.model.IndexationAwareElementDefinitionHelper; 030import org.ametys.core.user.UserIdentity; 031import org.ametys.plugins.workspaces.project.ProjectManager; 032import org.ametys.plugins.workspaces.project.objects.Project; 033import org.ametys.runtime.model.type.ModelItemTypeConstants; 034 035/** 036 * {@link Property} for member's projects 037 */ 038public class MemberProjectProperty extends AbstractProperty<String, Content> implements CriterionDefinitionAwareElementDefinition<String>, IndexationAwareElementDefinition<String, Content> 039{ 040 private ProjectManager _projectManager; 041 private CriterionDefinitionHelper _criterionDefinitionHelper; 042 private IndexationAwareElementDefinitionHelper _indexationAwareElementDefinitionHelper; 043 044 @Override 045 public void service(ServiceManager manager) throws ServiceException 046 { 047 super.service(manager); 048 _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE); 049 _indexationAwareElementDefinitionHelper = (IndexationAwareElementDefinitionHelper) manager.lookup(IndexationAwareElementDefinitionHelper.ROLE); 050 } 051 052 public Object getValue(Content content) 053 { 054 UserIdentity identity = content.getValue("user"); 055 056 if (identity != null) 057 { 058 return _getProjectManager().getUserProjects(identity) 059 .keySet() 060 .stream() 061 .map(Project::getId) 062 .toArray(String[]::new); 063 } 064 else 065 { 066 return new String[0]; 067 } 068 } 069 070 @Override 071 public boolean isMultiple() 072 { 073 return true; 074 } 075 076 /** 077 * Retrieves the project manager 078 * @return the project manager 079 * @throws IllegalStateException if an error occurs 080 */ 081 protected ProjectManager _getProjectManager() throws IllegalStateException 082 { 083 if (_projectManager == null) 084 { 085 try 086 { 087 _projectManager = (ProjectManager) __serviceManager.lookup(ProjectManager.ROLE); 088 } 089 catch (ServiceException e) 090 { 091 throw new IllegalStateException("Unable to lookup after the project manager component", e); 092 } 093 } 094 return _projectManager; 095 } 096 097 @Override 098 protected String getTypeId() 099 { 100 return ModelItemTypeConstants.STRING_TYPE_ID; 101 } 102 103 public IndexableElementType getDefaultCriterionType() 104 { 105 CMSDataContext context = CMSDataContext.newInstance() 106 .withModelItem(this); 107 108 String typeId = getType().getDefaultCriterionTypeId(context); 109 return _criterionDefinitionHelper.getCriterionDefinitionType(typeId); 110 } 111 112 public String getSolrSortFieldName() 113 { 114 return _indexationAwareElementDefinitionHelper.getDefaultSolrSortFieldName(this); 115 } 116}