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.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.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class EditEntityStringDefaultCommand
044        extends BaseAbstractEditCommand<EntityStringDefaultSpec, EntityStringDefaultEdit, EditEntityStringDefaultResult, EntityStringDefault, EntityAttribute> {
045    
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
048    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
049    
050    static {
051        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null)
054        ));
055
056        SPEC_FIELD_DEFINITIONS = List.of(
057                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
058                new FieldDefinition("Uuid", FieldType.UUID, false, null, null),
059                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, false, null, null),
060                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, false, null, null),
061                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("LanguageUuid", FieldType.ENTITY_NAME, false, null, null)
064        );
065
066        EDIT_FIELD_DEFINITIONS = List.of(
067                new FieldDefinition("StringAttribute", FieldType.STRING, true, 1L, 512L)
068        );
069    }
070
071    @Inject
072    EntityAttributeLogic entityAttributeLogic;
073
074    @Inject
075    LanguageLogic languageLogic;
076
077    
078    /** Creates a new instance of EditEntityStringDefaultCommand */
079    public EditEntityStringDefaultCommand() {
080        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
081    }
082    
083    @Override
084    public EditEntityStringDefaultResult getResult() {
085        return CoreResultFactory.getEditEntityStringDefaultResult();
086    }
087
088    @Override
089    public EntityStringDefaultEdit getEdit() {
090        return CoreEditFactory.getEntityStringDefaultEdit();
091    }
092
093    @Override
094    public EntityStringDefault getEntity(EditEntityStringDefaultResult result) {
095        var entityAttribute = entityAttributeLogic.getEntityAttributeByUniversalSpec(this, spec);
096        var language = languageLogic.getLanguage(this, spec, spec);
097        EntityStringDefault entityStringDefault = null;
098
099        if(!hasExecutionErrors()) {
100
101            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
102                entityStringDefault = coreControl.getEntityStringDefault(entityAttribute, language);
103            } else { // EditMode.UPDATE
104                entityStringDefault = coreControl.getEntityStringDefaultForUpdate(entityAttribute, language);
105            }
106
107            if(entityStringDefault == null) {
108                addExecutionError(ExecutionErrors.UnknownEntityStringDefault.name());
109            }
110        }
111
112        return entityStringDefault;
113    }
114
115    @Override
116    public EntityAttribute getLockEntity(EntityStringDefault entityStringDefault) {
117        return entityStringDefault.getEntityAttribute();
118    }
119
120    @Override
121    public void fillInResult(EditEntityStringDefaultResult result, EntityStringDefault entityStringDefault) {
122
123        result.setEntityStringDefault(coreControl.getEntityStringDefaultTransfer(getUserVisit(), entityStringDefault));
124    }
125
126    @Override
127    public void doLock(EntityStringDefaultEdit edit, EntityStringDefault entityStringDefault) {
128        edit.setStringAttribute(entityStringDefault.getStringAttribute());
129    }
130
131    @Override
132    public void doUpdate(EntityStringDefault entityStringDefault) {
133        var entityStringDefaultValue = coreControl.getEntityStringDefaultValueForUpdate(entityStringDefault);
134
135        entityStringDefaultValue.setStringAttribute(String.valueOf(edit.getStringAttribute()));
136
137        coreControl.updateEntityStringDefaultFromValue(entityStringDefaultValue, getPartyPK());
138    }
139    
140}