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.control.user.core.server.command;
018
019import com.echothree.control.user.core.common.edit.CoreEditFactory;
020import com.echothree.control.user.core.common.edit.EntityDateDefaultEdit;
021import com.echothree.control.user.core.common.form.EditEntityDateDefaultForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditEntityDateDefaultResult;
024import com.echothree.control.user.core.common.spec.EntityDateDefaultSpec;
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.EntityDateDefault;
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.Dependent;
039import javax.inject.Inject;
040
041@Dependent
042public class EditEntityDateDefaultCommand
043        extends BaseAbstractEditCommand<EntityDateDefaultSpec, EntityDateDefaultEdit, EditEntityDateDefaultResult, EntityDateDefault, EntityAttribute> {
044    
045    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
046    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
047    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
048    
049    static {
050        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null)
053        ));
054
055        SPEC_FIELD_DEFINITIONS = List.of(
056                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
057                new FieldDefinition("Uuid", FieldType.UUID, false, null, null),
058                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, false, null, null),
059                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, false, null, null),
060                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null)
061        );
062
063        EDIT_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("DateAttribute", FieldType.DATE, true, null, null)
065        );
066    }
067
068    @Inject
069    EntityAttributeLogic entityAttributeLogic;
070
071    
072    /** Creates a new instance of EditEntityDateDefaultCommand */
073    public EditEntityDateDefaultCommand() {
074        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076    
077    @Override
078    public EditEntityDateDefaultResult getResult() {
079        return CoreResultFactory.getEditEntityDateDefaultResult();
080    }
081
082    @Override
083    public EntityDateDefaultEdit getEdit() {
084        return CoreEditFactory.getEntityDateDefaultEdit();
085    }
086
087    @Override
088    public EntityDateDefault getEntity(EditEntityDateDefaultResult result) {
089        var entityAttribute = entityAttributeLogic.getEntityAttributeByUniversalSpec(this, spec);
090        EntityDateDefault entityDateDefault = null;
091
092        if(!hasExecutionErrors()) {
093
094            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
095                entityDateDefault = coreControl.getEntityDateDefault(entityAttribute);
096            } else { // EditMode.UPDATE
097                entityDateDefault = coreControl.getEntityDateDefaultForUpdate(entityAttribute);
098            }
099
100            if(entityDateDefault == null) {
101                addExecutionError(ExecutionErrors.UnknownEntityDateDefault.name());
102            }
103        }
104
105        return entityDateDefault;
106    }
107
108    @Override
109    public EntityAttribute getLockEntity(EntityDateDefault entityDateDefault) {
110        return entityDateDefault.getEntityAttribute();
111    }
112
113    @Override
114    public void fillInResult(EditEntityDateDefaultResult result, EntityDateDefault entityDateDefault) {
115
116        result.setEntityDateDefault(coreControl.getEntityDateDefaultTransfer(getUserVisit(), entityDateDefault));
117    }
118
119    @Override
120    public void doLock(EntityDateDefaultEdit edit, EntityDateDefault entityDateDefault) {
121        edit.setDateAttribute(entityDateDefault.getDateAttribute().toString());
122    }
123
124    @Override
125    public void doUpdate(EntityDateDefault entityDateDefault) {
126        var entityDateDefaultValue = coreControl.getEntityDateDefaultValueForUpdate(entityDateDefault);
127
128        entityDateDefaultValue.setDateAttribute(Integer.valueOf(edit.getDateAttribute()));
129
130        coreControl.updateEntityDateDefaultFromValue(entityDateDefaultValue, getPartyPK());
131    }
132    
133}