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.EntityAttributeGroupDescriptionEdit;
021import com.echothree.control.user.core.common.form.EditEntityAttributeGroupDescriptionForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditEntityAttributeGroupDescriptionResult;
024import com.echothree.control.user.core.common.spec.EntityAttributeGroupDescriptionSpec;
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.EntityAttributeGroup;
030import com.echothree.model.data.core.server.entity.EntityAttributeGroupDescription;
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 EditEntityAttributeGroupDescriptionCommand
046        extends BaseAbstractEditCommand<EntityAttributeGroupDescriptionSpec, EntityAttributeGroupDescriptionEdit, EditEntityAttributeGroupDescriptionResult, EntityAttributeGroupDescription, EntityAttributeGroup> {
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.EntityAttributeGroup.name(), SecurityRoles.Description.name())
057                        ))
058                ));
059        
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("EntityAttributeGroupName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
063                );
064        
065        EDIT_FIELD_DEFINITIONS = List.of(
066                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
067                );
068    }
069    
070    /** Creates a new instance of EditEntityAttributeGroupDescriptionCommand */
071    public EditEntityAttributeGroupDescriptionCommand() {
072        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
073    }
074
075    @Override
076    public EditEntityAttributeGroupDescriptionResult getResult() {
077        return CoreResultFactory.getEditEntityAttributeGroupDescriptionResult();
078    }
079
080    @Override
081    public EntityAttributeGroupDescriptionEdit getEdit() {
082        return CoreEditFactory.getEntityAttributeGroupDescriptionEdit();
083    }
084
085    @Override
086    public EntityAttributeGroupDescription getEntity(EditEntityAttributeGroupDescriptionResult result) {
087        EntityAttributeGroupDescription entityAttributeGroupDescription = null;
088        var entityAttributeGroupName = spec.getEntityAttributeGroupName();
089        var entityAttributeGroup = coreControl.getEntityAttributeGroupByName(entityAttributeGroupName);
090
091        if(entityAttributeGroup != null) {
092            var partyControl = Session.getModelController(PartyControl.class);
093            var languageIsoName = spec.getLanguageIsoName();
094            var language = partyControl.getLanguageByIsoName(languageIsoName);
095
096            if(language != null) {
097                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
098                    entityAttributeGroupDescription = coreControl.getEntityAttributeGroupDescription(entityAttributeGroup, language);
099                } else { // EditMode.UPDATE
100                    entityAttributeGroupDescription = coreControl.getEntityAttributeGroupDescriptionForUpdate(entityAttributeGroup, language);
101                }
102
103                if(entityAttributeGroupDescription == null) {
104                    addExecutionError(ExecutionErrors.UnknownEntityAttributeGroupDescription.name(), entityAttributeGroupName, languageIsoName);
105                }
106            } else {
107                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
108            }
109        } else {
110            addExecutionError(ExecutionErrors.UnknownEntityAttributeGroupName.name(), entityAttributeGroupName);
111        }
112
113        return entityAttributeGroupDescription;
114    }
115
116    @Override
117    public EntityAttributeGroup getLockEntity(EntityAttributeGroupDescription entityAttributeGroupDescription) {
118        return entityAttributeGroupDescription.getEntityAttributeGroup();
119    }
120
121    @Override
122    public void fillInResult(EditEntityAttributeGroupDescriptionResult result, EntityAttributeGroupDescription entityAttributeGroupDescription) {
123
124        result.setEntityAttributeGroupDescription(coreControl.getEntityAttributeGroupDescriptionTransfer(getUserVisit(), entityAttributeGroupDescription, null));
125    }
126
127    @Override
128    public void doLock(EntityAttributeGroupDescriptionEdit edit, EntityAttributeGroupDescription entityAttributeGroupDescription) {
129        edit.setDescription(entityAttributeGroupDescription.getDescription());
130    }
131
132    @Override
133    public void doUpdate(EntityAttributeGroupDescription entityAttributeGroupDescription) {
134        var entityAttributeGroupDescriptionValue = coreControl.getEntityAttributeGroupDescriptionValue(entityAttributeGroupDescription);
135        
136        entityAttributeGroupDescriptionValue.setDescription(edit.getDescription());
137        
138        coreControl.updateEntityAttributeGroupDescriptionFromValue(entityAttributeGroupDescriptionValue, getPartyPK());
139    }
140
141    
142}