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.control.user.core.server.command;
018
019import com.echothree.control.user.core.common.edit.CoreEditFactory;
020import com.echothree.control.user.core.common.edit.EntityBooleanDefaultEdit;
021import com.echothree.control.user.core.common.form.EditEntityBooleanDefaultForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditEntityBooleanDefaultResult;
024import com.echothree.control.user.core.common.spec.EntityBooleanDefaultSpec;
025import com.echothree.model.control.core.server.logic.EntityAttributeLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.data.core.server.entity.EntityAttribute;
028import com.echothree.model.data.core.server.entity.EntityBooleanDefault;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.command.EditMode;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseAbstractEditCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import java.util.List;
038import javax.enterprise.context.RequestScoped;
039
040@RequestScoped
041public class EditEntityBooleanDefaultCommand
042        extends BaseAbstractEditCommand<EntityBooleanDefaultSpec, EntityBooleanDefaultEdit, EditEntityBooleanDefaultResult, EntityBooleanDefault, EntityAttribute> {
043    
044    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
045    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
046    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
047    
048    static {
049        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
050                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
051                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null)
052        ));
053
054        SPEC_FIELD_DEFINITIONS = List.of(
055                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
056                new FieldDefinition("Uuid", FieldType.UUID, false, null, null),
057                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, false, null, null),
058                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, false, null, null),
059                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null)
060        );
061
062        EDIT_FIELD_DEFINITIONS = List.of(
063                new FieldDefinition("BooleanAttribute", FieldType.BOOLEAN, true, null, null)
064        );
065    }
066    
067    /** Creates a new instance of EditEntityBooleanDefaultCommand */
068    public EditEntityBooleanDefaultCommand() {
069        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
070    }
071    
072    @Override
073    public EditEntityBooleanDefaultResult getResult() {
074        return CoreResultFactory.getEditEntityBooleanDefaultResult();
075    }
076
077    @Override
078    public EntityBooleanDefaultEdit getEdit() {
079        return CoreEditFactory.getEntityBooleanDefaultEdit();
080    }
081
082    @Override
083    public EntityBooleanDefault getEntity(EditEntityBooleanDefaultResult result) {
084        var entityAttribute = EntityAttributeLogic.getInstance().getEntityAttributeByUniversalSpec(this, spec);
085        EntityBooleanDefault entityBooleanDefault = null;
086
087        if(!hasExecutionErrors()) {
088
089            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
090                entityBooleanDefault = coreControl.getEntityBooleanDefault(entityAttribute);
091            } else { // EditMode.UPDATE
092                entityBooleanDefault = coreControl.getEntityBooleanDefaultForUpdate(entityAttribute);
093            }
094
095            if(entityBooleanDefault == null) {
096                addExecutionError(ExecutionErrors.UnknownEntityBooleanDefault.name());
097            }
098        }
099
100        return entityBooleanDefault;
101    }
102
103    @Override
104    public EntityAttribute getLockEntity(EntityBooleanDefault entityBooleanDefault) {
105        return entityBooleanDefault.getEntityAttribute();
106    }
107
108    @Override
109    public void fillInResult(EditEntityBooleanDefaultResult result, EntityBooleanDefault entityBooleanDefault) {
110
111        result.setEntityBooleanDefault(coreControl.getEntityBooleanDefaultTransfer(getUserVisit(), entityBooleanDefault));
112    }
113
114    @Override
115    public void doLock(EntityBooleanDefaultEdit edit, EntityBooleanDefault entityBooleanDefault) {
116        edit.setBooleanAttribute(entityBooleanDefault.getBooleanAttribute().toString());
117    }
118
119    @Override
120    public void doUpdate(EntityBooleanDefault entityBooleanDefault) {
121        var entityBooleanDefaultValue = coreControl.getEntityBooleanDefaultValueForUpdate(entityBooleanDefault);
122
123        entityBooleanDefaultValue.setBooleanAttribute(Boolean.valueOf(edit.getBooleanAttribute()));
124
125        coreControl.updateEntityBooleanDefaultFromValue(entityBooleanDefaultValue, getPartyPK());
126    }
127    
128}