001/* 002 * Copyright 2018 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.cms.content.referencetable.search; 017 018import java.util.HashMap; 019import java.util.List; 020import java.util.Locale; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025 026import org.ametys.cms.contenttype.ContentType; 027import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 028import org.ametys.cms.contenttype.ContentTypesHelper; 029import org.ametys.cms.contenttype.MetadataDefinition; 030import org.ametys.cms.repository.Content; 031import org.ametys.core.ui.Callable; 032import org.ametys.core.ui.ClientSideElement; 033import org.ametys.core.ui.StaticClientSideElement; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035 036/** 037 * {@link ClientSideElement} for search tool on contents with the same referencing values for a given metadata. 038 */ 039public class SearchReferencingContentsWithSameValuesClientSideElement extends StaticClientSideElement 040{ 041 private ContentTypesHelper _cTypeHelper; 042 private AmetysObjectResolver _resolver; 043 private ContentTypeExtensionPoint _cTypeEP; 044 045 @Override 046 public void service(ServiceManager smanager) throws ServiceException 047 { 048 super.service(smanager); 049 _cTypeHelper = (ContentTypesHelper) smanager.lookup(ContentTypesHelper.ROLE); 050 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 051 _cTypeEP = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE); 052 } 053 054 /** 055 * Get the search context 056 * @param initialContentId The id of initial content (from which the search was launched) 057 * @param metadataPath The path of the metadata 058 * @param refIds The current values for this content and metadata 059 * @return The search context 060 */ 061 @Callable 062 public Map<String, Object> getSearchContext(String initialContentId, String metadataPath, List<String> refIds) 063 { 064 Map<String, Object> searchCtx = new HashMap<>(); 065 066 Content initialContent = _resolver.resolveById(initialContentId); 067 068 Map<String, Object> initialContentInfos = new HashMap<>(); 069 initialContentInfos.put("contentId", initialContentId); 070 initialContentInfos.put("contentTitle", initialContent.getTitle()); 071 searchCtx.put("initialContent", initialContentInfos); 072 073 MetadataDefinition metadataDef = _cTypeHelper.getMetadataDefinition(metadataPath, initialContent); 074 if (metadataDef != null) 075 { 076 Map<String, Object> metadataInfos = new HashMap<>(); 077 078 metadataInfos.put("name", metadataDef.getName()); 079 metadataInfos.put("path", metadataDef.getId()); 080 metadataInfos.put("label", metadataDef.getLabel()); 081 082 String refContentTypeId = metadataDef.getReferenceContentType(); 083 ContentType refContentType = _cTypeEP.getExtension(refContentTypeId); 084 metadataInfos.put("refContentType", refContentType.getLabel()); 085 086 searchCtx.put("metadata", metadataInfos); 087 088 String refTableId = metadataDef.getContentType(); 089 if (refTableId != null) 090 { 091 Map<String, Object> refTableInfos = new HashMap<>(); 092 093 ContentType refTable = _cTypeEP.getExtension(refTableId); 094 refTableInfos.put("id", refTableId); 095 refTableInfos.put("label", refTable.getLabel()); 096 097 searchCtx.put("referenceTable", refTableInfos); 098 } 099 } 100 101 Locale defaultLocale = initialContent.getLanguage() != null ? new Locale(initialContent.getLanguage()) : null; 102 103 Map<String, String> refValues = new HashMap<>(); 104 for (String refId : refIds) 105 { 106 Content refContent = _resolver.resolveById(refId); 107 refValues.put(refId, refContent.getTitle(defaultLocale)); 108 } 109 110 searchCtx.put("refValues", refValues); 111 112 return searchCtx; 113 } 114}