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