package gw.solr.request uses gw.pl.util.JSONMultiObject uses gw.api.system.PLLoggerCategory uses gw.lang.reflect.TypeSystem uses gw.solr.mapper.ISolrIndexMapper uses gw.solr.schema.SolrSchemaConfigReader uses gw.solr.utils.PLSolrUtils uses org.json.simple.JSONArray uses org.json.simple.JSONObject uses java.io.File uses java.io.Reader uses java.lang.Exception uses java.util.Collection uses java.util.HashSet /** * Represents a Solr document (or the key thereof). A row in a Solr index, if you will. */ @ReadOnly abstract class AbstractIndexDocument { var _rawData = new DataHolderImpl(true) var _data : JSONObject = new JSONMultiObject() var _configReader : SolrSchemaConfigReader var _key : String construct(configFile : File) { _configReader = PLSolrUtils.getSchemaConfigReader(configFile) _rawData.loadRootMap(_configReader.Indexer.Root) } construct(reader : Reader) { _configReader = new SolrSchemaConfigReader(reader) _configReader.validate() } final function getConfigReader() : SolrSchemaConfigReader { return _configReader } final property get Key() : String { return _key } final function putData(theKey : String, value : JSONObject) { _data.put(theKey, value) } final function addIndexData(indexData : Object) { _rawData.addData(indexData) } final function addIndexData(dataKey : String, indexData : Object) { if(indexData != null) { if(_rawData.containsKey(dataKey)) { throw new Exception("Duplicate key added to data holder for virtual key " + dataKey + ". Use path based configuration or a unique virtual key to resolve this error.") } _rawData.put(dataKey, indexData) } } final function populateIndexData( keyOnly : boolean ) { // don't want to use CoreIterableEnhancement.partitionUniquely() because it drops the bean type in the map... var indexFields = getConfigReader().Indexer.IndexField for(var indexField in indexFields) { if(keyOnly && !indexField.Key) { continue } try { var mapper = indexField.Mapper var isim = TypeSystem.getByFullName(mapper).TypeInfo.getConstructor({}).Constructor.newInstance({}) as ISolrIndexMapper var processedValue = isim.mapIndex(indexField.DataProperty, _rawData) if (processedValue != null) { _data.put(indexField.Field, processedValue) } if (keyOnly) { _key = processedValue as String } } catch( e : Exception ) { PLLoggerCategory.SOLR_INDEX.warn("Exception loading value for index field ${indexField.Field}", e) } } populateReferencedBeans() } final function populateIndexData( docObj : JSONObject, keyOnly : boolean ) { var indexFields = getConfigReader().Indexer.IndexField for(var indexField in indexFields) { if(keyOnly && !indexField.Key) { continue } try { var fieldValue = docObj.get(indexField.Field) as String if (fieldValue != null && !fieldValue.Empty) { _data.put(indexField.Field, fieldValue) } if (keyOnly) { _key = fieldValue } } catch( e : Exception ) { PLLoggerCategory.SOLR_INDEX.warn("Exception loading value for index field ${indexField.Field}", e) } } populateReferencedBeans() } final function addReferencedBean( bean : KeyableBean ) { _rawData.addReference(bean) } final function addReferencedBeans( beans : Collection ) { _rawData.addReferences(beans) } final function populateReferencedBeans() { var refdBeanArray = new JSONArray() for(b in _rawData.ReferencedBeans) { var obj = new JSONObject() obj.put(PLSolrUtils.BEAN_TYPE_TAG, b.ID.Type.RelativeName) obj.put(PLSolrUtils.BEAN_PUBLIC_ID_TAG, b.PublicID) obj.put(PLSolrUtils.BEAN_ID_TAG, b.ID) if(b typeis Versionable) { obj.put(PLSolrUtils.BEAN_VERSION_TAG, b.BeanVersion) } refdBeanArray.add(obj) } _data.put(PLSolrUtils.VERSIONS_TAG, refdBeanArray) } function asJSON() : JSONObject { return _data } }