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.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 java.util.List;
026import javax.enterprise.context.ApplicationScoped;
027import javax.inject.Inject;
028
029@ApplicationScoped
030public class UserKeyLogic {
031
032    @Inject
033    UserKeyFactory userKeyFactory;
034
035    @Inject
036    UserControl userControl;
037
038    protected UserKeyLogic() {
039        super();
040    }
041    
042    public void removeInactiveUserKeys(final Long remainingTime, final Long inactiveTime) {
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.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 userKeyDetailValue = userControl.getUserKeyDetailValueForUpdate(userKey);
062
063        userKeyDetailValue.setPartyPK(null);
064        userKeyDetailValue.setPartyRelationshipPK(null);
065
066        userControl.updateUserKeyFromValue(userKeyDetailValue);
067    }
068
069    /** Calls clearUserKey(...) for each UserKey in the List.
070     */
071    public void clearUserKeys(List<UserKey> userKeys) {
072        userKeys.forEach((userKey) -> {
073            clearUserKey(userKey);
074        });
075    }
076
077    /** Sets the Party and PartyRelationship to null when a UserKey contains the specified Party.
078     */
079    public void clearUserKeysByParty(Party party) {
080        clearUserKeys(userControl.getUserKeysByPartyForUpdate(party));
081    }
082
083    /** Sets the Party and PartyRelationship to null when a UserKey contains the specified PartyRelationship.
084     */
085    public void clearUserKeysByPartyRelationship(PartyRelationship partyRelationship) {
086        clearUserKeys(userControl.getUserKeysByPartyRelationshipForUpdate(partyRelationship));
087    }
088    
089}