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 */ 016package org.ametys.cms.search.solr.schema; 017 018import java.util.ArrayList; 019import java.util.Collection; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.configuration.Configurable; 024import org.apache.avalon.framework.configuration.Configuration; 025import org.apache.avalon.framework.configuration.ConfigurationException; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.avalon.framework.service.Serviceable; 029import org.apache.excalibur.source.SourceResolver; 030import org.apache.solr.client.solrj.response.schema.SchemaRepresentation; 031 032import org.ametys.runtime.plugin.component.AbstractLogEnabled; 033 034/** 035 * Reads a given schema fragment file and provides the definitions defined in it. 036 */ 037public class StaticSchemaDefinitionProvider extends AbstractLogEnabled implements SchemaDefinitionProvider, Configurable, Serviceable 038{ 039 040 /** The source resolver. */ 041 protected SourceResolver _sourceResolver; 042 043 /** The schema helper. */ 044 protected SchemaHelper _schemaHelper; 045 046 private String _schemaLocation; 047 048 @Override 049 public void service(ServiceManager manager) throws ServiceException 050 { 051 _sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 052 _schemaHelper = (SchemaHelper) manager.lookup(SchemaHelper.ROLE); 053 } 054 055 @Override 056 public void configure(Configuration configuration) throws ConfigurationException 057 { 058 _schemaLocation = configuration.getChild("schema").getAttribute("location"); 059 } 060 061 @Override 062 public Collection<SchemaDefinition> getDefinitions() 063 { 064 SchemaRepresentation schema = _schemaHelper.getSchema(_schemaLocation); 065 066 return getDefinitions(schema); 067 } 068 069 /** 070 * Get the definitions from the given schema representation. 071 * @param schema The schema representation. 072 * @return The collection of schema definitions defined in the schema. 073 */ 074 protected Collection<SchemaDefinition> getDefinitions(SchemaRepresentation schema) 075 { 076 List<SchemaDefinition> definitions = new ArrayList<>(); 077 078 for (org.apache.solr.client.solrj.request.schema.FieldTypeDefinition fieldType : schema.getFieldTypes()) 079 { 080 definitions.add(new FieldTypeDefinition(fieldType)); 081 } 082 083 for (Map<String, Object> attributes : schema.getFields()) 084 { 085 definitions.add(new FieldDefinition(attributes)); 086 } 087 088 for (Map<String, Object> attributes : schema.getDynamicFields()) 089 { 090 definitions.add(new DynamicFieldDefinition(attributes)); 091 } 092 093 for (Map<String, Object> attributes : schema.getCopyFields()) 094 { 095 definitions.add(new CopyFieldDefinition(attributes)); 096 } 097 098 return definitions; 099 } 100}