001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.index.server.logic; 018 019import com.echothree.model.control.core.common.ComponentVendors; 020import com.echothree.model.control.core.common.EntityTypes; 021import com.echothree.model.control.core.server.database.EntityInstancePKResult; 022import com.echothree.model.control.core.server.database.EntityInstancePKsByEntityTypeQuery; 023import com.echothree.model.control.core.server.database.EntityInstancePKsByPartyTypeQuery; 024import com.echothree.model.control.index.common.exception.UnknownIndexNameException; 025import com.echothree.model.control.index.server.control.IndexControl; 026import com.echothree.model.control.party.server.logic.PartyLogic; 027import com.echothree.model.control.queue.common.QueueTypes; 028import com.echothree.model.control.queue.server.control.QueueControl; 029import com.echothree.model.control.queue.server.logic.QueueTypeLogic; 030import com.echothree.model.data.core.server.entity.EntityType; 031import com.echothree.model.data.index.server.entity.Index; 032import com.echothree.model.data.index.server.entity.IndexType; 033import com.echothree.model.data.queue.common.pk.QueueTypePK; 034import com.echothree.model.data.queue.server.value.QueuedEntityValue; 035import com.echothree.util.common.message.ExecutionErrors; 036import com.echothree.util.server.control.BaseLogic; 037import com.echothree.util.server.message.ExecutionErrorAccumulator; 038import com.echothree.util.server.persistence.Session; 039import java.util.ArrayList; 040import java.util.HashSet; 041import java.util.List; 042import java.util.Set; 043import javax.enterprise.context.ApplicationScoped; 044import javax.enterprise.inject.spi.CDI; 045import javax.inject.Inject; 046 047@ApplicationScoped 048public class IndexLogic 049 extends BaseLogic { 050 051 @Inject 052 IndexControl indexControl; 053 054 @Inject 055 QueueControl queueControl; 056 057 @Inject 058 PartyLogic partyLogic; 059 060 @Inject 061 QueueTypeLogic queueTypeLogic; 062 063 protected IndexLogic() { 064 super(); 065 } 066 067 public static IndexLogic getInstance() { 068 return CDI.current().select(IndexLogic.class).get(); 069 } 070 071 public Index getIndexByName(final ExecutionErrorAccumulator eea, final String indexName) { 072 var index = indexControl.getIndexByName(indexName); 073 074 if(index == null) { 075 handleExecutionError(UnknownIndexNameException.class, eea, ExecutionErrors.UnknownIndexName.name(), indexName); 076 } 077 078 return index; 079 } 080 081 private void queueEntityInstances(final Session session, final QueueControl queueControl, final QueueTypePK queueTypePK, 082 final List<EntityInstancePKResult> entityInstanceResults) { 083 List<QueuedEntityValue> queuedEntities = new ArrayList<>(entityInstanceResults.size()); 084 085 for(var entityInstanceResult : entityInstanceResults) { 086 queuedEntities.add(new QueuedEntityValue(queueTypePK, entityInstanceResult.getEntityInstancePK(), session.getStartTime())); 087 } 088 089 queueControl.createQueuedEntities(queuedEntities); 090 } 091 092 private void queueEntityInstancesByEntityType(final Session session, final ExecutionErrorAccumulator eea, final QueueControl queueControl, 093 final QueueTypePK queueTypePK, final Set<EntityType> queuedEntityTypes, final IndexType indexType) { 094 var indexTypeDetail = indexType.getLastDetail(); 095 var entityType = indexTypeDetail.getEntityType(); 096 var entityTypeDetail = entityType.getLastDetail(); 097 var componentVendor = entityTypeDetail.getComponentVendor(); 098 var entityTypeName = entityTypeDetail.getEntityTypeName(); 099 100 if(entityTypeName.equals(EntityTypes.Party.name()) 101 && componentVendor.getLastDetail().getComponentVendorName().equals(ComponentVendors.ECHO_THREE.name())) { 102 // The Party indexes aren't by Language, so we're not worried about de-dupping them. 103 var partyType = partyLogic.getPartyTypeByName(eea, indexTypeDetail.getIndexTypeName()); 104 105 if(eea == null || !eea.hasExecutionErrors()) { 106 queueEntityInstances(session, queueControl, queueTypePK, new EntityInstancePKsByPartyTypeQuery().execute(entityType, partyType)); 107 } 108 } else { 109 if(!queuedEntityTypes.contains(entityType)) { 110 queueEntityInstances(session, queueControl, queueTypePK, new EntityInstancePKsByEntityTypeQuery().execute(entityType)); 111 112 queuedEntityTypes.add(entityType); 113 } 114 } 115 } 116 117 /** 118 * Request that a specific Index or that all Indexes should be reindexed. 119 * @param eea the ExecutionErrorAccumulator that should gather any errors encountered during execution (Required) 120 * @param entityType the EntityType that should be reindexed. If null, all indexes will be reindexed (Optional) 121 */ 122 public void reindex(final Session session, final ExecutionErrorAccumulator eea, final EntityType entityType) { 123 var queueType = queueTypeLogic.getQueueTypeByName(eea, QueueTypes.INDEXING.name()); 124 125 if(eea == null || !eea.hasExecutionErrors()) { 126 var queueTypePK = queueType.getPrimaryKey(); 127 Set<EntityType> queuedEntityTypes = new HashSet<>(); 128 var indexTypes = entityType == null ? indexControl.getIndexTypes() : indexControl.getIndexTypesByEntityType(entityType); 129 130 for(var indexType : indexTypes) { 131 var indexes = indexControl.getIndexesByIndexType(indexType); 132 133 for(var index : indexes) { 134 var indexStatus = indexControl.getIndexStatusForUpdate(index); 135 136 queueEntityInstancesByEntityType(session, eea, queueControl, queueTypePK, queuedEntityTypes, indexType); 137 138 if(eea.hasExecutionErrors()) { 139 break; 140 } 141 142 indexStatus.setCreatedTime(null); 143 } 144 145 if(eea.hasExecutionErrors()) { 146 break; 147 } 148 } 149 } 150 } 151 152}