001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.model.data.user.server.value.UserKeyDetailValue;
025import com.echothree.util.server.persistence.EntityPermission;
026import com.echothree.util.server.persistence.Session;
027import java.util.List;
028
029public class UserKeyLogic {
030    
031    private UserKeyLogic() {
032        super();
033    }
034    
035    private static class UserKeyLogicHolder {
036        static UserKeyLogic instance = new UserKeyLogic();
037    }
038    
039    public static UserKeyLogic getInstance() {
040        return UserKeyLogicHolder.instance;
041    }
042    
043    public void removeInactiveUserKeys(final Long remainingTime, final Long inactiveTime) {
044        var userControl = Session.getModelController(UserControl.class);
045        long startTime = System.currentTimeMillis();
046        long entityCount = 0;
047        
048        for(UserKey userKey : userControl.getInactiveUserKeys(inactiveTime)) {
049            entityCount++;
050            if(entityCount % 1000 == 0 && System.currentTimeMillis() - startTime > remainingTime) {
051                break;
052            }
053            
054            userKey = UserKeyFactory.getInstance().getEntityFromPK(EntityPermission.READ_WRITE, userKey.getPrimaryKey());
055            
056            userControl.removeUserKey(userKey);
057        }
058    }
059
060    /** Sets the Party and PartyRelationship to null.
061     */
062    public void clearUserKey(UserKey userKey) {
063        var userControl = Session.getModelController(UserControl.class);
064        UserKeyDetailValue userKeyDetailValue = userControl.getUserKeyDetailValueForUpdate(userKey);
065
066        userKeyDetailValue.setPartyPK(null);
067        userKeyDetailValue.setPartyRelationshipPK(null);
068
069        userControl.updateUserKeyFromValue(userKeyDetailValue);
070    }
071
072    /** Calls clearUserKey(...) for each UserKey in the List.
073     */
074    public void clearUserKeys(List<UserKey> userKeys) {
075        userKeys.forEach((userKey) -> {
076            clearUserKey(userKey);
077        });
078    }
079
080    /** Sets the Party and PartyRelationship to null when a UserKey contains the specified Party.
081     */
082    public void clearUserKeysByParty(Party party) {
083        var userControl = Session.getModelController(UserControl.class);
084
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        var userControl = Session.getModelController(UserControl.class);
092
093        clearUserKeys(userControl.getUserKeysByPartyRelationshipForUpdate(partyRelationship));
094    }
095    
096}