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.EntityStringDefaultEdit;
021import com.echothree.control.user.core.common.form.EditEntityStringDefaultForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditEntityStringDefaultResult;
024import com.echothree.control.user.core.common.spec.EntityStringDefaultSpec;
025import com.echothree.model.control.core.server.logic.EntityAttributeLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.party.server.logic.LanguageLogic;
028import com.echothree.model.data.core.server.entity.EntityAttribute;
029import com.echothree.model.data.core.server.entity.EntityStringDefault;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.command.EditMode;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseAbstractEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import java.util.List;
039import javax.enterprise.context.RequestScoped;
040
041@RequestScoped
042public class EditEntityStringDefaultCommand
043        extends BaseAbstractEditCommand<EntityStringDefaultSpec, EntityStringDefaultEdit, EditEntityStringDefaultResult, EntityStringDefault, 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                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("LanguageUuid", FieldType.ENTITY_NAME, false, null, null)
063        );
064
065        EDIT_FIELD_DEFINITIONS = List.of(
066                new FieldDefinition("StringAttribute", FieldType.STRING, true, 1L, 512L)
067        );
068    }
069    
070    /** Creates a new instance of EditEntityStringDefaultCommand */
071    public EditEntityStringDefaultCommand() {
072        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
073    }
074    
075    @Override
076    public EditEntityStringDefaultResult getResult() {
077        return CoreResultFactory.getEditEntityStringDefaultResult();
078    }
079
080    @Override
081    public EntityStringDefaultEdit getEdit() {
082        return CoreEditFactory.getEntityStringDefaultEdit();
083    }
084
085    @Override
086    public EntityStringDefault getEntity(EditEntityStringDefaultResult result) {
087        var entityAttribute = EntityAttributeLogic.getInstance().getEntityAttributeByUniversalSpec(this, spec);
088        var language = LanguageLogic.getInstance().getLanguage(this, spec, spec);
089        EntityStringDefault entityStringDefault = null;
090
091        if(!hasExecutionErrors()) {
092
093            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
094                entityStringDefault = coreControl.getEntityStringDefault(entityAttribute, language);
095            } else { // EditMode.UPDATE
096                entityStringDefault = coreControl.getEntityStringDefaultForUpdate(entityAttribute, language);
097            }
098
099            if(entityStringDefault == null) {
100                addExecutionError(ExecutionErrors.UnknownEntityStringDefault.name());
101            }
102        }
103
104        return entityStringDefault;
105    }
106
107    @Override
108    public EntityAttribute getLockEntity(EntityStringDefault entityStringDefault) {
109        return entityStringDefault.getEntityAttribute();
110    }
111
112    @Override
113    public void fillInResult(EditEntityStringDefaultResult result, EntityStringDefault entityStringDefault) {
114
115        result.setEntityStringDefault(coreControl.getEntityStringDefaultTransfer(getUserVisit(), entityStringDefault));
116    }
117
118    @Override
119    public void doLock(EntityStringDefaultEdit edit, EntityStringDefault entityStringDefault) {
120        edit.setStringAttribute(entityStringDefault.getStringAttribute());
121    }
122
123    @Override
124    public void doUpdate(EntityStringDefault entityStringDefault) {
125        var entityStringDefaultValue = coreControl.getEntityStringDefaultValueForUpdate(entityStringDefault);
126
127        entityStringDefaultValue.setStringAttribute(String.valueOf(edit.getStringAttribute()));
128
129        coreControl.updateEntityStringDefaultFromValue(entityStringDefaultValue, getPartyPK());
130    }
131    
132}