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.EntityAttributeDescriptionEdit;
021import com.echothree.control.user.core.common.form.EditEntityAttributeDescriptionForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditEntityAttributeDescriptionResult;
024import com.echothree.control.user.core.common.spec.EntityAttributeDescriptionSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.core.server.entity.EntityAttribute;
030import com.echothree.model.data.core.server.entity.EntityAttributeDescription;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
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.common.command.EditMode;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043
044@Dependent
045public class EditEntityAttributeDescriptionCommand
046        extends BaseAbstractEditCommand<EntityAttributeDescriptionSpec, EntityAttributeDescriptionEdit, EditEntityAttributeDescriptionResult, EntityAttributeDescription, EntityAttribute> {
047    
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
050    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.EntityAttribute.name(), SecurityRoles.Description.name())
057                        ))
058                ));
059        
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
063                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
065                );
066        
067        EDIT_FIELD_DEFINITIONS = List.of(
068                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
069                );
070    }
071    
072    /** Creates a new instance of EditEntityAttributeDescriptionCommand */
073    public EditEntityAttributeDescriptionCommand() {
074        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076    
077    @Override
078    public EditEntityAttributeDescriptionResult getResult() {
079        return CoreResultFactory.getEditEntityAttributeDescriptionResult();
080    }
081
082    @Override
083    public EntityAttributeDescriptionEdit getEdit() {
084        return CoreEditFactory.getEntityAttributeDescriptionEdit();
085    }
086
087    @Override
088    public EntityAttributeDescription getEntity(EditEntityAttributeDescriptionResult result) {
089        EntityAttributeDescription entityAttributeDescription = null;
090        var componentVendorName = spec.getComponentVendorName();
091        var componentVendor = componentControl.getComponentVendorByName(componentVendorName);
092
093        if(componentVendor != null) {
094            var entityTypeName = spec.getEntityTypeName();
095            var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName);
096
097            if(entityType != null) {
098                var entityAttributeName = spec.getEntityAttributeName();
099                var entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName);
100
101                if(entityAttribute != null) {
102                    var partyControl = Session.getModelController(PartyControl.class);
103                    var languageIsoName = spec.getLanguageIsoName();
104                    var language = partyControl.getLanguageByIsoName(languageIsoName);
105
106                    if(language != null) {
107                        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
108                            entityAttributeDescription = coreControl.getEntityAttributeDescription(entityAttribute, language);
109                        } else { // EditMode.UPDATE
110                            entityAttributeDescription = coreControl.getEntityAttributeDescriptionForUpdate(entityAttribute, language);
111                        }
112
113                        if(entityAttributeDescription == null) {
114                            addExecutionError(ExecutionErrors.UnknownEntityAttributeDescription.name(), componentVendorName, entityTypeName, entityAttributeName, entityAttributeName, languageIsoName);
115                        }
116                    } else {
117                        addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
118                    }
119                } else {
120                    addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName);
121                }
122            } else {
123                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName);
124            }
125        } else {
126            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
127        }
128
129        return entityAttributeDescription;
130    }
131
132    @Override
133    public EntityAttribute getLockEntity(EntityAttributeDescription entityAttributeDescription) {
134        return entityAttributeDescription.getEntityAttribute();
135    }
136
137    @Override
138    public void fillInResult(EditEntityAttributeDescriptionResult result, EntityAttributeDescription entityAttributeDescription) {
139
140        result.setEntityAttributeDescription(coreControl.getEntityAttributeDescriptionTransfer(getUserVisit(), entityAttributeDescription, null));
141    }
142
143    @Override
144    public void doLock(EntityAttributeDescriptionEdit edit, EntityAttributeDescription entityAttributeDescription) {
145        edit.setDescription(entityAttributeDescription.getDescription());
146    }
147
148    @Override
149    public void doUpdate(EntityAttributeDescription entityAttributeDescription) {
150        var entityAttributeDescriptionValue = coreControl.getEntityAttributeDescriptionValue(entityAttributeDescription);
151        entityAttributeDescriptionValue.setDescription(edit.getDescription());
152
153        coreControl.updateEntityAttributeDescriptionFromValue(entityAttributeDescriptionValue, getPartyPK());
154    }
155    
156}