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.user.server.logic; 018 019import com.echothree.model.control.user.server.control.UserControl; 020import com.echothree.model.data.party.server.entity.Party; 021import com.echothree.model.data.party.server.entity.PartyRelationship; 022import com.echothree.model.data.user.server.entity.UserKey; 023import com.echothree.model.data.user.server.factory.UserKeyFactory; 024import com.echothree.util.server.persistence.EntityPermission; 025import com.echothree.util.server.persistence.Session; 026import java.util.List; 027import javax.enterprise.context.ApplicationScoped; 028import javax.enterprise.inject.spi.CDI; 029 030@ApplicationScoped 031public class UserKeyLogic { 032 033 protected UserKeyLogic() { 034 super(); 035 } 036 037 public static UserKeyLogic getInstance() { 038 return CDI.current().select(UserKeyLogic.class).get(); 039 } 040 041 public void removeInactiveUserKeys(final Long remainingTime, final Long inactiveTime) { 042 var userControl = Session.getModelController(UserControl.class); 043 var startTime = System.currentTimeMillis(); 044 long entityCount = 0; 045 046 for(var userKey : userControl.getInactiveUserKeys(inactiveTime)) { 047 entityCount++; 048 if(entityCount % 1000 == 0 && System.currentTimeMillis() - startTime > remainingTime) { 049 break; 050 } 051 052 userKey = UserKeyFactory.getInstance().getEntityFromPK(EntityPermission.READ_WRITE, userKey.getPrimaryKey()); 053 054 userControl.removeUserKey(userKey); 055 } 056 } 057 058 /** Sets the Party and PartyRelationship to null. 059 */ 060 public void clearUserKey(UserKey userKey) { 061 var userControl = Session.getModelController(UserControl.class); 062 var userKeyDetailValue = userControl.getUserKeyDetailValueForUpdate(userKey); 063 064 userKeyDetailValue.setPartyPK(null); 065 userKeyDetailValue.setPartyRelationshipPK(null); 066 067 userControl.updateUserKeyFromValue(userKeyDetailValue); 068 } 069 070 /** Calls clearUserKey(...) for each UserKey in the List. 071 */ 072 public void clearUserKeys(List<UserKey> userKeys) { 073 userKeys.forEach((userKey) -> { 074 clearUserKey(userKey); 075 }); 076 } 077 078 /** Sets the Party and PartyRelationship to null when a UserKey contains the specified Party. 079 */ 080 public void clearUserKeysByParty(Party party) { 081 var userControl = Session.getModelController(UserControl.class); 082 083 clearUserKeys(userControl.getUserKeysByPartyForUpdate(party)); 084 } 085 086 /** Sets the Party and PartyRelationship to null when a UserKey contains the specified PartyRelationship. 087 */ 088 public void clearUserKeysByPartyRelationship(PartyRelationship partyRelationship) { 089 var userControl = Session.getModelController(UserControl.class); 090 091 clearUserKeys(userControl.getUserKeysByPartyRelationshipForUpdate(partyRelationship)); 092 } 093 094}