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.ContentAttributeDefinition; 027import org.ametys.cms.contenttype.ContentType; 028import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 029import org.ametys.cms.repository.Content; 030import org.ametys.core.ui.Callable; 031import org.ametys.core.ui.ClientSideElement; 032import org.ametys.core.ui.StaticClientSideElement; 033import org.ametys.plugins.repository.AmetysObjectResolver; 034import org.ametys.runtime.model.ModelItem; 035 036/** 037 * {@link ClientSideElement} for search tool on contents with the same referencing values for a given attribute. 038 */ 039public class SearchReferencingContentsWithSameValuesClientSideElement extends StaticClientSideElement 040{ 041 private AmetysObjectResolver _resolver; 042 private ContentTypeExtensionPoint _cTypeEP; 043 044 @Override 045 public void service(ServiceManager smanager) throws ServiceException 046 { 047 super.service(smanager); 048 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 049 _cTypeEP = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE); 050 } 051 052 /** 053 * Get the search context 054 * @param initialContentId The id of initial content (from which the search was launched) 055 * @param attributePath The path of the attribute 056 * @param refIds The current values for this content and attribute 057 * @return The search context 058 */ 059 @Callable 060 public Map<String, Object> getSearchContext(String initialContentId, String attributePath, List<String> refIds) 061 { 062 Map<String, Object> searchCtx = new HashMap<>(); 063 064 Content initialContent = _resolver.resolveById(initialContentId); 065 066 Map<String, Object> initialContentInfos = new HashMap<>(); 067 initialContentInfos.put("contentId", initialContentId); 068 initialContentInfos.put("contentTitle", initialContent.getTitle()); 069 searchCtx.put("initialContent", initialContentInfos); 070 071 if (initialContent.hasDefinition(attributePath)) 072 { 073 ModelItem modelItem = initialContent.getDefinition(attributePath); 074 Map<String, Object> attributeInfos = new HashMap<>(); 075 076 attributeInfos.put("name", modelItem.getName()); 077 attributeInfos.put("path", modelItem.getPath()); 078 attributeInfos.put("label", modelItem.getLabel()); 079 080 ContentType refContentType = (ContentType) modelItem.getModel(); 081 attributeInfos.put("refContentType", refContentType.getLabel()); 082 083 searchCtx.put("attribute", attributeInfos); 084 085 if (modelItem instanceof ContentAttributeDefinition) 086 { 087 String refTableId = ((ContentAttributeDefinition) modelItem).getContentTypeId(); 088 if (refTableId != null) 089 { 090 Map<String, Object> refTableInfos = new HashMap<>(); 091 092 ContentType refTable = _cTypeEP.getExtension(refTableId); 093 refTableInfos.put("id", refTableId); 094 refTableInfos.put("label", refTable.getLabel()); 095 096 searchCtx.put("referenceTable", refTableInfos); 097 } 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}