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.util.server.control;
018
019import com.echothree.model.data.user.common.pk.UserVisitPK;
020import com.echothree.util.common.command.BaseResult;
021import com.echothree.util.common.command.SecurityResult;
022import com.echothree.util.common.form.BaseForm;
023import com.echothree.util.common.validation.FieldDefinition;
024import com.echothree.util.server.persistence.BaseEntity;
025import java.util.List;
026
027public abstract class BaseSingleEntityCommand<BE extends BaseEntity, F extends BaseForm>
028        extends BaseSimpleCommand<F>
029        implements GraphQlSecurityCommand<F> {
030    
031    protected BaseSingleEntityCommand(CommandSecurityDefinition commandSecurityDefinition,
032            List<FieldDefinition> formFieldDefinitions, boolean allowLimits) {
033        super(commandSecurityDefinition, formFieldDefinitions, allowLimits);
034    }
035    
036    protected abstract BE getEntity();
037    protected abstract BaseResult getResult(BE entity);
038
039    public BE getEntityForGraphQl(UserVisitPK userVisitPK, F form) {
040        BE entity = null;
041
042        if(canQueryByGraphQl(userVisitPK, form)) {
043            entity = getEntity();
044        }
045        
046        return entity;
047    }
048
049    @Override
050    public SecurityResult security(UserVisitPK userVisitPK, F form) {
051        this.form = form;
052        setUserVisitPK(userVisitPK);
053
054        return super.security();
055    }
056
057    @Override
058    protected BaseResult execute() {
059        var entity = getEntity();
060        
061        return getResult(entity);
062    }
063    
064}