001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
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// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.security.server.indexer;
018
019import com.echothree.model.control.index.common.IndexConstants;
020import com.echothree.model.control.index.common.IndexFieldVariations;
021import com.echothree.model.control.index.common.IndexFields;
022import com.echothree.model.control.index.server.analysis.SecurityRoleGroupAnalyzer;
023import com.echothree.model.control.index.server.indexer.BaseIndexer;
024import com.echothree.model.control.index.server.indexer.FieldTypes;
025import com.echothree.model.control.security.server.control.SecurityControl;
026import com.echothree.model.data.core.server.entity.EntityInstance;
027import com.echothree.model.data.index.server.entity.Index;
028import com.echothree.model.data.security.server.entity.SecurityRoleGroup;
029import com.echothree.model.data.security.server.entity.SecurityRoleGroupDetail;
030import com.echothree.util.server.message.ExecutionErrorAccumulator;
031import com.echothree.util.server.persistence.Session;
032import org.apache.lucene.analysis.Analyzer;
033import org.apache.lucene.document.Document;
034import org.apache.lucene.document.Field;
035import org.apache.lucene.document.SortedDocValuesField;
036import org.apache.lucene.util.BytesRef;
037
038public class SecurityRoleGroupIndexer
039        extends BaseIndexer<SecurityRoleGroup> {
040    
041    SecurityControl securityControl = Session.getModelController(SecurityControl.class);
042
043    /** Creates a new instance of SecurityRoleGroupIndexer */
044    public SecurityRoleGroupIndexer(final ExecutionErrorAccumulator eea, final Index index) {
045        super(eea, index);
046    }
047
048    @Override
049    protected Analyzer getAnalyzer() {
050        return new SecurityRoleGroupAnalyzer(eea, language, entityType, entityAliasTypes, entityAttributes, tagScopes);
051    }
052    
053    @Override
054    protected SecurityRoleGroup getEntity(final EntityInstance entityInstance) {
055        return securityControl.getSecurityRoleGroupByEntityInstance(entityInstance);
056    }
057    
058    @Override
059    protected Document convertToDocument(final EntityInstance entityInstance, final SecurityRoleGroup securityRoleGroup) {
060        SecurityRoleGroupDetail securityRoleGroupDetail = securityRoleGroup.getLastDetail();
061        SecurityRoleGroup parentSecurityRoleGroup = securityRoleGroupDetail.getParentSecurityRoleGroup();
062        String description = securityControl.getBestSecurityRoleGroupDescription(securityRoleGroup, language);
063
064        var document = newDocumentWithEntityInstanceFields(entityInstance, securityRoleGroup.getPrimaryKey());
065
066        document.add(new Field(IndexFields.securityRoleGroupName.name(), securityRoleGroupDetail.getSecurityRoleGroupName(), FieldTypes.NOT_STORED_TOKENIZED));
067        document.add(new SortedDocValuesField(IndexFields.securityRoleGroupName.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
068                new BytesRef(securityRoleGroupDetail.getSecurityRoleGroupName())));
069        if(parentSecurityRoleGroup != null) {
070            document.add(new Field(IndexFields.parentSecurityRoleGroupName.name(), parentSecurityRoleGroup.getLastDetail().getSecurityRoleGroupName(),
071                    FieldTypes.NOT_STORED_TOKENIZED));
072        }
073        
074        if(description != null) {
075            document.add(new Field(IndexFields.description.name(), description, FieldTypes.NOT_STORED_TOKENIZED));
076            document.add(new SortedDocValuesField(IndexFields.description.name() + IndexConstants.INDEX_FIELD_VARIATION_SEPARATOR + IndexFieldVariations.sortable.name(),
077                    new BytesRef(description)));
078        }
079
080        return document;
081    }
082
083}