001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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; 045 046@ApplicationScoped 047public class IndexLogic 048 extends BaseLogic { 049 050 protected IndexLogic() { 051 super(); 052 } 053 054 public static IndexLogic getInstance() { 055 return CDI.current().select(IndexLogic.class).get(); 056 } 057 058 public Index getIndexByName(final ExecutionErrorAccumulator eea, final String indexName) { 059 var indexControl = Session.getModelController(IndexControl.class); 060 var index = indexControl.getIndexByName(indexName); 061 062 if(index == null) { 063 handleExecutionError(UnknownIndexNameException.class, eea, ExecutionErrors.UnknownIndexName.name(), indexName); 064 } 065 066 return index; 067 } 068 069 private void queueEntityInstances(final Session session, final QueueControl queueControl, final QueueTypePK queueTypePK, 070 final List<EntityInstancePKResult> entityInstanceResults) { 071 List<QueuedEntityValue> queuedEntities = new ArrayList<>(entityInstanceResults.size()); 072 073 for(var entityInstanceResult : entityInstanceResults) { 074 queuedEntities.add(new QueuedEntityValue(queueTypePK, entityInstanceResult.getEntityInstancePK(), session.START_TIME_LONG)); 075 } 076 077 queueControl.createQueuedEntities(queuedEntities); 078 } 079 080 private void queueEntityInstancesByEntityType(final Session session, final ExecutionErrorAccumulator eea, final QueueControl queueControl, 081 final QueueTypePK queueTypePK, final Set<EntityType> queuedEntityTypes, final IndexType indexType) { 082 var indexTypeDetail = indexType.getLastDetail(); 083 var entityType = indexTypeDetail.getEntityType(); 084 var entityTypeDetail = entityType.getLastDetail(); 085 var componentVendor = entityTypeDetail.getComponentVendor(); 086 var entityTypeName = entityTypeDetail.getEntityTypeName(); 087 088 if(entityTypeName.equals(EntityTypes.Party.name()) 089 && componentVendor.getLastDetail().getComponentVendorName().equals(ComponentVendors.ECHO_THREE.name())) { 090 // The Party indexes aren't by Language, so we're not worried about de-dupping them. 091 var partyType = PartyLogic.getInstance().getPartyTypeByName(eea, indexTypeDetail.getIndexTypeName()); 092 093 if(eea == null || !eea.hasExecutionErrors()) { 094 queueEntityInstances(session, queueControl, queueTypePK, new EntityInstancePKsByPartyTypeQuery().execute(entityType, partyType)); 095 } 096 } else { 097 if(!queuedEntityTypes.contains(entityType)) { 098 queueEntityInstances(session, queueControl, queueTypePK, new EntityInstancePKsByEntityTypeQuery().execute(entityType)); 099 100 queuedEntityTypes.add(entityType); 101 } 102 } 103 } 104 105 /** 106 * Request that a specific Index or that all Indexes should be reindexed. 107 * @param eea the ExecutionErrorAccumulator that should gather any errors encountered during execution (Required) 108 * @param entityType the EntityType that should be reindexed. If null, all indexes will be reindexed (Optional) 109 */ 110 public void reindex(final Session session, final ExecutionErrorAccumulator eea, final EntityType entityType) { 111 var queueType = QueueTypeLogic.getInstance().getQueueTypeByName(eea, QueueTypes.INDEXING.name()); 112 113 if(eea == null || !eea.hasExecutionErrors()) { 114 var indexControl = Session.getModelController(IndexControl.class); 115 var queueControl = Session.getModelController(QueueControl.class); 116 var queueTypePK = queueType.getPrimaryKey(); 117 Set<EntityType> queuedEntityTypes = new HashSet<>(); 118 var indexTypes = entityType == null ? indexControl.getIndexTypes() : indexControl.getIndexTypesByEntityType(entityType); 119 120 for(var indexType : indexTypes) { 121 var indexes = indexControl.getIndexesByIndexType(indexType); 122 123 for(var index : indexes) { 124 var indexStatus = indexControl.getIndexStatusForUpdate(index); 125 126 queueEntityInstancesByEntityType(session, eea, queueControl, queueTypePK, queuedEntityTypes, indexType); 127 128 if(eea.hasExecutionErrors()) { 129 break; 130 } 131 132 indexStatus.setCreatedTime(null); 133 } 134 135 if(eea.hasExecutionErrors()) { 136 break; 137 } 138 } 139 } 140 } 141 142}