001/* 002 * Copyright 2017 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.search.cocoon; 017 018import java.util.ArrayList; 019import java.util.Arrays; 020import java.util.Collection; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024 025import org.apache.avalon.framework.parameters.Parameters; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.cocoon.ProcessingException; 029import org.apache.cocoon.acting.ServiceableAction; 030import org.apache.cocoon.environment.ObjectModelHelper; 031import org.apache.cocoon.environment.Redirector; 032import org.apache.cocoon.environment.Request; 033import org.apache.cocoon.environment.SourceResolver; 034 035import org.ametys.cms.contenttype.ContentTypesHelper; 036import org.ametys.cms.contenttype.MetadataDefinition; 037import org.ametys.cms.contenttype.MetadataType; 038import org.ametys.cms.contenttype.RepeaterDefinition; 039import org.ametys.cms.repository.Content; 040import org.ametys.cms.search.ui.model.SearchUIModelHelper; 041import org.ametys.cms.search.ui.model.impl.DefaultMetadataSearchUIColumn; 042import org.ametys.core.cocoon.JSonReader; 043import org.ametys.core.util.ServerCommHelper; 044import org.ametys.plugins.repository.AmetysObjectResolver; 045 046/** 047 * SAX the columns of a repeater for the search tool. 048 */ 049public class ModelRepeaterColumnsAction extends ServiceableAction 050{ 051 /** The servercomm helper */ 052 protected ServerCommHelper _serverCommHelper; 053 /** The Ametys object resolver */ 054 protected AmetysObjectResolver _resolver; 055 /** Content Types helper */ 056 protected ContentTypesHelper _contentTypesHelper; 057 /** Search UI Model helper */ 058 protected SearchUIModelHelper _searchUIModelHelper; 059 060 @Override 061 public void service(ServiceManager smanager) throws ServiceException 062 { 063 super.service(smanager); 064 _serverCommHelper = (ServerCommHelper) smanager.lookup(ServerCommHelper.ROLE); 065 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 066 _contentTypesHelper = (ContentTypesHelper) smanager.lookup(ContentTypesHelper.ROLE); 067 _searchUIModelHelper = (SearchUIModelHelper) smanager.lookup(SearchUIModelHelper.ROLE); 068 } 069 070 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 071 { 072 Request request = ObjectModelHelper.getRequest(objectModel); 073 Map<String, Object> results = new HashMap<>(); 074 075 Map<String, Object> jsParameters = _serverCommHelper.getJsParameters(); 076 077 String contentId = (String) jsParameters.get("contentId"); 078 String metadataPath = (String) jsParameters.get("metadataPath"); 079 080 Content content = _resolver.resolveById(contentId); 081 if (content != null && metadataPath != null) 082 { 083 MetadataDefinition metadataDefinition = _contentTypesHelper.getMetadataDefinition(metadataPath, content); 084 085 if (metadataDefinition != null && metadataDefinition instanceof RepeaterDefinition) 086 { 087 List<Object> columns = new ArrayList<>(); 088 for (String subMetadataName : metadataDefinition.getMetadataNames()) 089 { 090 MetadataDefinition subMetadataDefinition = metadataDefinition.getMetadataDefinition(subMetadataName); 091 _addMetadataToResults(columns, subMetadataDefinition, subMetadataName, Arrays.asList(content.getTypes())); 092 } 093 results.put("columns", columns); 094 } 095 } 096 097 results.put("success", true); 098 request.setAttribute(JSonReader.OBJECT_TO_READ, results); 099 return EMPTY_MAP; 100 } 101 102 private void _addMetadataToResults(List<Object> columns, MetadataDefinition metadataDefinition, String fullMetadataPath, Collection<String> contentTypes) throws ProcessingException 103 { 104 if (metadataDefinition.getType().equals(MetadataType.COMPOSITE) && !(metadataDefinition instanceof RepeaterDefinition)) 105 { 106 for (String subMetadataName : metadataDefinition.getMetadataNames()) 107 { 108 MetadataDefinition subMetadataDefinition = metadataDefinition.getMetadataDefinition(subMetadataName); 109 _addMetadataToResults(columns, subMetadataDefinition, fullMetadataPath + "." + subMetadataName, contentTypes); 110 } 111 } 112 else 113 { 114 DefaultMetadataSearchUIColumn column = new DefaultMetadataSearchUIColumn(metadataDefinition, fullMetadataPath, contentTypes); 115 Map<String, Object> columnInfo = _searchUIModelHelper.getColumnInfo(column); 116 columns.add(columnInfo); 117 } 118 } 119}